Skip to content

Commit 2b54478

Browse files
wa0x6eChaituVR
andauthored
feat: return owner of .sonic domains (#1168)
* feat: resolve sonic space controller * perf: code improvement --------- Co-authored-by: Chaitanya <[email protected]>
1 parent 45f5e06 commit 2b54478

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

src/utils.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ const ENS_ABI = [
4343
'function text(bytes32 node, string calldata key) external view returns (string memory)',
4444
'function resolver(bytes32 node) view returns (address)' // ENS registry ABI
4545
];
46+
const SONIC_ABI = [
47+
'function ownerOf(uint256 tokenId) external view returns (address address)'
48+
];
49+
const SONIC_CONTRACT_ADDRESS = '0xde1dadcf11a7447c3d093e97fdbd513f488ce3b4';
50+
const ENS_CHAIN_IDS = ['1', '11155111'];
51+
const SHIBARIUM_CHAIN_IDS = ['109', '157'];
52+
const SONIC_CHAIN_IDS = ['146'];
53+
const SONIC_TLD = '.sonic';
54+
const SHIBARIUM_TLD = '.shib';
4655
const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
4756

4857
const scoreApiHeaders = {
@@ -672,7 +681,7 @@ export async function getShibariumNameOwner(
672681
id: string,
673682
network: string
674683
): Promise<string> {
675-
if (!id.endsWith('.shib')) {
684+
if (!id.endsWith(SHIBARIUM_TLD)) {
676685
return EMPTY_ADDRESS;
677686
}
678687

@@ -692,15 +701,43 @@ export async function getShibariumNameOwner(
692701
return data.result;
693702
}
694703

704+
export async function getSonicNameOwner(
705+
id: string,
706+
network: string
707+
): Promise<string> {
708+
if (!id.endsWith(SONIC_TLD)) {
709+
return Promise.resolve(EMPTY_ADDRESS);
710+
}
711+
712+
try {
713+
const hash = namehash(ensNormalize(id));
714+
const tokenId = BigInt(hash).toString();
715+
const provider = getProvider(network);
716+
717+
return await call(
718+
provider,
719+
SONIC_ABI,
720+
[SONIC_CONTRACT_ADDRESS, 'ownerOf', [tokenId]],
721+
{
722+
blockTag: 'latest'
723+
}
724+
);
725+
} catch (e: any) {
726+
return EMPTY_ADDRESS;
727+
}
728+
}
729+
695730
export async function getSpaceController(
696731
id: string,
697732
network = '1',
698733
options: any = {}
699734
): Promise<string> {
700-
if (['1', '11155111'].includes(network)) {
735+
if (ENS_CHAIN_IDS.includes(network)) {
701736
return getEnsSpaceController(id, network, options);
702-
} else if (['109', '157'].includes(network)) {
737+
} else if (SHIBARIUM_CHAIN_IDS.includes(network)) {
703738
return getShibariumNameOwner(id, network);
739+
} else if (SONIC_CHAIN_IDS.includes(network)) {
740+
return getSonicNameOwner(id, network);
704741
}
705742

706743
throw new Error(`Network not supported: ${network}`);

test/e2e/utils.spec.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, test, expect } from 'vitest';
2-
import { getEnsOwner, getShibariumNameOwner } from '../../src/utils';
2+
import {
3+
getEnsOwner,
4+
getShibariumNameOwner,
5+
getSonicNameOwner
6+
} from '../../src/utils';
37

48
const EMPTY_ADDRESS = '0x0000000000000000000000000000000000000000';
59

@@ -92,5 +96,25 @@ describe('utils', () => {
9296
).resolves.toBe('0xc4B06a671831CdD66fdA1A287263103103DEC80D');
9397
});
9498
});
99+
100+
describe('sonic resolver', () => {
101+
test('return an empty address for unrecognized extension', () => {
102+
expect(getSonicNameOwner('invalid.domain', '146')).resolves.toBe(
103+
EMPTY_ADDRESS
104+
);
105+
});
106+
107+
test('return an empty address for un-existing domain', () => {
108+
expect(
109+
getSonicNameOwner('snapshot-not-exist.sonic', '146')
110+
).resolves.toBe(EMPTY_ADDRESS);
111+
});
112+
113+
test('return the name owner on sonic mainnet', () => {
114+
expect(getSonicNameOwner('boorger.sonic', '146')).resolves.toBe(
115+
'0x17Af7086649580ab880060c92F46fc931AB3588B'
116+
);
117+
});
118+
});
95119
});
96120
});

0 commit comments

Comments
 (0)