Transfer NFTs from Unity

How to Transfer NFTs in Unity, I am trying to Send This NFT to another Wallet in Rinkeby Testnet, I am noob so can anyone mention code for this.

If the NFT contract has a transfer function you call the smart contract transfer function as in the below thread.

@johnversus that is for readonly contract calls, to transfer an NFT, you have to call a contract function, all you need is an abi (you can get the general erc721 abi if you dont have a contract)

you can see different standard abi for erc 20 and erc 721 here

@gauravbarai answered above

if you also need any other thing, take a lot at the docs, it has unity in it and should be a good reference to go to when stuck

1 Like

using Moralis.Web3Api.Models;
using Moralis.WebGL.Hex.HexTypes;
using Moralis.WebGL.Models;
using MoralisWeb3ApiSdk;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using System.Threading.Tasks;
using UnityEngine;

public class MoralisNFTOperations : MonoBehaviour
{

string FromAddress = "0x857999320D99b35B31C3747eDeC91652b814a388";
public string ToAddress = "0x1A3DDF82e71A174a665F7FCFBf7Faf29AE72C216";
string tokenAddress;

public static NFT NFTforTransfer;

private void OnCollisionEnter(Collision collision)
{
    if(collision.gameObject.tag == "Player")
    {
        TransferNft(NFTforTransfer, ToAddress);
    }
}

public async static Task TransferNft(NFT nft, string toAddress)
{
    string fromAddress = PlayerManager.currentPlayer.moralisData.WalletAddress;
    if (fromAddress == null) fromAddress = "0x857999320D99b35B31C3747eDeC91652b814a388";
    BigInteger bi = 0;

    if (BigInteger.TryParse("60372454673033295026678316771131967667703896654649004225366799682777676513281", out bi))
    {
        // Convert token id to hex as this is what the contract call expects
        object[] pars = new object[] { 

            fromAddress, 
            toAddress, 
            bi.ToString(),
            (new HexBigInteger(1)).ToString(),
            new byte[] {0}

        };

        // Set gas estimate
        Nethereum.Hex.HexTypes.HexBigInteger gas = new Nethereum.Hex.HexTypes.HexBigInteger(30000);

        try
        {
            // Call the contract to transfer the NFT.
            string resp = await MoralisInterface.SendEvmTransactionAsync("0x88b48f654c30e99bc2e4a1559b4dcf1ad93fa656", "rinkeby", "safeTransferFrom", fromAddress, gas, new Nethereum.Hex.HexTypes.HexBigInteger("0x0"), pars);
            Debug.Log($"$Send Transaction respo: {resp}");
        }
        catch (Exception exp)
        {
            Debug.Log($"Send transaction failed: {exp.Message}");
        }
    }
}

}

Still getting the following log:

[WebSocket] Exception Unexpected character encountered while parsing value: M. Path β€˜β€™, line 0, position 0.
UnityEngine.Debug:Log (object)
WalletConnectSharp.Unity.Network.NativeWebSocketTransport/d__29:MoveNext () (at Assets/MoralisWeb3ApiSdk/WalletConnectSharp.Unity/Network/NativeWebSocketTransport.cs:199)
System.Runtime.CompilerServices.AsyncVoidMethodBuilder:Start<WalletConnectSharp.Unity.Network.NativeWebSocketTransport/d__29> (WalletConnectSharp.Unity.Network.NativeWebSocketTransport/d__29&)
WalletConnectSharp.Unity.Network.NativeWebSocketTransport:OnMessageReceived (byte[])
NativeWebSocket.WebSocket:DispatchMessageQueue () (at Assets/MoralisWeb3ApiSdk/WalletConnectSharp.Unity/Network/Client/WebSocket.cs:637)
WalletConnectSharp.Unity.Network.NativeWebSocketTransport:Update () (at Assets/MoralisWeb3ApiSdk/WalletConnectSharp.Unity/Network/NativeWebSocketTransport.cs:208)

@0xprof can you help with this

this is the NFT I am trying to transfer

This should just be an async void function
and what is

?

did you look at the links ?
where is the abi ?
take a look at the link, calling smart contract functions, then you can add it with the one in the sdk demo scene awardable.cs

Why are the parameters like this ?
you need a key at the beginning not your address

I am getting the Same Error even in the Awardable script, even without making any changes

This isnt an error, it is a message gotten from wallet connect websocket
and the endpoints should work fine