Ethereum unity boilerplate

@Mamba,

With the latest release, in Mobile (if built for iOS or Android) it should open the wallet app. For Android it opens the wallet directly, unless you have more than one installed in which case you choose first. In iOS you (the dev) need to select which wallets to display (this is because there are over 100 and if you do not filter Wallet Connect tries to load all which takes a long time). Once you build for iOS it should show you the wallets you choose at build time. Once you click on one of those it should open the wallet directly.

The previous versions (1.0.0 - 1.0.2) did show the browser since Wallet Connect had an issue in mobile.

Regards,

David

David,

The transaction confirm/signature doesnā€™t come up when it open right into the wallet app.

I noticed I didnā€™t attach the right video before.

Please tell me if this works

Youā€™ll notice the wallet is opened (thatā€™s correct).

But the TRUSTED APPLICATION message to Confirm & SIGN/APPROVE

Thoughts?

1 Like

@dgoodrich

David,

We have a set of TRIGGERS/EVENTS that need to help understanding how to implement them in our game. Can you please inform me as to where I would handle these scenarios WHILE PLAYING in the game

NFTs

  1. The user would like to Purchase, Sell, Transfer, Claim, Award an NFT inside of an Native Game App

  2. Player inside game would like to use an NFT inside game. Maybe the NFT grants certain abilities to the user. How would we do this?

  3. How does a user VIEW all the NFTs from multiple wallets at the same time (globally)?

TOKENS REWARDS & PRIZES

  1. Award tournament WINNER with prize (split the POT between 1st 2nd & 3rd place). Want to be able to sent token prize to them. How would I get the game to understand x amount of tokens needs to be sent to users.

FUNDS & FEES

  1. Player needs to add more in game currency from their crypto wallet. How do we trigger this event?

  2. Collect Tournament entry fee

  3. How do we check balance

  4. How do we check for balances of multiple wallets at the same time (globally)?

WALLETS

  1. How do we link multiple wallets to the users profile in order to CRUD all wallets at the same time (globally)?

OTHER FUNCTIONS & EVENTS

  1. Trigger X amount of Tokens to be gifted to a player if they accomplished a Mission.

  2. If a player achieved a certain high score

  3. If a player beats a level in a certain amount of TIME. System needs to Recognize that event happen and then Awards user with TOKEN & or NFTs for accomplishment.

ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”ā€”-

From whatā€™s Iā€™ve read, these are usually supposed to be coded into the ABI file. If thatā€™s the case. Where do I find the ABI file to edit it?

I also read that with Moralis this is much easier. So if thatā€™s the case how would I use Moralis to accomplish what Iā€™m trying to do above? If I am awarding a user with a Prize (NFT or TOKENS). Do I need to store my NFTs inside Moralis? If so where?

Your help is always appreciated!

Thanks David

2 Likes

Ah okay, yes this is a normal flow for Mobile Wallet apps. You approve, slide back to your app. It might hesitate a second or two but should navigate back to the wallet with the signature. You sign and then need to slide back to your app.

The second issue you are having is wallet Connect / Metamask Mobile Wallet interaction. We have seen this even with React Native. The Wallet Connect project says they are working with MetaMask to correct this. Please try a different mobile wallet. I use imToken wallet and Trust wallet (note: trust does not allow test nets or custom node connections)

You will need to call the Web3Api MoralisInterface.GetClient().Web3Api.Account.getNfts for each of the chains you want to show NFTs for.

For Multiple addresses you would need to require the user provide all of their addresses to you and then loop through the address.

That is a function of your contract / game logic. It will depend on many factors that are unique to each game.

These questions all have to do with your unique game logic.

Triggering would be handled by in game events based on specific game conditions - what these are is different from game to game.

To collect fees use the Web3.SendTransactionAndWaitForReceiptAsync method. The MoralisInterface provides one version. If you use the Web3 object directly there are several overrides for this method.

Checking for balances is done vis Web3Api, the Account endpoint.

To check multiple balances means two things - cross chain and different address - see my first answer above.

I am not sure this is possible. Transactions that change state (contract state updates, transfers, minting, etc. Require interaction with the individual wallets.

For read operations you can use Web3Api. For multiple address see my first answer above.

These questions all have to do with your specific game logic and can differ from game to game.

When you create your contract (Remix, HardHat, etc.) and compile it the ABI is created. If there is a third party contract you are interacting with please see any documentation provided by the owners of that contract, they should publish anything that is meant to be used publically.

NFTs are stored on contract but NFT Metadata is stored externally, usually on IPFS. Please see the ERC721 standard and ERC1155 standard for more information about NFT metadata.

You can choose to store stats or other info about userā€™s and their NFTs or your gameā€™s NFTs on the Moralis database. Most of the information you need for these though can be easily accessed via Moralis Web3Api.

Moralis saves a lot of time with server functions and everything related to reading, parsing, and searching chain data. Hopefully what I have provided above gives a good place to start. The demoscene of the boilerplate provides examples for reading Token and wallet balances (TokenListController.cs, WalletBalanace.cs) and searching ownership for specific NFTs. It also demos how to arawd and NFT (AwardableController.cs). There are a lot of videos on youtube that take you step by step how to create games and interact with the Moralis Web3Api for balanaces, tokens, NFTs, executing read functions against contracts, working with IPFS, etc. Please the the Moralis Web3 channel.

Always happy to help with questions about Morlais and the Moralis / Unity SDK.

Regards,

David

Keep getting this error on the right saying the script canā€™t be loaded. Any idea why?

1 Like

Usually happens when there are errors. Look at the ā€œConsoleā€ pane to see if you have an error about ā€œunsafeā€ code. If so please read through the setup steps in the readme. There is a setting required by Nethereum.

Hey! how can i call my Smart contract function?, I tried using -> RunContractFunction(string address, string functionName, RunContractDto abi, ChainList chain, string subdomain=null, string providerUrl=null) method in nativeApi (Api namespace and not the cloud one) but its throwing error when calling from unity
(error : ApiException: Error calling RunContractFunction: Error: ConnectFailure (No connection could be made because the target machine actively refused it.))
Our contract is basically storing some amount and then the other user can retrieve that amount from smart contract so the storing part is working fine ( for storing we used MoralisInterface.SendEvmTransactionAsync(arguments) ) but this same funtion is not workable for retrieve function from smart contract which has no arguments.

1 Like

David,

Any update on WalletConnect issue being resolved. When I donā€™t select a default wallet inside the SDK on Unity. Iā€™d like all the wallets on WalletConnect to populate. We donā€™t want our users to be limited to only one wallet. When i do this and play the scene the game takes forever when trying to find a wallet to connect with.

Iā€™d like to have a popup window that comes up a displays the wallets that WalletConnect supports.

Thoughts?

1 Like

Thank! for your response,
I just wanted to send Native coins ETH/BNB etc OR Bep-20 tokens to one meta-mask wallet to another how I do this?
The 1 line code to send ETH/BNB provide on documentation, I tried as shown in figure but its not working.
can you please guide me where I made mistake ? or any other simple way of doing this

1 Like

HI, you do not need to instantiate MoralisClient directly, this is done for you in MoralisINterface - please see the Moralis/Unity README.

Also Moralis.transfer is a JS SDK call. The Moralis / Unity SDK equivalent is the MoralisInterface.SendEvmTransactionAsync or MoralisInterface.SendTransactionAndWaitForReceiptAsync functions I described to you earlier.

Here is an example from the README that shows how to run a read only function on a contract:

// 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);

For iOS ONLY: At design time select all of the wallets listed in the iOS wallet menu. Please be advised that due to how Wallet Connect loads the images it may take over a minute for the selection menu to appear, I have no control over this - this is why I added the selection tool.

Which issue are you referring to?

Thanks,

David

Can you share exact code of how to send Eth/Bnb in unity3d from one wallet to another,
new here and didnā€™t understand all of these stuffs. Thanks

David, how do you require the wallet to open up. Iā€™ve noticed that when a smart contract is interacting with Unity and it requires me to sign and approve. The wallet doesnā€™t open automatically to do thatā€¦ I have to know that an event SHOULD be happening. Then I need to switch over to my wallet. Once I do that I can see my wallet is waiting for me to perform an action like approve and signā€¦

Any advice as to how I can get the wallet to automatically open. Thatā€™s not really user friendly if you ask me.

1 Like

Here is an example from the README that shows how to run a read-only function on a contract:
there is no relevant data available on README!

also, I want to execute External functions in a smart contract that has no argument, in short Other than the Read-Only function.

Hereā€™s the test smart contract which we are trying to callā€¦ in it, thereā€™s a function called ā€œwithdraw()ā€ which I need to call from unity.
https://www.toptal.com/developers/hastebin/sufivinesi.typescript

If you have set up Nethereum properly and are calling a Nethereum function that interacts with a contract in a way that changes state, it will automatically open your wallet.

With the release of the 1.0.3 package a few weeks ago, Nethereum was integrated into the MoralisInterface. Please see the example I provided in the ClaimRewardAsync method of the AwardableControler.cs file. The code to claim the reward is here and serves as a demonstration of how to call a contract function that changes state. When called the wallet will automatically open.

The MoralisInterface in v1.0.3 automatically setups the Web3 (Nethereum) connection for you if you provide an RPC URL (please refer to the README).

Regards,

David

Yes there is! Here is the direct link to that section of the README: RunContractFunction.

With the release of the 1.0.3 package a few weeks ago, Nethereum was integrated into the MoralisInterface . Please see the example I provided in the ClaimRewardAsync method of the AwardableControler.cs file. The code to claim the reward is here and serves as a demonstration of how to call a contract function that changes state.

You should look through the code called by MoralisInterface.SendEvmTransactionAsync to see how this code is interacting with Nethereum. I also advise you to read through the Nethereum documentation.

Here is how to specifically use the MoralisInterface to call your contract

  1. Read this README section
  2. Register your contract in the provided contract manager. From the MainMenuScript.cs InitializeWeb3 method:
       MoralisInterface.InsertContractInstance("MyContract", "[MY ABI STRING HERE]", "[TARGET CHAIN KEY]", "[CONTRACT ADDRESS ON TARGET CHAIN]");

Substitute your values to represent your contract
3. Call the function like this:

        Nethereum.Contracts.Function func = MoralisInterface.EvmContractFunctionInstance("MyContract", "[TARGET CHAIN KEY]", "MyFunctionName");
        object[] pars = new object[0];
        string jsonResult = await func.CallAsync(pars);

The jsonResult is whatever is returned from your contract by the call.

Regards,

David

Hi there !
Digging your AwardableController script, amazing stuff!
Is there a way to have a glimpse at the smart contract ? or template ?

It seems strange that the player has to sign the transaction when claiming the nft. Is to make him pay the fees ?
also, i am interested in how you automated the transfer from the contract (and contract owner) to the player (without having to sign each transaction manually)

Thanks a lot !