@fuel-ts/abi-coder
Advanced tools
Comparing version 0.0.0-master-756d35d to 0.0.0-master-80945c8
@@ -16,2 +16,3 @@ import type { ParamType } from '@ethersproject/abi'; | ||
readonly name: string; | ||
readonly strictInputs: boolean; | ||
readonly inputs: Array<ParamType>; | ||
@@ -23,2 +24,3 @@ readonly outputs: Array<ParamType>; | ||
readonly name: string; | ||
readonly strictInputs: boolean; | ||
readonly inputs: Array<ParamType>; | ||
@@ -25,0 +27,0 @@ readonly outputs: Array<ParamType>; |
@@ -12,4 +12,5 @@ "use strict"; | ||
this.outputs = params.outputs; | ||
this.strictInputs = params.strictInputs; | ||
} | ||
} | ||
exports.Fragment = Fragment; |
@@ -0,1 +1,2 @@ | ||
import type { JsonFragmentType } from '@ethersproject/abi'; | ||
import type { JsonFragment } from './fragment'; | ||
@@ -5,4 +6,6 @@ import { Fragment } from './fragment'; | ||
static fromObject(value: JsonFragment): FunctionFragment; | ||
static strictArguments(fragment: readonly JsonFragmentType[]): boolean; | ||
static allowOnlyArguments(inputs: readonly JsonFragmentType[]): readonly JsonFragmentType[]; | ||
format(): string; | ||
} | ||
//# sourceMappingURL=function-fragment.d.ts.map |
@@ -20,3 +20,4 @@ "use strict"; | ||
name: value.name, | ||
inputs: inputs.map(abi_1.ParamType.fromObject), | ||
inputs: FunctionFragment.allowOnlyArguments(inputs).map(abi_1.ParamType.fromObject), | ||
strictInputs: FunctionFragment.strictArguments(inputs), | ||
outputs: outputs.map(abi_1.ParamType.fromObject), | ||
@@ -26,2 +27,11 @@ }; | ||
} | ||
static strictArguments(fragment) { | ||
return !(fragment.length === 4 && | ||
fragment[0].type === 'u64' && | ||
fragment[1].type === 'u64' && | ||
fragment[2].type === 'b256'); | ||
} | ||
static allowOnlyArguments(inputs) { | ||
return FunctionFragment.strictArguments(inputs) ? inputs : inputs.slice(3); | ||
} | ||
format() { | ||
@@ -28,0 +38,0 @@ const inputFormat = this.inputs.map((input) => formatOverride.call(input)); |
import type { BytesLike } from '@ethersproject/bytes'; | ||
import AbiCoder from './abi-coder'; | ||
import type { Values } from './coders/abstract-coder'; | ||
import type { Fragment, JsonFragment } from './fragments/fragment'; | ||
@@ -15,3 +16,3 @@ import FunctionFragment from './fragments/function-fragment'; | ||
decodeFunctionData(functionFragment: FunctionFragment | string, data: BytesLike): any; | ||
encodeFunctionData(functionFragment: FunctionFragment | string, values?: ReadonlyArray<any>): string; | ||
encodeFunctionData(functionFragment: FunctionFragment | string, values?: ReadonlyArray<Values>): string; | ||
decodeFunctionResult(functionFragment: FunctionFragment | string, data: BytesLike): any; | ||
@@ -18,0 +19,0 @@ encodeFunctionResult(functionFragment: FunctionFragment | string, values?: ReadonlyArray<any>): string; |
@@ -75,3 +75,3 @@ "use strict"; | ||
const selector = Interface.getSighash(fragment); | ||
const args = this.abiCoder.encode(fragment.inputs, values); | ||
const args = this.abiCoder.encode(fragment.inputs, fragment.strictInputs || values.length === fragment.inputs.length ? values : values.slice(3)); | ||
if (fragment.inputs.length !== 1) { | ||
@@ -78,0 +78,0 @@ throw new Error('For now, ABI functions must take exactly one parameter'); |
@@ -145,2 +145,117 @@ "use strict"; | ||
}); | ||
it('can remove the first 3 arguments from the abi if they have gas, coin and color arguments', () => { | ||
const json = { | ||
type: 'function', | ||
inputs: [ | ||
{ | ||
name: 'gas_', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'amount_', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'color_', | ||
type: 'b256', | ||
}, | ||
{ name: 'arg', type: 'u64' }, | ||
], | ||
name: 'entry_one', | ||
outputs: [], | ||
}; | ||
functionInterface = new interface_1.default([json]); | ||
const newFragment = function_fragment_1.default.fromObject(json); | ||
expect(Object.values(functionInterface.functions)).toHaveLength(1); | ||
expect(functionInterface.getFunction('entry_one(u64,u64,b256,u64)')).toEqual(newFragment); | ||
expect(functionInterface.getFunction('entry_one')).toEqual(newFragment); | ||
expect(functionInterface.getFunction('0x0000000044aa0fa9')).toEqual(newFragment); | ||
expect(functionInterface.encodeFunctionData('entry_one', [42])).toEqual('0x0000000044aa0fa90000000000000000000000000000002a'); | ||
expect(functionInterface.encodeFunctionData('entry_one', [ | ||
42, | ||
42, | ||
'0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930bd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b', | ||
42, | ||
])).toEqual('0x0000000044aa0fa90000000000000000000000000000002a'); | ||
const decoded = functionInterface.decodeFunctionData('entry_one', '0x0000000044aa0fa9000000000000002a'); | ||
// toEqual can't handle BigNumbers so JSON.stringify is used | ||
expect(JSON.stringify(decoded)).toEqual(JSON.stringify([bignumber_1.BigNumber.from(42)])); | ||
functionInterface = new interface_1.default([ | ||
{ | ||
inputs: [ | ||
{ | ||
name: 'gas_', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'amount_', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'color_', | ||
type: 'b256', | ||
}, | ||
{ | ||
name: 'person', | ||
type: 'tuple', | ||
components: [ | ||
{ | ||
name: 'name', | ||
type: 'str[20]', | ||
}, | ||
{ | ||
name: 'address', | ||
type: 'address', | ||
}, | ||
], | ||
}, | ||
], | ||
name: 'tuple_function', | ||
outputs: [], | ||
type: 'function', | ||
}, | ||
]); | ||
expect(functionInterface.encodeFunctionData('tuple_function', [ | ||
{ | ||
address: '0xd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b', | ||
name: 'foo', | ||
}, | ||
])).toEqual('0x00000000ba463b0d0000000000000000666f6f00000000d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'); | ||
functionInterface = new interface_1.default([ | ||
{ | ||
type: 'function', | ||
name: 'entry_one', | ||
inputs: [ | ||
{ | ||
name: 'gas_', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'amount_', | ||
type: 'u64', | ||
}, | ||
{ | ||
name: 'color_', | ||
type: 'b256', | ||
}, | ||
{ | ||
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,s(bool,u64))'); | ||
}); | ||
}); |
{ | ||
"name": "@fuel-ts/abi-coder", | ||
"version": "0.0.0-master-756d35d", | ||
"version": "0.0.0-master-80945c8", | ||
"description": "", | ||
"author": "Fuel Labs <contact@fuel.sh> (https://fuel.network/)", | ||
"typedocMain": "src/index.ts", | ||
"main": "dist/index.js", | ||
@@ -19,3 +20,3 @@ "files": [ | ||
}, | ||
"gitHead": "e3a52f041e9c0dfc3078c9802865a8b101b1fdf8" | ||
"gitHead": "697d4fb967e5631f1e634c0827b92806ccc4b9bb" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
76536
1227