-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from ensdomains/feat/getAvailable-function
feat: add getAvailable function
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters