Skip to content

Commit

Permalink
Contract.FetchAbi Helper (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored Apr 10, 2024
1 parent ec4ce3e commit 9e90d61
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
26 changes: 12 additions & 14 deletions Assets/Tests/CustomReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,21 @@ public IEnumerator GetContract_Success()
}

[UnityTest]
public IEnumerator Custom_WithoutAbi_FailNativeSucceedWebGL()
public IEnumerator Custom_WithoutAbi_Fetch()
{
var contract = ThirdwebManager.Instance.SDK.GetContract(_dropErc20Address);
var abiTask = Contract.FetchAbi(_dropErc20Address, 421614);
yield return new WaitUntil(() => abiTask.IsCompleted);
if (abiTask.IsFaulted)
throw abiTask.Exception;
Assert.IsTrue(abiTask.IsCompletedSuccessfully);
Assert.NotNull(abiTask.Result);
var contract = ThirdwebManager.Instance.SDK.GetContract(_dropErc20Address, abiTask.Result);
var readTask = contract.Read<BigInteger>("balanceOf", _dropErc20Address);
yield return new WaitUntil(() => readTask.IsCompleted);
if (Utils.IsWebGLBuild())
{
if (readTask.IsFaulted)
throw readTask.Exception;
Assert.IsTrue(readTask.IsCompletedSuccessfully);
Assert.NotNull(readTask.Result);
}
else
{
Assert.IsTrue(readTask.IsFaulted);
Assert.AreEqual("You must pass an ABI for native platform custom calls", readTask.Exception.InnerException.Message);
}
if (readTask.IsFaulted)
throw readTask.Exception;
Assert.IsTrue(readTask.IsCompletedSuccessfully);
Assert.NotNull(readTask.Result);
}

[UnityTest]
Expand Down
8 changes: 4 additions & 4 deletions Assets/Tests/SmartWalletTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public IEnumerator CreateSessionKey_WithValidSignerCheck_Success()
yield return Connect_WithGaslessManagedAccountFactory_Success();

var task = ThirdwebManager.Instance.SDK.Wallet.CreateSessionKey(
signerAddress: "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb",
signerAddress: "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c",
approvedTargets: new List<string>() { "0x450b943729Ddba196Ab58b589Cea545551DF71CC" },
nativeTokenLimitPerTransactionInWei: "0",
permissionStartTimestamp: "0",
Expand All @@ -88,7 +88,7 @@ public IEnumerator CreateSessionKey_WithValidSignerCheck_Success()
bool exists = false;
foreach (var signer in getAllActiveSignersTask.Result)
{
if (signer.signer == "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb")
if (signer.signer == "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c")
{
exists = true;
break;
Expand Down Expand Up @@ -120,7 +120,7 @@ public IEnumerator RevokeSessionKey_WithValidSignerCheck_Success()
{
yield return CreateSessionKey_WithValidSignerCheck_Success();

var task = ThirdwebManager.Instance.SDK.Wallet.RevokeSessionKey(signerAddress: "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb");
var task = ThirdwebManager.Instance.SDK.Wallet.RevokeSessionKey(signerAddress: "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c");
yield return new WaitUntil(() => task.IsCompleted);
Assert.IsTrue(task.IsCompletedSuccessfully);
Assert.IsNotNull(task.Result);
Expand All @@ -134,7 +134,7 @@ public IEnumerator RevokeSessionKey_WithValidSignerCheck_Success()
bool exists = false;
foreach (var signer in getAllActiveSignersTask.Result)
{
if (signer.signer == "0x22b79AD6c6009525933ac2FF40bC9F30dF14Ecfb")
if (signer.signer == "0xA86F78b995a3899785FA1508eB1E62aEa501fc3c")
{
exists = true;
break;
Expand Down
22 changes: 19 additions & 3 deletions Assets/Thirdweb/Core/Scripts/Contract.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Nethereum.Contracts;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.ABI.FunctionEncoding.Attributes;
using UnityEngine.Networking;
using Thirdweb.Redcode.Awaiting;

namespace Thirdweb
{
Expand Down Expand Up @@ -198,7 +200,7 @@ public async Task<TransactionResult> Write(string functionName, TransactionReque
else
{
if (this.ABI == null)
throw new UnityException("You must pass an ABI for native platform custom calls");
this.ABI = await FetchAbi(this.Address, await ThirdwebManager.Instance.SDK.Wallet.GetChainId());

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

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

if (this.ABI == null)
throw new UnityException("You must pass an ABI for native platform custom calls");
this.ABI = await FetchAbi(this.Address, await ThirdwebManager.Instance.SDK.Wallet.GetChainId());

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

if (this.ABI == null)
throw new UnityException("You must pass an ABI for native platform custom calls");
this.ABI = await FetchAbi(this.Address, await ThirdwebManager.Instance.SDK.Wallet.GetChainId());

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

public static async Task<string> FetchAbi(string contractAddress, BigInteger chainId)
{
var url = $"https://contract.thirdweb.com/abi/{chainId}/{contractAddress}";
using (var request = UnityWebRequest.Get(url))
{
await request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
throw new UnityException($"Failed to fetch ABI! Error: {request.error}");
}
return request.downloadHandler.text;
}
}

private T ConvertValue<T>(object value)
{
if (value is T result)
Expand Down
Loading

0 comments on commit 9e90d61

Please sign in to comment.