Skip to content

Commit

Permalink
api: name resolution fallback to NNS v1 during transition
Browse files Browse the repository at this point in the history
  • Loading branch information
apbigcod committed Nov 29, 2024
1 parent cd0b740 commit 02e2119
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 5 deletions.
50 changes: 45 additions & 5 deletions packages/api/resolver/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
import { Request } from "lambda-api";
import { isAddress, isHex } from "viem";
import { Address, isAddress, isHex } from "viem";
import z from "zod";
import RESOLVER_ABI from "../abi/IResolver";
import { createChainClient } from "../shared/chain";
import { createChainClient, Network } from "../shared/chain";
import config from "../shared/config";

const inputSchema = z.object({
address: z.string().refine(isAddress),
clds: z.string().refine(isHex).array().optional(),
fallback: z.boolean().optional(),
disable_v1: z.boolean().optional(),
});

type Input = z.infer<typeof inputSchema>;

type Output = {
name: string | null;
};

export default async function resolveHandler(req: Request): Promise<Output> {
const input = await inputSchema.parseAsync(req.body);
const disableV1 = input.disable_v1 ?? false;

let name = await resolveNNS(input);
if (!name && !disableV1) {
name = await resolveV1(input.address);
}

return {
name: name || null,
};
}

async function resolveNNS(input: Input): Promise<string | null> {
const fallback = input.fallback ?? true;
const clds = input.clds?.map(BigInt) || [];

Expand All @@ -32,7 +48,31 @@ export default async function resolveHandler(req: Request): Promise<Output> {
console.log(e);
return null;
});
return {
name,
};
return name || null;
}

const nnsV1ResolverABI = [
{
stateMutability: "view",
type: "function",
inputs: [{ name: "addr", internalType: "address", type: "address" }],
name: "resolve",
outputs: [{ name: "", internalType: "string", type: "string" }],
},
] as const;

async function resolveV1(address: Address): Promise<string | null> {
const chain = createChainClient(Network.ETH_MAINNET);
const name = await chain
.readContract({
abi: nnsV1ResolverABI,
address: "0x849F92178950f6254db5D16D1ba265E70521aC1B",
functionName: "resolve",
args: [address],
})
.catch((e) => {
console.log(e);
return null;
});
return name || null;
}
67 changes: 67 additions & 0 deletions packages/api/test/resolve.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { describe, expect, test } from "@jest/globals";
import { Request } from "lambda-api";
import { namehash } from "viem";
import resolveHandler from "../resolver/resolve";

describe("Registration - Register", () => {
test.each([
{
test: "has v2 name",
request: {
address: "0x543D53d6f6d15adB6B6c54ce2C4c28a5f2cCb036",
},
expName: "2707.⌐◨-◨",
},
{
test: "only has v1 name",
request: {
// Randomly picked from opensea. Has 9488.⌐◨-◨ but no v2 name yet. Could fail at some point.
address: "0x03DB74Df4Ef8b29fe210abcf027C757747C14f00",
},
expName: null,
},
{
test: "only has eth name",
request: {
// Randomly picked from opensea. Has ooooo1.eth and no NNS.
address: "0xBBF61308d129ee97578Dce421A60F49354F0f4Dd",
},
expName: "ooooo1.eth",
},
{
test: "no v2 name disabling v1",
request: {
address: "0x03DB74Df4Ef8b29fe210abcf027C757747C14f00",
disable_v1: true,
},
expName: null,
},
{
test: "specific cld, no fallback",
request: {
// this account only has 2707.⌐◨-◨
address: "0x543D53d6f6d15adB6B6c54ce2C4c28a5f2cCb036",
clds: [namehash("nouns")],
fallback: false,
disable_v1: true,
},
expName: null,
},
{
test: "specific cld, with fallback",
request: {
// this account only has 2707.⌐◨-◨
address: "0x543D53d6f6d15adB6B6c54ce2C4c28a5f2cCb036",
clds: [namehash("nouns")],
disable_v1: true,
},
expName: "2707.⌐◨-◨",
},
])("$test", async (t) => {
const res = await resolveHandler({
body: t.request,
} as unknown as Request);

expect(res.name).toBe(t.expName);
});
});

0 comments on commit 02e2119

Please sign in to comment.