Skip to content

Commit ef71693

Browse files
authored
Merge pull request #73 from ethereum-boilerplate/features/webgl-metamask
Features/webgl metamask
2 parents 31f7183 + ac4975b commit ef71693

File tree

155 files changed

+2737
-708
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

155 files changed

+2737
-708
lines changed

Assets/MoralisWeb3ApiSdk/Constants.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
namespace MoralisWeb3ApiSdk
3+
{
4+
public class Constants
5+
{
6+
public static string MUG_CONTRACT_ADDRESS = "0x05af21b57d2E378F90106B229E717DC96c7Bb5e2";
7+
public static string MUG_CHAIN = "mumbai";
8+
public static string MUG_CLAIM_FUNCTION = "claimReward";
9+
public static string MUG_ABI = "[{\"inputs\":[{\"internalType\":\"address\",\"name\":\"nftContractAddress\",\"type\":\"address\"}],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"ApprovalForAll\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"previousOwner\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"OwnershipTransferred\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"indexed\":false,\"internalType\":\"uint256[]\",\"name\":\"values\",\"type\":\"uint256[]\"}],\"name\":\"TransferBatch\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"TransferSingle\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"string\",\"name\":\"value\",\"type\":\"string\"},{\"indexed\":true,\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"URI\",\"type\":\"event\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"balanceOf\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address[]\",\"name\":\"accounts\",\"type\":\"address[]\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"}],\"name\":\"balanceOfBatch\",\"outputs\":[{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"}],\"name\":\"claimReward\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"account\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"isApprovedForAll\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"string\",\"name\":\"url\",\"type\":\"string\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"mint\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"\",\"type\":\"uint256[]\"},{\"internalType\":\"bytes\",\"name\":\"\",\"type\":\"bytes\"}],\"name\":\"onERC1155BatchReceived\",\"outputs\":[{\"internalType\":\"bytes4\",\"name\":\"\",\"type\":\"bytes4\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"\",\"type\":\"bytes\"}],\"name\":\"onERC1155Received\",\"outputs\":[{\"internalType\":\"bytes4\",\"name\":\"\",\"type\":\"bytes4\"}],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"internalType\":\"address\",\"name\":\"\",\"type\":\"address\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"renounceOwnership\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256[]\",\"name\":\"ids\",\"type\":\"uint256[]\"},{\"internalType\":\"uint256[]\",\"name\":\"amounts\",\"type\":\"uint256[]\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeBatchTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"from\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"to\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"id\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"data\",\"type\":\"bytes\"}],\"name\":\"safeTransferFrom\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"operator\",\"type\":\"address\"},{\"internalType\":\"bool\",\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"setApprovalForAll\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes4\",\"name\":\"interfaceId\",\"type\":\"bytes4\"}],\"name\":\"supportsInterface\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"address\",\"name\":\"contractAddress\",\"type\":\"address\"}],\"name\":\"updateTargetContract\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"uri\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}],\"stateMutability\":\"view\",\"type\":\"function\"}]";
10+
}
11+
}

Assets/MoralisWeb3ApiSdk/Constants.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/MoralisWeb3ApiSdk/EvmContractInstance.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@
2525
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2626
* SOFTWARE.
2727
*/
28-
using Assets.Scripts.Moralis;
28+
using Moralis;
29+
#if !UNITY_WEBGL
2930
using Nethereum.Contracts;
31+
#endif
3032

3133
namespace MoralisWeb3ApiSdk
3234
{
@@ -39,10 +41,14 @@ public class EvmContractInstance
3941
/// Contract address on this chain.
4042
/// </summary>
4143
public string ContractAddress { get; set; }
44+
45+
#if !UNITY_WEBGL
4246
/// <summary>
4347
/// Contract Instance derived from ABI
4448
/// </summary>
4549
public Contract ContractInstance { get; set; }
50+
51+
#endif
4652
/// <summary>
4753
/// Evm Chain information.
4854
/// </summary>

Assets/MoralisWeb3ApiSdk/EvmContractItem.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
* SOFTWARE.
2727
*/
2828
using System.Collections.Generic;
29-
using Assets.Scripts.Moralis;
29+
using Moralis;
30+
31+
#if !UNITY_WEBGL
32+
3033
using Nethereum.Contracts;
3134
using Nethereum.Web3;
3235

@@ -84,3 +87,4 @@ public void AddChainInstance(Web3 client, string chainId, string contractAddress
8487
}
8588
}
8689
}
90+
#endif

Assets/MoralisWeb3ApiSdk/EvmContractManager.cs

+11-90
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,28 @@
2727
*/
2828
using System;
2929
using System.Collections.Generic;
30-
using Nethereum.Contracts;
31-
using Nethereum.Hex.HexTypes;
32-
using Nethereum.RPC.Eth.DTOs;
33-
using Nethereum.Web3;
3430

3531
#if UNITY_WEBGL
32+
using Moralis.WebGL;
33+
using Moralis.WebGL.Models;
34+
using Moralis.WebGL.Hex.HexTypes;
3635
using Cysharp.Threading.Tasks;
3736
#else
3837
using System.Threading.Tasks;
38+
using Nethereum.Contracts;
39+
using Nethereum.Hex.HexTypes;
40+
using Nethereum.RPC.Eth.DTOs;
41+
using Nethereum.Web3;
3942
#endif
43+
4044
namespace MoralisWeb3ApiSdk
4145
{
4246
/// <summary>
4347
/// Creates a simple way to create and access a set of contracts by chain.
4448
/// </summary>
4549
public class EvmContractManager
4650
{
51+
#if !UNITY_WEBGL
4752
/// <summary>
4853
/// All defined contract instances
4954
/// </summary>
@@ -54,6 +59,8 @@ public EvmContractManager()
5459
Contracts = new Dictionary<string, EvmContractItem>();
5560
}
5661

62+
63+
5764
/// <summary>
5865
/// Adds a chain instance of a contract to a specific contract set.
5966
/// The contract for key must already exist, If key is not found,
@@ -151,92 +158,6 @@ public Function GetContractFunction(string contractKey, string chainId, string f
151158
return functionInstance;
152159
}
153160

154-
#if UNITY_WEBGL
155-
/// <summary>
156-
/// Executes a contract SendTransaction, used for executing contract functions that change state.
157-
/// </summary>
158-
/// <param name="contractKey"></param>
159-
/// <param name="chainId"></param>
160-
/// <param name="functionName"></param>
161-
/// <param name="transactionInput"></param>
162-
/// <param name="functionInput"></param>
163-
/// <returns></returns>
164-
public async UniTask<Tuple<bool, string, string>> SendTransactionAsync(string contractKey, string chainId, string functionName, TransactionInput transactionInput, object[] functionInput)
165-
{
166-
Tuple<bool, string, string> result = new Tuple<bool, string, string>(false, "", "");
167-
168-
Function targetFunction = GetContractFunction(contractKey, chainId, functionName);
169-
170-
if (targetFunction != null)
171-
{
172-
try
173-
{
174-
string resp = await targetFunction.SendTransactionAsync(transactionInput, functionInput);
175-
176-
result = new Tuple<bool, string, string>(true, resp, "");
177-
}
178-
catch (Exception exp)
179-
{
180-
result = new Tuple<bool, string, string>(false, "", exp.Message);
181-
}
182-
}
183-
184-
return result;
185-
}
186-
187-
public async UniTask<Tuple<bool,string,string>> SendTransactionAsync(string contractKey, string chainId, string functionName, string fromAddress, HexBigInteger gas, HexBigInteger value, object[] functionInput)
188-
{
189-
Tuple<bool, string, string> result = new Tuple<bool, string, string>(false,"","");
190-
191-
Function targetFunction = GetContractFunction(contractKey, chainId, functionName);
192-
193-
if (targetFunction != null)
194-
{
195-
try
196-
{
197-
string resp = await targetFunction.SendTransactionAsync(fromAddress, gas, value, functionInput);
198-
result = new Tuple<bool, string, string>(true, resp, "");
199-
}
200-
catch (Exception exp)
201-
{
202-
result = new Tuple<bool, string, string>(false, "", exp.Message);
203-
}
204-
}
205-
206-
return result;
207-
}
208-
209-
210-
public async UniTask<Tuple<bool, string, string>> SendTransactionAndWaitForReceiptAsync(string contractKey, string chainId, string functionName, string fromAddress, HexBigInteger gas, HexBigInteger value, object[] functionInput)
211-
{
212-
Tuple<bool, string, string> result = new Tuple<bool, string, string>(false, "", "");
213-
214-
Function targetFunction = GetContractFunction(contractKey, chainId, functionName);
215-
216-
if (targetFunction != null)
217-
{
218-
try
219-
{
220-
TransactionReceipt resp = await targetFunction.SendTransactionAndWaitForReceiptAsync(fromAddress, gas, value, new System.Threading.CancellationTokenSource(), functionInput);
221-
if (resp.Succeeded() && resp.HasErrors() == false)
222-
{
223-
result = new Tuple<bool, string, string>(true, resp.TransactionHash, "");
224-
}
225-
else
226-
{
227-
result = new Tuple<bool, string, string>(true, "", "Transaction Failed.");
228-
}
229-
}
230-
catch (Exception exp)
231-
{
232-
result = new Tuple<bool, string, string>(false, "", exp.Message);
233-
}
234-
}
235-
236-
return result;
237-
}
238-
#else
239-
240161
/// <summary>
241162
/// Executes a contract SendTransaction, used for executing contract functions that change state.
242163
/// </summary>

Assets/MoralisWeb3ApiSdk/Example/Scripts/AwardableController.cs

+21-5
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,21 @@
2828
* SOFTWARE.
2929
*/
3030
using MoralisWeb3ApiSdk;
31-
using Nethereum.Hex.HexTypes;
3231
using System;
3332
using System.Collections;
3433
using System.Collections.Generic;
3534
using System.Linq;
3635
using UnityEngine;
3736
using Cysharp.Threading.Tasks;
38-
using Nethereum.Contracts;
3937
using System.Numerics;
4038
#if UNITY_WEBGL
4139
using Moralis.WebGL.Platform.Objects;
4240
using Moralis.WebGL.Web3Api.Models;
41+
using Moralis.WebGL.Hex.HexTypes;
4342
#else
4443
using System.Threading.Tasks;
44+
using Nethereum.Contracts;
45+
using Nethereum.Hex.HexTypes;
4546
using Moralis.Platform.Objects;
4647
using Moralis.Web3Api.Models;
4748
#endif
@@ -97,10 +98,15 @@ where n.TokenId.Equals(NftTokenId.ToString())
9798

9899
if (ownership != null && ownership.Count() > 0)
99100
{
101+
Debug.Log("Already Owns Mug.");
100102
isOwned = true;
101103
// Hide the NFT Gmae object since it is already owned.
102104
transform.gameObject.SetActive(false);
103105
}
106+
else
107+
{
108+
Debug.Log("Does not own Mug.");
109+
}
104110
}
105111
catch (Exception exp)
106112
{
@@ -146,15 +152,25 @@ private async UniTask ClaimRewardAsync()
146152

147153
if (BigInteger.TryParse(NftTokenId, out bi))
148154
{
155+
156+
#if UNITY_WEBGL
157+
149158
// Convert token id to hex as this is what the contract call expects
150-
object[] pars = new object[] { bi.ToString("x") };
159+
object[] pars = new object[] { bi.ToString() };
151160

152161
// Set gas estimate
153-
HexBigInteger gas = new HexBigInteger(80000);
162+
HexBigInteger gas = new HexBigInteger(0);
163+
string resp = await MoralisInterface.ExecuteFunction(Constants.MUG_CONTRACT_ADDRESS, Constants.MUG_ABI, Constants.MUG_CLAIM_FUNCTION, pars, new HexBigInteger("0x0"), gas, gas);
164+
#else
165+
166+
// Convert token id to hex as this is what the contract call expects
167+
object[] pars = new object[] { bi.ToString("x") };
154168

169+
// Set gas estimate
170+
HexBigInteger gas = new HexBigInteger(0);
155171
// Call the contract to claim the NFT reward.
156172
string resp = await MoralisInterface.SendEvmTransactionAsync("Rewards", "mumbai", "claimReward", addr, gas, new HexBigInteger("0x0"), pars);
157-
173+
#endif
158174
// Hide the NFT GameObject since it has been claimed
159175
// You could also play a victory sound etc.
160176
transform.gameObject.SetActive(false);

0 commit comments

Comments
 (0)