Skip to content

Commit

Permalink
ERC20 Extensions + Caching ABI (#26)
Browse files Browse the repository at this point in the history
* ERC20 Extensions

* null checks

* generate pk wallet + tests update + gaspub bump

* Update Thirdweb.ERC20Extension.Tests copy.cs

* extra tests
  • Loading branch information
0xFirekeeper authored Jun 12, 2024
1 parent b22d405 commit 1191e4e
Show file tree
Hide file tree
Showing 13 changed files with 431 additions and 32 deletions.
14 changes: 6 additions & 8 deletions Thirdweb.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
var client = ThirdwebClient.Create(secretKey: secretKey, fetchTimeoutOptions: new TimeoutOptions(storage: 30000, rpc: 60000));

var contract = await ThirdwebContract.Create(client: client, address: "0x81ebd23aA79bCcF5AaFb9c9c5B0Db4223c39102e", chain: 421614);
var readResult = await ThirdwebContract.Read<string>(contract, "name");
var readResult = await contract.ERC20_Name();
Console.WriteLine($"Contract read result: {readResult}");

// Create wallets (this is an advanced use case, typically one wallet is plenty)
Expand Down Expand Up @@ -91,13 +91,11 @@
// var txHash = await ThirdwebTransaction.Send(transaction: tx);
// Console.WriteLine($"Transaction hash: {txHash}");

var zkSmartWallet = await SmartWallet.Create(client: client, personalWallet: privateKeyWallet, chainId: 302, gasless: true);
Console.WriteLine($"Smart wallet address: {await zkSmartWallet.GetAddress()}");
var zkAaTx = await ThirdwebTransaction.Create(client, zkSmartWallet, new ThirdwebTransactionInput() { From = await zkSmartWallet.GetAddress(), To = await zkSmartWallet.GetAddress(), }, 302);
var zkSyncSignatureBasedAaTxHash = await ThirdwebTransaction.Send(zkAaTx);
Console.WriteLine($"Transaction hash: {zkSyncSignatureBasedAaTxHash}");


// var zkSmartWallet = await SmartWallet.Create(client: client, personalWallet: privateKeyWallet, chainId: 302, gasless: true);
// Console.WriteLine($"Smart wallet address: {await zkSmartWallet.GetAddress()}");
// var zkAaTx = await ThirdwebTransaction.Create(client, zkSmartWallet, new ThirdwebTransactionInput() { From = await zkSmartWallet.GetAddress(), To = await zkSmartWallet.GetAddress(), }, 302);
// var zkSyncSignatureBasedAaTxHash = await ThirdwebTransaction.Send(zkAaTx);
// Console.WriteLine($"Transaction hash: {zkSyncSignatureBasedAaTxHash}");

// Create smart wallet with InAppWallet signer
// var smartWallet = await SmartWallet.Create(client: client, personalWallet: inAppWallet, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
Expand Down
2 changes: 1 addition & 1 deletion Thirdweb.Tests/Thirdweb.Contracts.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public async Task WriteTest_PrivateKeyAccount()
public async Task SignatureMint_Generate()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var signer = await PrivateKeyWallet.Create(client, _testPrivateKey);
var signer = await PrivateKeyWallet.Generate(client);

var randomDomain = "Test";
var randomVersion = "1.0.0";
Expand Down
188 changes: 188 additions & 0 deletions Thirdweb.Tests/Thirdweb.ERC20Extension.Tests copy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using System.Numerics;

namespace Thirdweb.Tests
{
public class ERC20ExtensionTests : BaseTests
{
private readonly string _erc20ContractAddress = "0x81ebd23aA79bCcF5AaFb9c9c5B0Db4223c39102e";
private readonly BigInteger _chainId = 421614;

public ERC20ExtensionTests(ITestOutputHelper output)
: base(output) { }

private async Task<IThirdwebWallet> GetWallet()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyWallet = await PrivateKeyWallet.Create(client, _testPrivateKey);
var smartAccount = await SmartWallet.Create(client, personalWallet: privateKeyWallet, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
return smartAccount;
}

[Fact]
public async Task NullChecks()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);
var wallet = await GetWallet();

_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_BalanceOf(null));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_BalanceOf(string.Empty));

_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Allowance(null, null));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Allowance(string.Empty, null));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Allowance(null, string.Empty));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Allowance(Constants.ADDRESS_ZERO, null));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Allowance(null, Constants.ADDRESS_ZERO));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Approve(null, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Approve(wallet, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Approve(wallet, string.Empty, BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Transfer(null, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Transfer(wallet, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_Transfer(wallet, string.Empty, BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_TransferFrom(null, null, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_TransferFrom(wallet, null, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_TransferFrom(wallet, string.Empty, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_TransferFrom(wallet, Constants.ADDRESS_ZERO, null, BigInteger.Zero));
_ = await Assert.ThrowsAsync<ArgumentException>(async () => await contract.ERC20_TransferFrom(wallet, Constants.ADDRESS_ZERO, string.Empty, BigInteger.Zero));

contract = null;

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_BalanceOf(Constants.ADDRESS_ZERO));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_TotalSupply());

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Decimals());

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Symbol());

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Name());

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Allowance(null, null));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Approve(wallet, Constants.ADDRESS_ZERO, BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_Transfer(wallet, Constants.ADDRESS_ZERO, BigInteger.Zero));

_ = await Assert.ThrowsAsync<ArgumentNullException>(async () => await contract.ERC20_TransferFrom(wallet, Constants.ADDRESS_ZERO, Constants.ADDRESS_ZERO, BigInteger.Zero));
}

[Fact]
public async Task ERC20_BalanceOf()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);
var ownerAddress = Constants.ADDRESS_ZERO;

var balance = await contract.ERC20_BalanceOf(ownerAddress);

Assert.True(balance >= 0);
}

[Fact]
public async Task ERC20_TotalSupply()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);

var totalSupply = await contract.ERC20_TotalSupply();

Assert.True(totalSupply > 0);
}

[Fact]
public async Task ERC20_Decimals()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);

var decimals = await contract.ERC20_Decimals();

Assert.InRange(decimals, 0, 18);
}

[Fact]
public async Task ERC20_Symbol()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);

var symbol = await contract.ERC20_Symbol();

Assert.False(string.IsNullOrEmpty(symbol));
}

[Fact]
public async Task ERC20_Name()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);

var name = await contract.ERC20_Name();

Assert.False(string.IsNullOrEmpty(name));
}

[Fact]
public async Task ERC20_Allowance()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);
var ownerAddress = Constants.ADDRESS_ZERO;
var spenderAddress = contract.Address;

var allowance = await contract.ERC20_Allowance(ownerAddress, spenderAddress);

Assert.True(allowance >= 0);
}

[Fact]
public async Task ERC20_Approve()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);
var wallet = await GetWallet();
var spenderAddress = contract.Address;
var amount = BigInteger.Parse("1000000000000000000");

var receipt = await contract.ERC20_Approve(wallet, spenderAddress, amount);

Assert.True(receipt.TransactionHash.Length == 66);
}

[Fact]
public async Task ERC20_Transfer()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);
var wallet = await GetWallet();
var toAddress = await wallet.GetAddress();
var amount = BigInteger.Parse("1000000000000000000");

var receipt = await contract.ERC20_Transfer(wallet, toAddress, amount);

Assert.True(receipt.TransactionHash.Length == 66);
}

[Fact]
public async Task ERC20_TransferFrom()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var contract = await ThirdwebContract.Create(client, _erc20ContractAddress, _chainId);
var wallet = await GetWallet();

// SW Approval
_ = await contract.ERC20_Approve(wallet, await wallet.GetAddress(), BigInteger.Parse("1000000000000000000"));

var fromAddress = await wallet.GetAddress();
var toAddress = await wallet.GetAddress();
var amount = BigInteger.Parse("1000000000000000000");

var receipt = await contract.ERC20_TransferFrom(wallet, fromAddress, toAddress, amount);

Assert.True(receipt.TransactionHash.Length == 66);
}
}
}
2 changes: 1 addition & 1 deletion Thirdweb.Tests/Thirdweb.PrivateKeyWallet.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public PrivateKeyWalletTests(ITestOutputHelper output)
private async Task<PrivateKeyWallet> GetAccount()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyAccount = await PrivateKeyWallet.Create(client: client, privateKeyHex: _testPrivateKey);
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
return privateKeyAccount;
}

Expand Down
10 changes: 5 additions & 5 deletions Thirdweb.Tests/Thirdweb.SmartWallet.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task Initialization_Success()
public async Task Initialization_Fail()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey);
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
await privateKeyAccount.Disconnect();
var ex = await Assert.ThrowsAsync<InvalidOperationException>(
async () => await SmartWallet.Create(client, personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614)
Expand All @@ -40,7 +40,7 @@ public async Task Initialization_Fail()
public async Task ForceDeploy_Success()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey);
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
var smartAccount = await SmartWallet.Create(client, personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
await smartAccount.ForceDeploy();
Assert.True(await smartAccount.IsDeployed());
Expand All @@ -57,7 +57,7 @@ public async Task IsDeployed_True()
public async Task IsDeployed_False()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey);
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
var smartAccount = await SmartWallet.Create(
client,
personalWallet: privateKeyAccount,
Expand Down Expand Up @@ -88,7 +88,7 @@ public async Task SendTransaction_Success()
public async Task SendTransaction_ClientBundleId_Success()
{
var client = ThirdwebClient.Create(clientId: _clientIdBundleIdOnly, bundleId: _bundleIdBundleIdOnly);
var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey);
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
var smartAccount = await SmartWallet.Create(client, personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
var tx = await smartAccount.SendTransaction(
new ThirdwebTransactionInput()
Expand Down Expand Up @@ -130,7 +130,7 @@ public async Task GetPersonalAccount()
public async Task GetAddress_WithOverride()
{
var client = ThirdwebClient.Create(secretKey: _secretKey);
var privateKeyAccount = await PrivateKeyWallet.Create(client, _testPrivateKey);
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
var smartAccount = await SmartWallet.Create(
client,
personalWallet: privateKeyAccount,
Expand Down
Loading

0 comments on commit 1191e4e

Please sign in to comment.