Skip to content

Commit

Permalink
Merge pull request #162 from ensdomains/abi-encoder-tests
Browse files Browse the repository at this point in the history
added few encodeAbi tests
  • Loading branch information
storywithoutend authored Jan 19, 2024
2 parents de7ad28 + a02c47b commit bb0b584
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/ensjs/src/utils/encoders/encodeAbi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ describe('encodeAbi', () => {
expect(result.encodedData).toEqual('0x7b22666f6f223a22626172227d')
})

// Null JSON data
it('encodes null JSON data', async () => {
const result = await encodeAbi({ encodeAs: 'json', data: null })
expect(result.contentType).toEqual(1)
expect(result.encodedData).toEqual('0x')
})

it('encodes data as zlib', async () => {
const data = { foo: 'bar' }
const result = await encodeAbi({ encodeAs: 'zlib', data })
Expand All @@ -21,19 +28,40 @@ describe('encodeAbi', () => {
)
})

// Null zlib data
it('encodes null zlib data', async () => {
const result = await encodeAbi({ encodeAs: 'zlib', data: null })
expect(result.contentType).toEqual(2)
expect(result.encodedData).toEqual('0x')
})

it('encodes data as cbor', async () => {
const data = { foo: 'bar' }
const result = await encodeAbi({ encodeAs: 'cbor', data })
expect(result.contentType).toEqual(4)
expect(result.encodedData).toEqual('0xa163666f6f63626172')
})

// Null CBOR data
it('encodes null CBOR data', async () => {
const result = await encodeAbi({ encodeAs: 'cbor', data: null })
expect(result.contentType).toEqual(4)
expect(result.encodedData).toEqual('0x')
})

it('encodes data as uri', async () => {
const data = 'foo=bar'
const result = await encodeAbi({ encodeAs: 'uri', data })
expect(result.contentType).toEqual(8)
expect(result.encodedData).toEqual('0x666f6f3d626172')
})

// Null URI data
it('encodes null URI data', async () => {
const result = await encodeAbi({ encodeAs: 'uri', data: null })
expect(result.contentType).toEqual(8)
expect(result.encodedData).toEqual('0x')
})
})

describe('encodeAsToContentType', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/ensjs/src/utils/encoders/encodeSetAbi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('encodeSetAbi', () => {
encodedData,
}

it('encodes the setAbi function data correctly', () => {
it('encodes the setAbi function data correctly with null encodedData', async () => {
const expected =
'0x623195b01234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000'
const result = encodeSetAbi(parameters)
Expand Down
27 changes: 27 additions & 0 deletions packages/ensjs/src/utils/generateRecordCallArray.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateRecordCallArray } from './generateRecordCallArray.js'
import { encodeAbi } from './index.js'
import { namehash } from './normalise.js'

it('generates a record call array', () => {
Expand Down Expand Up @@ -78,3 +79,29 @@ it('adds coin calls when coins array is defined and not empty', () => {
]
`)
})
it('adds abi call when data is null', async () => {
const result = await encodeAbi({ encodeAs: 'uri', data: null })
expect(
generateRecordCallArray({
namehash: namehash('test.eth'),
abi: result,
}),
).toMatchInlineSnapshot(`
[
"0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f1000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000000",
]
`)
})
it('adds abi call when data is not empty', async () => {
const result = await encodeAbi({ encodeAs: 'json', data: { foo: 'bar' } })
expect(
generateRecordCallArray({
namehash: namehash('test.eth'),
abi: result,
}),
).toMatchInlineSnapshot(`
[
"0x623195b0eb4f647bea6caa36333c816d7b46fdcb05f9466ecacc140ea8c66faf15b3d9f100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000d7b22666f6f223a22626172227d00000000000000000000000000000000000000",
]
`)
})

0 comments on commit bb0b584

Please sign in to comment.