Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Cover some cases in abi tests #57

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/abi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ describe('abi', () => {

})

it('codec errors', () => {
// Exception on encoding
expect(() => abi.encodeParameter('bytes32', '0xdf3234')).to.throw()
// @ts-ignore
expect(() => abi.encodeParameter('WRONG', 10)).to.throw()

// Exception on decoding
expect(() => abi.decodeParameter('uint256', 'WRONG_UINT')).to.throw(Error, "invalid hexidecimal string")
})

it('function', () => {
expect(f1.signature).equal('0x27fcbb2f')
expect(f1.encode(1, 'foo')).equal('0x27fcbb2f000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003666f6f0000000000000000000000000000000000000000000000000000000000')
Expand All @@ -222,6 +232,30 @@ describe('abi', () => {
r1: '0xabc0000000000000000000000000000000000001',
r2: '0x666f6f'
})

// Wrong definition of function (wrong formatSignature)
expect(() => {
// @ts-ignore
new abi.Function({
"constant": false,
// @ts-ignore
"inputs": "WRONG_FORMAT",
"name": "f1",
"outputs": [
{
"name": "r1",
"type": "address"
},
{
"name": "r2",
"type": "bytes"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
})
}).to.throw()
})

it('event', () => {
Expand Down Expand Up @@ -295,6 +329,9 @@ describe('abi', () => {

const hexSlice = '0x' + Buffer.from('hello').toString('hex')
expect(e5.encode({ a1: hexSlice })).deep.equal([e5.signature, hash])

// Wrong encoding of event hex format
expect(() => e5.encode({ a1: "WRONG_HEX_FORMAT" })).to.throw(Error, "event.encode: invalid bytes value")
})

it('v2: Function', () => {
Expand Down