Skip to content

Commit 9e90d61

Browse files
authored
Contract.FetchAbi Helper (#174)
1 parent ec4ce3e commit 9e90d61

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

Assets/Tests/CustomReadTests.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,21 @@ public IEnumerator GetContract_Success()
4646
}
4747

4848
[UnityTest]
49-
public IEnumerator Custom_WithoutAbi_FailNativeSucceedWebGL()
49+
public IEnumerator Custom_WithoutAbi_Fetch()
5050
{
51-
var contract = ThirdwebManager.Instance.SDK.GetContract(_dropErc20Address);
51+
var abiTask = Contract.FetchAbi(_dropErc20Address, 421614);
52+
yield return new WaitUntil(() => abiTask.IsCompleted);
53+
if (abiTask.IsFaulted)
54+
throw abiTask.Exception;
55+
Assert.IsTrue(abiTask.IsCompletedSuccessfully);
56+
Assert.NotNull(abiTask.Result);
57+
var contract = ThirdwebManager.Instance.SDK.GetContract(_dropErc20Address, abiTask.Result);
5258
var readTask = contract.Read<BigInteger>("balanceOf", _dropErc20Address);
5359
yield return new WaitUntil(() => readTask.IsCompleted);
54-
if (Utils.IsWebGLBuild())
55-
{
56-
if (readTask.IsFaulted)
57-
throw readTask.Exception;
58-
Assert.IsTrue(readTask.IsCompletedSuccessfully);
59-
Assert.NotNull(readTask.Result);
60-
}
61-
else
62-
{
63-
Assert.IsTrue(readTask.IsFaulted);
64-
Assert.AreEqual("You must pass an ABI for native platform custom calls", readTask.Exception.InnerException.Message);
65-
}
60+
if (readTask.IsFaulted)
61+
throw readTask.Exception;
62+
Assert.IsTrue(readTask.IsCompletedSuccessfully);
63+
Assert.NotNull(readTask.Result);
6664
}
6765

6866
[UnityTest]

Assets/Tests/SmartWalletTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public IEnumerator CreateSessionKey_WithValidSignerCheck_Success()
6666
yield return Connect_WithGaslessManagedAccountFactory_Success();
6767

6868
var task = ThirdwebManager.Instance.SDK.Wallet.CreateSessionKey(
69-
signerAddress: "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb",
69+
signerAddress: "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c",
7070
approvedTargets: new List<string>() { "0x450b943729Ddba196Ab58b589Cea545551DF71CC" },
7171
nativeTokenLimitPerTransactionInWei: "0",
7272
permissionStartTimestamp: "0",
@@ -88,7 +88,7 @@ public IEnumerator CreateSessionKey_WithValidSignerCheck_Success()
8888
bool exists = false;
8989
foreach (var signer in getAllActiveSignersTask.Result)
9090
{
91-
if (signer.signer == "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb")
91+
if (signer.signer == "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c")
9292
{
9393
exists = true;
9494
break;
@@ -120,7 +120,7 @@ public IEnumerator RevokeSessionKey_WithValidSignerCheck_Success()
120120
{
121121
yield return CreateSessionKey_WithValidSignerCheck_Success();
122122

123-
var task = ThirdwebManager.Instance.SDK.Wallet.RevokeSessionKey(signerAddress: "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb");
123+
var task = ThirdwebManager.Instance.SDK.Wallet.RevokeSessionKey(signerAddress: "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c");
124124
yield return new WaitUntil(() => task.IsCompleted);
125125
Assert.IsTrue(task.IsCompletedSuccessfully);
126126
Assert.IsNotNull(task.Result);
@@ -134,7 +134,7 @@ public IEnumerator RevokeSessionKey_WithValidSignerCheck_Success()
134134
bool exists = false;
135135
foreach (var signer in getAllActiveSignersTask.Result)
136136
{
137-
if (signer.signer == "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb")
137+
if (signer.signer == "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c")
138138
{
139139
exists = true;
140140
break;

Assets/Thirdweb/Core/Scripts/Contract.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Nethereum.Contracts;
1111
using Nethereum.RPC.Eth.DTOs;
1212
using Nethereum.ABI.FunctionEncoding.Attributes;
13+
using UnityEngine.Networking;
14+
using Thirdweb.Redcode.Awaiting;
1315

1416
namespace Thirdweb
1517
{
@@ -198,7 +200,7 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
198200
else
199201
{
200202
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());
202204

203205
var contract = ThirdwebManager.Instance.SDK.Session.Web3.Eth.GetContract(this.ABI, this.Address);
204206

@@ -256,7 +258,7 @@ public async Task<T> Read<T>(string functionName, params object[] args)
256258
}
257259

258260
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());
260262

261263
var contract = Utils.GetWeb3().Eth.GetContract(this.ABI, this.Address);
262264
var function = contract.GetFunction(functionName);
@@ -392,13 +394,27 @@ public async Task<T> ReadRaw<T>(string functionName, params object[] args)
392394
}
393395

394396
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());
396398

397399
var contract = Utils.GetWeb3().Eth.GetContract(this.ABI, this.Address);
398400
var function = contract.GetFunction(functionName);
399401
return await function.CallDeserializingToObjectAsync<T>(args);
400402
}
401403

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+
402418
private T ConvertValue<T>(object value)
403419
{
404420
if (value is T result)

0 commit comments

Comments
 (0)