* 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)
| 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 = []; |
no test coverage detected