Skip to content

Commit 3d7b52b

Browse files
committed
Ensure correct args parsing
1 parent 7cff4f1 commit 3d7b52b

File tree

1 file changed

+46
-7
lines changed

1 file changed

+46
-7
lines changed

src/utils/nftTransfer.ts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,54 @@ export const transferNFT = async ({
2424
fromChainName,
2525
}: TransferNFTParams): Promise<ethers.ContractTransactionResponse> => {
2626
try {
27+
// Validate input parameters
28+
if (
29+
!tokenId ||
30+
!currentChainContractAddress ||
31+
!destinationChain ||
32+
!receiverAddress
33+
) {
34+
throw new Error('Missing required parameters for NFT transfer');
35+
}
36+
2737
// Get the ZRC20 token address for the destination chain
2838
const destinationAddress = getZRC20Address(destinationChain);
2939
if (!destinationAddress) {
30-
throw new Error(`ZRC20 address not found for destination chain: ${destinationChain}`);
40+
throw new Error(
41+
`ZRC20 address not found for destination chain: ${destinationChain}`
42+
);
3143
}
3244

45+
console.log('Transfer parameters:', {
46+
tokenId,
47+
currentChainContractAddress,
48+
destinationChain,
49+
destinationAddress,
50+
receiverAddress,
51+
fromChainName,
52+
});
53+
3354
// Create the NFT contract instance using the current chain's contract address
3455
const nftContract = createNFTContract(currentChainContractAddress, signer);
3556

36-
// Step 1: Approve the contract to transfer the NFT
57+
// Verify ownership before proceeding
58+
const userAddress = await signer.getAddress();
59+
console.log('Verifying NFT ownership...', { tokenId, userAddress });
60+
61+
// Check if the user owns the NFT (this is a basic check, the contract will also verify)
62+
try {
63+
const balance = await nftContract.balanceOf(userAddress);
64+
if (balance === 0n) {
65+
throw new Error('You do not own any NFTs from this contract');
66+
}
67+
} catch (error) {
68+
console.warn('Could not verify NFT balance:', error);
69+
// Continue anyway as the contract will handle the ownership check
70+
}
71+
72+
// Step 1: Approve the ZRC20 contract to transfer the NFT
3773
console.log('Approving NFT for transfer...');
38-
const approveTx = await nftContract.approve(currentChainContractAddress, tokenId, {
74+
const approveTx = await nftContract.approve(destinationAddress, tokenId, {
3975
gasLimit: 1000000,
4076
});
4177
await approveTx.wait();
@@ -47,19 +83,19 @@ export const transferNFT = async ({
4783
receiverAddress,
4884
destinationAddress,
4985
});
50-
const result = await nftContract.transferCrossChain(
86+
const result = (await nftContract.transferCrossChain(
5187
tokenId,
5288
receiverAddress,
5389
destinationAddress,
5490
{
5591
gasLimit: 1000000,
5692
value: ethers.parseEther('0.01'), // Start with 0 gas amount, can be adjusted
5793
}
58-
) as ethers.ContractTransactionResponse;
94+
)) as ethers.ContractTransactionResponse;
5995

6096
// Wait for the transaction to be mined
6197
const receipt = await result.wait();
62-
98+
6399
if (!receipt) {
64100
throw new Error('Transaction receipt not found');
65101
}
@@ -69,7 +105,10 @@ export const transferNFT = async ({
69105
// Update NFT storage with transfer information
70106
try {
71107
updateNFTTransfer(tokenId, contractAddress, {
72-
fromChainId: await signer.provider?.getNetwork().then(network => network.chainId.toString()) || '0',
108+
fromChainId:
109+
(await signer.provider
110+
?.getNetwork()
111+
.then((network) => network.chainId.toString())) || '0',
73112
fromChainName,
74113
toChainId: getChainId(destinationChain),
75114
toChainName: destinationChain,

0 commit comments

Comments
 (0)