Skip to content

Commit

Permalink
viem fix avatar bug with ethers5
Browse files Browse the repository at this point in the history
  • Loading branch information
zapaz committed May 6, 2024
1 parent e9cd36c commit ab67abc
Show file tree
Hide file tree
Showing 18 changed files with 565 additions and 230 deletions.
4 changes: 2 additions & 2 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
"@kredeum/skale": "workspace:^",
"ethers": "^5.7.2",
"multiformats": "^13.1.0",
"viem": "^2.9.26"
"viem": "^2.9.32"
},
"devDependencies": {
"@ipld/dag-pb": "^4.1.0",
"@types/findup-sync": "^4.0.5",
"@types/node": "^20.12.7",
"@types/node": "^20.12.8",
"findup-sync": "^5.0.0",
"tslib": "^2.6.2",
"typescript": "^5.4.5"
Expand Down
14 changes: 14 additions & 0 deletions common/scripts/ens/ethers5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { providers } from "ethers";

const main = async () => {
const rpcUrl = "https://rpc.ankr.com/eth";
const provider = new providers.JsonRpcProvider(rpcUrl);

const ensName = await provider.resolveName("zapaz.eth");
console.log("ensName:", ensName);

const ensAvatar = await provider.getAvatar("zapaz.eth");
console.log("ensAvatar:", ensAvatar);
};

main().catch(console.error);
24 changes: 24 additions & 0 deletions common/scripts/ens/viem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { createPublicClient, http } from "viem";
import { normalize } from "viem/ens";
import { mainnet } from "viem/chains";

export const publicClient = createPublicClient({
chain: mainnet,
transport: http()
});

const address = "0x981ab0D817710d8FFFC5693383C00D985A3BDa38";

const main = async () => {
const ensName = await publicClient.getEnsName({ address });
console.log("ensName :", ensName);

if (!ensName) return;

const ensAvatar = await publicClient.getEnsAvatar({
name: normalize(ensName)
});
console.log("ensAvatar:", ensAvatar);
};

main().catch(console.error);
43 changes: 43 additions & 0 deletions common/src/viem/ens-get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Address, createPublicClient, http } from "viem";
import { normalize } from "viem/ens";
import { mainnet } from "viem/chains";

const publicClient = createPublicClient({
chain: mainnet,
transport: http()
});

const ens = (() => {
const getName = async (address: string): Promise<string> => {
let name: string | null = null;

try {
name = await publicClient.getEnsName({ address: address as Address });
} catch (e) {
console.error("ENS lookupAddress not found");
}

console.info("getName", address, "=>", name);
return name || address || "";
};

const getAvatar = async (address: string): Promise<string> => {
let avatar: string | null = null;

try {
// avatar = await ensProvider.getAvatar(address);
avatar = await publicClient.getEnsAvatar({
name: normalize(address)
});
} catch (e) {
console.error("ENS lookupAddress not found");
}

console.info("getAvatar", address, "=>", avatar);
return avatar || "";
};

return { getName, getAvatar };
})();

export { ens };
2 changes: 1 addition & 1 deletion common/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"esModuleInterop": true,
"resolveJsonModule": true
},
"include": ["src/**/*.ts"]
"include": ["src/**/*.ts", "scripts/**/*.ts"]
}
2 changes: 1 addition & 1 deletion config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"license": "ISC",
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.12.7",
"@types/node": "^20.12.8",
"eslint-plugin-json": "^3.1.0",
"fs-extra": "^11.2.0",
"handlebars": "^4.7.8"
Expand Down
4 changes: 2 additions & 2 deletions gulp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"del": "^7.1.0",
"gulp": "^4.0.2",
"gulp-if": "^3.0.0",
"gulp-imagemin": "^9.0.0",
"gulp-imagemin": "^9.1.0",
"gulp-newer": "^1.4.0",
"gulp-noop": "^1.0.1",
"gulp-plumber": "^1.2.1",
Expand All @@ -27,7 +27,7 @@
"gulp-sourcemaps": "^3.0.0",
"gulp-uglify": "^3.0.2",
"postcss": "^8.4.38",
"sass": "^1.75.0"
"sass": "^1.76.0"
},
"type": "module"
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
"pnpm": "^8"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.2.5",
"ts-node": "^10.9.2",
"turbo": "^1.13.2"
"turbo": "^1.13.3"
},
"type": "module"
}
Loading

0 comments on commit ab67abc

Please sign in to comment.