I try to build a game in unity, in which you can earn a blockchain token by minting it.
The API of the mint function looks like this:
{
"constant": false,
"inputs": [{"internalType": "address","name": "account","type": "address"}, {"internalType": "uint256","name": "amount","type": "uint256"}],
"name": "mint",
"outputs": [{"internalType": "bool","name": "","type": "bool"}],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
I first checked the approach in the flappy-bird-documentation and tried to change the set to score as follows:
public Function mintToken() {
return contract.GetFunction("mint");
}
CreateMintTokenTransactionInput(string addressFrom, string addressOwner, string privateKey, string adress , BigInteger amount, HexBigInteger gas = null, HexBigInteger valueAmount = null) {
var function = mintToken ();
return function.CreateTransactionInput(addressFrom, gas, valueAmount, adress, amount);
}
In the Start method of unity I go:
var transactionInput = CreateMintTokenTransactionInput(_userAddress, _addressOwner, _privateKey, adress, amount);
var transactionSignedRequest = new TransactionSignedUnityRequest(_url, GameControl.instance.Key, _userAddress);
//send and wait
yield return transactionSignedRequest.SignAndSendTransaction(transactionInput);
But the parameters of the function seem to be wrong.
I wonder if there is an easier way to do this or how I could fix my approach. In more general terms: How can I call an arbitrary smart contract method with parameters in Ethereum?
The next Problem ist that i dont get a transactionHash and I am not sure how to Debug this:
yield return transactionMintRequest.SignAndSendTransaction(mint, contractAddress);
if (transactionMintRequest.Exception != null)
{
Debug.Log(transactionMintRequest.Exception.Message);
yield break;
}
Debug.Log(transactionMintRequest);
var transactionMintHash = transactionMintRequest.Result;
Debug.Log("Transfer txn hash:" transactionMintHash);
CodePudding user response:
Following the documentation http://docs.nethereum.com/en/latest/unity3d-smartcontracts-getting-started/#transfer-transaction
The simplest thing to do is to create first your Mint Function, this has been code generated using the http://playground.nethereum.com/
public partial class MintFunction : MintFunctionBase { }
[Function("mint", "bool")]
public class MintFunctionBase : FunctionMessage
{
[Parameter("address", "account", 1)]
public virtual string Account { get; set; }
[Parameter("uint256", "amount", 2)]
public virtual BigInteger Amount { get; set; }
}
Then create your TransactionSignedUnityRequest as follows:
var transactionMintRequest = new TransactionSignedUnityRequest(url, privateKey, "YOURCHAINID");
var mint = new Mint
{
Account= account,
Amount = acount,
};
yield return transactionMintRequest.SignAndSendTransaction(mint, contractAddress);
var transactionMintHash = transactionMintRequest.Result;