Skip to content

Commit

Permalink
refactor(treewide): move nft info fetching into factory
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Sep 18, 2024
1 parent 0e68dba commit 71d9c9a
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 210 deletions.
11 changes: 10 additions & 1 deletion src/factory/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { nearHandler } from "../handlers/near";
import { secretHandler } from "../handlers/secret";
import { tezosHandler } from "../handlers/tezos";
import { raise, tonHandler } from "../handlers/ton";
import { fetchHttpOrIpfs } from "../handlers/utils";
import { TChainParams } from "./config";

export namespace Chain {
Expand Down Expand Up @@ -67,7 +68,10 @@ export function ChainFactory(cp: Partial<TChainParams>): TChainFactory {
return handler;
},
async getClaimData(chain, txHash) {
const data = await chain.getClaimData(txHash);
const storage = chain.getStorageContract();
const data = await chain.decodeLockedEvent(txHash);
const royaltyReceiver = await storage.chainRoyalty(data.destinationChain);
const fee = await storage.chainFee(data.destinationChain);
const sc = await this.inner(
data.sourceChain as unknown as TSupportedChain,
);
Expand All @@ -76,10 +80,15 @@ export function ChainFactory(cp: Partial<TChainParams>): TChainFactory {
data.sourceNftContractAddress,
undefined,
);
const imgUri = (await fetchHttpOrIpfs(ogNftData.metadata)).image;
return {
...data,
...ogNftData,
royalty: ogNftData.royalty.toString(),
royaltyReceiver: royaltyReceiver,
fee: fee.toString(),
imgUri,
lockTxChain: chain.identifier,
};
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/factory/types/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { MetaMap } from ".";
import type {
TClaimSFT,
TGetChainIdentifier,
TGetClaimData,
TGetStorage,
TNftTransferDetailsObject,
} from "../../handlers/index";

Expand All @@ -19,7 +21,7 @@ export type TInferChainH<K extends TSupportedChain> = MetaMap[K][0];
export type TChainFactory = {
inner: <T extends TSupportedChain>(chain: T) => Promise<TInferChainH<T>>;
getClaimData: (
chain: TGetClaimData,
chain: TGetClaimData & TGetStorage & TGetChainIdentifier,
txHash: string,
) => Promise<TNftTransferDetailsObject>;
};
Expand Down
27 changes: 3 additions & 24 deletions src/handlers/aptos/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Aptos, AptosConfig } from "@aptos-labs/ts-sdk";
import { HexString } from "aptos";
import { raise } from "../ton";
import { TNFTData } from "../types";
import { BridgeClient } from "./bridge-client";
import { TAptosHandler, TAptosParams } from "./types";

Expand All @@ -16,6 +15,7 @@ export function aptosHandler({
const bc = new BridgeClient(aptos, bridge);

return {
identifier,
async getValidatorCount() {
const bd = await bc.getBridgeData();
if (!bd) throw new Error("Failed to fetch bridge data");
Expand Down Expand Up @@ -123,7 +123,7 @@ export function aptosHandler({
symbol: data.current_collection?.collection_name ?? "ANFT",
};
},
async getClaimData(transactionHash) {
async decodeLockedEvent(transactionHash) {
const tx = await aptos.waitForTransaction({ transactionHash });
const events = await aptos.getEvents({
options: {
Expand All @@ -138,22 +138,6 @@ export function aptosHandler({
e.type.includes("aptos_nft_bridge::LockedEvent"),
);
if (!event) throw new Error("Event not found");
const destinationChain = hexStringToUtf8(event.data.destination_chain);
const fee = await storage.chainFee(destinationChain);
const royaltyReceiver = await storage.chainRoyalty(destinationChain);
let data: TNFTData = {
metadata: "",
name: "",
royalty: 0n,
symbol: "",
};
try {
data = await this.nftData(
hexStringToUtf8(event.data.source_nft_contract_address),
"",
undefined,
);
} catch (e) {}
return {
tokenAmount: event.data.token_amount,
sourceChain: hexStringToUtf8(event.data.self_chain),
Expand All @@ -164,14 +148,9 @@ export function aptosHandler({
destinationChain: hexStringToUtf8(event.data.destination_chain),
destinationUserAddress: event.data.destination_user_address,
nftType: hexStringToUtf8(event.data.nft_type),
fee: fee.toString(),
royaltyReceiver,
metadata: data.metadata,
name: data.name,
royalty: data.royalty.toString(),
transactionHash: transactionHash,
symbol: data.symbol,
lockTxChain: identifier,
metaDataUri: "",
};
},
async claimSft(signer, claimData, sigs) {
Expand Down
26 changes: 3 additions & 23 deletions src/handlers/cosmwasm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
} from "@cosmjs/cosmwasm-stargate";

import { Bridge, CosmNft } from "@xp/cosmos-client";
import { TNFTData } from "../types";
import { CosmWasmExtraArgs, TCosmWasmHandler, TCosmWasmParams } from "./types";

export async function cosmWasmHandler({
Expand Down Expand Up @@ -58,6 +57,7 @@ export async function cosmWasmHandler({
}
const bc = new Bridge.BridgeQueryClient(provider, bridge);
return {
identifier,
async approveNft(signer, tokenId, contract, extraArgs) {
const cosmSigner = await SigningCosmWasmClient.connectWithSigner(
rpc,
Expand Down Expand Up @@ -195,7 +195,7 @@ export async function cosmWasmHandler({
transaction_hash: input.transactionHash,
};
},
async getClaimData(txHash) {
async decodeLockedEvent(txHash) {
const tx = await provider.getTx(txHash);
if (!tx) {
throw new Error("Transaction not found");
Expand Down Expand Up @@ -223,21 +223,6 @@ export async function cosmWasmHandler({
source_chain: sourceChain, // Source chain of NFT
} = JSON.parse(data.value);

const fee = await storage.chainFee(destinationChain);
const royaltyReceiver = await storage.chainRoyalty(destinationChain);

let nft: TNFTData = {
metadata: "",
name: "",
royalty: 0n,
symbol: "",
};

try {
await provider.getAccount(sourceNftContractAddress);
nft = await nftData(tokenId, sourceNftContractAddress, {});
} catch (e) {}

return {
destinationChain,
destinationUserAddress,
Expand All @@ -246,14 +231,9 @@ export async function cosmWasmHandler({
tokenAmount,
nftType,
sourceChain,
fee: fee.toString(),
royaltyReceiver: royaltyReceiver,
metadata: nft.metadata,
name: nft.name,
symbol: nft.symbol,
royalty: nft.royalty.toString(),
transactionHash: txHash,
lockTxChain: identifier,
metaDataUri: "",
};
},
async lockNft(
Expand Down
21 changes: 4 additions & 17 deletions src/handlers/evm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function evmHandler({
identifier,
}: TEvmParams): TEvmHandler {
return {
identifier,
async claimNft(wallet, claimData, sigs, extraArgs) {
const contract = Bridge__factory.connect(bridge, wallet);
const ret = await contract.claimNFT721(
Expand Down Expand Up @@ -143,7 +144,7 @@ export function evmHandler({
getProvider() {
return provider;
},
async getClaimData(txHash) {
async decodeLockedEvent(txHash) {
const receipt = await provider.getTransactionReceipt(txHash);
if (!receipt) {
throw new Error("Transaction not found");
Expand All @@ -163,15 +164,6 @@ export function evmHandler({
if (!locked) {
throw new Error("Failed to parse log");
}
const fee = await storage.chainFee(locked.args.destinationChain);
const royaltyReceiver = await storage.chainRoyalty(
locked.args.destinationChain,
);
const data = await this.nftData(
locked.args.tokenId,
locked.args.sourceNftContractAddress,
{},
);
return {
destinationChain: locked.args.destinationChain,
destinationUserAddress: locked.args.destinationUserAddress,
Expand All @@ -180,14 +172,9 @@ export function evmHandler({
tokenAmount: locked.args.tokenAmount.toString(),
nftType: locked.args.nftType,
sourceChain: locked.args.sourceChain,
fee: fee.toString(),
royaltyReceiver: royaltyReceiver,
transactionHash: txHash,
metadata: data.metadata,
name: data.name,
symbol: data.symbol,
royalty: data.royalty.toString(),
lockTxChain: identifier,
metaDataUri: locked.args.metaDataUri,
transactionHash: txHash,
};
},
getBalance(signer) {
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/hedera/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function hederaHandler({
});
let hsdk: typeof import("@hashgraph/sdk") | undefined = undefined;
return {
identifier,
injectSDK(sdk) {
console.log("INJECTED", sdk);
hsdk = sdk;
Expand All @@ -52,7 +53,7 @@ export function hederaHandler({
console.log(mint);
return mint;
},
getClaimData: web3Helper.getClaimData,
decodeLockedEvent: web3Helper.decodeLockedEvent,
getProvider: web3Helper.getProvider,
transform: web3Helper.transform,
getValidatorCount: web3Helper.getValidatorCount,
Expand Down
21 changes: 3 additions & 18 deletions src/handlers/icp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { _SERVICE as LedgerService } from "../../contractsTypes/icp/ledger/ledge
import { idlFactory as NftIdl } from "../../contractsTypes/icp/nft/nft";
import { init } from "../../contractsTypes/icp/nft/nft";
import { _SERVICE } from "../../contractsTypes/icp/nft/nft.types";
import { TNFTData } from "../types";
import { NftByteCode } from "./nft.wasm.gz.hex";
import { BrowserSigners, TICPHandler, TICPParams } from "./types";

Expand Down Expand Up @@ -86,6 +85,7 @@ export async function icpHandler({
const bc = await createBridgeActor(bridge, { agent });
await agent.fetchRootKey();
return {
identifier,
async nftList(owner, contract) {
const nft = await createNftActor(contract, { agent });
const tokens = await nft.icrc7_tokens_of(
Expand Down Expand Up @@ -236,36 +236,21 @@ export async function icpHandler({
symbol: symbol === "" ? "TICP" : symbol,
};
},
async getClaimData(txHash) {
async decodeLockedEvent(txHash) {
const [le] = await bc.get_locked_data(txHash);
if (!le) throw new Error("No locked event found for the hash");

const fee = await storage.chainFee(le.destination_chain);
const royaltyReceiver = await storage.chainRoyalty(le.destination_chain);

const nft: TNFTData = {
metadata: "",
name: "TICP",
royalty: 0n,
symbol: "TICP",
};

return {
destinationChain: le.destination_chain,
destinationUserAddress: le.destination_user_address,
fee: fee.toString(),
royaltyReceiver,
royalty: "0",
sourceNftContractAddress: le.source_nft_contract_address.toString(),
nftType: le.nft_type,
sourceChain: le.source_chain,
tokenId: le.token_id.toString(),
tokenAmount: le.token_amount.toString(),
transactionHash: txHash,
metadata: nft.metadata,
name: nft.name,
symbol: nft.symbol,
lockTxChain: identifier,
metaDataUri: "",
};
},
getStorageContract() {
Expand Down
30 changes: 3 additions & 27 deletions src/handlers/multiversx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import { UserSigner } from "@multiversx/sdk-wallet/out";
import axios from "axios";
import { multiversXBridgeABI } from "../../contractsTypes/multiversx";
import { raise } from "../ton";
import { TNFTData } from "../types";
import { fetchHttpOrIpfs } from "../utils";
import {
TMultiversXHandler,
Expand Down Expand Up @@ -91,6 +90,7 @@ export function multiversxHandler({
};
};
return {
identifier,
async nftData(nonce, collection) {
const nftDetails =
await provider.getDefinitionOfTokenCollection(collection);
Expand Down Expand Up @@ -309,7 +309,7 @@ export function multiversxHandler({
async approveNft(_signer, _tokenId, _contract) {
return Promise.resolve("Not Required for MultiversX");
},
async getClaimData(txHash) {
async decodeLockedEvent(txHash) {
await txWatcher.awaitCompleted(txHash);
const transactionOnNetworkMultisig = await provider.getTransaction(
txHash,
Expand All @@ -331,23 +331,6 @@ export function multiversxHandler({
const tokenId = parsed.token_id.toString();
const tokenAmount = parsed.token_amount.toString();

const fee = await storage.chainFee(destinationChain);
const royaltyReceiver = await storage.chainRoyalty(destinationChain);
let metadata: TNFTData = {
metadata: "",
name: "",
royalty: 0n,
symbol: "",
};
if (sourceChain === "MULTIVERSX") {
metadata = await this.nftData(
tokenId,
parsed.source_nft_contract_address,
);
}

const imgUri = (await fetchHttpOrIpfs(metadata.metadata)).image;

return {
destinationChain,
destinationUserAddress:
Expand All @@ -358,14 +341,7 @@ export function multiversxHandler({
sourceNftContractAddress: parsed.source_nft_contract_address,
sourceChain,
transactionHash: txHash,
fee: fee.toString(),
royaltyReceiver,
metadata: metadata.metadata,
name: metadata.name,
symbol: metadata.symbol,
royalty: metadata.royalty.toString(),
lockTxChain: identifier,
imgUri: imgUri,
metaDataUri: "",
};
},
async lockNft(signer, sourceNft, destinationChain, to, tokenId) {
Expand Down
Loading

0 comments on commit 71d9c9a

Please sign in to comment.