Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
[LIVE-1911][LIVE-1912] Feature - Add collection name resolver to eth …
Browse files Browse the repository at this point in the history
…familly (#1863)

* Change nfts resolvers from Bridge type

* Update eth nft resolvers & add collection resolver

* Add loadCollectionMetadata to nft context

* Change eth NFT prepareTransaction to use collection call instead of nft metadata call
  • Loading branch information
lambertkevin committed Apr 11, 2022
1 parent 73686de commit f13dd89
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 100 deletions.
29 changes: 28 additions & 1 deletion src/api/Ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import invariant from "invariant";
import { BigNumber } from "bignumber.js";
import { LedgerAPINotAvailable } from "@ledgerhq/errors";
import JSONBigNumber from "@ledgerhq/json-bignumber";
import type { CryptoCurrency, NFTMetadataResponse } from "../types";
import type {
CryptoCurrency,
NFTCollectionMetadataResponse,
NFTMetadataResponse,
} from "../types";
import type { EthereumGasLimitRequest } from "../families/ethereum/types";
import network from "../network";
import { blockchainBaseURL } from "./Ledger";
Expand Down Expand Up @@ -83,6 +87,11 @@ export type NFTMetadataInput = Readonly<
tokenId: string;
}>
>;
export type NFTCollectionMetadataInput = Readonly<
Array<{
contract: string;
}>
>;
export type API = {
getTransactions: (
address: string,
Expand All @@ -100,6 +109,10 @@ export type API = {
input: NFTMetadataInput,
chainId: string
) => Promise<NFTMetadataResponse[]>;
getNFTCollectionMetadata: (
input: NFTCollectionMetadataInput,
chainId: string
) => Promise<NFTCollectionMetadataResponse[]>;
getAccountBalance: (address: string) => Promise<BigNumber>;
roughlyEstimateGasLimit: (address: string) => Promise<BigNumber>;
getERC20ApprovalsPerContract: (
Expand Down Expand Up @@ -220,6 +233,20 @@ export const apiForCurrency = (currency: CryptoCurrency): API => {
return data;
},

async getNFTCollectionMetadata(input, chainId) {
const { data }: { data: NFTCollectionMetadataResponse[] } = await network(
{
method: "POST",
url: `${getEnv(
"NFT_ETH_METADATA_SERVICE"
)}/v1/ethereum/${chainId}/contracts/infos`,
data: input,
}
);

return data;
},

async getERC20ApprovalsPerContract(owner, contract) {
try {
const { data } = await network({
Expand Down
7 changes: 5 additions & 2 deletions src/families/ethereum/bridge/js.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { signOperation } from "../signOperation";
import { modes } from "../modules";
import postSyncPatch from "../postSyncPatch";
import { inferDynamicRange } from "../../../range";
import nftMetadataResolver from "../nftMetadataResolver";
import { nftMetadata, collectionMetadata } from "../nftResolvers";

const receive = makeAccountBridgeReceive();

Expand Down Expand Up @@ -211,7 +211,10 @@ const currencyBridge: CurrencyBridge = {
preload,
hydrate,
scanAccounts,
nftMetadataResolver,
nftResolvers: {
nftMetadata,
collectionMetadata,
},
};
const accountBridge: AccountBridge<Transaction> = {
createTransaction,
Expand Down
3 changes: 1 addition & 2 deletions src/families/ethereum/modules/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ export async function prepareTransaction(
const { collection, collectionName, tokenIds } = transaction;
if (collection && tokenIds && typeof collectionName === "undefined") {
const api = apiForCurrency(account.currency);
const [{ status, result }] = await api.getNFTMetadata(
const [{ status, result }] = await api.getNFTCollectionMetadata(
[
{
contract: collection,
tokenId: tokenIds[0],
},
],
account.currency?.ethereumLikeInfo?.chainId?.toString() || "1"
Expand Down
43 changes: 0 additions & 43 deletions src/families/ethereum/nftMetadataResolver.ts

This file was deleted.

79 changes: 79 additions & 0 deletions src/families/ethereum/nftResolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
CurrencyBridge,
NFTCollectionMetadataResponse,
NFTMetadataResponse,
} from "../../types";
import { getCryptoCurrencyById } from "../../currencies";
import { metadataCallBatcher } from "../../nft";

const SUPPORTED_CHAIN_IDS = new Set([
1, // Ethereum
137, // Polygon
]);

type NftResolvers = NonNullable<CurrencyBridge["nftResolvers"]>;

export const nftMetadata: NftResolvers["nftMetadata"] = async ({
contract,
tokenId,
currencyId,
metadata,
}): Promise<NFTMetadataResponse> => {
// This is for test/mock purposes
if (typeof metadata !== "undefined") {
return {
status: 200,
result: {
contract,
tokenId,
...metadata,
},
};
}

const currency = getCryptoCurrencyById(currencyId);
const chainId = currency?.ethereumLikeInfo?.chainId;

if (!chainId || !SUPPORTED_CHAIN_IDS.has(chainId)) {
throw new Error("Ethereum Bridge NFT Resolver: Unsupported chainId");
}

const response = (await metadataCallBatcher(currency).loadNft({
contract,
tokenId,
})) as NFTMetadataResponse;

return response;
};

export const collectionMetadata: NftResolvers["collectionMetadata"] = async ({
contract,
currencyId,
metadata,
}): Promise<NFTCollectionMetadataResponse> => {
// This is for test/mock purposes
if (typeof metadata !== "undefined") {
return {
status: 200,
result: {
contract,
...metadata,
},
};
}

const currency = getCryptoCurrencyById(currencyId);
const chainId = currency?.ethereumLikeInfo?.chainId;

if (!chainId || !SUPPORTED_CHAIN_IDS.has(chainId)) {
throw new Error("Ethereum Bridge NFT Resolver: Unsupported chainId");
}

const response = (await metadataCallBatcher(currency).loadCollection({
contract,
})) as NFTCollectionMetadataResponse;

return response;
};

export default { nftMetadata, collectionMetadata };
Loading

0 comments on commit f13dd89

Please sign in to comment.