Skip to content

Commit 371170e

Browse files
authored
Automatic Smart Wallet network switching // Require chain id lowest level (#60)
1 parent 1cc06e0 commit 371170e

File tree

13 files changed

+332
-132
lines changed

13 files changed

+332
-132
lines changed

Thirdweb.Tests/Thirdweb.Extensions/Thirdweb.Extensions.Tests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,59 @@ public async Task GetBalance_Wallet_WithERC20()
197197
Assert.True(balance >= 0);
198198
}
199199

200+
[Fact(Timeout = 120000)]
201+
public async Task GetTransactionCountRaw()
202+
{
203+
var address = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; // vitalik.eth
204+
var chainId = BigInteger.One;
205+
var transactionCount = await ThirdwebExtensions.GetTransactionCountRaw(this._client, chainId, address);
206+
Assert.True(transactionCount >= 0);
207+
}
208+
209+
[Fact(Timeout = 120000)]
210+
public async Task GetTransactionCountRaw_WithBlockTag()
211+
{
212+
var address = "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"; // vitalik.eth
213+
var chainId = this._chainId;
214+
var blockTag = "latest";
215+
var transactionCount = await ThirdwebExtensions.GetTransactionCountRaw(this._client, chainId, address, blockTag);
216+
Assert.True(transactionCount >= 0);
217+
}
218+
219+
[Fact(Timeout = 120000)]
220+
public async Task GetTransactionCount_Contract()
221+
{
222+
var contract = await this.GetTokenERC20Contract();
223+
var transactionCount = await contract.GetTransactionCount();
224+
Assert.True(transactionCount >= 0);
225+
}
226+
227+
[Fact(Timeout = 120000)]
228+
public async Task GetTransactionCount_Contract_WithBlockTag()
229+
{
230+
var contract = await this.GetTokenERC20Contract();
231+
var blockTag = "latest";
232+
var transactionCount = await contract.GetTransactionCount(blockTag);
233+
Assert.True(transactionCount >= 0);
234+
}
235+
236+
[Fact(Timeout = 120000)]
237+
public async Task GetTransactionCount_Wallet()
238+
{
239+
var wallet = await this.GetSmartWallet();
240+
var transactionCount = await wallet.GetTransactionCount(this._chainId);
241+
Assert.True(transactionCount >= 0);
242+
}
243+
244+
[Fact(Timeout = 120000)]
245+
public async Task GetTransactionCount_Wallet_WithBlockTag()
246+
{
247+
var wallet = await this.GetSmartWallet();
248+
var blockTag = "latest";
249+
var transactionCount = await wallet.GetTransactionCount(this._chainId, blockTag);
250+
Assert.True(transactionCount >= 0);
251+
}
252+
200253
[Fact(Timeout = 120000)]
201254
public async Task Transfer()
202255
{

Thirdweb.Tests/Thirdweb.Transactions/Thirdweb.Transactions.Tests.cs

Lines changed: 47 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ private async Task<ThirdwebTransaction> CreateSampleTransaction()
99
{
1010
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
1111
var wallet = await PrivateKeyWallet.Generate(client);
12-
var chainId = new BigInteger(421614);
13-
14-
var transaction = await ThirdwebTransaction.Create(wallet, new ThirdwebTransactionInput() { To = await wallet.GetAddress(), }, chainId);
12+
var transaction = await ThirdwebTransaction.Create(wallet, new ThirdwebTransactionInput(421614) { To = await wallet.GetAddress(), });
1513
return transaction;
1614
}
1715

@@ -50,9 +48,8 @@ public async Task Create_ValidatesInputParameters()
5048
{
5149
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
5250
var wallet = await PrivateKeyWallet.Generate(client);
53-
var txInput = new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO };
54-
var chainId = new BigInteger(421614);
55-
var transaction = await ThirdwebTransaction.Create(wallet, txInput, chainId);
51+
var txInput = new ThirdwebTransactionInput(421614) { To = Constants.ADDRESS_ZERO };
52+
var transaction = await ThirdwebTransaction.Create(wallet, txInput);
5653
Assert.NotNull(transaction);
5754
}
5855

@@ -61,8 +58,8 @@ public async Task Create_ThrowsOnNoTo()
6158
{
6259
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
6360
var wallet = await PrivateKeyWallet.Generate(client);
64-
var txInput = new ThirdwebTransactionInput() { };
65-
var ex = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebTransaction.Create(wallet, txInput, 421614));
61+
var txInput = new ThirdwebTransactionInput(421614) { };
62+
var ex = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebTransaction.Create(wallet, txInput));
6663
Assert.Contains("Transaction recipient (to) must be provided", ex.Message);
6764
}
6865

@@ -71,8 +68,8 @@ public async Task Create_ThrowsOnNoWallet()
7168
{
7269
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
7370
var wallet = await PrivateKeyWallet.Generate(client);
74-
var txInput = new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO };
75-
var ex = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebTransaction.Create(null, txInput, 421614));
71+
var txInput = new ThirdwebTransactionInput(421614) { To = Constants.ADDRESS_ZERO };
72+
var ex = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebTransaction.Create(null, txInput));
7673
Assert.Contains("Wallet must be provided", ex.Message);
7774
}
7875

@@ -81,8 +78,7 @@ public async Task Create_ThrowsOnChainIdZero()
8178
{
8279
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
8380
var wallet = await PrivateKeyWallet.Generate(client);
84-
var txInput = new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO };
85-
var ex = await Assert.ThrowsAsync<ArgumentException>(() => ThirdwebTransaction.Create(wallet, txInput, BigInteger.Zero));
81+
var ex = Assert.Throws<ArgumentException>(() => new ThirdwebTransactionInput(0) { To = Constants.ADDRESS_ZERO });
8682
Assert.Contains("Invalid Chain ID", ex.Message);
8783
}
8884

@@ -166,7 +162,7 @@ public async Task Sign_SmartWallet_SignsTransaction()
166162
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
167163
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
168164
var smartAccount = await SmartWallet.Create(personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
169-
var transaction = await ThirdwebTransaction.Create(smartAccount, new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO, }, 421614);
165+
var transaction = await ThirdwebTransaction.Create(smartAccount, new ThirdwebTransactionInput(421614) { To = Constants.ADDRESS_ZERO, });
170166
var signed = await ThirdwebTransaction.Sign(transaction);
171167
Assert.NotNull(signed);
172168
}
@@ -218,43 +214,43 @@ public async Task SetZkSyncOptions_DefaultsToZeroNull()
218214
Assert.Null(transaction.Input.ZkSync?.FactoryDeps);
219215
}
220216

221-
// [Fact(Timeout = 120000)]
222-
// public async Task Send_ZkSync_TransfersGaslessly()
223-
// {
224-
// var transaction = await CreateSampleTransaction();
225-
// _ = transaction.SetChainId(300);
226-
// _ = transaction.SetTo("0xbA226d47Cbb2731CBAA67C916c57d68484AA269F");
227-
// _ = transaction.SetValue(BigInteger.Zero);
228-
// _ = transaction.SetZkSyncOptions(
229-
// new ZkSyncOptions(
230-
// paymaster: "0xbA226d47Cbb2731CBAA67C916c57d68484AA269F",
231-
// paymasterInput: "0x8c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
232-
// gasPerPubdataByteLimit: 50000,
233-
// factoryDeps: new List<byte[]>()
234-
// )
235-
// );
236-
// var receipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
237-
// Assert.NotNull(receipt);
238-
// Assert.StartsWith("0x", receipt.TransactionHash);
239-
// }
217+
[Fact(Timeout = 120000)]
218+
public async Task Send_ZkSync_TransfersGaslessly()
219+
{
220+
var transaction = await this.CreateSampleTransaction();
221+
_ = transaction.SetChainId(300);
222+
_ = transaction.SetTo("0xbA226d47Cbb2731CBAA67C916c57d68484AA269F");
223+
_ = transaction.SetValue(BigInteger.Zero);
224+
_ = transaction.SetZkSyncOptions(
225+
new ZkSyncOptions(
226+
paymaster: "0xbA226d47Cbb2731CBAA67C916c57d68484AA269F",
227+
paymasterInput: "0x8c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000",
228+
gasPerPubdataByteLimit: 50000,
229+
factoryDeps: new List<byte[]>()
230+
)
231+
);
232+
var receipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
233+
Assert.NotNull(receipt);
234+
Assert.StartsWith("0x", receipt.TransactionHash);
235+
}
240236

241-
// [Fact(Timeout = 120000)]
242-
// public async Task Send_ZkSync_NoGasPerPubFactoryDepsTransfersGaslessly()
243-
// {
244-
// var transaction = await CreateSampleTransaction();
245-
// _ = transaction.SetChainId(300);
246-
// _ = transaction.SetTo("0xbA226d47Cbb2731CBAA67C916c57d68484AA269F");
247-
// _ = transaction.SetValue(BigInteger.Zero);
248-
// _ = transaction.SetZkSyncOptions(
249-
// new ZkSyncOptions(
250-
// paymaster: "0xbA226d47Cbb2731CBAA67C916c57d68484AA269F",
251-
// paymasterInput: "0x8c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"
252-
// )
253-
// );
254-
// var receipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
255-
// Assert.NotNull(receipt);
256-
// Assert.StartsWith("0x", receipt.TransactionHash);
257-
// }
237+
[Fact(Timeout = 120000)]
238+
public async Task Send_ZkSync_NoGasPerPubFactoryDepsTransfersGaslessly()
239+
{
240+
var transaction = await this.CreateSampleTransaction();
241+
_ = transaction.SetChainId(300);
242+
_ = transaction.SetTo("0xbA226d47Cbb2731CBAA67C916c57d68484AA269F");
243+
_ = transaction.SetValue(BigInteger.Zero);
244+
_ = transaction.SetZkSyncOptions(
245+
new ZkSyncOptions(
246+
paymaster: "0xbA226d47Cbb2731CBAA67C916c57d68484AA269F",
247+
paymasterInput: "0x8c5a344500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000"
248+
)
249+
);
250+
var receipt = await ThirdwebTransaction.SendAndWaitForTransactionReceipt(transaction);
251+
Assert.NotNull(receipt);
252+
Assert.StartsWith("0x", receipt.TransactionHash);
253+
}
258254

259255
[Fact(Timeout = 120000)]
260256
public async Task EstimateTotalCosts_CalculatesCostsCorrectly()
@@ -355,9 +351,7 @@ public async Task EstimateGasFees_ReturnsCorrectly()
355351
{
356352
var transaction = await ThirdwebTransaction.Create(
357353
await PrivateKeyWallet.Generate(ThirdwebClient.Create(secretKey: this.SecretKey)),
358-
new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO, },
359-
250 // fantom for 1559 non zero prio
360-
);
354+
new ThirdwebTransactionInput(250) { To = Constants.ADDRESS_ZERO, });
361355

362356
(var maxFee, var maxPrio) = await ThirdwebTransaction.EstimateGasFees(transaction);
363357

@@ -393,7 +387,7 @@ public async Task Simulate_ReturnsDataOrThrowsIntrinsic()
393387
var client = ThirdwebClient.Create(secretKey: this.SecretKey);
394388
var privateKeyAccount = await PrivateKeyWallet.Generate(client);
395389
var smartAccount = await SmartWallet.Create(personalWallet: privateKeyAccount, factoryAddress: "0xbf1C9aA4B1A085f7DA890a44E82B0A1289A40052", gasless: true, chainId: 421614);
396-
var transaction = await ThirdwebTransaction.Create(smartAccount, new ThirdwebTransactionInput() { To = Constants.ADDRESS_ZERO, Gas = new HexBigInteger(250000), }, 421614);
390+
var transaction = await ThirdwebTransaction.Create(smartAccount, new ThirdwebTransactionInput(421614) { To = Constants.ADDRESS_ZERO, Gas = new HexBigInteger(250000), });
397391

398392
try
399393
{

Thirdweb.Tests/Thirdweb.Transactions/Thirdweb.ZkSmartWallet.Tests.cs

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public async Task SendGaslessZkTx_Success()
7878
{
7979
var account = await this.GetSmartAccount();
8080
var hash = await account.SendTransaction(
81-
new ThirdwebTransactionInput()
81+
new ThirdwebTransactionInput(300)
8282
{
8383
From = await account.GetAddress(),
8484
To = await account.GetAddress(),
@@ -90,29 +90,29 @@ public async Task SendGaslessZkTx_Success()
9090
Assert.True(hash.Length == 66);
9191
}
9292

93-
// [Fact(Timeout = 120000)]
94-
// public async Task SendGaslessZkTx_ZkCandy_Success()
95-
// {
96-
// var account = await GetSmartAccount(zkChainId: 302);
97-
// var hash = await account.SendTransaction(
98-
// new ThirdwebTransactionInput()
99-
// {
100-
// From = await account.GetAddress(),
101-
// To = await account.GetAddress(),
102-
// Value = new Nethereum.Hex.HexTypes.HexBigInteger(0),
103-
// Data = "0x"
104-
// }
105-
// );
106-
// Assert.NotNull(hash);
107-
// Assert.True(hash.Length == 66);
108-
// }
93+
[Fact(Timeout = 120000)]
94+
public async Task SendGaslessZkTx_ZkCandy_Success()
95+
{
96+
var account = await this.GetSmartAccount(zkChainId: 302);
97+
var hash = await account.SendTransaction(
98+
new ThirdwebTransactionInput(302)
99+
{
100+
From = await account.GetAddress(),
101+
To = await account.GetAddress(),
102+
Value = new Nethereum.Hex.HexTypes.HexBigInteger(0),
103+
Data = "0x"
104+
}
105+
);
106+
Assert.NotNull(hash);
107+
Assert.True(hash.Length == 66);
108+
}
109109

110110
[Fact(Timeout = 120000)]
111111
public async Task SendGaslessZkTx_Abstract_Success()
112112
{
113113
var account = await this.GetSmartAccount(zkChainId: 11124);
114114
var hash = await account.SendTransaction(
115-
new ThirdwebTransactionInput()
115+
new ThirdwebTransactionInput(11124)
116116
{
117117
From = await account.GetAddress(),
118118
To = await account.GetAddress(),
@@ -123,4 +123,19 @@ public async Task SendGaslessZkTx_Abstract_Success()
123123
Assert.NotNull(hash);
124124
Assert.True(hash.Length == 66);
125125
}
126+
127+
[Fact(Timeout = 120000)]
128+
public async Task ZkSync_Switch()
129+
{
130+
var account = await this.GetSmartAccount(zkChainId: 300);
131+
_ = await account.SendTransaction(
132+
new ThirdwebTransactionInput(302)
133+
{
134+
From = await account.GetAddress(),
135+
To = await account.GetAddress(),
136+
Value = new Nethereum.Hex.HexTypes.HexBigInteger(0),
137+
Data = "0x"
138+
}
139+
);
140+
}
126141
}

0 commit comments

Comments
 (0)