Skip to content

Commit

Permalink
api: load ENS names once (#1080)
Browse files Browse the repository at this point in the history
fixes #1048
  • Loading branch information
dcposch authored May 28, 2024
1 parent d4c0130 commit 5094073
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions packages/daimo-api/src/contract/nameRegistry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ export class NameRegistry extends Indexer {
private addrToReg = new Map<Address, Registration>();
private accounts: DAccount[] = [];

private ensReverseLookups = new Map<Address, Promise<string | undefined>>();

logs: Registration[] = [];

constructor(
Expand Down Expand Up @@ -225,25 +227,36 @@ export class NameRegistry extends Indexer {
if (label) return { addr: address, label };

// Finally, ENS reverse lookup
let ensName: string | undefined = undefined;
let promise = this.ensReverseLookups.get(address);
if (promise == null) {
promise = this.getENSReverseLookup(address);
}
const ensName = await promise;

// Bare addresses are fine too, ensName can be undefined
return { addr: address, ensName };
}

async getENSReverseLookup(address: Address): Promise<string | undefined> {
try {
console.log(`[NAME-REG] looking up ENS name for ${address}`);
ensName = (await this.vc.getEnsName({ address })) || undefined;
const ensName = (await this.vc.getEnsName({ address })) || undefined;
if (ensName == null) {
return undefined;
}

// Verify the forward lookup
if (ensName != null) {
const addr = await this.vc.getEnsAddress({ name: ensName });
if (addr !== address) {
console.warn(`[NAME-REG] bad ENS ${address} > ${ensName} > ${addr}`);
ensName = undefined;
}
const addr = await this.vc.getEnsAddress({ name: ensName });
if (addr !== address) {
console.warn(`[NAME-REG] bad ENS ${address} > ${ensName} > ${addr}`);
return undefined;
}
console.log(`[NAME-REG] ENS name for ${address}: ${ensName}`);
return ensName;
} catch (e) {
console.log(`[NAME-REG] ENS lookup failed for ${address}: ${e}`);
return undefined;
}

// Bare addresses are fine too, ensName can be undefined
return { addr: address, ensName };
}

/** Gets an Ethereum account given "alice", "bob.eth", or "0x..." */
Expand Down

0 comments on commit 5094073

Please sign in to comment.