Skip to content

Commit

Permalink
normalise coinid
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Sep 7, 2023
1 parent d4a1384 commit 2928aac
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
34 changes: 21 additions & 13 deletions packages/ensjs/src/functions/public/_getAddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type {
} from '../../types.js'
import { EMPTY_ADDRESS } from '../../utils/consts.js'
import { generateFunction } from '../../utils/generateFunction.js'
import { normaliseCoinId } from '../../utils/normaliseCoinId.js'

export type InternalGetAddrParameters = {
/** Name to get the address record for */
Expand All @@ -31,7 +32,11 @@ const encode = (
_client: ClientWithEns,
{ name, coin = 60, bypassFormat }: InternalGetAddrParameters,
): SimpleTransactionRequest => {
if (coin === 60 || coin === '60' || coin === 'eth') {
const normalisedCoin = normaliseCoinId(coin)
if (
(normalisedCoin.type === 'id' && normalisedCoin.value === 60) ||
(normalisedCoin.type === 'name' && normalisedCoin.value === 'ETH')
) {
return {
to: EMPTY_ADDRESS,
data: encodeFunctionData({
Expand All @@ -53,11 +58,12 @@ const encode = (
}
}
const formatter =
typeof coin === 'string' && Number.isNaN(parseInt(coin))
? formatsByName[coin]
: formatsByCoinType[typeof coin === 'number' ? coin : parseInt(coin)]
normalisedCoin.type === 'name'
? formatsByName[normalisedCoin.value]
: formatsByCoinType[normalisedCoin.value]

if (!formatter) throw new CoinFormatterNotFoundError({ coinType: coin })
if (!formatter)
throw new CoinFormatterNotFoundError({ coinType: normalisedCoin.value })

return {
to: EMPTY_ADDRESS,
Expand All @@ -72,21 +78,23 @@ const encode = (
const decode = async (
_client: ClientWithEns,
data: Hex,
{ coin }: InternalGetAddrParameters,
{ coin = 60 }: InternalGetAddrParameters,
): Promise<InternalGetAddrReturnType> => {
if (data === '0x') return null
if (!coin) {
coin = 60
}

const normalisedCoin = normaliseCoinId(coin)

const formatter =
typeof coin === 'string' && Number.isNaN(parseInt(coin))
? formatsByName[coin]
: formatsByCoinType[typeof coin === 'number' ? coin : parseInt(coin)]
normalisedCoin.type === 'name'
? formatsByName[normalisedCoin.value]
: formatsByCoinType[normalisedCoin.value]

let response: Hex

if (coin === 60 || coin === '60' || coin === 'eth') {
if (
(normalisedCoin.type === 'id' && normalisedCoin.value === 60) ||
(normalisedCoin.type === 'name' && normalisedCoin.value === 'ETH')
) {
response = decodeFunctionResult({
abi: publicResolverSingleAddrSnippet,
functionName: 'addr',
Expand Down
8 changes: 6 additions & 2 deletions packages/ensjs/src/functions/public/getRecords.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
decodeFunctionResult,
encodeFunctionData,
getContractError,
hexToBigInt,
toHex,
type Address,
type Hex,
Expand Down Expand Up @@ -190,7 +191,7 @@ const decode = async <TParams extends GetRecordsParameters>(
client: ClientWithEns,
data: Hex | BaseError,
passthrough: (CallObj | null)[],
{ name, resolver }: TParams,
{ name, resolver, records: recordsParams }: TParams,
): Promise<GetRecordsReturnType<TParams>> => {
const calls = passthrough
let recordData: (Hex | null)[] = []
Expand Down Expand Up @@ -280,7 +281,7 @@ const decode = async <TParams extends GetRecordsParameters>(
[{ type: 'bytes' }] as const,
item,
)[0]
if (BigInt(decodedFromAbi) === 0n) {
if (decodedFromAbi === '0x' || hexToBigInt(decodedFromAbi) === 0n) {
return { ...baseItem, value: null }
}
}
Expand Down Expand Up @@ -338,6 +339,9 @@ const decode = async <TParams extends GetRecordsParameters>(
},
{
resolverAddress,
...('texts' in recordsParams ? { texts: [] } : {}),
...('coins' in recordsParams ? { coins: [] } : {}),
...('contentHash' in recordsParams ? { contentHash: null } : {}),
} as GetRecordsReturnType<
GetRecordsParameters & {
records: Required<GetRecordsParameters['records']>
Expand Down
11 changes: 11 additions & 0 deletions packages/ensjs/src/utils/normaliseCoinId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const normaliseCoinId = (coinId: string | number) => {
const isString = typeof coinId === 'string'

if (isString && Number.isNaN(parseInt(coinId))) {
return { type: 'name', value: coinId.toUpperCase() } as const
}
return {
type: 'id',
value: isString ? parseInt(coinId as string) : (coinId as number),
} as const
}

0 comments on commit 2928aac

Please sign in to comment.