qtumproject / qtum-ethjs-abi

Forked ABI encoding and decoding
MIT License
1 stars 15 forks source link

ABI decoding with option to decode Solidity integers to JavaScript number #1

Open hayeah opened 6 years ago

hayeah commented 6 years ago

Right now all numbers are decoded to BigNumber instances, which are fussy to use in the application code.

IEEE 754 and can only safely represent numbers between -(2^53 - 1) and 2^53 - 1 .

Would like a way to configure ethjs-abi use decode Solidity integers to plain JS numbers, and throw an error if not safe.

BigNumber.toNumber() throws if the conversion is unsafe, and we can rely on this behaviour.

Xuefeng-Zhu commented 6 years ago

@hayeah Could you please provide more details or examples? What is expected behavior?

hayeah commented 6 years ago

Solidity numbers are decoded to BigNumber, see these test cases:

https://github.com/qtumproject/qtum-ethjs-abi/blob/master/src/tests/test.web3.decodeParam.js#L111-L136

For most applications Number.MAX_SAFE_INTEGER is enough. It'd be good to be able to configure the decoding to return JavaScript numbers instead.

Xuefeng-Zhu commented 6 years ago

@hayeah Thank you for the response. For decoding, what is the expected behavior? Will it be automatically decoded to js number, or users need to specify that?

hayeah commented 6 years ago

I think it'd be best if user has a way to create multiple instances of the library. Maybe an interface like

const abi = new ABIEncoding({ useBigNumber: false, use0xPrefix: false })
abi.decodeMethod(...)
abi.decodeMethod(...)
// etc...