import { JsonRpcProvider } from '@ethersproject/providers' import { FNS } from '..' import setup from '../tests/setup' import { FNSJSError } from '../utils/errors' import { Owner } from './getOwner' let fnsInstance: FNS let revert: Awaited>['revert'] let provider: JsonRpcProvider let accounts: string[] beforeAll(async () => { ;({ fnsInstance, revert, provider } = await setup()) accounts = await provider.listAccounts() }) afterAll(async () => { await revert() }) describe('getOwner', () => { it('should return undefined for an nonexistent .ftm name', async () => { const result = await fnsInstance.getOwner('nonexistent.ftm', { skipGraph: false, }) expect(result).toBeUndefined() }) it('should return correct ownership level and values for a wrapped .ftm name', async () => { const result = await fnsInstance.getOwner('wrapped.ftm') expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: accounts[1], expired: false, }) }) it('should return correct ownership level and values for an expired wrapped .ftm name', async () => { const result = await fnsInstance.getOwner('expired-wrapped.ftm', { skipGraph: false, }) expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: '0x0000000000000000000000000000000000000000', expired: true, }) }) it('should return correct ownership level and values for an unwrapped .ftm name', async () => { const result = await fnsInstance.getOwner('test123.ftm') expect(result).toEqual({ ownershipLevel: 'registrar', owner: accounts[1], registrant: accounts[1], expired: false, }) }) it('should return correct ownership level and values for an expired unwrapped .ftm name', async () => { const result = await fnsInstance.getOwner('expired.ftm', { skipGraph: false, }) expect(result).toEqual({ ownershipLevel: 'registrar', owner: accounts[1], registrant: accounts[1].toLowerCase(), expired: true, }) }) describe('subname', () => { it('should return correct ownership level and values for a unwrapped name', async () => { const result = await fnsInstance.getOwner('test.with-subnames.ftm') expect(result).toEqual({ ownershipLevel: 'registry', owner: accounts[2], }) }) it('should return correct ownership level and values for a wrapped name', async () => { const result = await fnsInstance.getOwner( 'test.wrapped-with-subnames.ftm', ) expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: accounts[2], }) }) it('should return correct ownership level and values for an expired wrapped name', async () => { const result = await fnsInstance.getOwner('test.expired-wrapped.ftm') expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: accounts[2], }) }) }) // Only 2LDftm names need to be tested describe('skipGraph', () => { it('should return undefined for an nonexistent .ftm name', async () => { const result = await fnsInstance.getOwner('nonexistent.ftm', { skipGraph: true, }) expect(result).toBeUndefined() }) it('should return correct ownership level and values for a wrapped .ftm name', async () => { const result = await fnsInstance.getOwner('wrapped.ftm', { skipGraph: true, }) expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: accounts[1], expired: false, }) }) it('should return correct ownership level and values for an expired wrapped .ftm name', async () => { const result = await fnsInstance.getOwner('expired-wrapped.ftm', { skipGraph: true, }) expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: '0x0000000000000000000000000000000000000000', expired: true, }) }) it('should return correct ownership level and values for an unwrapped .ftm name', async () => { const result = await fnsInstance.getOwner('test123.ftm', { skipGraph: true, }) expect(result).toEqual({ ownershipLevel: 'registrar', owner: accounts[1], registrant: accounts[1], expired: false, }) }) it('should return registrant undefined if skipGraph is true for an unwrapped .ftm name', async () => { const result = await fnsInstance.getOwner('expired.ftm', { skipGraph: true, }) expect(result).toEqual({ ownershipLevel: 'registrar', owner: accounts[1], expired: true, }) }) it('should return correct ownership level and values for a unwrapped subname', async () => { const result = await fnsInstance.getOwner('test.with-subnames.ftm', { skipGraph: true, }) expect(result).toEqual({ ownershipLevel: 'registry', owner: accounts[2], }) }) it('should return correct ownership level and values for a wrapped subname', async () => { const result = await fnsInstance.getOwner( 'test.wrapped-with-subnames.ftm', { skipGraph: true }, ) expect(result).toEqual({ ownershipLevel: 'nameWrapper', owner: accounts[2], }) }) }) describe('errors', () => { beforeAll(() => { process.env.NEXT_PUBLIC_FNSJS_DEBUG = 'on' localStorage.setItem('fnsjs-debug', 'FNSJSSubgraphError') }) afterAll(() => { process.env.NEXT_PUBLIC_FNS_DEBUG = 'on' localStorage.removeItem('fnsjs-debug') }) it('should return correct ownership level and values for an expired wrapped .ftm name', async () => { try { await fnsInstance.getOwner('expired-wrapped.ftm', { skipGraph: false, }) expect(true).toBeFalsy() } catch (e: unknown) { const error = e as FNSJSError expect(error).toBeInstanceOf(FNSJSError) expect(error.name).toBe('FNSJSSubgraphError') expect(error.data).toEqual({ owner: '0x0000000000000000000000000000000000000000', ownershipLevel: 'nameWrapper', expired: true, }) } }) it('should return registrant undefined for an expired unwrapped .ftm name', async () => { try { await fnsInstance.getOwner('expired.ftm', { skipGraph: false }) expect(true).toBeFalsy() } catch (e) { const error = e as FNSJSError expect(error).toBeInstanceOf(FNSJSError) expect(error.name).toBe('FNSJSSubgraphError') expect(error.data).toEqual({ owner: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8', ownershipLevel: 'registrar', expired: true, }) } }) it('should return undefined for a name that does not exist', async () => { try { await fnsInstance.getOwner('notexistent.ftm', { skipGraph: false, }) expect(true).toBeFalsy() } catch (e) { const error = e as FNSJSError expect(error).toBeInstanceOf(FNSJSError) expect(error.name).toBe('FNSJSSubgraphError') expect(error.data).toBeUndefined() } }) it('should not throw error for subname ftm', async () => { await expect( fnsInstance.getOwner('test.expired-wrapped.ftm', { skipGraph: false, }), ).resolves.toBeDefined() }) }) })