diff --git a/src/factory/factory.ts b/src/factory/factory.ts index da0bef48..ca5a8252 100644 --- a/src/factory/factory.ts +++ b/src/factory/factory.ts @@ -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 { @@ -67,7 +68,10 @@ export function ChainFactory(cp: Partial): 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, ); @@ -76,10 +80,15 @@ export function ChainFactory(cp: Partial): 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, }; }, }; diff --git a/src/factory/types/utils.ts b/src/factory/types/utils.ts index 17a525b8..d5ab0135 100644 --- a/src/factory/types/utils.ts +++ b/src/factory/types/utils.ts @@ -1,7 +1,9 @@ import { MetaMap } from "."; import type { TClaimSFT, + TGetChainIdentifier, TGetClaimData, + TGetStorage, TNftTransferDetailsObject, } from "../../handlers/index"; @@ -19,7 +21,7 @@ export type TInferChainH = MetaMap[K][0]; export type TChainFactory = { inner: (chain: T) => Promise>; getClaimData: ( - chain: TGetClaimData, + chain: TGetClaimData & TGetStorage & TGetChainIdentifier, txHash: string, ) => Promise; }; diff --git a/src/handlers/aptos/index.ts b/src/handlers/aptos/index.ts index 62a58ce7..9d883fce 100644 --- a/src/handlers/aptos/index.ts +++ b/src/handlers/aptos/index.ts @@ -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"; @@ -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"); @@ -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: { @@ -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), @@ -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) { diff --git a/src/handlers/cosmwasm/index.ts b/src/handlers/cosmwasm/index.ts index 03c8b28d..475bcd60 100644 --- a/src/handlers/cosmwasm/index.ts +++ b/src/handlers/cosmwasm/index.ts @@ -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({ @@ -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, @@ -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"); @@ -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, @@ -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( diff --git a/src/handlers/evm/index.ts b/src/handlers/evm/index.ts index cfbd9187..7805fa18 100644 --- a/src/handlers/evm/index.ts +++ b/src/handlers/evm/index.ts @@ -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( @@ -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"); @@ -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, @@ -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) { diff --git a/src/handlers/hedera/index.ts b/src/handlers/hedera/index.ts index 98689e28..980ffc3f 100644 --- a/src/handlers/hedera/index.ts +++ b/src/handlers/hedera/index.ts @@ -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; @@ -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, diff --git a/src/handlers/icp/index.ts b/src/handlers/icp/index.ts index 85653b56..432cd271 100644 --- a/src/handlers/icp/index.ts +++ b/src/handlers/icp/index.ts @@ -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"; @@ -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( @@ -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() { diff --git a/src/handlers/multiversx/index.ts b/src/handlers/multiversx/index.ts index c89c6e25..1f0e2360 100644 --- a/src/handlers/multiversx/index.ts +++ b/src/handlers/multiversx/index.ts @@ -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, @@ -91,6 +90,7 @@ export function multiversxHandler({ }; }; return { + identifier, async nftData(nonce, collection) { const nftDetails = await provider.getDefinitionOfTokenCollection(collection); @@ -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, @@ -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: @@ -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) { diff --git a/src/handlers/near/index.ts b/src/handlers/near/index.ts index 362ad701..70a056e8 100644 --- a/src/handlers/near/index.ts +++ b/src/handlers/near/index.ts @@ -1,8 +1,6 @@ import { Contract, connect } from "near-api-js"; import { parseNearAmount } from "near-api-js/lib/utils/format"; import { unimplemented } from "../../utils"; -import { TNFTData } from "../types"; -import { fetchHttpOrIpfs } from "../utils"; import { TNearHandler, TNearParams } from "./types"; export async function nearHandler({ @@ -45,6 +43,7 @@ export async function nearHandler({ }; } return { + identifier, nftData, async deployNftCollection(_signer, _da, _ga) { unimplemented(); @@ -129,7 +128,7 @@ export async function nearHandler({ }); return approve.transaction.hash; }, - async getClaimData(txHash) { + async decodeLockedEvent(txHash) { const receipts = await provider.connection.provider.txStatusReceipts( txHash, bridge, @@ -147,20 +146,6 @@ export async function nearHandler({ const tokenId = parsed.token_id; 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 === "NEAR") { - metadata = await nftData(tokenId, parsed.source_nft_contract_address); - } - - const imgUri = (await fetchHttpOrIpfs(metadata.metadata)).image; - return { destinationChain, destinationUserAddress: parsed.destination_user_address, @@ -170,14 +155,8 @@ export async function nearHandler({ 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: parsed.metadata_uri, }; }, async lockNft( diff --git a/src/handlers/secret/index.ts b/src/handlers/secret/index.ts index 0db452d9..32eb056c 100644 --- a/src/handlers/secret/index.ts +++ b/src/handlers/secret/index.ts @@ -2,7 +2,6 @@ import { StdSignature, toBase64 } from "secretjs"; import { Pubkey } from "secretjs/dist/wallet_amino"; import { Lock721, Lock1155 } from "../../contractsTypes/secret/secretBridge"; import { raise } from "../ton"; -import { TNFTData } from "../types"; import { TSecretHandler, TSecretParams } from "./types"; export function secretHandler({ @@ -14,6 +13,7 @@ export function secretHandler({ identifier, }: TSecretParams): TSecretHandler { return { + identifier, getProvider() { return provider; }, @@ -138,7 +138,7 @@ export function secretHandler({ getStorageContract() { return storage; }, - async getClaimData(txHash) { + async decodeLockedEvent(txHash) { const eventId = "LockedEventInfo"; const tx = await provider.query.getTx(txHash); if (!tx) { @@ -160,20 +160,6 @@ export function secretHandler({ nft_type: nftType, // Sigular or multiple ( 721 / 1155) source_chain: sourceChain, // Source chain of NFT } = JSON.parse(log.value); - - const fee = await storage.chainFee(destinationChain); - const royaltyReceiver = await storage.chainRoyalty(destinationChain); - - let nft: TNFTData = { - metadata: "", - name: "", - royalty: 0n, - symbol: "", - }; - if (sourceNftContractAddress.startsWith("secret")) { - nft = await this.nftData(tokenId, sourceNftContractAddress, {}); - } - return { destinationChain, destinationUserAddress, @@ -182,14 +168,9 @@ export function secretHandler({ tokenAmount: tokenAmount.toString(), 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 claimSft(signer, claimData, sigs, extraArgs) { diff --git a/src/handlers/tezos/index.ts b/src/handlers/tezos/index.ts index f3dc9450..d63bbfce 100644 --- a/src/handlers/tezos/index.ts +++ b/src/handlers/tezos/index.ts @@ -30,7 +30,6 @@ import { import axios from "axios"; import { NFTCode } from "../../contractsTypes/tezos/NFT.code"; import { raise } from "../ton"; -import { TNFTData } from "../types"; import { TTezosHandler, TTezosParams, TezosSigner } from "./types"; export function tezosHandler({ @@ -176,6 +175,7 @@ export function tezosHandler({ return bytes2Char(metaDataInHex); }; return { + identifier, getStorageContract() { return storage; }, @@ -229,7 +229,7 @@ export function tezosHandler({ extraArgs, ); }, - async getClaimData(txHash) { + async decodeLockedEvent(txHash) { const txs = await api.operationsGetTransactionByHash(txHash); const tx = txs[0] ?? raise("No such txn found"); const op = await eventsGetContractEvents({ @@ -250,18 +250,8 @@ export function tezosHandler({ token_amount: tokenAmount, // amount of nfts to be transfered ( 1 in 721 case ) nft_type: nftType, // Sigular or multiple ( 721 / 1155) source_chain: sourceChain, // Source chain of NFT + metadata_uri, } = data; - const fee = await storage.chainFee(destinationChain); - const royaltyReceiver = await storage.chainRoyalty(destinationChain); - let nft: TNFTData = { - metadata: "", - name: "", - royalty: 0n, - symbol: "", - }; - if (validateAddress(sourceNftContractAddress) === 3) { - nft = await this.nftData(tokenId, sourceNftContractAddress, {}); - } return { tokenId, destinationChain, @@ -271,13 +261,8 @@ export function tezosHandler({ sourceChain, transactionHash: claimData?.transactionId?.toString() ?? "", sourceNftContractAddress, - fee: fee.toString(), - royaltyReceiver, - name: nft.name, - symbol: nft.symbol, - royalty: nft.royalty.toString(), - metadata: nft.metadata, lockTxChain: identifier, + metaDataUri: metadata_uri, }; }, async mintNft(signer, ma, gasArgs) { diff --git a/src/handlers/ton/index.ts b/src/handlers/ton/index.ts index 4c00fc0e..d46d0eb8 100644 --- a/src/handlers/ton/index.ts +++ b/src/handlers/ton/index.ts @@ -10,7 +10,6 @@ import { import { NftCollection } from "../../contractsTypes/ton/tonNftCollection"; import { NftItem } from "../../contractsTypes/ton/tonNftContract"; -import { TNFTData } from "../types"; import { fetchHttpOrIpfs } from "../utils"; import { TestnetNftCollection } from "./nftc"; import { buildJettonContent } from "./tep64"; @@ -40,6 +39,7 @@ export function tonHandler({ } return { + identifier, getStorageContract() { return storage; }, @@ -157,7 +157,7 @@ export function tonHandler({ } return nft.address.toString(); }, - async getClaimData(txHash) { + async decodeLockedEvent(txHash) { const txs = await client.getTransactions(bridge.address, { hash: txHash, limit: 15, @@ -180,15 +180,9 @@ export function tonHandler({ tokenAmount, // amount of nfts to be transfered ( 1 in 721 case ) nftType, // Sigular or multiple ( 721 / 1155) sourceChain, // Source chain of NFT + metaDataUri, } = loadLockedEvent(msg.body.asSlice()); - const fee = await storage.chainFee( - destinationChain.asSlice().loadStringRefTail(), - ); - const royaltyReceiver = await storage.chainRoyalty( - destinationChain.asSlice().loadStringRefTail(), - ); - const getSourceNftContractAddress = () => { try { return sourceNftContractAddress @@ -199,22 +193,6 @@ export function tonHandler({ return sourceNftContractAddress.asSlice().loadStringTail(); } }; - - let nft: TNFTData = { - metadata: "", - name: "", - royalty: 0n, - symbol: "", - }; - - try { - nft = await this.nftData( - tokenId.toString(), - getSourceNftContractAddress(), - undefined, - ); - } catch (_) {} - return { destinationChain: destinationChain.asSlice().loadStringRefTail(), destinationUserAddress: destinationUserAddress @@ -225,14 +203,9 @@ export function tonHandler({ tokenAmount: tokenAmount.toString(), nftType: nftType.toString(), sourceChain: sourceChain.toString(), - fee: fee.toString(), - royaltyReceiver: royaltyReceiver.toString(), - metadata: nft.metadata, - name: nft.name, - symbol: nft.symbol, - royalty: nft.royalty.toString(), transactionHash: hash, lockTxChain: identifier, + metaDataUri: metaDataUri.asSlice().loadStringRefTail(), }; } } diff --git a/src/handlers/types/chain.ts b/src/handlers/types/chain.ts index b023baba..90ae37d0 100644 --- a/src/handlers/types/chain.ts +++ b/src/handlers/types/chain.ts @@ -218,6 +218,18 @@ export type TNftTransferDetailsObject = { imgUri?: string; }; +export type LockEvent = { + tokenId: string; + destinationChain: string; + destinationUserAddress: string; + sourceNftContractAddress: string; + tokenAmount: string; + nftType: string; + sourceChain: string; + transactionHash: string; + metaDataUri: string; +}; + /** * Represents a type that defines a function to get claim data. */ @@ -227,7 +239,7 @@ export type TGetClaimData = { * @param txHash - The transaction hash. * @returns A promise that resolves to an object containing claim data @type {TNftTransferDetailsObject}. */ - getClaimData: (txHash: string) => Promise; + decodeLockedEvent: (txHash: string) => Promise; }; /** @@ -292,6 +304,10 @@ export type TGetStorage = { getStorageContract: () => BridgeStorage; }; +export type TGetChainIdentifier = { + identifier: string; +}; + export type TGetProvider = { getProvider: () => T; }; @@ -317,7 +333,8 @@ export type TSingularNftChain = TGetProvider & TMapTransferDetailsToChainClaimData & TGetValidatorCount & - TGetStorage; + TGetStorage & + TGetChainIdentifier; /** * Represents a type that has all the methods required to implement on a chain that can be used in the bridge to transfer Semi Fungible Tokens. It is a combination of some of the types defined above.