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

fix setting abi and add tests #161

Merged
merged 8 commits into from
Jan 21, 2024
Prev Previous commit
Next Next commit
simplify encodeAbi + setAbitRecord.test
  • Loading branch information
storywithoutend committed Jan 19, 2024
commit 7f3b1225f730251978409abde8b07053d5af61b2
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