diff --git a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts index 0b095497..10c7af42 100644 --- a/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts +++ b/packages/ensjs/src/functions/wallet/setAbiRecord.test.ts @@ -136,12 +136,16 @@ it('should allow an abi record to be set with uri content type', async () => { expect(response!.decoded).toBe(true) }) -it('should allow an abi record to be set to blank', async () => { +it('should allow an abi record to be set to null with json content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'json', + data: null, + }) const tx = await setAbiRecord(walletClient, { name: 'with-type-1-abi.eth', - encodedAbi: null, + encodedAbi, resolverAddress: (await getResolver(publicClient, { - name: 'test123.eth', + name: 'with-type-1-abi.eth', }))!, account: accounts[1], }) @@ -150,7 +154,76 @@ it('should allow an abi record to be set to blank', async () => { expect(receipt.status).toBe('success') const response = await getAbiRecord(publicClient, { - name: 'test123.eth', + name: 'with-type-1-abi.eth', + }) + expect(response).toBeNull() +}) + +it('should allow an abi record to be set to null with zlib content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'zlib', + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name: 'with-type-2-abi.eth', + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name: 'with-type-2-abi.eth', + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name: 'with-type-2-abi.eth', + }) + expect(response).toBeNull() +}) + +it('should allow an abi record to be set to null with cbor content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'cbor', + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name: 'with-type-4-abi.eth', + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name: 'with-type-4-abi.eth', + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name: 'with-type-4-abi.eth', + }) + expect(response).toBeNull() +}) + +it('should allow an abi record to be set to null with uri content type', async () => { + const encodedAbi = await encodeAbi({ + encodeAs: 'uri', + data: null, + }) + const tx = await setAbiRecord(walletClient, { + name: 'with-type-8-abi.eth', + encodedAbi, + resolverAddress: (await getResolver(publicClient, { + name: 'with-type-8-abi.eth', + }))!, + account: accounts[1], + }) + expect(tx).toBeTruthy() + const receipt = await waitForTransaction(tx) + expect(receipt.status).toBe('success') + + const response = await getAbiRecord(publicClient, { + name: 'with-type-8-abi.eth', }) expect(response).toBeNull() }) diff --git a/packages/ensjs/src/functions/wallet/setAbiRecord.ts b/packages/ensjs/src/functions/wallet/setAbiRecord.ts index 59f096c6..2f4bbd1e 100644 --- a/packages/ensjs/src/functions/wallet/setAbiRecord.ts +++ b/packages/ensjs/src/functions/wallet/setAbiRecord.ts @@ -22,7 +22,7 @@ export type SetAbiRecordDataParameters = { /** Name to set ABI for */ name: string /** Encoded ABI data to set */ - encodedAbi: EncodedAbi | null + encodedAbi: EncodedAbi /** Resolver address to set ABI on */ resolverAddress: Address } @@ -47,12 +47,11 @@ export const makeFunctionData = < _wallet: WalletWithEns, { name, encodedAbi, resolverAddress }: SetAbiRecordDataParameters, ): SetAbiRecordDataReturnType => { - const encodedAbi_ = encodedAbi || { contentType: 0, encodedData: null } return { to: resolverAddress, data: encodeSetAbi({ namehash: namehash(name), - ...encodedAbi_, + ...encodedAbi, } as EncodeSetAbiParameters), } } diff --git a/packages/ensjs/src/functions/wallet/setRecords.test.ts b/packages/ensjs/src/functions/wallet/setRecords.test.ts index a5e76a56..d9525d47 100644 --- a/packages/ensjs/src/functions/wallet/setRecords.test.ts +++ b/packages/ensjs/src/functions/wallet/setRecords.test.ts @@ -93,6 +93,63 @@ it('should return a transaction to the resolver and set successfully', async () ] `) }) +it('should return a transaction to the resolver and delete successfully', async () => { + const setupTx = await setRecords(walletClient, { + name: 'test123.eth', + resolverAddress: (await getResolver(publicClient, { + name: 'test123.eth', + }))!, + coins: [ + { + coin: 'etcLegacy', + value: '0x42D63ae25990889E35F215bC95884039Ba354115', + }, + ], + texts: [{ key: 'foo', value: 'bar' }], + abi: await encodeAbi({ encodeAs: 'json', data: dummyABI }), + account: accounts[1], + }) + await waitForTransaction(setupTx) + const checkRecords = await getRecords(publicClient, { + name: 'test123.eth', + records: { + coins: ['etcLegacy'], + texts: ['foo'], + abi: true, + }, + }) + expect(checkRecords.abi!.abi).not.toBeNull() + expect(checkRecords.coins).toHaveLength(1) + expect(checkRecords.texts).toHaveLength(1) + const tx = await setRecords(walletClient, { + name: 'test123.eth', + resolverAddress: (await getResolver(publicClient, { + name: 'test123.eth', + }))!, + coins: [ + { + coin: 'etcLegacy', + value: '', + }, + ], + texts: [{ key: 'foo', value: '' }], + abi: await encodeAbi({ encodeAs: 'json', data: null }), + account: accounts[1], + }) + await waitForTransaction(tx) + + const records = await getRecords(publicClient, { + name: 'test123.eth', + records: { + coins: ['etcLegacy'], + texts: ['foo'], + abi: true, + }, + }) + expect(records.abi).toBeNull() + expect(records.coins).toHaveLength(0) + expect(records.texts).toHaveLength(0) +}) it('should error if there are no records to set', async () => { await expect( setRecords(walletClient, { diff --git a/packages/ensjs/src/utils/encoders/encodeAbi.ts b/packages/ensjs/src/utils/encoders/encodeAbi.ts index 2cd13c3e..16d54e37 100644 --- a/packages/ensjs/src/utils/encoders/encodeAbi.ts +++ b/packages/ensjs/src/utils/encoders/encodeAbi.ts @@ -20,11 +20,11 @@ export type EncodeAbiParameters = TEncodeAs extends 'uri' ? { encodeAs: TEncodeAs - data: string + data: string | null } : { encodeAs: TEncodeAs - data: Record + data: Record | null } export type EncodedAbi = { @@ -76,23 +76,23 @@ export const encodeAbi = async < switch (encodeAs) { case 'json': contentType = 1 - encodedData = stringToHex(JSON.stringify(data)) + encodedData = data ? stringToHex(JSON.stringify(data)) : '0x' break case 'zlib': { contentType = 2 const { deflate } = await import('pako/dist/pako_deflate.min.js') - encodedData = bytesToHex(deflate(JSON.stringify(data))) + encodedData = data ? bytesToHex(deflate(JSON.stringify(data))) : '0x' break } case 'cbor': { contentType = 4 const { cborEncode } = await import('@ensdomains/address-encoder/utils') - encodedData = bytesToHex(new Uint8Array(cborEncode(data))) + encodedData = data ? bytesToHex(new Uint8Array(cborEncode(data))) : '0x' break } default: { contentType = 8 - encodedData = stringToHex(data as string) + encodedData = data ? stringToHex(data as string) : '0x' break } } diff --git a/packages/ensjs/src/utils/encoders/encodeSetAbi.ts b/packages/ensjs/src/utils/encoders/encodeSetAbi.ts index ea263e85..460583ed 100644 --- a/packages/ensjs/src/utils/encoders/encodeSetAbi.ts +++ b/packages/ensjs/src/utils/encoders/encodeSetAbi.ts @@ -4,7 +4,7 @@ import type { EncodedAbi } from './encodeAbi.js' export type EncodeSetAbiParameters = { namehash: Hex -} & (EncodedAbi | { contentType: 0; encodedData: null }) +} & EncodedAbi export type EncodeSetAbiReturnType = Hex @@ -16,6 +16,6 @@ export const encodeSetAbi = ({ return encodeFunctionData({ abi: publicResolverSetAbiSnippet, functionName: 'setABI', - args: [namehash, BigInt(contentType), encodedData ?? '0x'], + args: [namehash, BigInt(contentType), encodedData], }) } diff --git a/packages/ensjs/src/utils/generateRecordCallArray.ts b/packages/ensjs/src/utils/generateRecordCallArray.ts index 83b0ec6d..61656375 100644 --- a/packages/ensjs/src/utils/generateRecordCallArray.ts +++ b/packages/ensjs/src/utils/generateRecordCallArray.ts @@ -26,7 +26,7 @@ export type RecordOptions = Prettify<{ /** Array of coin records */ coins?: Omit[] /** ABI value */ - abi?: EncodedAbi | null + abi?: EncodedAbi }> export const generateRecordCallArray = ({ @@ -49,8 +49,7 @@ export const generateRecordCallArray = ({ } if (abi !== undefined) { - const abi_ = abi ?? { contentType: 0, encodedData: null } - const data = encodeSetAbi({ namehash, ...abi_ } as EncodeSetAbiParameters) + const data = encodeSetAbi({ namehash, ...abi } as EncodeSetAbiParameters) if (data) calls.push(data) }