|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Numerics; |
| 5 | +using NUnit.Framework; |
| 6 | +using Thirdweb; |
| 7 | +using UnityEngine; |
| 8 | +using UnityEngine.TestTools; |
| 9 | + |
| 10 | +public class ThirdwebSDKTests : ConfigManager |
| 11 | +{ |
| 12 | + private ThirdwebSDK _sdk; |
| 13 | + private readonly string _dropErc20Address = "0xEBB8a39D865465F289fa349A67B3391d8f910da9"; |
| 14 | + |
| 15 | + [SetUp] |
| 16 | + public void SetUp() |
| 17 | + { |
| 18 | + var chainId = 421614; |
| 19 | + var clientId = GetClientId(); |
| 20 | + var bundleId = Application.identifier.ToLower(); |
| 21 | + var options = new ThirdwebSDK.Options() |
| 22 | + { |
| 23 | + clientId = clientId, |
| 24 | + bundleId = bundleId, |
| 25 | + supportedChains = new ThirdwebChainData[] { ThirdwebSession.FetchChainData(chainId) }, |
| 26 | + smartWalletConfig = new ThirdwebSDK.SmartWalletConfig() |
| 27 | + { |
| 28 | + factoryAddress = Thirdweb.AccountAbstraction.Constants.DEFAULT_FACTORY_ADDRESS, |
| 29 | + gasless = true, |
| 30 | + bundlerUrl = $"https://{chainId}.bundler.thirdweb.com", |
| 31 | + paymasterUrl = $"https://{chainId}.bundler.thirdweb.com", |
| 32 | + entryPointAddress = Thirdweb.AccountAbstraction.Constants.DEFAULT_ENTRYPOINT_ADDRESS, |
| 33 | + } |
| 34 | + }; |
| 35 | + _sdk = new ThirdwebSDK($"https://{chainId}.rpc.thirdweb.com/{clientId}&bundleId={bundleId}", 421614, options); |
| 36 | + } |
| 37 | + |
| 38 | + [TearDown] |
| 39 | + public void TearDown() |
| 40 | + { |
| 41 | + _sdk = null; |
| 42 | + } |
| 43 | + |
| 44 | + [UnityTest] |
| 45 | + public IEnumerator Initialization_Success() |
| 46 | + { |
| 47 | + Assert.IsNotNull(_sdk); |
| 48 | + Assert.AreEqual(_sdk.Session.Options.clientId, GetClientId()); |
| 49 | + Assert.AreEqual(_sdk.Session.Options.bundleId, Application.identifier.ToLower()); |
| 50 | + yield return null; |
| 51 | + } |
| 52 | + |
| 53 | + [UnityTest] |
| 54 | + public IEnumerator ContractRead_Success() |
| 55 | + { |
| 56 | + var contract = _sdk.GetContract(_dropErc20Address); |
| 57 | + var readTask = contract.ERC20.BalanceOf(_dropErc20Address); |
| 58 | + yield return new WaitUntil(() => readTask.IsCompleted); |
| 59 | + if (readTask.IsFaulted) |
| 60 | + throw readTask.Exception; |
| 61 | + Assert.IsTrue(readTask.IsCompletedSuccessfully); |
| 62 | + Assert.NotNull(readTask.Result); |
| 63 | + } |
| 64 | + |
| 65 | + [UnityTest] |
| 66 | + public IEnumerator ContractWrite_Success() |
| 67 | + { |
| 68 | + Utils.DeleteLocalAccount(); |
| 69 | + var connection = new WalletConnection(provider: WalletProvider.SmartWallet, chainId: 421614, personalWallet: WalletProvider.LocalWallet); |
| 70 | + var connectTask = _sdk.Wallet.Connect(connection); |
| 71 | + yield return new WaitUntil(() => connectTask.IsCompleted); |
| 72 | + if (connectTask.IsFaulted) |
| 73 | + throw connectTask.Exception; |
| 74 | + Assert.IsTrue(connectTask.IsCompletedSuccessfully); |
| 75 | + |
| 76 | + var contract = _sdk.GetContract(_dropErc20Address); |
| 77 | + var task = contract.ERC20.SetAllowance(_dropErc20Address, "0"); |
| 78 | + yield return new WaitUntil(() => task.IsCompleted); |
| 79 | + if (task.IsFaulted) |
| 80 | + throw task.Exception; |
| 81 | + Assert.IsTrue(task.IsCompletedSuccessfully); |
| 82 | + Assert.IsNotNull(task.Result); |
| 83 | + Assert.IsTrue(task.Result.receipt.transactionHash.Length == 66); |
| 84 | + } |
| 85 | + |
| 86 | + [UnityTest] |
| 87 | + public IEnumerator CustomContractRead_Success() |
| 88 | + { |
| 89 | + var contract = _sdk.GetContract(_dropErc20Address); |
| 90 | + var readTask = contract.Read<BigInteger>("balanceOf", _dropErc20Address); |
| 91 | + yield return new WaitUntil(() => readTask.IsCompleted); |
| 92 | + if (readTask.IsFaulted) |
| 93 | + throw readTask.Exception; |
| 94 | + Assert.IsTrue(readTask.IsCompletedSuccessfully); |
| 95 | + Assert.NotNull(readTask.Result); |
| 96 | + } |
| 97 | + |
| 98 | + [UnityTest] |
| 99 | + public IEnumerator CustomContractWrite_Success() |
| 100 | + { |
| 101 | + Utils.DeleteLocalAccount(); |
| 102 | + var connection = new WalletConnection(provider: WalletProvider.SmartWallet, chainId: 421614, personalWallet: WalletProvider.LocalWallet); |
| 103 | + var connectTask = _sdk.Wallet.Connect(connection); |
| 104 | + yield return new WaitUntil(() => connectTask.IsCompleted); |
| 105 | + if (connectTask.IsFaulted) |
| 106 | + throw connectTask.Exception; |
| 107 | + Assert.IsTrue(connectTask.IsCompletedSuccessfully); |
| 108 | + |
| 109 | + var contract = _sdk.GetContract(_dropErc20Address); |
| 110 | + var task = contract.ERC20.SetAllowance(_dropErc20Address, "0"); |
| 111 | + yield return new WaitUntil(() => task.IsCompleted); |
| 112 | + if (task.IsFaulted) |
| 113 | + throw task.Exception; |
| 114 | + Assert.IsTrue(task.IsCompletedSuccessfully); |
| 115 | + Assert.IsNotNull(task.Result); |
| 116 | + Assert.IsTrue(task.Result.receipt.transactionHash.Length == 66); |
| 117 | + } |
| 118 | + |
| 119 | + [UnityTest] |
| 120 | + public IEnumerator WalletSignMessage_Success() |
| 121 | + { |
| 122 | + Utils.DeleteLocalAccount(); |
| 123 | + var connection = new WalletConnection(provider: WalletProvider.LocalWallet, chainId: 421614); |
| 124 | + var connectTask = _sdk.Wallet.Connect(connection); |
| 125 | + yield return new WaitUntil(() => connectTask.IsCompleted); |
| 126 | + if (connectTask.IsFaulted) |
| 127 | + throw connectTask.Exception; |
| 128 | + Assert.IsTrue(connectTask.IsCompletedSuccessfully); |
| 129 | + |
| 130 | + var message = "Hello, World!"; |
| 131 | + var task = _sdk.Wallet.Sign(message); |
| 132 | + yield return new WaitUntil(() => task.IsCompleted); |
| 133 | + if (task.IsFaulted) |
| 134 | + throw task.Exception; |
| 135 | + Assert.IsTrue(task.IsCompletedSuccessfully); |
| 136 | + Assert.IsNotNull(task.Result); |
| 137 | + } |
| 138 | + |
| 139 | + [UnityTest] |
| 140 | + public IEnumerator IPFSDownload_Success() |
| 141 | + { |
| 142 | + string url = "ipfs://QmNQ2djT2u4my5xpKPgJMnQEpoNjYZE8ugpLndvgEJBb3X"; |
| 143 | + |
| 144 | + var downloadTask = _sdk.Storage.DownloadText<string>(url); |
| 145 | + yield return new WaitUntil(() => downloadTask.IsCompleted); |
| 146 | + Assert.IsTrue(downloadTask.IsCompletedSuccessfully); |
| 147 | + Assert.IsNotNull(downloadTask.Result); |
| 148 | + Assert.IsTrue(downloadTask.Result.Length > 0); |
| 149 | + Assert.IsTrue(downloadTask.Result.StartsWith("{\"compiler\":{\"version\":\"0.8.23+commit.f704f362\"},\"language\":\"Solidity\"")); |
| 150 | + } |
| 151 | + |
| 152 | + [UnityTest] |
| 153 | + public IEnumerator IPFSUpload_Success() |
| 154 | + { |
| 155 | + string text = "Hello World!"; |
| 156 | + var uploadTask = _sdk.Storage.UploadText(text); |
| 157 | + yield return new WaitUntil(() => uploadTask.IsCompleted); |
| 158 | + Assert.IsTrue(uploadTask.IsCompletedSuccessfully); |
| 159 | + Assert.IsNotNull(uploadTask.Result); |
| 160 | + Assert.IsNotNull(uploadTask.Result.IpfsHash); |
| 161 | + |
| 162 | + string url = "ipfs://" + uploadTask.Result.IpfsHash; |
| 163 | + var downloadTask = _sdk.Storage.DownloadText<string>(url); |
| 164 | + yield return new WaitUntil(() => downloadTask.IsCompleted); |
| 165 | + Assert.IsTrue(downloadTask.IsCompletedSuccessfully); |
| 166 | + Assert.IsNotNull(downloadTask.Result); |
| 167 | + Assert.AreEqual(downloadTask.Result, text); |
| 168 | + } |
| 169 | +} |
0 commit comments