|
10 | 10 | using Nethereum.Contracts; |
11 | 11 | using Nethereum.RPC.Eth.DTOs; |
12 | 12 | using Nethereum.ABI.FunctionEncoding.Attributes; |
| 13 | +using UnityEngine.Networking; |
| 14 | +using Thirdweb.Redcode.Awaiting; |
13 | 15 |
|
14 | 16 | namespace Thirdweb |
15 | 17 | { |
@@ -198,7 +200,7 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque |
198 | 200 | else |
199 | 201 | { |
200 | 202 | if (this.ABI == null) |
201 | | - throw new UnityException("You must pass an ABI for native platform custom calls"); |
| 203 | + this.ABI = await FetchAbi(this.Address, await ThirdwebManager.Instance.SDK.Wallet.GetChainId()); |
202 | 204 |
|
203 | 205 | var contract = ThirdwebManager.Instance.SDK.Session.Web3.Eth.GetContract(this.ABI, this.Address); |
204 | 206 |
|
@@ -256,7 +258,7 @@ public async Task<T> Read<T>(string functionName, params object[] args) |
256 | 258 | } |
257 | 259 |
|
258 | 260 | if (this.ABI == null) |
259 | | - throw new UnityException("You must pass an ABI for native platform custom calls"); |
| 261 | + this.ABI = await FetchAbi(this.Address, await ThirdwebManager.Instance.SDK.Wallet.GetChainId()); |
260 | 262 |
|
261 | 263 | var contract = Utils.GetWeb3().Eth.GetContract(this.ABI, this.Address); |
262 | 264 | var function = contract.GetFunction(functionName); |
@@ -392,13 +394,27 @@ public async Task<T> ReadRaw<T>(string functionName, params object[] args) |
392 | 394 | } |
393 | 395 |
|
394 | 396 | if (this.ABI == null) |
395 | | - throw new UnityException("You must pass an ABI for native platform custom calls"); |
| 397 | + this.ABI = await FetchAbi(this.Address, await ThirdwebManager.Instance.SDK.Wallet.GetChainId()); |
396 | 398 |
|
397 | 399 | var contract = Utils.GetWeb3().Eth.GetContract(this.ABI, this.Address); |
398 | 400 | var function = contract.GetFunction(functionName); |
399 | 401 | return await function.CallDeserializingToObjectAsync<T>(args); |
400 | 402 | } |
401 | 403 |
|
| 404 | + public static async Task<string> FetchAbi(string contractAddress, BigInteger chainId) |
| 405 | + { |
| 406 | + var url = $"https://contract.thirdweb.com/abi/{chainId}/{contractAddress}"; |
| 407 | + using (var request = UnityWebRequest.Get(url)) |
| 408 | + { |
| 409 | + await request.SendWebRequest(); |
| 410 | + if (request.result != UnityWebRequest.Result.Success) |
| 411 | + { |
| 412 | + throw new UnityException($"Failed to fetch ABI! Error: {request.error}"); |
| 413 | + } |
| 414 | + return request.downloadHandler.text; |
| 415 | + } |
| 416 | + } |
| 417 | + |
402 | 418 | private T ConvertValue<T>(object value) |
403 | 419 | { |
404 | 420 | if (value is T result) |
|
0 commit comments