NFT swap contract on SEI blockchain not transferring new token

I’m working on an NFT swap contract using cosmWASM for the SEI blockchain. The contract is supposed to exchange old collection NFTs for new ones. It works in tests but fails on the SEI testnet.

Here’s what I’m doing:

  1. Set up old and new NFT collections (cw721)
  2. Mint NFTs
  3. Send an old NFT to the swap contract
  4. The contract should burn the old NFT and transfer a new one to the original owner

The receive_nft function is implemented like this:

pub fn token_exchange(data: MessageInfo, token_info: Cw721ReceiveMsg) -> Result<Response, ContractError> {
    let token_num = token_info.token_id.as_str();
    let user = token_info.sender.as_str();
    let msg_data: InnerMsg = from_json(&token_info.msg)?;
    let fresh_collection = msg_data.new_collection_addr;

    // Verify collection address
    let old_collection = data.sender.as_str();
    if old_collection != Addr::unchecked("sei1...6") {
        return Err(ContractError::Unauthorized {});
    }

    // Burn the old token
    Cw721Contract::<Empty, Empty>(
        Addr::unchecked(old_collection),
        PhantomData,
        PhantomData
    ).call(Cw2981ExecuteMsg::Burn { token_id: token_num.to_string() }).unwrap();

    // Transfer the new token
    Cw721Contract::<Empty, Empty>(
        fresh_collection,
        PhantomData,
        PhantomData
    ).call(Cw2981ExecuteMsg::TransferNft { recipient: user.to_string(), token_id: token_num.to_string() }).unwrap();

    Ok(Response::new()
        .add_attribute("action", "swap_complete")
        .add_attribute("old_token", old_collection)
        .add_attribute("new_token", fresh_collection.to_string())
        .add_attribute("user", user)
        .add_attribute("token_num", token_num))
}

The contract successfully burns the old NFT, but the new one is never transferred. Any suggestions on what might be causing this issue?

yo Nina_63Paint, I’ve seen this before. check if ur contract has enough gas for both burn and transfer. SEI can be finicky with gas limits. also, double-check ur new collection address is correct. sometimes a typo can mess everything up. if that don’t work, try adding some debug prints to see where it’s failing exactly. good luck!

hey there Nina_63Paint! that’s a pretty interesting project you’ve got goin on. i’ve been tinkering with NFT stuff on SEI too, so i feel your pain :sweat_smile:

have you checked if the new collection contract is actually approved to transfer tokens? sometimes we forget that step and it can cause silent failures. you might wanna try adding an approval step before the transfer:

// Approve the swap contract to transfer
Cw721Contract::<Empty, Empty>(
    fresh_collection.clone(),
    PhantomData,
    PhantomData
).call(Cw2981ExecuteMsg::Approve { spender: env.contract.address.to_string(), token_id: token_num.to_string(), expires: None }).unwrap();

// Then do the transfer

also, have you tried logging more info about what’s happening? maybe add some attributes or events to see exactly where it’s failing?

oh, and one more thing - are you sure the new collection has enough tokens minted? if it’s trying to transfer a token that doesn’t exist, that could cause issues too.

let me know if any of that helps! always happy to brainstorm more if you’re still stuck :slightly_smiling_face:

I’ve encountered similar issues with NFT transfers on SEI. One potential problem is that your contract might not have the necessary permissions to transfer tokens from the new collection. Ensure you’ve granted appropriate approvals.

Another point to consider is error handling. Instead of using unwrap(), which can mask failures, try using ? to propagate errors so you can get more informative messages.

Lastly, verify that the token IDs in the new collection are aligned with those of the old one, as mismatched IDs could prevent transfers.

Implementing more detailed logging or events might also help pinpoint the exact failure point.