Skip to content

Commit f62c6b0

Browse files
authored
Fetch Stack from Chains Database (#80)
Signed-off-by: Firekeeper <0xFirekeeper@gmail.com>
1 parent da9a3c6 commit f62c6b0

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

Thirdweb.Tests/Thirdweb.Utils/Thirdweb.Utils.Tests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,18 @@ public async Task FetchThirdwebChainDataAsync_ReturnsChainData_WhenResponseIsSuc
466466
Assert.True(timeAttempt1 > timeAttempt2);
467467
}
468468

469+
[Fact(Timeout = 120000)]
470+
public async Task FetchThirdwebChainDataAsync_ReturnsStack_WhenResponseIsSuccessful()
471+
{
472+
var chainId = new BigInteger(4654);
473+
474+
var chainData = await Utils.GetChainMetadata(this.Client, chainId);
475+
Assert.NotNull(chainData);
476+
_ = Assert.IsType<ThirdwebChainData>(chainData);
477+
Assert.NotNull(chainData.StackType);
478+
Assert.Contains("zksync", chainData.StackType);
479+
}
480+
469481
[Fact(Timeout = 120000)]
470482
public async Task FetchThirdwebChainDataAsync_ThrowsException_WhenResponseHasError()
471483
{

Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transa
231231
var rpc = ThirdwebRPC.GetRpcInstance(transaction._wallet.Client, transaction.Input.ChainId.Value);
232232
var chainId = transaction.Input.ChainId.Value;
233233

234-
if (Utils.IsZkSync(transaction.Input.ChainId.Value))
234+
if (await Utils.IsZkSync(transaction._wallet.Client, transaction.Input.ChainId.Value).ConfigureAwait(false))
235235
{
236236
var fees = await rpc.SendRequestAsync<JToken>("zks_estimateFee", transaction.Input).ConfigureAwait(false);
237237
var maxFee = fees["max_fee_per_gas"].ToObject<HexBigInteger>().Value;
@@ -293,7 +293,7 @@ public static async Task<BigInteger> EstimateGasLimit(ThirdwebTransaction transa
293293
{
294294
var rpc = ThirdwebRPC.GetRpcInstance(transaction._wallet.Client, transaction.Input.ChainId.Value);
295295

296-
if (Utils.IsZkSync(transaction.Input.ChainId.Value))
296+
if (await Utils.IsZkSync(transaction._wallet.Client, transaction.Input.ChainId.Value).ConfigureAwait(false))
297297
{
298298
var hex = (await rpc.SendRequestAsync<JToken>("zks_estimateFee", transaction.Input).ConfigureAwait(false))["gas_limit"].ToString();
299299
return new HexBigInteger(hex).Value * 10 / 5;
@@ -388,7 +388,7 @@ public static async Task<string> Send(ThirdwebTransaction transaction)
388388

389389
var rpc = ThirdwebRPC.GetRpcInstance(transaction._wallet.Client, transaction.Input.ChainId.Value);
390390
string hash;
391-
if (Utils.IsZkSync(transaction.Input.ChainId.Value) && transaction.Input.ZkSync.HasValue)
391+
if (await Utils.IsZkSync(transaction._wallet.Client, transaction.Input.ChainId.Value).ConfigureAwait(false) && transaction.Input.ZkSync.HasValue)
392392
{
393393
var zkTx = await ConvertToZkSyncTransaction(transaction).ConfigureAwait(false);
394394
var zkTxSigned = await EIP712.GenerateSignature_ZkSyncTransaction("zkSync", "2", transaction.Input.ChainId.Value, zkTx, transaction._wallet).ConfigureAwait(false);

Thirdweb/Thirdweb.Utils/Utils.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,20 @@ public static string GenerateSIWE(LoginPayloadData loginPayloadData)
289289
/// <summary>
290290
/// Checks if the chain ID corresponds to zkSync.
291291
/// </summary>
292+
/// <param name="client">The Thirdweb client.</param>
292293
/// <param name="chainId">The chain ID.</param>
293294
/// <returns>True if it is a zkSync chain ID, otherwise false.</returns>
294-
public static bool IsZkSync(BigInteger chainId)
295+
public static async Task<bool> IsZkSync(ThirdwebClient client, BigInteger chainId)
295296
{
296-
return chainId.Equals(324) || chainId.Equals(300) || chainId.Equals(302) || chainId.Equals(11124) || chainId.Equals(4654);
297+
if (chainId.Equals(324) || chainId.Equals(300) || chainId.Equals(302) || chainId.Equals(11124) || chainId.Equals(4654) || chainId.Equals(333271))
298+
{
299+
return true;
300+
}
301+
else
302+
{
303+
var chainData = await GetChainMetadata(client, chainId).ConfigureAwait(false);
304+
return !string.IsNullOrEmpty(chainData.StackType) && chainData.StackType.Contains("zksync", StringComparison.OrdinalIgnoreCase);
305+
}
297306
}
298307

299308
/// <summary>
@@ -371,6 +380,7 @@ public static async Task<ThirdwebChainData> GetChainMetadata(ThirdwebClient clie
371380
}
372381
else
373382
{
383+
deserializedResponse.Data.Explorers = deserializedResponse.Data.Explorers == null || deserializedResponse.Data.Explorers.Count == 0 ? null : deserializedResponse.Data.Explorers;
374384
_chainDataCache[chainId] = deserializedResponse.Data;
375385
return deserializedResponse.Data;
376386
}

Thirdweb/Thirdweb.Wallets/SmartWallet/SmartWallet.cs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static async Task<SmartWallet> Create(
133133
ThirdwebContract factoryContract = null;
134134
ThirdwebContract accountContract = null;
135135

136-
if (!Utils.IsZkSync(chainId))
136+
if (!await Utils.IsZkSync(personalWallet.Client, chainId).ConfigureAwait(false))
137137
{
138138
var entryPointAbi = entryPointVersion == 6 ? Constants.ENTRYPOINT_V06_ABI : Constants.ENTRYPOINT_V07_ABI;
139139
var factoryAbi = entryPointVersion == 6 ? Constants.FACTORY_V06_ABI : Constants.FACTORY_V07_ABI;
@@ -195,10 +195,10 @@ public async Task SwitchNetwork(BigInteger chainId)
195195
}
196196

197197
this._chainId = chainId;
198-
199198
this._bundlerUrl = this._bundlerUrl.Contains(".thirdweb.com") ? $"https://{chainId}.bundler.thirdweb.com/v2" : this._bundlerUrl;
200199
this._paymasterUrl = this._paymasterUrl.Contains(".thirdweb.com") ? $"https://{chainId}.bundler.thirdweb.com/v2" : this._paymasterUrl;
201-
if (!Utils.IsZkSync(chainId))
200+
201+
if (!await Utils.IsZkSync(this.Client, chainId).ConfigureAwait(false))
202202
{
203203
this._entryPointContract = await ThirdwebContract.Create(this.Client, this._entryPointContract.Address, chainId, this._entryPointContract.Abi).ConfigureAwait(false);
204204
this._factoryContract = await ThirdwebContract.Create(this.Client, this._factoryContract.Address, chainId, this._factoryContract.Abi).ConfigureAwait(false);
@@ -208,7 +208,7 @@ public async Task SwitchNetwork(BigInteger chainId)
208208

209209
public async Task<bool> IsDeployed()
210210
{
211-
if (Utils.IsZkSync(this._chainId))
211+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
212212
{
213213
return true;
214214
}
@@ -226,11 +226,13 @@ public async Task<string> SendTransaction(ThirdwebTransactionInput transactionIn
226226

227227
await this.SwitchNetwork(transactionInput.ChainId.Value).ConfigureAwait(false);
228228

229-
var transaction = await ThirdwebTransaction.Create(Utils.IsZkSync(this._chainId) ? this._personalAccount : this, transactionInput).ConfigureAwait(false);
229+
var transaction = await ThirdwebTransaction
230+
.Create(await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false) ? this._personalAccount : this, transactionInput)
231+
.ConfigureAwait(false);
230232
transaction = await ThirdwebTransaction.Prepare(transaction).ConfigureAwait(false);
231233
transactionInput = transaction.Input;
232234

233-
if (Utils.IsZkSync(this._chainId))
235+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
234236
{
235237
if (this._gasless)
236238
{
@@ -668,7 +670,7 @@ private static UserOperationHexifiedV7 EncodeUserOperation(UserOperationV7 userO
668670

669671
public async Task ForceDeploy()
670672
{
671-
if (Utils.IsZkSync(this._chainId))
673+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
672674
{
673675
return;
674676
}
@@ -700,7 +702,9 @@ public Task<IThirdwebWallet> GetPersonalWallet()
700702

701703
public async Task<string> GetAddress()
702704
{
703-
return Utils.IsZkSync(this._chainId) ? await this._personalAccount.GetAddress().ConfigureAwait(false) : this._accountContract.Address.ToChecksumAddress();
705+
return await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false)
706+
? await this._personalAccount.GetAddress().ConfigureAwait(false)
707+
: this._accountContract.Address.ToChecksumAddress();
704708
}
705709

706710
public Task<string> EthSign(byte[] rawMessage)
@@ -725,7 +729,7 @@ public Task<string> PersonalSign(byte[] rawMessage)
725729

726730
public async Task<string> PersonalSign(string message)
727731
{
728-
if (Utils.IsZkSync(this._chainId))
732+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
729733
{
730734
return await this._personalAccount.PersonalSign(message).ConfigureAwait(false);
731735
}
@@ -800,7 +804,7 @@ public async Task<bool> IsValidSignature(string message, string signature)
800804

801805
public async Task<List<string>> GetAllAdmins()
802806
{
803-
if (Utils.IsZkSync(this._chainId))
807+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
804808
{
805809
throw new InvalidOperationException("Account Permissions are not supported in ZkSync");
806810
}
@@ -811,7 +815,7 @@ public async Task<List<string>> GetAllAdmins()
811815

812816
public async Task<List<SignerPermissions>> GetAllActiveSigners()
813817
{
814-
if (Utils.IsZkSync(this._chainId))
818+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
815819
{
816820
throw new InvalidOperationException("Account Permissions are not supported in ZkSync");
817821
}
@@ -830,7 +834,7 @@ public async Task<ThirdwebTransactionReceipt> CreateSessionKey(
830834
string reqValidityEndTimestamp
831835
)
832836
{
833-
if (Utils.IsZkSync(this._chainId))
837+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
834838
{
835839
throw new InvalidOperationException("Account Permissions are not supported in ZkSync");
836840
}
@@ -863,14 +867,14 @@ string reqValidityEndTimestamp
863867

864868
public async Task<ThirdwebTransactionReceipt> RevokeSessionKey(string signerAddress)
865869
{
866-
return Utils.IsZkSync(this._chainId)
870+
return await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false)
867871
? throw new InvalidOperationException("Account Permissions are not supported in ZkSync")
868872
: await this.CreateSessionKey(signerAddress, new List<string>(), "0", "0", "0", "0", Utils.GetUnixTimeStampIn10Years().ToString()).ConfigureAwait(false);
869873
}
870874

871875
public async Task<ThirdwebTransactionReceipt> AddAdmin(string admin)
872876
{
873-
if (Utils.IsZkSync(this._chainId))
877+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
874878
{
875879
throw new InvalidOperationException("Account Permissions are not supported in ZkSync");
876880
}
@@ -902,7 +906,7 @@ public async Task<ThirdwebTransactionReceipt> AddAdmin(string admin)
902906

903907
public async Task<ThirdwebTransactionReceipt> RemoveAdmin(string admin)
904908
{
905-
if (Utils.IsZkSync(this._chainId))
909+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
906910
{
907911
throw new InvalidOperationException("Account Permissions are not supported in ZkSync");
908912
}
@@ -953,7 +957,7 @@ public async Task<BigInteger> EstimateUserOperationGas(ThirdwebTransactionInput
953957
{
954958
await this.SwitchNetwork(transaction.ChainId.Value).ConfigureAwait(false);
955959

956-
if (Utils.IsZkSync(this._chainId))
960+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
957961
{
958962
throw new Exception("User Operations are not supported in ZkSync");
959963
}
@@ -982,7 +986,7 @@ public async Task<string> SignTransaction(ThirdwebTransactionInput transaction)
982986
{
983987
await this.SwitchNetwork(transaction.ChainId.Value).ConfigureAwait(false);
984988

985-
if (Utils.IsZkSync(this._chainId))
989+
if (await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false))
986990
{
987991
throw new Exception("Offline Signing is not supported in ZkSync");
988992
}
@@ -1006,7 +1010,7 @@ public async Task<string> SignTransaction(ThirdwebTransactionInput transaction)
10061010

10071011
public async Task<bool> IsConnected()
10081012
{
1009-
return Utils.IsZkSync(this._chainId) ? await this._personalAccount.IsConnected().ConfigureAwait(false) : this._accountContract != null;
1013+
return await Utils.IsZkSync(this.Client, this._chainId).ConfigureAwait(false) ? await this._personalAccount.IsConnected().ConfigureAwait(false) : this._accountContract != null;
10101014
}
10111015

10121016
public Task Disconnect()

0 commit comments

Comments
 (0)