MCPcopy Create free account
hub / github.com/AztecProtocol/aztec-packages / decodeNext

Method decodeNext

yarn-project/stdlib/src/abi/decoder.ts:45–111  ·  view source on GitHub ↗

* Decodes a single return value from field to the given type. * @param abiType - The type of the return value. * @returns The decoded return value.

(abiType: AbiType)

Source from the content-addressed store, hash-verified

43 * @returns The decoded return value.
44 */
45 private decodeNext(abiType: AbiType): AbiDecoded {
46 switch (abiType.kind) {
47 case 'field':
48 return this.getNextField().toBigInt();
49 case 'integer': {
50 const nextField = this.getNextField();
51
52 if (abiType.sign === 'signed') {
53 // We parse the buffer using 2's complement
54 return parseSignedInt(nextField.toBuffer(), abiType.width);
55 }
56
57 return nextField.toBigInt();
58 }
59 case 'boolean':
60 return !this.getNextField().isZero();
61 case 'array': {
62 const array = [];
63 for (let i = 0; i < abiType.length; i += 1) {
64 array.push(this.decodeNext(abiType.type));
65 }
66 return array;
67 }
68 case 'struct': {
69 const struct: { [key: string]: AbiDecoded } = {};
70 if (isAztecAddressStruct(abiType)) {
71 return new AztecAddress(this.getNextField().toBuffer());
72 }
73 if (isEthAddressStruct(abiType)) {
74 return EthAddress.fromField(this.getNextField());
75 }
76 if (isFunctionSelectorStruct(abiType)) {
77 return FunctionSelector.fromField(this.getNextField());
78 }
79 if (isWrappedFieldStruct(abiType)) {
80 return this.getNextField();
81 }
82 if (isOptionStruct(abiType)) {
83 const isSome = this.decodeNext(abiType.fields[0].type);
84 const value = this.decodeNext(abiType.fields[1].type);
85 return isSome ? value : undefined;
86 }
87
88 for (const field of abiType.fields) {
89 struct[field.name] = this.decodeNext(field.type);
90 }
91 return struct;
92 }
93 case 'string': {
94 let str = '';
95 for (let i = 0; i < abiType.length; i += 1) {
96 const charCode = Number(this.getNextField().toBigInt());
97 str += String.fromCharCode(charCode);
98 }
99 return str;
100 }
101 case 'tuple': {
102 const array = [];

Callers 1

decodeMethod · 0.95

Calls 12

getNextFieldMethod · 0.95
parseSignedIntFunction · 0.85
isAztecAddressStructFunction · 0.85
isEthAddressStructFunction · 0.85
isFunctionSelectorStructFunction · 0.85
isWrappedFieldStructFunction · 0.85
isOptionStructFunction · 0.85
toBufferMethod · 0.65
pushMethod · 0.65
toBigIntMethod · 0.45
isZeroMethod · 0.45
fromFieldMethod · 0.45

Tested by

no test coverage detected