Ethereum Unity3D Boilerplate Questions

Continuing the discussion from Ethereum Unity3D Boilerplate Questions:

Hi there ! I don’t see any errors in the log.
This is a small part of the log,

Followed by this

Since the server time retrieval failed, there should be errors in the log of you server.
There are two possible causes:

  1. You are not connected to the server (check that when you pasted your sever uri in the MoralisSetup Moralis Server URI field it is complete and correct. A common mistake is to paste into and not over the default text in the field.
  2. Your Moralis server needs to be re-started, or is not up to date. To correct either, re-start your server.

Regards,

David

I am currently working on this.
The WebGL / Browser Wallet interface is done and working. I am currently cleaning up the code. This is going to be a major change and may break some current projects so I will probably do a pre-release for teams to examine.

Hi, can i have a sample code on subscribe livequery on playercontroller?

using Moralis;
using Moralis.Platform.Objects;
MoralisLiveQueryClient<Hero> heroSubscription = moralis.Query<Hero>().Subscribe(callbacks);

is this the right direction? tq

1 Like

Try this format:

MoralisQuery<Hero> q = MoralisInterface.GetClient().Query<Hero>();
MoralisLiveQueryController.AddSubscription<Hero>("Hero", q, callbacks);

The MoralisLiveQueryController will respond to Unity life cycles to pauses and re-establish your queries automatically.

Let me know if you have any issues.

David

Hi there !
I would like to send an amount of ERC20 from one address to an other within Unity.
I couldn’t find a proper function for with moralis that takes in an receiver address, a token address and an amount.
So, I made a “transfer” function within the smart contract and would like to interact with it from unity with moralis.

I found the RunContractFunction methode but i am struggling to use this code exemple:

// Function ABI input parameters
object[] inputParams = new object[1];
inputParams[0] = new { internalType="uint256", name="id", type="uint256" };
// Function ABI Output parameters
object[] outputParams = new object[1];
outputParams[0] = new { internalType="string", name="", type="string" };
// Function ABI
object[] abi = new object[1];
abi[0] = new { inputs=inputParams, name="uri", outputs=outputParams, stateMutability="view", type="function" };

// Define request object
RunContractDto rcd = new RunContractDto()
{
    Abi = abi,
    Params = new { id = "15310200874782" }
};

// Call contract function, response is always a string.
string resp = MoralisInterface.GetClient().Web3Api.Native.RunContractFunction("0x698d7D745B7F5d8EF4fdB59CeB660050b3599AC3", "uri", rcd, ChainList.mumbai);

I would like to use my function called “transfer” that takes in an Address and uint256 of the smart contract “0x0000”.
Any guidance would be much appreciated ! thanks !

hey, to send a custom token, you need to have the contract abi in your code, take a look at this
(https://github.com/0xPr0f/Moralis-Unity/blob/master/Assets/SendCustomToken.cs) this was a template i made.
you can also read down from here to see how it is done and the parameters
(https://github.com/ethereum-boilerplate/ethereum-unity-boilerplate#web3)

1 Like

Thank you ! this helped me a lot !
However, when I try to confirm the payement with imtoken wallet, it keeps saying “network is busy, please try again later”. On metamask it says “internal JSON-RPC error” and on trust wallet it says nothing.
any idea where this might come from ?

Thanks again !

1 Like

Also, is it the same function for sending nfts?

I would need to be able to exchange an Erc20 for an erc1155 minted nft between two accounts.
Security wise, how can I be sure both transactions happen at the same time ?

Thanks again !

hey, for imtoken, it could be the wallet or network specifically
for metamask, did you set the wallet connection to metamask? and did you set the chain id ?
for wallet connect too, did you set the chainid ?

and that code sample allows you to call a contract function, it can be anything as long as it is a function in the contract abi so, it isnt specific (you can use it to call a smart contract function)

1 Like

Hello im new here and I have a noob question, … So I logged in succesfuly using the wallet connect QR code … I have the wallet address displaying on the screen but how do I display certain nfts from the wallet onto a canvas image … thanks for the help … !

1 Like

you can use the getnft web3api method for that
(https://github.com/ethereum-boilerplate/ethereum-unity-boilerplate#getnfts)
take a look at this in practical usage
(https://github.com/0xPr0f/Moralis-Unity/blob/master/README.md)
then you have to get the image and then render it on a image object in unity
this is an example in more complex code
(https://github.com/0xPr0f/chillHouse/blob/9cf6e3c19676fdf121e55fa14e7341f929c59ab2/Assets/Script/CovelentData.cs#L209)
here, the image is been rendered

2 Likes

hey bro thanks for answering … going to check it out right now . .any questions I will get back to you … cheers my man !

Hi @dgoodrich

Regarding this: https://github.com/ethereum-boilerplate/ethereum-unity-boilerplate/issues/32#issue-1119418735

What is ETA for merging in-Browser Wallet for WebGL in the main release?

1 Like

For Metamask, I have set the wallet connection to metamask and the chain ID is always Mumbai 80001 but it still shows error “internal JSON-RPC error” but in the error log in unity I found that I might have missused the SendTransactionAsync function.

This is my code:

public async void SendToken()
    {
        MoralisInterface.InsertContractInstance("AKT", ABI, "mumbai", tokenAddress);
        Function f = MoralisInterface.EvmContractFunctionInstance("AKT", "mumbai", "transfer");
        string jsonresult = await f.SendTransactionAsync(senderAccount, receiverAccount, 100000000000000000);
        print(jsonresult);
        
    }

My transfer function in the smart contract takes in only a receiver address and an amount. the solidity code is here:

function transfer(address receiver, uint256 numTokens) public override returns (bool) {
        require(numTokens <= balances[msg.sender]);
        balances[msg.sender] = balances[msg.sender].sub(numTokens);
        balances[receiver] = balances[receiver].add(numTokens);
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }

Thank you very much for your help and support.
Best

Did you set the chainId as that in the wallet connect option ? It shouldn’t return this error :thinking:

Yes I did!

And this is the unity log from this error. The P2P transaction script L.125 corresponds to this line:

string jsonresult = await f.SendTransactionAsync(senderAccount, receiverAccount, 100000000000000000);

Thanks a lot !

1 Like

that drop down that says trust is supposed to be metamask in the defualt wallet option, :thinking: this is weird, let me test this and get back to you

1 Like