Skip to content

Commit

Permalink
Merge pull request #80 from ensdomains/feat/getAvailable-function
Browse files Browse the repository at this point in the history
feat: add getAvailable function
  • Loading branch information
TateB authored Nov 18, 2022
2 parents 861e217 + 4ad8ff8 commit 87dd2f1
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/ensjs/src/functions/getAvailable.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ENS } from '..'
import setup from '../tests/setup'

let ensInstance: ENS

beforeAll(async () => {
;({ ensInstance } = await setup())
})

describe('getAvailable', () => {
it('should return false for a name that is unavailable', async () => {
const result = await ensInstance.getAvailable('test123.eth')
expect(typeof result).toBe('boolean')
expect(result).toBe(false)
})
it('should return true for a name that is available', async () => {
const result = await ensInstance.getAvailable('available-name.eth')
expect(typeof result).toBe('boolean')
expect(result).toBe(true)
})
})
37 changes: 37 additions & 0 deletions packages/ensjs/src/functions/getAvailable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ENSArgs } from '..'
import { labelhash } from '../utils/labels'

const raw = async ({ contracts }: ENSArgs<'contracts'>, name: string) => {
const baseRegistrar = await contracts?.getBaseRegistrar()!

const labels = name.split('.')
if (labels.length !== 2 || labels[1] !== 'eth') {
throw new Error('Currently only .eth names can be checked for availability')
}

return {
to: baseRegistrar.address,
data: baseRegistrar.interface.encodeFunctionData('available', [
labelhash(labels[0]),
]),
}
}

const decode = async ({ contracts }: ENSArgs<'contracts'>, data: string) => {
if (data === null) return
const baseRegistrar = await contracts?.getBaseRegistrar()!
try {
const result = baseRegistrar.interface.decodeFunctionResult(
'available',
data,
)
return result['0'] as boolean
} catch {
return
}
}

export default {
raw,
decode,
}
8 changes: 7 additions & 1 deletion packages/ensjs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type burnFuses from './functions/burnFuses'
import type commitName from './functions/commitName'
import type createSubname from './functions/createSubname'
import type deleteSubname from './functions/deleteSubname'
import type getAvailable from './functions/getAvailable'
import type getDNSOwner from './functions/getDNSOwner'
import type getExpiry from './functions/getExpiry'
import type { getHistory } from './functions/getHistory'
Expand All @@ -33,7 +34,7 @@ import type {
} from './functions/getSpecificRecord'
import type getSubnames from './functions/getSubnames'
import type getWrapperData from './functions/getWrapperData'
import importDNSSECName from './functions/importDNSSECName'
import type importDNSSECName from './functions/importDNSSECName'
import type registerName from './functions/registerName'
import type {
// eslint-disable-next-line import/no-named-default
Expand Down Expand Up @@ -533,6 +534,11 @@ export class ENS {
[],
)

public getAvailable = this.generateRawFunction<typeof getAvailable>(
'getAvailable',
['contracts'],
)

public universalWrapper = this.generateRawFunction<typeof universalWrapper>(
'initialGetters',
['contracts'],
Expand Down

0 comments on commit 87dd2f1

Please sign in to comment.