Split ABI Encoder/Decoder tests into separate files
This commit is contained in:
@@ -13,7 +13,7 @@ export class Address extends PayloadDataType {
|
||||
private static readonly _SIZE_KNOWN_AT_COMPILE_TIME: boolean = true;
|
||||
private static readonly _ADDRESS_SIZE_IN_BYTES = 20;
|
||||
private static readonly _DECODED_ADDRESS_OFFSET_IN_BYTES = Constants.EVM_WORD_WIDTH_IN_BYTES -
|
||||
Address._ADDRESS_SIZE_IN_BYTES;
|
||||
Address._ADDRESS_SIZE_IN_BYTES;
|
||||
|
||||
public static matchType(type: string): boolean {
|
||||
return type === 'address';
|
||||
|
@@ -46,7 +46,7 @@ export class StaticBytes extends PayloadDataType {
|
||||
if (valueBuf.byteLength > this._width) {
|
||||
throw new Error(
|
||||
`Tried to assign ${value} (${
|
||||
valueBuf.byteLength
|
||||
valueBuf.byteLength
|
||||
} bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`,
|
||||
);
|
||||
} else if (value.length % 2 !== 0) {
|
||||
|
1094
packages/utils/test/abi_encoder/evm_data_types_test.ts
Normal file
1094
packages/utils/test/abi_encoder/evm_data_types_test.ts
Normal file
File diff suppressed because it is too large
Load Diff
401
packages/utils/test/abi_encoder/methods_test.ts
Normal file
401
packages/utils/test/abi_encoder/methods_test.ts
Normal file
File diff suppressed because one or more lines are too long
293
packages/utils/test/abi_encoder/optimizer_test.ts
Normal file
293
packages/utils/test/abi_encoder/optimizer_test.ts
Normal file
@@ -0,0 +1,293 @@
|
||||
import * as chai from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import { AbiEncoder, BigNumber } from '../../src/';
|
||||
import { chaiSetup } from '../utils/chai_setup';
|
||||
|
||||
import * as OptimizedAbis from './abi_samples/optimizer_abis';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('ABI Encoder: Optimized Method Encoding/Decoding', () => {
|
||||
it('Duplicate Dynamic Arrays with Static Elements', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateDynamicArraysWithStaticElements);
|
||||
const array1 = [new BigNumber(100), new BigNumber(150)];
|
||||
const array2 = array1;
|
||||
const args = [array1, array2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x7221063300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000096';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Dynamic Arrays with Dynamic Elements', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateDynamicArraysWithDynamicElements);
|
||||
const array1 = ['Hello', 'World'];
|
||||
const array2 = array1;
|
||||
const args = [array1, array2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0xbb4f12e300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Static Arrays with Static Elements (should not optimize)', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateStaticArraysWithStaticElements);
|
||||
const array1 = [new BigNumber(100), new BigNumber(150)];
|
||||
const array2 = array1;
|
||||
const args = [array1, array2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x7f8130430000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000096';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
const unoptimizedCalldata = method.encode(args);
|
||||
expect(optimizedCalldata).to.be.equal(unoptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Static Arrays with Dynamic Elements', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateStaticArraysWithDynamicElements);
|
||||
const array1 = ['Hello', 'World'];
|
||||
const array2 = array1;
|
||||
const args = [array1, array2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x9fe31f8e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Array Elements (should optimize)', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateArrayElements);
|
||||
const strings = ['Hello', 'World', 'Hello', 'World'];
|
||||
const args = [strings];
|
||||
// Validate calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x13e751a900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Tuple Fields', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateTupleFields);
|
||||
const tuple = ['Hello', 'Hello'];
|
||||
const args = [tuple];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x16780a5e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Strings', async () => {
|
||||
// Description:
|
||||
// Two dynamic arrays with the same values.
|
||||
// In the optimized calldata, only one set of elements should be included.
|
||||
// Both arrays should point to this set.
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateStrings);
|
||||
const args = ['Hello', 'Hello'];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x07370bfa00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Bytes', async () => {
|
||||
// Description:
|
||||
// Two dynamic arrays with the same values.
|
||||
// In the optimized calldata, only one set of elements should be included.
|
||||
// Both arrays should point to this set.
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateBytes);
|
||||
const value = '0x01020304050607080910111213141516171819202122232425262728293031323334353637383940';
|
||||
const args = [value, value];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x6045e42900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002801020304050607080910111213141516171819202122232425262728293031323334353637383940000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Tuples', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateTuples);
|
||||
const tuple1 = ['Hello, World!', new BigNumber(424234)];
|
||||
const tuple2 = tuple1;
|
||||
const args = [tuple1, tuple2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x564f826d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000006792a000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c642100000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Fields Across Two Tuples', async () => {
|
||||
// Description:
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateTuples);
|
||||
const tuple1 = ['Hello, World!', new BigNumber(1)];
|
||||
const tuple2 = [tuple1[0], new BigNumber(2)];
|
||||
const args = [tuple1, tuple2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x564f826d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c642100000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Arrays, Nested in Separate Tuples', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateArraysNestedInTuples);
|
||||
const array = [new BigNumber(100), new BigNumber(150), new BigNumber(200)];
|
||||
const tuple1 = [array];
|
||||
const tuple2 = [array, 'extra argument to prevent exactly matching the tuples'];
|
||||
const args = [tuple1, tuple2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x18970a9e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000c80000000000000000000000000000000000000000000000000000000000000035657874726120617267756d656e7420746f2070726576656e742065786163746c79206d61746368696e6720746865207475706c65730000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Tuples, Nested in Separate Tuples', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateTuplesNestedInTuples);
|
||||
const nestedTuple = ['Hello, World!'];
|
||||
const tuple1 = [nestedTuple];
|
||||
const tuple2 = [nestedTuple, 'extra argument to prevent exactly matching the tuples'];
|
||||
const args = [tuple1, tuple2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x0b4d2e6a000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035657874726120617267756d656e7420746f2070726576656e742065786163746c79206d61746368696e6720746865207475706c65730000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Two-Dimensional Arrays', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateTwoDimensionalArrays);
|
||||
const twoDimArray1 = [['Hello', 'World'], ['Foo', 'Bar', 'Zaa']];
|
||||
const twoDimArray2 = twoDimArray1;
|
||||
const args = [twoDimArray1, twoDimArray2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: false });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x0d28c4f9000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000003466f6f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003426172000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a61610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000003466f6f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003426172000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a61610000000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Duplicate Array, Nested within Separate Two-Dimensional Arrays', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.duplicateTwoDimensionalArrays);
|
||||
const twoDimArray1 = [['Hello', 'World'], ['Foo']];
|
||||
const twoDimArray2 = [['Hello', 'World'], ['Bar']];
|
||||
const args = [twoDimArray1, twoDimArray2];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x0d28c4f900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003466f6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034261720000000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Array Elements Duplicated as Tuple Fields', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.arrayElementsDuplicatedAsTupleFields);
|
||||
const array = [new BigNumber(100), new BigNumber(150), new BigNumber(200), new BigNumber(225)];
|
||||
const tuple = [[array[0]], [array[1]], [array[2]], [array[3]]];
|
||||
const args = [array, tuple];
|
||||
// Validata calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0x5b5c78fd0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000e1';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
it('Array Elements Duplicated as Separate Parameter', async () => {
|
||||
// Generate calldata
|
||||
const method = new AbiEncoder.Method(OptimizedAbis.arrayElementsDuplicatedAsSeparateParameter);
|
||||
const array = ['Hello', 'Hello', 'Hello', 'World'];
|
||||
const str = 'Hello';
|
||||
const args = [array, str];
|
||||
// Validate calldata
|
||||
const optimizedCalldata = method.encode(args, { optimize: true });
|
||||
const expectedOptimizedCalldata =
|
||||
'0xe0e0d34900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000';
|
||||
expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata);
|
||||
// Validate decoding
|
||||
const decodedArgs = method.decode(optimizedCalldata);
|
||||
const decodedArgsJson = JSON.stringify(decodedArgs);
|
||||
const argsJson = JSON.stringify(args);
|
||||
expect(decodedArgsJson).to.be.equal(argsJson);
|
||||
});
|
||||
});
|
78
packages/utils/test/abi_encoder/return_values_test.ts
Normal file
78
packages/utils/test/abi_encoder/return_values_test.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import * as chai from 'chai';
|
||||
import 'mocha';
|
||||
|
||||
import { AbiEncoder } from '../../src/';
|
||||
import { chaiSetup } from '../utils/chai_setup';
|
||||
|
||||
import * as ReturnValueAbis from './abi_samples/return_value_abis';
|
||||
|
||||
chaiSetup.configure();
|
||||
const expect = chai.expect;
|
||||
|
||||
describe('ABI Encoder: Return Value Encoding/Decoding', () => {
|
||||
it('No Return Value', async () => {
|
||||
// Decode return value
|
||||
const method = new AbiEncoder.Method(ReturnValueAbis.noReturnValues);
|
||||
const returnValue = '0x';
|
||||
const decodedReturnValue = method.decodeReturnValues(returnValue);
|
||||
const expectedDecodedReturnValue: any[] = [];
|
||||
const decodedReturnValueJson = JSON.stringify(decodedReturnValue);
|
||||
const expectedDecodedReturnValueJson = JSON.stringify(expectedDecodedReturnValue);
|
||||
expect(decodedReturnValueJson).to.be.equal(expectedDecodedReturnValueJson);
|
||||
});
|
||||
it('Single static return value', async () => {
|
||||
// Generate Return Value
|
||||
const method = new AbiEncoder.Method(ReturnValueAbis.singleStaticReturnValue);
|
||||
const returnValue = ['0x01020304'];
|
||||
const encodedReturnValue = method.encodeReturnValues(returnValue);
|
||||
const decodedReturnValue = method.decodeReturnValues(encodedReturnValue);
|
||||
// Validate decoded return value
|
||||
const decodedReturnValueJson = JSON.stringify(decodedReturnValue);
|
||||
const expectedDecodedReturnValueJson = JSON.stringify(returnValue);
|
||||
expect(decodedReturnValueJson).to.be.equal(expectedDecodedReturnValueJson);
|
||||
});
|
||||
it('Multiple static return values', async () => {
|
||||
// Generate Return Value
|
||||
const method = new AbiEncoder.Method(ReturnValueAbis.multipleStaticReturnValues);
|
||||
const returnValue = ['0x01020304', '0x05060708'];
|
||||
const encodedReturnValue = method.encodeReturnValues(returnValue);
|
||||
const decodedReturnValue = method.decodeReturnValues(encodedReturnValue);
|
||||
// Validate decoded return value
|
||||
const decodedReturnValueJson = JSON.stringify(decodedReturnValue);
|
||||
const expectedDecodedReturnValueJson = JSON.stringify(returnValue);
|
||||
expect(decodedReturnValueJson).to.be.equal(expectedDecodedReturnValueJson);
|
||||
});
|
||||
it('Single dynamic return value', async () => {
|
||||
// Generate Return Value
|
||||
const method = new AbiEncoder.Method(ReturnValueAbis.singleDynamicReturnValue);
|
||||
const returnValue = ['0x01020304'];
|
||||
const encodedReturnValue = method.encodeReturnValues(returnValue);
|
||||
const decodedReturnValue = method.decodeReturnValues(encodedReturnValue);
|
||||
// Validate decoded return value
|
||||
const decodedReturnValueJson = JSON.stringify(decodedReturnValue);
|
||||
const expectedDecodedReturnValueJson = JSON.stringify(returnValue);
|
||||
expect(decodedReturnValueJson).to.be.equal(expectedDecodedReturnValueJson);
|
||||
});
|
||||
it('Multiple dynamic return values', async () => {
|
||||
// Generate Return Value
|
||||
const method = new AbiEncoder.Method(ReturnValueAbis.multipleDynamicReturnValues);
|
||||
const returnValue = ['0x01020304', '0x05060708'];
|
||||
const encodedReturnValue = method.encodeReturnValues(returnValue);
|
||||
const decodedReturnValue = method.decodeReturnValues(encodedReturnValue);
|
||||
// Validate decoded return value
|
||||
const decodedReturnValueJson = JSON.stringify(decodedReturnValue);
|
||||
const expectedDecodedReturnValueJson = JSON.stringify(returnValue);
|
||||
expect(decodedReturnValueJson).to.be.equal(expectedDecodedReturnValueJson);
|
||||
});
|
||||
it('Mixed static/dynamic return values', async () => {
|
||||
// Generate Return Value
|
||||
const method = new AbiEncoder.Method(ReturnValueAbis.mixedStaticAndDynamicReturnValues);
|
||||
const returnValue = ['0x01020304', '0x05060708'];
|
||||
const encodedReturnValue = method.encodeReturnValues(returnValue);
|
||||
const decodedReturnValue = method.decodeReturnValues(encodedReturnValue);
|
||||
// Validate decoded return value
|
||||
const decodedReturnValueJson = JSON.stringify(decodedReturnValue);
|
||||
const expectedDecodedReturnValueJson = JSON.stringify(returnValue);
|
||||
expect(decodedReturnValueJson).to.be.equal(expectedDecodedReturnValueJson);
|
||||
});
|
||||
});
|
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user