diff --git a/.env.example b/.env.example index 52800290..5094b85e 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,4 @@ -L1_TEST_NET_RPC_URL=https://goerli.infura.io/v3/ -L1_TEST_NET_RPC_WSS_URL=wss://goerli.infura.io/ws/v3/ -L2_TEST_NET_RPC_URL=https://polygon-mumbai.infura.io/v3/ -L2_TEST_NET_RPC_WSS_URL=wss://polygon-mumbai.g.alchemy.com/v2/ +L1_TEST_NET_RPC_URL=https://sepolia.infura.io/v3/ +L1_TEST_NET_RPC_WSS_URL=wss://sepolia.infura.io/ws/v3/ +L2_TEST_NET_RPC_URL=https://polygon-amoy.infura.io/v3/ +L2_TEST_NET_RPC_WSS_URL=wss://polygon-amoy.g.alchemy.com/v2/ diff --git a/src/Ens.ts b/src/Ens.ts index d1e67ee9..31fdd22d 100644 --- a/src/Ens.ts +++ b/src/Ens.ts @@ -15,7 +15,6 @@ import { Locations, NamingServiceName, Provider, - TokenUriMetadata, BlockchainType, DnsRecordType, DnsRecord, @@ -289,7 +288,6 @@ export default class Ens extends NamingService { let domainName = ''; const nameWrapperMetadataResponse = await Networking.fetch( `https://metadata.ens.domains/${this.networkName}/${this.nameWrapperContract.address}/${hash}`, - {}, ); if (nameWrapperMetadataResponse.status === 200) { const jsonResponse = await nameWrapperMetadataResponse.json(); @@ -299,7 +297,6 @@ export default class Ens extends NamingService { const baseRegistrarMetadataResponse = await Networking.fetch( `https://metadata.ens.domains/${this.networkName}/${this.baseRegistrarContract.address}/${hash}`, - {}, ); if (baseRegistrarMetadataResponse.status === 200) { @@ -614,19 +611,4 @@ export default class Ens extends NamingService { this.namehash(domain), ]); } - - private async getMetadataFromTokenURI( - tokenUri: string, - ): Promise { - const resp = await Networking.fetch(tokenUri, {}); - if (resp.ok) { - return resp.json(); - } - - throw new ResolutionError(ResolutionErrorCode.ServiceProviderError, { - providerMessage: await resp.text(), - method: 'UDAPI', - methodName: 'tokenURIMetadata', - }); - } } diff --git a/src/Resolution.ts b/src/Resolution.ts index c479f7d6..4b34714a 100644 --- a/src/Resolution.ts +++ b/src/Resolution.ts @@ -662,7 +662,7 @@ export default class Resolution { async email(domain: string): Promise { domain = prepareAndValidateDomain(domain); let key = 'whois.email.value'; - const serviceName = findNamingServiceName(domain); + const serviceName = await findNamingServiceName(domain); if (serviceName === 'ENS') { key = 'email'; } @@ -1062,7 +1062,7 @@ export default class Resolution { private async getMetadataFromTokenURI( tokenUri: string, ): Promise { - const resp = await Networking.fetch(tokenUri, {}); + const resp = await Networking.fetch(tokenUri); if (resp.ok) { return resp.json(); } @@ -1102,7 +1102,7 @@ export default class Resolution { domain: string, func: (service: NamingService) => T, ): Promise> { - const serviceName = findNamingServiceName(domain); + const serviceName = await findNamingServiceName(domain); if (!serviceName) { throw new ResolutionError(ResolutionErrorCode.UnsupportedDomain, { domain, @@ -1141,7 +1141,7 @@ export default class Resolution { func: (service: NamingService) => Promise, options: {throwIfUnsupportedDomain: boolean; expectedValue: boolean}, ): Promise { - const serviceName = findNamingServiceName(domain); + const serviceName = await findNamingServiceName(domain); if (!serviceName) { if (!options.throwIfUnsupportedDomain) { return !options.expectedValue; diff --git a/src/UdApi.ts b/src/UdApi.ts index 142e55da..81b8a0b3 100644 --- a/src/UdApi.ts +++ b/src/UdApi.ts @@ -31,9 +31,9 @@ export default class UdApi extends NamingService { constructor(api?: Api) { super(); - this.url = api?.url || 'https://unstoppabledomains.com/api/v1'; + this.url = api?.url || 'https://unstoppabledomains.com/api/v1'; // TODO: this is about to be deprecated const DefaultUserAgent = - 'cross-fetch/3.1.4 (+https://github.com/lquixada/cross-fetch)'; + 'cross-fetch/4.0.0 (+https://github.com/lquixada/cross-fetch)'; const CustomUserAgent = `${DefaultUserAgent} Resolution`; this.headers = {'X-user-agent': CustomUserAgent}; } @@ -77,7 +77,7 @@ export default class UdApi extends NamingService { } async twitter(domain: string): Promise { - const serviceName = findNamingServiceName(domain); + const serviceName = await findNamingServiceName(domain); if (serviceName !== NamingServiceName.UNS) { throw new ResolutionError(ResolutionErrorCode.UnsupportedMethod, { domain, @@ -150,7 +150,7 @@ export default class UdApi extends NamingService { private async getMetadata(tokenId: string): Promise { const tokenUri = `${this.url}/${tokenId}`; - const resp = await Networking.fetch(tokenUri, {}).catch((err) => { + const resp = await Networking.fetch(tokenUri).catch((err) => { throw new ResolutionError(ResolutionErrorCode.MetadataEndpointError, { tokenUri: tokenUri || 'undefined', errorMessage: err.message, diff --git a/src/Uns.ts b/src/Uns.ts index 66b35dd5..f0459606 100644 --- a/src/Uns.ts +++ b/src/Uns.ts @@ -99,7 +99,7 @@ export default class Uns extends NamingService { this.unsl2 = new UnsInternal( UnsLocation.Layer2, source.locations.Layer2, - BlockchainType.MATIC, + BlockchainType.POL, ); } @@ -447,7 +447,7 @@ export default class Uns extends NamingService { private async getMetadata(tokenId: string): Promise { const tokenUri = await this.getTokenUri(tokenId); - const resp = await Networking.fetch(tokenUri, {}).catch((err) => { + const resp = await Networking.fetch(tokenUri).catch((err) => { throw new ResolutionError(ResolutionErrorCode.MetadataEndpointError, { tokenUri: tokenUri || 'undefined', errorMessage: err.message, diff --git a/src/tests/Ens.test.ts b/src/tests/Ens.test.ts index 1cf6f9c1..4d006b72 100644 --- a/src/tests/Ens.test.ts +++ b/src/tests/Ens.test.ts @@ -11,6 +11,8 @@ import { } from './helpers'; import Ens from '../Ens'; +const ETH_L1_TESTNET_NAME = 'sepolia'; + let resolution: Resolution; let ens: Ens; @@ -24,7 +26,7 @@ beforeEach(() => { ProviderProtocol.http, NamingServiceName.ENS, ), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, }, }); diff --git a/src/tests/Resolution.test.ts b/src/tests/Resolution.test.ts index 65bcde74..0b1a9339 100644 --- a/src/tests/Resolution.test.ts +++ b/src/tests/Resolution.test.ts @@ -55,6 +55,9 @@ let uns: Uns; let zns: Zns; let ens: Ens; +const ETH_L1_TESTNET_NAME = 'sepolia'; +const POL_L2_TESTNET_NAME = 'polygon-amoy'; + beforeEach(() => { nock.cleanAll(); jest.restoreAllMocks(); @@ -64,11 +67,11 @@ beforeEach(() => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -77,7 +80,7 @@ beforeEach(() => { ProviderProtocol.http, NamingServiceName.ENS, ), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, zns: {network: 'testnet'}, }, @@ -102,8 +105,8 @@ describe('Resolution', () => { describe('.Basic setup', () => { it('should work with autonetwork url configuration', async () => { - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); - const goerliUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); // mocking getNetworkConfigs because no access to inner provider.request const UnsGetNetworkOriginal = Uns.autoNetwork; const EnsGetNetworkOriginal = Ens.autoNetwork; @@ -112,29 +115,29 @@ describe('Resolution', () => { new Uns({ locations: { Layer1: { - network: 'goerli', - provider: new FetchProvider(UnsLocation.Layer1, goerliUrl), + network: ETH_L1_TESTNET_NAME, + provider: new FetchProvider(UnsLocation.Layer1, l1Url), }, Layer2: { - network: 'polygon-mumbai', - provider: new FetchProvider(UnsLocation.Layer2, polygonUrl), + network: POL_L2_TESTNET_NAME, + provider: new FetchProvider(UnsLocation.Layer2, l2Url), }, }, }), ); Ens.autoNetwork = jest.fn().mockReturnValue( new Ens({ - url: goerliUrl, - network: 'goerli', - provider: new FetchProvider(NamingServiceName.ENS, goerliUrl), + url: l1Url, + network: ETH_L1_TESTNET_NAME, + provider: new FetchProvider(NamingServiceName.ENS, l1Url), }), ); } const resolution = await Resolution.autoNetwork({ uns: { - locations: {Layer1: {url: goerliUrl}, Layer2: {url: polygonUrl}}, + locations: {Layer1: {url: l1Url}, Layer2: {url: l2Url}}, }, - ens: {url: goerliUrl}, + ens: {url: l1Url}, }); // We need to manually restore the function as jest.restoreAllMocks and simillar works only with spyOn Uns.autoNetwork = UnsGetNetworkOriginal; @@ -142,52 +145,53 @@ describe('Resolution', () => { expect( (resolution.serviceMap[NamingServiceName.UNS].native as Uns).unsl1 .network, - ).toBe('goerli'); + ).toBe(ETH_L1_TESTNET_NAME); expect( (resolution.serviceMap[NamingServiceName.UNS].native as Uns).unsl2 .network, - ).toBe('polygon-mumbai'); + ).toBe(POL_L2_TESTNET_NAME); expect( (resolution.serviceMap[NamingServiceName.ENS].native as Ens).network, ).toBe(5); }); it('should not work with invalid proxyReader configuration #1', async () => { - const goerliUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); - const customNetwork = 'goerli'; - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + await expectConfigurationErrorCode(() => { new Uns({ locations: { Layer1: { - network: customNetwork, - url: goerliUrl, + network: ETH_L1_TESTNET_NAME, + url: l1Url, proxyReaderAddress: '0x012312931293', }, Layer2: { - network: 'polygon-mumbai', - url: polygonUrl, + network: POL_L2_TESTNET_NAME, + url: l2Url, proxyReaderAddress: '0x012312931293', }, }, }); }, ConfigurationErrorCode.InvalidConfigurationField); }); + it('should not work with invalid proxyReader configuration #2', async () => { - const goerliUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); - const customNetwork = 'goerli'; - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + await expect(() => { new Uns({ locations: { Layer1: { - network: customNetwork, - url: goerliUrl, + network: ETH_L1_TESTNET_NAME, + url: l1Url, proxyReaderAddress: '0xe7474D07fD2FA286e7e0aa23cd107F8379025037', }, Layer2: { - network: 'polygon-mumbai', - url: polygonUrl, + network: POL_L2_TESTNET_NAME, + url: l2Url, proxyReaderAddress: '0x012312931293', }, }, @@ -204,22 +208,22 @@ describe('Resolution', () => { }); it('should not work with invalid proxyReader configuration #3', async () => { - const goerliUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); - const provider = new FetchProvider(NamingServiceName.UNS, goerliUrl); - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); - const polygonProvider = new FetchProvider(UnsLocation.Layer2, polygonUrl); - const customNetwork = 'goerli'; + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l1Provider = new FetchProvider(NamingServiceName.UNS, l1Url); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const l2Provider = new FetchProvider(UnsLocation.Layer2, l2Url); + await expectConfigurationErrorCode(() => { new Uns({ locations: { Layer1: { - network: customNetwork, - provider, + network: ETH_L1_TESTNET_NAME, + provider: l1Provider, proxyReaderAddress: '0xe7474D07fD2FA286e7e0aa23cd107F8379025037', }, Layer2: { - network: 'polygon-mumbai', - provider: polygonProvider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider, proxyReaderAddress: '0x332a8191905fa8e6eea7350b5799f225b8ed', }, }, @@ -228,20 +232,19 @@ describe('Resolution', () => { }); it('should work with proxyReader configuration', async () => { - const goerliUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); - const customNetwork = 'goerli'; + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); const uns = new Uns({ locations: { Layer1: { - network: customNetwork, - url: goerliUrl, + network: ETH_L1_TESTNET_NAME, + url: l1Url, proxyReaderAddress: '0xe7474D07fD2FA286e7e0aa23cd107F8379025037', }, Layer2: { - network: 'polygon-mumbai', - url: polygonUrl, + network: POL_L2_TESTNET_NAME, + url: l2Url, proxyReaderAddress: '0x332a8191905fa8e6eea7350b5799f225b8ed30a9', }, }, @@ -250,21 +253,21 @@ describe('Resolution', () => { }); it('should work with custom network configuration with provider', async () => { - const goerliUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); - const provider = new FetchProvider(NamingServiceName.UNS, goerliUrl); - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); - const polygonProvider = new FetchProvider(UnsLocation.Layer2, polygonUrl); - const customNetwork = 'goerli'; + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l1Provider = new FetchProvider(NamingServiceName.UNS, l1Url); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const l2Provider = new FetchProvider(UnsLocation.Layer2, l2Url); + const uns = new Uns({ locations: { Layer1: { - network: customNetwork, - provider, + network: ETH_L1_TESTNET_NAME, + provider: l1Provider, proxyReaderAddress: '0xe7447Fdd52FA286e7e0aa23cd107F83790250897', }, Layer2: { - network: 'polygon-mumbai', - provider: polygonProvider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider, proxyReaderAddress: '0x332a8191905fa8e6eea7350b5799f225b8ed30a9', }, }, @@ -278,13 +281,13 @@ describe('Resolution', () => { getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), ); - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); - const polygonProvider = new FetchProvider(UnsLocation.Layer2, polygonUrl); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const l2Provider = new FetchProvider(UnsLocation.Layer2, l2Url); const spy = mockAsyncMethod(provider, 'request', '1'); - const spyTwo = mockAsyncMethod(polygonProvider, 'request', '80001'); + const spyTwo = mockAsyncMethod(l2Provider, 'request', '80001'); const resolution = await Resolution.autoNetwork({ uns: { - locations: {Layer1: {provider}, Layer2: {provider: polygonProvider}}, + locations: {Layer1: {provider}, Layer2: {provider: l2Provider}}, }, ens: {provider}, }); @@ -297,7 +300,7 @@ describe('Resolution', () => { expect( (resolution.serviceMap[NamingServiceName.UNS].native as Uns).unsl2 .network, - ).toBe('polygon-mumbai'); + ).toBe(POL_L2_TESTNET_NAME); expect( (resolution.serviceMap[NamingServiceName.ENS].native as Ens).network, ).toBe(1); @@ -367,15 +370,9 @@ describe('Resolution', () => { }); it('should fail because of unsupported test network for uns', async () => { - const blockchainUrl = getProtocolLinkFromEnv( - ProviderProtocol.http, - 'UNSL1', - ); - const polygonUrl = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); - const mockedProvider = new FetchProvider( - NamingServiceName.UNS, - blockchainUrl, - ); + const l1Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'); + const l2Url = getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'); + const mockedProvider = new FetchProvider(NamingServiceName.UNS, l1Url); mockAsyncMethod(mockedProvider, 'request', () => '3'); mockAsyncMethod(FetchProvider, 'factory', () => mockedProvider); @@ -383,8 +380,8 @@ describe('Resolution', () => { Resolution.autoNetwork({ uns: { locations: { - Layer1: {url: blockchainUrl}, - Layer2: {url: polygonUrl}, + Layer1: {url: l1Url}, + Layer2: {url: l2Url}, }, }, }), @@ -407,32 +404,40 @@ describe('Resolution', () => { const resolution = Resolution.infura('api-key', { uns: { locations: { - Layer1: {network: 'goerli'}, - Layer2: {network: 'polygon-mumbai'}, + Layer1: {network: ETH_L1_TESTNET_NAME}, + Layer2: {network: POL_L2_TESTNET_NAME}, }, }, - ens: {network: 'goerli'}, + ens: {network: ETH_L1_TESTNET_NAME}, }); uns = resolution.serviceMap[NamingServiceName.UNS].native as Uns; ens = resolution.serviceMap[NamingServiceName.ENS].native as Ens; - expect(uns.unsl1.url).toBe(`https://goerli.infura.io/v3/api-key`); - expect(uns.unsl2.url).toBe(`https://polygon-mumbai.infura.io/v3/api-key`); - expect(ens.url).toBe(`https://goerli.infura.io/v3/api-key`); + expect(uns.unsl1.url).toBe( + `https://${ETH_L1_TESTNET_NAME}.infura.io/v3/api-key`, + ); + expect(uns.unsl2.url).toBe( + `https://${POL_L2_TESTNET_NAME}.infura.io/v3/api-key`, + ); + expect(ens.url).toBe( + `https://${ETH_L1_TESTNET_NAME}.infura.io/v3/api-key`, + ); }); it('should get a valid resolution instance with .alchemy', async () => { const resolution = Resolution.alchemy('api-key', { uns: { locations: { - Layer1: {network: 'goerli'}, - Layer2: {network: 'polygon-mumbai'}, + Layer1: {network: ETH_L1_TESTNET_NAME}, + Layer2: {network: POL_L2_TESTNET_NAME}, }, }, }); uns = resolution.serviceMap[NamingServiceName.UNS].native as Uns; - expect(uns.unsl1.url).toBe(`https://eth-goerli.alchemyapi.io/v2/api-key`); + expect(uns.unsl1.url).toBe( + `https://eth-${ETH_L1_TESTNET_NAME}.g.alchemy.com/v2/api-key`, + ); expect(uns.unsl2.url).toBe( - `https://polygon-mumbai.g.alchemy.com/v2/api-key`, + `https://${POL_L2_TESTNET_NAME}.g.alchemy.com/v2/api-key`, ); }); @@ -615,6 +620,7 @@ describe('Resolution', () => { ); expectSpyToBeCalled([unsSpy, znsSpy]); }); + it('checks Resolution#addr error #2', async () => { const resolution = new Resolution({ sourceConfig: { @@ -622,11 +628,11 @@ describe('Resolution', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -775,6 +781,7 @@ describe('Resolution', () => { expectSpyToBeCalled(eyes, 2); expect(capital).toStrictEqual(lower); }); + describe('.multichain', () => { it('should work with usdt on different erc20', async () => { const erc20Spy = mockAsyncMethod(uns, 'get', { @@ -852,15 +859,15 @@ describe('Resolution', () => { describe('.Providers', () => { it('should work with web3HttpProvider', async () => { // web3-providers-http has problems with type definitions - const provider = new (Web3HttpProvider as any)( + const l1Provider = new (Web3HttpProvider as any)( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), ); - const polygonProvider = new (Web3HttpProvider as any)( + const l2Provider = new (Web3HttpProvider as any)( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), ); // mock the send function with different implementations (each should call callback right away with different answers) const eye = mockAsyncMethod( - provider, + l1Provider, 'send', (payload: JsonRpcPayload, callback) => { const result = caseMock( @@ -879,13 +886,12 @@ describe('Resolution', () => { uns: { locations: { Layer1: { - network: 'goerli', - provider: provider as unknown as Web3Version1Provider, + network: ETH_L1_TESTNET_NAME, + provider: l1Provider as Web3Version1Provider, }, Layer2: { - network: 'polygon-mumbai', - provider: - polygonProvider as unknown as Web3Version1Provider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider as Web3Version1Provider, }, }, }, @@ -905,14 +911,14 @@ describe('Resolution', () => { it('should work with webSocketProvider', async () => { // web3-providers-ws has problems with type definitions - const provider = new (Web3WsProvider as any)( + const l1Provider = new (Web3WsProvider as any)( getProtocolLinkFromEnv(ProviderProtocol.wss, 'UNSL1'), ); - const polygonProvider = new (Web3WsProvider as any)( + const l2Provider = new (Web3WsProvider as any)( getProtocolLinkFromEnv(ProviderProtocol.wss, 'UNSL2'), ); const eye = mockAsyncMethod( - provider, + l1Provider, 'send', (payload, callback) => { const result = caseMock( @@ -930,13 +936,12 @@ describe('Resolution', () => { uns: { locations: { Layer1: { - network: 'goerli', - provider: provider as unknown as Web3Version1Provider, + network: ETH_L1_TESTNET_NAME, + provider: l1Provider as Web3Version1Provider, }, Layer2: { - network: 'polygon-mumbai', - provider: - polygonProvider as unknown as Web3Version1Provider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider as Web3Version1Provider, }, }, }, @@ -946,7 +951,7 @@ describe('Resolution', () => { Promise.resolve([NullAddress, NullAddress, {}]), ); const ethAddress = await resolution.addr('brad.crypto', 'ETH'); - provider.disconnect(1000, 'end of test'); + l1Provider.disconnect(1000, 'end of test'); expectSpyToBeCalled([eye]); expect(ethAddress).toBe( '0x8aaD44321A86b170879d7A244c1e8d360c99DdA8', @@ -954,21 +959,21 @@ describe('Resolution', () => { }); it('should work for ethers jsonrpc provider', async () => { - const provider = new JsonRpcProvider( + const l1Provider = new JsonRpcProvider( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - 'goerli', + ETH_L1_TESTNET_NAME, ); - const polygonProvider = new JsonRpcProvider( + const l2Provider = new JsonRpcProvider( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - 'maticmum', + POL_L2_TESTNET_NAME, ); const resolution = Resolution.fromEthersProvider({ uns: { locations: { - Layer1: {network: 'goerli', provider}, + Layer1: {network: ETH_L1_TESTNET_NAME, provider: l1Provider}, Layer2: { - network: 'polygon-mumbai', - provider: polygonProvider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider, }, }, }, @@ -977,7 +982,7 @@ describe('Resolution', () => { mockAsyncMethod(uns.unsl2.readerContract, 'call', (params) => Promise.resolve([NullAddress, NullAddress, {}]), ); - const eye = mockAsyncMethod(provider, 'call', (params) => + const eye = mockAsyncMethod(l1Provider, 'call', (params) => Promise.resolve(caseMock(params, RpcProviderTestCases)), ); const ethAddress = await resolution.addr('brad.crypto', 'ETH'); @@ -989,14 +994,14 @@ describe('Resolution', () => { it('should work with webSocketProvider', async () => { // web3-providers-ws has problems with type definitions - const provider = new (Web3WsProvider as any)( + const l1Provider = new (Web3WsProvider as any)( getProtocolLinkFromEnv(ProviderProtocol.wss, 'UNSL1'), ); - const polygonProvider = new (Web3WsProvider as any)( + const l2Provider = new (Web3WsProvider as any)( getProtocolLinkFromEnv(ProviderProtocol.wss, 'UNSL2'), ); const eye = mockAsyncMethod( - provider, + l1Provider, 'send', (payload, callback) => { const result = caseMock( @@ -1015,13 +1020,12 @@ describe('Resolution', () => { uns: { locations: { Layer1: { - network: 'goerli', - provider: provider as unknown as Web3Version1Provider, + network: ETH_L1_TESTNET_NAME, + provider: l1Provider as Web3Version1Provider, }, Layer2: { - network: 'polygon-mumbai', - provider: - polygonProvider as unknown as Web3Version1Provider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider as Web3Version1Provider, }, }, }, @@ -1031,7 +1035,7 @@ describe('Resolution', () => { Promise.resolve([NullAddress, NullAddress, {}]), ); const ethAddress = await resolution.addr('brad.crypto', 'ETH'); - provider.disconnect(1000, 'end of test'); + l1Provider.disconnect(1000, 'end of test'); expectSpyToBeCalled([eye]); expect(ethAddress).toBe( '0x8aaD44321A86b170879d7A244c1e8d360c99DdA8', @@ -1039,21 +1043,21 @@ describe('Resolution', () => { }); it('should work for ethers jsonrpc provider', async () => { - const provider = new JsonRpcProvider( + const l1Provider = new JsonRpcProvider( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - 'goerli', + ETH_L1_TESTNET_NAME, ); - const polygonProvider = new JsonRpcProvider( + const l2Provider = new JsonRpcProvider( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - 'maticmum', + POL_L2_TESTNET_NAME, ); const resolution = Resolution.fromEthersProvider({ uns: { locations: { - Layer1: {network: 'goerli', provider}, + Layer1: {network: ETH_L1_TESTNET_NAME, provider: l1Provider}, Layer2: { - network: 'polygon-mumbai', - provider: polygonProvider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider, }, }, }, @@ -1062,7 +1066,7 @@ describe('Resolution', () => { mockAsyncMethod(uns.unsl2.readerContract, 'call', (params) => Promise.resolve([NullAddress, NullAddress, {}]), ); - const eye = mockAsyncMethod(provider, 'call', (params) => + const eye = mockAsyncMethod(l1Provider, 'call', (params) => Promise.resolve(caseMock(params, RpcProviderTestCases)), ); const ethAddress = await resolution.addr('brad.crypto', 'ETH'); @@ -1073,25 +1077,25 @@ describe('Resolution', () => { }); it('should work with ethers default provider', async () => { - const provider = new InfuraProvider( - 'goerli', + const l1Provider = new InfuraProvider( + ETH_L1_TESTNET_NAME, '213fff28936343858ca9c5115eff1419', ); - const polygonProvider = new InfuraProvider( - 'maticmum', + const l2Provider = new InfuraProvider( + POL_L2_TESTNET_NAME, 'a32aa2ace9704ee9a1a9906418bcabe5', ); - const eye = mockAsyncMethod(provider, 'call', (params) => + const eye = mockAsyncMethod(l1Provider, 'call', (params) => Promise.resolve(caseMock(params, RpcProviderTestCases)), ); const resolution = Resolution.fromEthersProvider({ uns: { locations: { - Layer1: {network: 'goerli', provider}, + Layer1: {network: ETH_L1_TESTNET_NAME, provider: l1Provider}, Layer2: { - network: 'polygon-mumbai', - provider: polygonProvider, + network: POL_L2_TESTNET_NAME, + provider: l2Provider, }, }, }, @@ -1789,7 +1793,7 @@ describe('Resolution', () => { registryAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', resolverAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', networkId: 80001, - blockchain: BlockchainType.MATIC, + blockchain: BlockchainType.POL, ownerAddress: '0x499dD6D875787869670900a2130223D85d4F6Aa7', blockchainProviderUrl: getProtocolLinkFromEnv( ProviderProtocol.http, @@ -1802,7 +1806,7 @@ describe('Resolution', () => { registryAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', resolverAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', networkId: 80001, - blockchain: BlockchainType.MATIC, + blockchain: BlockchainType.POL, ownerAddress: '0x499dD6D875787869670900a2130223D85d4F6Aa7', blockchainProviderUrl: getProtocolLinkFromEnv( ProviderProtocol.http, @@ -1817,7 +1821,7 @@ describe('Resolution', () => { registryAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', resolverAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', networkId: 80001, - blockchain: BlockchainType.MATIC, + blockchain: BlockchainType.POL, ownerAddress: '0x499dD6D875787869670900a2130223D85d4F6Aa7', blockchainProviderUrl: getProtocolLinkFromEnv( ProviderProtocol.http, diff --git a/src/tests/Uns.test.ts b/src/tests/Uns.test.ts index 88c21b93..7ea9c85e 100644 --- a/src/tests/Uns.test.ts +++ b/src/tests/Uns.test.ts @@ -30,6 +30,9 @@ import UnsConfig from '../config/uns-config.json'; import {eip137Namehash, fromHexStringToDecimals} from '../utils/namehash'; import Zns from '../Zns'; +const ETH_L1_TESTNET_NAME = 'sepolia'; +const POL_L2_TESTNET_NAME = 'polygon-amoy'; + describe('UNS', () => { describe('constructor', () => { it('should define the default uns contract', () => { @@ -37,20 +40,20 @@ describe('UNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }); expect(uns).toBeDefined(); - expect(uns.unsl1.network).toBe('goerli'); + expect(uns.unsl1.network).toBe(ETH_L1_TESTNET_NAME); expect(uns.unsl1.url).toBe( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), ); - expect(uns.unsl2.network).toBe('polygon-mumbai'); + expect(uns.unsl2.network).toBe(POL_L2_TESTNET_NAME); expect(uns.unsl2.url).toBe( getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), ); @@ -101,9 +104,9 @@ describe('UNS', () => { sourceConfig: { uns: { locations: { - Layer1: {network: 'goerli'}, + Layer1: {network: ETH_L1_TESTNET_NAME}, Layer2: { - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, url: 'https://someurl.com', }, }, @@ -121,9 +124,12 @@ describe('UNS', () => { sourceConfig: { uns: { locations: { - Layer1: {network: 'goerli', url: 'https://someurl.com'}, + Layer1: { + network: ETH_L1_TESTNET_NAME, + url: 'https://someurl.com', + }, Layer2: { - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -146,11 +152,11 @@ describe('UNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -1534,9 +1540,9 @@ describe('UNS', () => { sourceConfig: { uns: { locations: { - Layer1: {url, provider, network: 'goerli'}, + Layer1: {url, provider, network: ETH_L1_TESTNET_NAME}, Layer2: { - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, provider: polygonProvider, }, }, diff --git a/src/tests/UnsInternal.test.ts b/src/tests/UnsInternal.test.ts index a9319325..2e5173d5 100644 --- a/src/tests/UnsInternal.test.ts +++ b/src/tests/UnsInternal.test.ts @@ -20,6 +20,9 @@ import {ResolutionError, ResolutionErrorCode} from '../errors/resolutionError'; import {eip137Namehash, fromHexStringToDecimals} from '../utils/namehash'; import Networking from '../utils/Networking'; +const ETH_L1_TESTNET_NAME = 'sepolia'; +const POL_L2_TESTNET_NAME = 'polygon-amoy'; + let unsInternalL1: UnsInternal; let unsInternalL2: UnsInternal; @@ -29,7 +32,7 @@ beforeEach(async () => { UnsLocation.Layer1, { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, BlockchainType.ETH, ); @@ -37,9 +40,9 @@ beforeEach(async () => { UnsLocation.Layer2, { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, - BlockchainType.MATIC, + BlockchainType.POL, ); }); @@ -131,7 +134,7 @@ describe('UnsInternal', () => { UnsLocation.Layer1, { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, proxyServiceApiKey: 'some key', }, BlockchainType.ETH, @@ -399,7 +402,7 @@ describe('UnsInternal', () => { registryAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', resolverAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', networkId: 80001, - blockchain: BlockchainType.MATIC, + blockchain: BlockchainType.POL, ownerAddress: '0x499dD6D875787869670900a2130223D85d4F6Aa7', blockchainProviderUrl: getProtocolLinkFromEnv( ProviderProtocol.http, @@ -423,7 +426,7 @@ describe('UnsInternal', () => { registryAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', resolverAddress: '0x2a93C52E7B6E7054870758e15A1446E769EdfB93', networkId: 80001, - blockchain: BlockchainType.MATIC, + blockchain: BlockchainType.POL, ownerAddress: '0x499dD6D875787869670900a2130223D85d4F6Aa7', blockchainProviderUrl: getProtocolLinkFromEnv( ProviderProtocol.http, @@ -439,7 +442,7 @@ describe('UnsInternal', () => { UnsLocation.Layer1, { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, proxyServiceApiKey: 'some key', }, BlockchainType.ETH, diff --git a/src/tests/Zns.test.ts b/src/tests/Zns.test.ts index a1a154e1..0aa6e0e3 100644 --- a/src/tests/Zns.test.ts +++ b/src/tests/Zns.test.ts @@ -15,6 +15,9 @@ import {ConfigurationErrorCode} from '../errors/configurationError'; import Uns from '../Uns'; import ResolutionError from '../errors/resolutionError'; +const ETH_L1_TESTNET_NAME = 'sepolia'; +const POL_L2_TESTNET_NAME = 'polygon-amoy'; + let resolution: Resolution; let zns: Zns; let uns: Uns; @@ -28,11 +31,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -63,11 +66,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -86,11 +89,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -109,11 +112,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -134,11 +137,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -159,11 +162,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -185,11 +188,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -212,11 +215,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -239,11 +242,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -265,11 +268,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -288,11 +291,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, @@ -317,11 +320,11 @@ describe('ZNS', () => { locations: { Layer1: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL1'), - network: 'goerli', + network: ETH_L1_TESTNET_NAME, }, Layer2: { url: getProtocolLinkFromEnv(ProviderProtocol.http, 'UNSL2'), - network: 'polygon-mumbai', + network: POL_L2_TESTNET_NAME, }, }, }, diff --git a/src/tests/testData/mockData.json b/src/tests/testData/mockData.json index baa3a175..15b7c26a 100644 --- a/src/tests/testData/mockData.json +++ b/src/tests/testData/mockData.json @@ -602,7 +602,7 @@ "METHOD": "POST", "REQUEST": { "method": "POST", - "url": "https://goerli.infura.io/v3/a32aa2ace9704ee9a1a9906418bcabe5", + "url": "https://sepolia.infura.io/v3/a32aa2ace9704ee9a1a9906418bcabe5", "headers": { "content-type": ["application/json"], "accept": ["*/*"], diff --git a/src/types/index.ts b/src/types/index.ts index 4d1631d0..972b7d02 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -130,7 +130,7 @@ export const UnsSupportedNetwork = StringUnion( export type UnsSupportedNetwork = typeof UnsSupportedNetwork.type; -export const EnsSupportedNetwork = StringUnion('mainnet', 'goerli'); +export const EnsSupportedNetwork = StringUnion('mainnet', 'sepolia'); export const ZnsSupportedNetwork = StringUnion('mainnet', 'testnet'); diff --git a/src/types/publicTypes.ts b/src/types/publicTypes.ts index 3d2cfa48..8fe924f3 100644 --- a/src/types/publicTypes.ts +++ b/src/types/publicTypes.ts @@ -156,7 +156,6 @@ export interface EthersProvider { getLogs(filter: EventFilter): Promise; } -export const UDApiDefaultUrl = 'https://unstoppabledomains.com/api/v1'; export type NamehashOptions = { readonly format?: 'dec' | 'hex'; readonly prefix?: boolean; @@ -279,7 +278,7 @@ export interface DomainMetadata extends Erc721Metadata { export enum BlockchainType { ETH = 'ETH', - MATIC = 'MATIC', + POL = 'POL', ZIL = 'ZIL', BASE = 'BASE', } diff --git a/src/utils/Networking.ts b/src/utils/Networking.ts index 3ebd5d3b..1822b89f 100644 --- a/src/utils/Networking.ts +++ b/src/utils/Networking.ts @@ -3,7 +3,11 @@ import crossFetch from 'cross-fetch'; export default class Networking { static async fetch( url: string, - options: {body?: string; headers?: Record; method?: string}, + options?: { + body?: string; + headers?: Record; + method?: string; + }, ): Promise { return crossFetch(url, options); } diff --git a/src/utils/Tlds.ts b/src/utils/Tlds.ts new file mode 100644 index 00000000..ecf51c66 --- /dev/null +++ b/src/utils/Tlds.ts @@ -0,0 +1,42 @@ +import type {BlockchainType} from '../types/publicTypes'; +import Networking from './Networking'; + +type TldsMeta = Record< + string, + { + namingService: 'UNS' | 'ENS' | 'ZNS' | 'DNS'; + registrationBlockchain: BlockchainType; + } +>; + +export default class Tlds { + private static supportedTlds: string[]; + private static tldsMeta: TldsMeta; + + private static async loadSupportedTldsData() { + try { + const res = await Networking.fetch( + 'https://api.unstoppabledomains.com/resolve/supported_tlds', + ); + const data: {tlds: string[]; meta: TldsMeta} = await res.json(); + Tlds.supportedTlds = data.tlds; + Tlds.tldsMeta = data.meta; + } catch (error) { + throw new Error(`Failed to load supported TLDs data: ${error}`); + } + } + + public static async getSupportedTlds(): Promise> { + if (!Tlds.supportedTlds) { + await Tlds.loadSupportedTldsData(); + } + return Tlds.supportedTlds; + } + + public static async getTldsMeta(): Promise> { + if (!Tlds.tldsMeta) { + await Tlds.loadSupportedTldsData(); + } + return Tlds.tldsMeta; + } +} diff --git a/src/utils/index.ts b/src/utils/index.ts index 0e5ce080..4cf06b52 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,6 +1,12 @@ -import {CryptoRecords, NamingServiceName} from '../types/publicTypes'; +import { + BlockchainType, + CryptoRecords, + NamingServiceName, +} from '../types/publicTypes'; import {NullAddresses} from '../types'; import {UnsSupportedNetwork} from '../types'; +import Tlds from './Tlds'; +import ResolutionError, {ResolutionErrorCode} from '../errors/resolutionError'; type Providers = 'infura' | 'alchemy'; type NetworkSignedLinkURLs = Record; @@ -14,8 +20,8 @@ const ProviderURLMap: Record = { 'base-sepolia': 'https://base-sepolia.infura.io/v3/', }, alchemy: { - mainnet: 'https://eth-mainnet.alchemyapi.io/v2/', - sepolia: 'https://eth-sepolia.alchemyapi.io/v2/', + mainnet: 'https://eth-mainnet.g.alchemy.com/v2/', + sepolia: 'https://eth-sepolia.g.alchemy.com/v2/', 'polygon-mainnet': 'https://polygon-mainnet.g.alchemy.com/v2/', 'polygon-amoy': 'https://polygon-amoy.g.alchemy.com/v2/', 'base-mainnet': 'https://base-mainnet.g.alchemy.com/v2/', @@ -80,30 +86,49 @@ export function constructRecords( return records; } -// TODO: Replace this with supported_tlds calls -export const domainExtensionToNamingServiceName = { - crypto: NamingServiceName.UNS, - zil: NamingServiceName.ZNS, - eth: NamingServiceName.ENS, - luxe: NamingServiceName.ENS, - xyz: NamingServiceName.ENS, - kred: NamingServiceName.ENS, - reverse: NamingServiceName.ENS, - udtest: NamingServiceName.UNS_BASE, -}; +const EnsTlds = ['eth', 'luxe', 'xyz', 'kred', 'reverse']; -export const findNamingServiceName = ( +export const findNamingServiceName = async ( domain: string, -): NamingServiceName | '' => { - const extension = domain.split('.').pop(); - - if (!extension) { - return ''; - } else if (extension in domainExtensionToNamingServiceName) { - return domainExtensionToNamingServiceName[extension]; - } else { - return domainExtensionToNamingServiceName.crypto; +): Promise => { + const tld = domain.split('.').pop(); + + if (!tld) { + return null; + } + + if (EnsTlds.includes(tld)) { + return NamingServiceName.ENS; } + + const tldsMeta = await Tlds.getTldsMeta(); + + if (!tldsMeta) { + throw new ResolutionError(ResolutionErrorCode.ServiceProviderError, { + providerMessage: + 'TLD meta is not available. Possibly failed to load supported TLDs data', + }); + } + + if (tld in tldsMeta) { + const {namingService, registrationBlockchain} = tldsMeta[tld]; + switch (namingService) { + case 'UNS': { + switch (registrationBlockchain) { + case BlockchainType.BASE: + return NamingServiceName.UNS_BASE; + default: + return NamingServiceName.UNS; + } + } + case 'ENS': + return NamingServiceName.ENS; + case 'ZNS': + return NamingServiceName.ZNS; + } + } + + return null; }; export const EthereumNetworks = {