Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple singleton // Instance based classes #193

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Assets/Tests/StorageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IEnumerator Gateway_WithoutClientId_Success()
Assert.AreEqual(ThirdwebManager.Instance.SDK.Storage.IPFSGateway, "https://cloudflare-ipfs.com/ipfs/");

string testIpfsRawUrl = "ipfs://Qblabla";
Assert.AreEqual(Utils.ReplaceIPFS(testIpfsRawUrl), "https://cloudflare-ipfs.com/ipfs/Qblabla");
Assert.AreEqual(Utils.ReplaceIPFS(testIpfsRawUrl, ThirdwebManager.Instance.SDK.Storage.IPFSGateway), "https://cloudflare-ipfs.com/ipfs/Qblabla");

yield return null;
}
Expand All @@ -52,7 +52,7 @@ public IEnumerator Gateway_WithClientId_Success()
Assert.AreEqual(ThirdwebManager.Instance.SDK.Storage.IPFSGateway, $"https://{clientId}.ipfscdn.io/ipfs/");

string testIpfsRawUrl = "ipfs://Qblabla";
Assert.AreEqual(Utils.ReplaceIPFS(testIpfsRawUrl), $"https://{clientId}.ipfscdn.io/ipfs/Qblabla");
Assert.AreEqual(Utils.ReplaceIPFS(testIpfsRawUrl, ThirdwebManager.Instance.SDK.Storage.IPFSGateway), $"https://{clientId}.ipfscdn.io/ipfs/Qblabla");

yield return null;
}
Expand All @@ -67,7 +67,7 @@ public IEnumerator Gateway_WithOverride_Success()
Assert.AreEqual(ThirdwebManager.Instance.SDK.Storage.IPFSGateway, ipfsGatewayUrl);

string testIpfsRawUrl = "ipfs://Qblabla";
Assert.AreEqual(Utils.ReplaceIPFS(testIpfsRawUrl), "https://ipfs.io/ipfs/Qblabla");
Assert.AreEqual(Utils.ReplaceIPFS(testIpfsRawUrl, ThirdwebManager.Instance.SDK.Storage.IPFSGateway), "https://ipfs.io/ipfs/Qblabla");

yield return null;
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/ThirdwebManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public IEnumerator Initialization_WithClientIdNoBundleIdOverride_AppliesCorrectl
ThirdwebManager.Instance.clientId = "testClientId";
Assert.IsNull(ThirdwebManager.Instance.bundleIdOverride);

string bundleId = Utils.GetBundleId();
string bundleId = Application.identifier.ToLower();
Assert.IsNotNull(bundleId);

ThirdwebManager.Instance.supportedChains = new List<ChainData> { new("arbitrum-sepolia", "421614", null), };
Expand Down
169 changes: 169 additions & 0 deletions Assets/Tests/ThirdwebSDKTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
using NUnit.Framework;
using Thirdweb;
using UnityEngine;
using UnityEngine.TestTools;

public class ThirdwebSDKTests : ConfigManager
{
private ThirdwebSDK _sdk;
private readonly string _dropErc20Address = "0xEBB8a39D865465F289fa349A67B3391d8f910da9";

[SetUp]
public void SetUp()
{
var chainId = 421614;
var clientId = GetClientId();
var bundleId = Application.identifier.ToLower();
var options = new ThirdwebSDK.Options()
{
clientId = clientId,
bundleId = bundleId,
supportedChains = new ThirdwebChainData[] { ThirdwebSession.FetchChainData(chainId) },
smartWalletConfig = new ThirdwebSDK.SmartWalletConfig()
{
factoryAddress = Thirdweb.AccountAbstraction.Constants.DEFAULT_FACTORY_ADDRESS,
gasless = true,
bundlerUrl = $"https://{chainId}.bundler.thirdweb.com",
paymasterUrl = $"https://{chainId}.bundler.thirdweb.com",
entryPointAddress = Thirdweb.AccountAbstraction.Constants.DEFAULT_ENTRYPOINT_ADDRESS,
}
};
_sdk = new ThirdwebSDK($"https://{chainId}.rpc.thirdweb.com/{clientId}&bundleId={bundleId}", 421614, options);
}

[TearDown]
public void TearDown()
{
_sdk = null;
}

[UnityTest]
public IEnumerator Initialization_Success()
{
Assert.IsNotNull(_sdk);
Assert.AreEqual(_sdk.Session.Options.clientId, GetClientId());
Assert.AreEqual(_sdk.Session.Options.bundleId, Application.identifier.ToLower());
yield return null;
}

[UnityTest]
public IEnumerator ContractRead_Success()
{
var contract = _sdk.GetContract(_dropErc20Address);
var readTask = contract.ERC20.BalanceOf(_dropErc20Address);
yield return new WaitUntil(() => readTask.IsCompleted);
if (readTask.IsFaulted)
throw readTask.Exception;
Assert.IsTrue(readTask.IsCompletedSuccessfully);
Assert.NotNull(readTask.Result);
}

[UnityTest]
public IEnumerator ContractWrite_Success()
{
Utils.DeleteLocalAccount();
var connection = new WalletConnection(provider: WalletProvider.SmartWallet, chainId: 421614, personalWallet: WalletProvider.LocalWallet);
var connectTask = _sdk.Wallet.Connect(connection);
yield return new WaitUntil(() => connectTask.IsCompleted);
if (connectTask.IsFaulted)
throw connectTask.Exception;
Assert.IsTrue(connectTask.IsCompletedSuccessfully);

var contract = _sdk.GetContract(_dropErc20Address);
var task = contract.ERC20.SetAllowance(_dropErc20Address, "0");
yield return new WaitUntil(() => task.IsCompleted);
if (task.IsFaulted)
throw task.Exception;
Assert.IsTrue(task.IsCompletedSuccessfully);
Assert.IsNotNull(task.Result);
Assert.IsTrue(task.Result.receipt.transactionHash.Length == 66);
}

[UnityTest]
public IEnumerator CustomContractRead_Success()
{
var contract = _sdk.GetContract(_dropErc20Address);
var readTask = contract.Read<BigInteger>("balanceOf", _dropErc20Address);
yield return new WaitUntil(() => readTask.IsCompleted);
if (readTask.IsFaulted)
throw readTask.Exception;
Assert.IsTrue(readTask.IsCompletedSuccessfully);
Assert.NotNull(readTask.Result);
}

[UnityTest]
public IEnumerator CustomContractWrite_Success()
{
Utils.DeleteLocalAccount();
var connection = new WalletConnection(provider: WalletProvider.SmartWallet, chainId: 421614, personalWallet: WalletProvider.LocalWallet);
var connectTask = _sdk.Wallet.Connect(connection);
yield return new WaitUntil(() => connectTask.IsCompleted);
if (connectTask.IsFaulted)
throw connectTask.Exception;
Assert.IsTrue(connectTask.IsCompletedSuccessfully);

var contract = _sdk.GetContract(_dropErc20Address);
var task = contract.ERC20.SetAllowance(_dropErc20Address, "0");
yield return new WaitUntil(() => task.IsCompleted);
if (task.IsFaulted)
throw task.Exception;
Assert.IsTrue(task.IsCompletedSuccessfully);
Assert.IsNotNull(task.Result);
Assert.IsTrue(task.Result.receipt.transactionHash.Length == 66);
}

[UnityTest]
public IEnumerator WalletSignMessage_Success()
{
Utils.DeleteLocalAccount();
var connection = new WalletConnection(provider: WalletProvider.LocalWallet, chainId: 421614);
var connectTask = _sdk.Wallet.Connect(connection);
yield return new WaitUntil(() => connectTask.IsCompleted);
if (connectTask.IsFaulted)
throw connectTask.Exception;
Assert.IsTrue(connectTask.IsCompletedSuccessfully);

var message = "Hello, World!";
var task = _sdk.Wallet.Sign(message);
yield return new WaitUntil(() => task.IsCompleted);
if (task.IsFaulted)
throw task.Exception;
Assert.IsTrue(task.IsCompletedSuccessfully);
Assert.IsNotNull(task.Result);
}

[UnityTest]
public IEnumerator IPFSDownload_Success()
{
string url = "ipfs://QmNQ2djT2u4my5xpKPgJMnQEpoNjYZE8ugpLndvgEJBb3X";

var downloadTask = _sdk.Storage.DownloadText<string>(url);
yield return new WaitUntil(() => downloadTask.IsCompleted);
Assert.IsTrue(downloadTask.IsCompletedSuccessfully);
Assert.IsNotNull(downloadTask.Result);
Assert.IsTrue(downloadTask.Result.Length > 0);
Assert.IsTrue(downloadTask.Result.StartsWith("{\"compiler\":{\"version\":\"0.8.23+commit.f704f362\"},\"language\":\"Solidity\""));
}

[UnityTest]
public IEnumerator IPFSUpload_Success()
{
string text = "Hello World!";
var uploadTask = _sdk.Storage.UploadText(text);
yield return new WaitUntil(() => uploadTask.IsCompleted);
Assert.IsTrue(uploadTask.IsCompletedSuccessfully);
Assert.IsNotNull(uploadTask.Result);
Assert.IsNotNull(uploadTask.Result.IpfsHash);

string url = "ipfs://" + uploadTask.Result.IpfsHash;
var downloadTask = _sdk.Storage.DownloadText<string>(url);
yield return new WaitUntil(() => downloadTask.IsCompleted);
Assert.IsTrue(downloadTask.IsCompletedSuccessfully);
Assert.IsNotNull(downloadTask.Result);
Assert.AreEqual(downloadTask.Result, text);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Assets/Tests/TransactionReadTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void TearDown()
public IEnumerator Static_WaitForTransactionResult_Success()
{
string txHash = "0x52b79681f549d7b01b12b8be5fa9dd88f7fee1411f965cbe7ec6e157ccb48af1";
var task = Transaction.WaitForTransactionResult(txHash);
var task = Transaction.WaitForTransactionResult(txHash, ThirdwebManager.Instance.SDK.Session.ChainId);
yield return new WaitUntil(() => task.IsCompleted);
Assert.IsTrue(task.IsCompletedSuccessfully);
Assert.IsNotNull(task.Result);
Expand Down
64 changes: 32 additions & 32 deletions Assets/Tests/WalletTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,38 +91,38 @@ public IEnumerator Export_WithLocalWallet_Success()
Assert.IsTrue(exportTask.Result.Length > 0);
}

[UnityTest]
public IEnumerator Authenticate_WithLocalWallet_Success()
{
yield return Connect_WithLocalWallet_Success();

var authenticateTask = ThirdwebManager.Instance.SDK.Wallet.Authenticate("example.com");
yield return new WaitUntil(() => authenticateTask.IsCompleted);
Assert.IsTrue(authenticateTask.IsCompletedSuccessfully);
Assert.IsNotNull(authenticateTask.Result);
Assert.IsTrue(authenticateTask.Result.signature.Length == 132);
}

[UnityTest]
public IEnumerator Verify_WithLocalWallet_Success()
{
yield return Connect_WithLocalWallet_Success();

var authenticateTask = ThirdwebManager.Instance.SDK.Wallet.Authenticate("example.com");
yield return new WaitUntil(() => authenticateTask.IsCompleted);
Assert.IsTrue(authenticateTask.IsCompletedSuccessfully);
Assert.IsNotNull(authenticateTask.Result);
Assert.IsTrue(authenticateTask.Result.signature.Length == 132);

var verifyTask = ThirdwebManager.Instance.SDK.Wallet.Verify(authenticateTask.Result);
yield return new WaitUntil(() => verifyTask.IsCompleted);
Assert.IsTrue(verifyTask.IsCompletedSuccessfully);

var getAddressTask = ThirdwebManager.Instance.SDK.Wallet.GetAddress();
yield return new WaitUntil(() => getAddressTask.IsCompleted);
Assert.IsTrue(getAddressTask.IsCompletedSuccessfully);
Assert.AreEqual(verifyTask.Result, getAddressTask.Result);
}
// [UnityTest]
// public IEnumerator Authenticate_WithLocalWallet_Success()
// {
// yield return Connect_WithLocalWallet_Success();

// var authenticateTask = ThirdwebManager.Instance.SDK.Wallet.Authenticate("example.com");
// yield return new WaitUntil(() => authenticateTask.IsCompleted);
// Assert.IsTrue(authenticateTask.IsCompletedSuccessfully);
// Assert.IsNotNull(authenticateTask.Result);
// Assert.IsTrue(authenticateTask.Result.signature.Length == 132);
// }

// [UnityTest]
// public IEnumerator Verify_WithLocalWallet_Success()
// {
// yield return Connect_WithLocalWallet_Success();

// var authenticateTask = ThirdwebManager.Instance.SDK.Wallet.Authenticate("example.com");
// yield return new WaitUntil(() => authenticateTask.IsCompleted);
// Assert.IsTrue(authenticateTask.IsCompletedSuccessfully);
// Assert.IsNotNull(authenticateTask.Result);
// Assert.IsTrue(authenticateTask.Result.signature.Length == 132);

// var verifyTask = ThirdwebManager.Instance.SDK.Wallet.Verify(authenticateTask.Result);
// yield return new WaitUntil(() => verifyTask.IsCompleted);
// Assert.IsTrue(verifyTask.IsCompletedSuccessfully);

// var getAddressTask = ThirdwebManager.Instance.SDK.Wallet.GetAddress();
// yield return new WaitUntil(() => getAddressTask.IsCompleted);
// Assert.IsTrue(getAddressTask.IsCompletedSuccessfully);
// Assert.AreEqual(verifyTask.Result, getAddressTask.Result);
// }

[UnityTest]
public IEnumerator GetBalance_WithLocalWallet_Success()
Expand Down
Binary file not shown.

This file was deleted.

Binary file not shown.
Loading
Loading