Skip to content

Commit

Permalink
migrate ethers to v6 (#44)
Browse files Browse the repository at this point in the history
* improve url origin verification

* ethers migrated to v6

* update npm version matrix of actions

* re-update node version matrix of ci

* fix lint

* update test based on ethers v6 req/res need

* increase the package size limit due to v6 changes

* version bump

* update readme
  • Loading branch information
mdtanrikulu authored Jun 8, 2024
1 parent b687351 commit cb5aa7f
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 217 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Avatar resolver library for both nodejs and browser.

## Note!: ENS-Avatar >= 1.0.0 is only compatible with ethers v6. If your project is using v5, keep your ens-avatar on latest 0.x version.

## Getting started

### Prerequisites
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.3.6",
"version": "1.0.0-alpha.1.ethers.6",
"license": "MIT",
"main": "dist/index.js",
"module": "dist/index.esm.js",
Expand All @@ -22,7 +22,7 @@
},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
"pre-commit": "tsdx lint src test"
}
},
"prettier": {
Expand Down Expand Up @@ -74,6 +74,7 @@
"axios-cache-interceptor": "^0.9.3",
"buffer": "^6.0.3",
"dompurify": "^3.0.6",
"ethers": "6.12.0",
"is-svg": "^4.3.2",
"multiformats": "^9.6.2",
"url-join": "^4.0.1"
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseProvider } from '@ethersproject/providers';
import { JsonRpcProvider } from 'ethers';
import ERC1155 from './specs/erc1155';
import ERC721 from './specs/erc721';
import URI from './specs/uri';
Expand All @@ -23,14 +23,14 @@ export interface UnsupportedNamespace {}
export class UnsupportedNamespace extends BaseError {}

export interface AvatarResolver {
provider: BaseProvider;
provider: JsonRpcProvider;
options?: AvatarResolverOpts;
getAvatar(ens: string, data: AvatarRequestOpts): Promise<string | null>;
getMetadata(ens: string): Promise<any | null>;
}

export class AvatarResolver implements AvatarResolver {
constructor(provider: BaseProvider, options?: AvatarResolverOpts) {
constructor(provider: JsonRpcProvider, options?: AvatarResolverOpts) {
this.provider = provider;
this.options = options;
if (options?.cache && options?.cache > 0) {
Expand Down
9 changes: 4 additions & 5 deletions src/specs/erc1155.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseProvider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import { Contract, Provider } from 'ethers';
import { Buffer } from 'buffer/';
import { fetch, resolveURI } from '../utils';
import { AvatarResolverOpts } from '../types';
Expand All @@ -21,7 +20,7 @@ function getMarketplaceAPIKey(uri: string, options?: AvatarResolverOpts) {

export default class ERC1155 {
async getMetadata(
provider: BaseProvider,
provider: Provider,
ownerAddress: string | undefined | null,
contractAddress: string,
tokenID: string,
Expand All @@ -34,10 +33,10 @@ export default class ERC1155 {
const contract = new Contract(contractAddress, abi, provider);
const [tokenURI, balance] = await Promise.all([
contract.uri(tokenID),
ownerAddress && contract.balanceOf(ownerAddress, tokenID),
ownerAddress ? contract.balanceOf(ownerAddress, tokenID) : BigInt(0),
]);
// if user has valid address and if token balance of given address is greater than 0
const isOwner = !!(ownerAddress && balance.gt(0));
const isOwner = !!(ownerAddress && balance > BigInt(0));

const { uri: resolvedURI, isOnChain, isEncoded } = resolveURI(
tokenURI,
Expand Down
5 changes: 2 additions & 3 deletions src/specs/erc721.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseProvider } from '@ethersproject/providers';
import { Contract } from '@ethersproject/contracts';
import { Contract, Provider } from 'ethers';
import { Buffer } from 'buffer/';
import { fetch, resolveURI } from '../utils';
import { AvatarResolverOpts } from '../types';
Expand All @@ -11,7 +10,7 @@ const abi = [

export default class ERC721 {
async getMetadata(
provider: BaseProvider,
provider: Provider,
ownerAddress: string | undefined | null,
contractAddress: string,
tokenID: string,
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { BaseProvider } from '@ethersproject/providers';
import { Provider } from 'ethers';

export interface Spec {
getMetadata: (
provider: BaseProvider,
provider: Provider,
ownerAddress: string | undefined | null,
contractAddress: string,
tokenID: string,
Expand Down
Loading

0 comments on commit cb5aa7f

Please sign in to comment.