@fuel-ts/abi-coder
Advanced tools
Comparing version 0.0.0-master-6bce3f7 to 0.0.0-master-756d35d
@@ -5,2 +5,11 @@ "use strict"; | ||
const fragment_1 = require("./fragment"); | ||
/** | ||
* An override for the `format` method of Ethers' ParamType to handle Fuel/Ethereum ABI incompatibilities | ||
*/ | ||
function formatOverride(format) { | ||
if ((!format || format === abi_1.FormatTypes.sighash) && this.type.startsWith('struct ')) { | ||
return `s${this.format(format)}`; | ||
} | ||
return this.format(format); | ||
} | ||
class FunctionFragment extends fragment_1.Fragment { | ||
@@ -18,3 +27,3 @@ static fromObject(value) { | ||
format() { | ||
const inputFormat = this.inputs.map((input) => input.format()); | ||
const inputFormat = this.inputs.map((input) => formatOverride.call(input)); | ||
return `${this.name}(${['u64', 'u64', 'b256', ...inputFormat].join(',')})`; | ||
@@ -21,0 +30,0 @@ } |
@@ -11,2 +11,3 @@ "use strict"; | ||
const abi_coder_1 = __importDefault(require("./abi-coder")); | ||
const boolean_1 = __importDefault(require("./coders/boolean")); | ||
const function_fragment_1 = __importDefault(require("./fragments/function-fragment")); | ||
@@ -74,3 +75,12 @@ const logger = new logger_1.Logger('0.0.1'); | ||
} | ||
return (0, bytes_1.hexlify)((0, bytes_1.concat)([Interface.getSighash(fragment), this.abiCoder.encode(fragment.inputs, values)])); | ||
const selector = Interface.getSighash(fragment); | ||
const args = this.abiCoder.encode(fragment.inputs, values); | ||
if (fragment.inputs.length !== 1) { | ||
throw new Error('For now, ABI functions must take exactly one parameter'); | ||
} | ||
return (0, bytes_1.hexlify)((0, bytes_1.concat)([ | ||
selector, | ||
new boolean_1.default('isStruct').encode(fragment.inputs[0].type.startsWith('struct')), | ||
args, | ||
])); | ||
} | ||
@@ -77,0 +87,0 @@ // Decode the result of a function call |
@@ -37,3 +37,3 @@ "use strict"; | ||
functionInterface = new interface_1.default([jsonFragment]); | ||
expect(functionInterface.encodeFunctionData('entry_one', [42])).toEqual('0x0000000044aa0fa9000000000000002a'); | ||
expect(functionInterface.encodeFunctionData('entry_one', [42])).toEqual('0x0000000044aa0fa90000000000000000000000000000002a'); | ||
const decoded = functionInterface.decodeFunctionData('entry_one', '0x0000000044aa0fa9000000000000002a'); | ||
@@ -62,3 +62,3 @@ // toEqual can't handle BigNumbers so JSON.stringify is used | ||
]); | ||
expect(functionInterface.encodeFunctionData('takes_array', [[1, 2, 3]])).toEqual('0x0000000053030075000000000000000100000000000000020000000000000003'); | ||
expect(functionInterface.encodeFunctionData('takes_array', [[1, 2, 3]])).toEqual('0x00000000530300750000000000000000000000000000000100000000000000020000000000000003'); | ||
}); | ||
@@ -94,8 +94,9 @@ it('can encode and decodes function data with tuple values', () => { | ||
}, | ||
])).toEqual('0x00000000ba463b0d666f6f00000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'); | ||
])).toEqual('0x00000000ba463b0d0000000000000000666f6f00000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'); | ||
expect(functionInterface.encodeFunctionData('tuple_function', [ | ||
['foo', '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'], | ||
])).toEqual('0x00000000ba463b0d666f6f00000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'); | ||
])).toEqual('0x00000000ba463b0d0000000000000000666f6f00000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'); | ||
}); | ||
it('can encode and decodes function data with empty values', () => { | ||
// TODO: Enable this test when zero arg functions are supported | ||
it.skip('can encode and decodes function data with empty values', () => { | ||
functionInterface = new interface_1.default([ | ||
@@ -117,2 +118,32 @@ { type: 'function', inputs: [], name: 'entry_one', outputs: [] }, | ||
}); | ||
it('can encode struct', () => { | ||
functionInterface = new interface_1.default([ | ||
{ | ||
type: 'function', | ||
name: 'entry_one', | ||
inputs: [ | ||
{ | ||
name: 'my_u64', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'my_struct', | ||
type: 'struct MyStruct', | ||
components: [ | ||
{ | ||
name: 'dummy_a', | ||
type: 'bool', | ||
}, | ||
{ | ||
name: 'dummy_b', | ||
type: 'u64', | ||
}, | ||
], | ||
}, | ||
], | ||
outputs: [{ name: 'ret', type: 'u64' }], | ||
}, | ||
]); | ||
expect(functionInterface.getFunction('entry_one').format()).toEqual('entry_one(u64,u64,b256,u64,s(bool,u64))'); | ||
}); | ||
}); |
{ | ||
"name": "@fuel-ts/abi-coder", | ||
"version": "0.0.0-master-6bce3f7", | ||
"version": "0.0.0-master-756d35d", | ||
"description": "", | ||
"author": "Fuel Labs <contact@fuel.sh> (https://fuel.sh/)", | ||
"author": "Fuel Labs <contact@fuel.sh> (https://fuel.network/)", | ||
"main": "dist/index.js", | ||
@@ -19,3 +19,3 @@ "files": [ | ||
}, | ||
"gitHead": "02c507f516d28bd200aa57b05444a5c37f2f4af2" | ||
"gitHead": "e3a52f041e9c0dfc3078c9802865a8b101b1fdf8" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
70642
1095