Skip to content

Commit

Permalink
simplify encodeAbi + setAbitRecord.test
Browse files Browse the repository at this point in the history
  • Loading branch information
storywithoutend committed Jan 19, 2024
1 parent 18b8716 commit 7f3b122
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 40 deletions.
33 changes: 10 additions & 23 deletions packages/ensjs/src/functions/wallet/setAbiRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,14 @@ it('should allow an abi record to be set with uri content type', async () => {
})

type EncodeAs = Parameters<typeof encodeAbi>[0]['encodeAs']
const ABI_TEST_CASES: { encodeAs: EncodeAs; name: string }[] = [
{
encodeAs: 'json',
name: 'with-type-1-abi.eth',
},
{
encodeAs: 'zlib',
name: 'with-type-2-abi.eth',
},
{
encodeAs: 'cbor',
name: 'with-type-4-abi.eth',
},
{
encodeAs: 'uri',
name: 'with-type-8-abi.eth',
},
]

ABI_TEST_CASES.forEach(({ encodeAs, name }) => {
it(`should allow an abi record to be set to null with ${encodeAs} content type`, async () => {
it.each([
['json', 'with-type-1-abi.eth'],
['zlib', 'with-type-2-abi.eth'],
['cbor', 'with-type-4-abi.eth'],
['uri', 'with-type-8-abi.eth'],
] as [EncodeAs, string][])(
`should allow an abi record to be set to null with %s content type`,
async (encodeAs, name) => {
const encodedAbi = await encodeAbi({
encodeAs,
data: null,
Expand All @@ -178,5 +165,5 @@ ABI_TEST_CASES.forEach(({ encodeAs, name }) => {
name,
})
expect(response).toBeNull()
})
})
},
)
16 changes: 6 additions & 10 deletions packages/ensjs/src/functions/wallet/setRecords.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,9 @@ it('should return a transaction to the resolver and delete successfully', async
await waitForTransaction(setupTx)
const checkRecords = await getRecords(publicClient, {
name: 'test123.eth',
records: {
coins: ['etcLegacy'],
texts: ['foo'],
abi: true,
},
coins: ['etcLegacy'],
texts: ['foo'],
abi: true,
})
expect(checkRecords.abi!.abi).not.toBeNull()
expect(checkRecords.coins).toHaveLength(1)
Expand All @@ -138,11 +136,9 @@ it('should return a transaction to the resolver and delete successfully', async

const records = await getRecords(publicClient, {
name: 'test123.eth',
records: {
coins: ['etcLegacy'],
texts: ['foo'],
abi: true,
},
coins: ['etcLegacy'],
texts: ['foo'],
abi: true,
})
expect(records.abi).toBeNull()
expect(records.coins).toHaveLength(0)
Expand Down
18 changes: 11 additions & 7 deletions packages/ensjs/src/utils/encoders/encodeAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,27 +72,31 @@ export const encodeAbi = async <
Prettify<EncodeAbiReturnType<TContentType>>
> => {
let contentType: AbiContentType
let encodedData: Hex
let encodedData: Hex = '0x'
switch (encodeAs) {
case 'json':
contentType = 1
encodedData = data ? stringToHex(JSON.stringify(data)) : '0x'
if (data) encodedData = stringToHex(JSON.stringify(data))
break
case 'zlib': {
contentType = 2
const { deflate } = await import('pako/dist/pako_deflate.min.js')
encodedData = data ? bytesToHex(deflate(JSON.stringify(data))) : '0x'
if (data) {
const { deflate } = await import('pako/dist/pako_deflate.min.js')
encodedData = bytesToHex(deflate(JSON.stringify(data)))
}
break
}
case 'cbor': {
contentType = 4
const { cborEncode } = await import('@ensdomains/address-encoder/utils')
encodedData = data ? bytesToHex(new Uint8Array(cborEncode(data))) : '0x'
if (data) {
const { cborEncode } = await import('@ensdomains/address-encoder/utils')
encodedData = bytesToHex(new Uint8Array(cborEncode(data)))
}
break
}
default: {
contentType = 8
encodedData = data ? stringToHex(data as string) : '0x'
if (data) encodedData = stringToHex(data as string)
break
}
}
Expand Down

0 comments on commit 7f3b122

Please sign in to comment.