Skip to content

Commit

Permalink
feat: add multicoin support
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherdro committed Jan 4, 2022
1 parent 9882d6e commit 505b474
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
28 changes: 27 additions & 1 deletion src/abi.ts
Expand Up @@ -89,5 +89,31 @@ export const ABI = [
],
stateMutability: 'view',
type: 'function'
},
{
constant: true,
inputs: [
{
internalType: 'bytes32',
name: 'node',
type: 'bytes32'
},
{
internalType: 'uint256',
name: 'coinType',
type: 'uint256'
}
],
name: 'addr',
outputs: [
{
internalType: 'bytes',
name: '',
type: 'bytes'
}
],
payable: false,
stateMutability: 'view',
type: 'function'
}
]
];
25 changes: 22 additions & 3 deletions src/index.ts
Expand Up @@ -34,6 +34,11 @@ export interface ResolvedENS {
* Reverse lookup domain
*/
domain: string | null

/**
* Multicoin records
*/
coinTypes: any
}

const ENDPOINT = 'https://api.thegraph.com/subgraphs/name/ensdomains/ens'
Expand All @@ -46,6 +51,7 @@ query($domain: String!) {
}
resolver {
texts
coinTypes
}
owner {
id
Expand Down Expand Up @@ -88,6 +94,7 @@ export const getENS = (
const contract = new Contract(contractAddress, ABI, provider)

const getRecord = async (node: string, record: string) => await contract.text(node, record)
const getMulticoinAddress = async (node: string, coinType: number) => await contract.addr(node, coinType)

return async function getENS(_domain: string, fetchOptions?: RequestInit): Promise<ResolvedENS> {
const domain = ADDRESS_REGEX.test(_domain) ? await provider.lookupAddress(_domain) : _domain
Expand All @@ -97,7 +104,8 @@ export const getENS = (
owner: _domain,
address: _domain,
domain: null,
records: {}
records: {},
coinTypes: {}
}
}

Expand All @@ -111,8 +119,8 @@ export const getENS = (
},
fetchOptions
)

const records: ENSRecords = {}
const coinTypes = {}

if (domains?.[0]) {
const { resolvedAddress: address, resolver, owner } = domains?.[0]
Expand All @@ -121,7 +129,9 @@ export const getENS = (
owner: null,
address: null,
domain,
records: {}
records: {},
coinTypes: {}

}

if (owner) data.owner = owner.id
Expand All @@ -131,6 +141,7 @@ export const getENS = (
if (!resolver?.texts) {
return data
} else {

for (const record of resolver.texts) {
if (record.startsWith('com.') || record.startsWith('vnd.')) {
records[record.slice(record.indexOf('.') + 1)] = await getRecord(node, record)
Expand All @@ -140,9 +151,17 @@ export const getENS = (
}

data.records = records
}

if (!resolver?.coinTypes) {
return data
} else {
for (const coinType of resolver.coinTypes) {
coinTypes[coinType] = await getMulticoinAddress(node, coinType)
}
data.coinTypes = coinTypes
}
return data
}
}
}
27 changes: 22 additions & 5 deletions tests/getENS.test.ts
Expand Up @@ -25,7 +25,14 @@ t('resolves the address and text records', async () => {
github: 'talentlessguy',
reddit: 'v1rtl'
},
domain: 'v1rtl.eth'
domain: 'v1rtl.eth',
coinTypes: {
'0': '0xa9140575e888bbb4dc7fa0c344a20b07e99516ee9c0d87',
'128': '0x124306de2e02868589c0709894b6c71daea74ed66b6007b17cbc08735a615703693b6b5d64bdc33e7f3920873b93d256116f5c4f8d47cf5adfd718a7a43f43c9495cdf6b7d',
'2': '0xa914922122dc91b59071af4725d9d8dbcff796657f6d87',
'3': '0x76a914b484c576c107ca6a614ce0dd024d1021f7ee4abe88ac',
'60': '0xd3b282e9880cdcb1142830731cd83f7ac0e1043f'
},
})
})

Expand All @@ -34,7 +41,8 @@ t('resolves without address', async () => {
address: null,
owner: '0xe5501bc2b0df6d0d7daafc18d2ef127d9e612963',
domain: 'lilnasx.eth',
records: {}
records: {},
coinTypes: {}
})
})

Expand All @@ -45,7 +53,8 @@ t('supports custom fetch options', async () => {
address: null,
owner: '0xe5501bc2b0df6d0d7daafc18d2ef127d9e612963',
domain: 'lilnasx.eth',
records: {}
records: {},
coinTypes: {}
})
})

Expand All @@ -65,7 +74,14 @@ t('does a reverse lookup if ethereum address is passed', async () => {
github: 'talentlessguy',
reddit: 'v1rtl'
},
domain: 'v1rtl.eth'
domain: 'v1rtl.eth',
coinTypes: {
'0': '0xa9140575e888bbb4dc7fa0c344a20b07e99516ee9c0d87',
'128': '0x124306de2e02868589c0709894b6c71daea74ed66b6007b17cbc08735a615703693b6b5d64bdc33e7f3920873b93d256116f5c4f8d47cf5adfd718a7a43f43c9495cdf6b7d',
'2': '0xa914922122dc91b59071af4725d9d8dbcff796657f6d87',
'3': '0x76a914b484c576c107ca6a614ce0dd024d1021f7ee4abe88ac',
'60': '0xd3b282e9880cdcb1142830731cd83f7ac0e1043f'
},
})
})

Expand All @@ -76,7 +92,8 @@ t('returns "null" for a domain if ENS is not present', async () => {
address: '0x604Ee422975E74050Eeaa3fC74BAbf6E008C0acC',
owner: '0x604Ee422975E74050Eeaa3fC74BAbf6E008C0acC',
domain: null,
records: {}
records: {},
coinTypes: {},
})
})

Expand Down

0 comments on commit 505b474

Please sign in to comment.