Skip to content

Commit 7503447

Browse files
committed
Broadcast from engine
1 parent 6a8bf32 commit 7503447

File tree

4 files changed

+80
-32
lines changed

4 files changed

+80
-32
lines changed

Thirdweb/Thirdweb.Transactions/ThirdwebTransaction.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public static async Task<BigInteger> EstimateGasPrice(ThirdwebTransaction transa
162162
var maxFee = fees["max_fee_per_gas"].ToObject<HexBigInteger>().Value;
163163
var maxPriorityFee = fees["max_priority_fee_per_gas"].ToObject<HexBigInteger>().Value;
164164
maxPriorityFee = maxPriorityFee == 0 ? maxFee : maxPriorityFee;
165-
return withBump ? (maxFee * 10 / 9, maxPriorityFee * 10 / 9) : (maxFee, maxPriorityFee);
165+
return withBump ? (maxFee * 10 / 5, maxPriorityFee * 10 / 5) : (maxFee, maxPriorityFee);
166166
}
167167

168168
var gasPrice = await EstimateGasPrice(transaction, withBump);
@@ -267,22 +267,7 @@ public static async Task<string> Send(ThirdwebTransaction transaction)
267267
string hash;
268268
if (IsZkSyncTransaction(transaction) && transaction.Input.ZkSync.HasValue && transaction.Input.ZkSync.Value.Paymaster != 0 && transaction.Input.ZkSync.Value.PaymasterInput != null)
269269
{
270-
var zkTx = new AccountAbstraction.ZkSyncAATransaction
271-
{
272-
TxType = 0x71,
273-
From = new HexBigInteger(transaction.Input.From).Value,
274-
To = new HexBigInteger(transaction.Input.To).Value,
275-
GasLimit = transaction.Input.Gas.Value,
276-
GasPerPubdataByteLimit = transaction.Input.ZkSync?.GasPerPubdataByteLimit ?? await GetGasPerPubData(transaction),
277-
MaxFeePerGas = transaction.Input.MaxFeePerGas?.Value ?? transaction.Input.GasPrice.Value,
278-
MaxPriorityFeePerGas = transaction.Input.MaxPriorityFeePerGas?.Value ?? transaction.Input.GasPrice.Value,
279-
Paymaster = transaction.Input.ZkSync.Value.Paymaster,
280-
Nonce = transaction.Input.Nonce ?? new HexBigInteger(await rpc.SendRequestAsync<string>("eth_getTransactionCount", transaction.Input.From, "latest")),
281-
Value = transaction.Input.Value.Value,
282-
Data = transaction.Input.Data.HexToByteArray(),
283-
FactoryDeps = transaction.Input.ZkSync.Value.FactoryDeps,
284-
PaymasterInput = transaction.Input.ZkSync.Value.PaymasterInput
285-
};
270+
var zkTx = await ConvertToZkSyncTransaction(transaction);
286271
var zkTxSigned = await EIP712.GenerateSignature_ZkSyncTransaction("zkSync", "2", transaction.Input.ChainId.Value, zkTx, transaction._wallet);
287272
hash = await rpc.SendRequestAsync<string>("eth_sendRawTransaction", zkTxSigned);
288273
}
@@ -355,6 +340,27 @@ public static async Task<TransactionReceipt> WaitForTransactionReceipt(ThirdwebC
355340
return receipt;
356341
}
357342

343+
public static async Task<AccountAbstraction.ZkSyncAATransaction> ConvertToZkSyncTransaction(ThirdwebTransaction transaction)
344+
{
345+
var rpc = ThirdwebRPC.GetRpcInstance(transaction._client, transaction.Input.ChainId.Value);
346+
return new AccountAbstraction.ZkSyncAATransaction
347+
{
348+
TxType = 0x71,
349+
From = new HexBigInteger(transaction.Input.From).Value,
350+
To = new HexBigInteger(transaction.Input.To).Value,
351+
GasLimit = transaction.Input.Gas.Value,
352+
GasPerPubdataByteLimit = transaction.Input.ZkSync?.GasPerPubdataByteLimit ?? await GetGasPerPubData(transaction),
353+
MaxFeePerGas = transaction.Input.MaxFeePerGas?.Value ?? transaction.Input.GasPrice.Value,
354+
MaxPriorityFeePerGas = transaction.Input.MaxPriorityFeePerGas?.Value ?? transaction.Input.GasPrice.Value,
355+
Paymaster = transaction.Input.ZkSync.Value.Paymaster,
356+
Nonce = transaction.Input.Nonce ?? new HexBigInteger(await rpc.SendRequestAsync<string>("eth_getTransactionCount", transaction.Input.From, "latest")),
357+
Value = transaction.Input.Value.Value,
358+
Data = transaction.Input.Data.HexToByteArray(),
359+
FactoryDeps = transaction.Input.ZkSync.Value.FactoryDeps,
360+
PaymasterInput = transaction.Input.ZkSync.Value.PaymasterInput
361+
};
362+
}
363+
358364
private static bool IsZkSyncTransaction(ThirdwebTransaction transaction)
359365
{
360366
return transaction.Input.ChainId.Value.Equals(324) || transaction.Input.ChainId.Value.Equals(300);

Thirdweb/Thirdweb.Wallets/SmartWallet/SmartWallet.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,37 @@ public async Task<string> SendTransaction(ThirdwebTransactionInput transactionIn
130130
(var maxFee, _) = await ThirdwebTransaction.EstimateGasFees(transaction);
131131
_ = transaction.SetMaxFeePerGas(maxFee);
132132
}
133+
133134
if (_gasless)
134135
{
135-
(var paymaster, var paymasterInput) = await GetPaymasterInput(transactionInput);
136+
(var paymaster, var paymasterInput) = await ZkPaymasterData(transactionInput);
136137
transaction = transaction.SetZkSyncOptions(new ZkSyncOptions(paymaster: paymaster, paymasterInput: paymasterInput));
138+
var zkTx = await ThirdwebTransaction.ConvertToZkSyncTransaction(transaction);
139+
var zkTxSigned = await EIP712.GenerateSignature_ZkSyncTransaction("zkSync", "2", transaction.Input.ChainId.Value, zkTx, this);
140+
// Match bundler ZkTransactionInput type without recreating
141+
var hash = await ZkBroadcastTransaction(
142+
new
143+
{
144+
nonce = zkTx.Nonce.ToString(),
145+
from = zkTx.From,
146+
to = zkTx.To,
147+
gas = zkTx.GasLimit.ToString(),
148+
gasPrice = string.Empty,
149+
value = zkTx.Value.ToString(),
150+
data = Utils.BytesToHex(zkTx.Data),
151+
maxFeePerGas = zkTx.MaxFeePerGas.ToString(),
152+
maxPriorityFeePerGas = zkTx.MaxPriorityFeePerGas.ToString(),
153+
chainId = _chainId.ToString(),
154+
signedTransaction = zkTxSigned,
155+
paymaster = paymaster
156+
}
157+
);
158+
return hash;
159+
}
160+
else
161+
{
162+
return await ThirdwebTransaction.Send(transaction);
137163
}
138-
return await ThirdwebTransaction.Send(transaction);
139164
}
140165
else
141166
{
@@ -242,19 +267,25 @@ private async Task<BigInteger> GetNonce()
242267
return await ThirdwebContract.Read<BigInteger>(_entryPointContract, "getNonce", await GetAddress(), randomInt192);
243268
}
244269

245-
private async Task<(string, string)> GetPaymasterInput(ThirdwebTransactionInput transactionInput)
270+
private async Task<(string, string)> ZkPaymasterData(ThirdwebTransactionInput transactionInput)
246271
{
247272
if (_gasless)
248273
{
249-
var paymasterInput = await BundlerClient.PMSponsorTransaction(_client, _paymasterUrl, 1, transactionInput);
250-
return (paymasterInput.paymaster, paymasterInput.paymasterInput);
274+
var result = await BundlerClient.ZkPaymasterData(_client, _paymasterUrl, 1, transactionInput);
275+
return (result.paymaster, result.paymasterInput);
251276
}
252277
else
253278
{
254279
return (null, null);
255280
}
256281
}
257282

283+
private async Task<string> ZkBroadcastTransaction(object transactionInput)
284+
{
285+
var result = await BundlerClient.ZkBroadcastTransaction(_client, _bundlerUrl, 1, transactionInput);
286+
return result.transactionHash;
287+
}
288+
258289
private async Task<byte[]> GetPaymasterAndData(object requestId, UserOperationHexified userOp)
259290
{
260291
if (_gasless)

Thirdweb/Thirdweb.Wallets/SmartWallet/Thirdweb.AccountAbstraction/AATypes.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ public class PMSponsorOperationResponse
9191
public string paymasterAndData { get; set; }
9292
}
9393

94-
public class PMSponsorTransactionResponse
95-
{
96-
public string paymaster { get; set; }
97-
public string paymasterInput { get; set; }
98-
}
99-
10094
public class ThirdwebGetUserOperationGasPriceResponse
10195
{
10296
public string maxFeePerGas { get; set; }
@@ -222,4 +216,15 @@ public class ZkSyncAATransaction
222216
[Parameter("bytes", "paymasterInput", 13)]
223217
public virtual byte[] PaymasterInput { get; set; }
224218
}
219+
220+
public class ZkPaymasterDataResponse
221+
{
222+
public string paymaster { get; set; }
223+
public string paymasterInput { get; set; }
224+
}
225+
226+
public class ZkBroadcastTransactionResponse
227+
{
228+
public string transactionHash { get; set; }
229+
}
225230
}

Thirdweb/Thirdweb.Wallets/SmartWallet/Thirdweb.AccountAbstraction/BundlerClient.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,25 @@ public static async Task<PMSponsorOperationResponse> PMSponsorUserOperation(Thir
5555
}
5656
}
5757

58-
public static async Task<PMSponsorTransactionResponse> PMSponsorTransaction(ThirdwebClient client, string paymasterUrl, object requestId, ThirdwebTransactionInput txInput)
58+
public static async Task<ZkPaymasterDataResponse> ZkPaymasterData(ThirdwebClient client, string paymasterUrl, object requestId, ThirdwebTransactionInput txInput)
5959
{
60-
var response = await BundlerRequest(client, paymasterUrl, requestId, "pm_sponsorTransaction", txInput);
60+
var response = await BundlerRequest(client, paymasterUrl, requestId, "zk_paymasterData", txInput);
6161
try
6262
{
63-
return JsonConvert.DeserializeObject<PMSponsorTransactionResponse>(response.Result.ToString());
63+
return JsonConvert.DeserializeObject<ZkPaymasterDataResponse>(response.Result.ToString());
6464
}
6565
catch
6666
{
67-
return new PMSponsorTransactionResponse() { paymaster = null, paymasterInput = null };
67+
return new ZkPaymasterDataResponse() { paymaster = null, paymasterInput = null };
6868
}
6969
}
7070

71+
public static async Task<ZkBroadcastTransactionResponse> ZkBroadcastTransaction(ThirdwebClient client, string paymasterUrl, object requestId, object txInput)
72+
{
73+
var response = await BundlerRequest(client, paymasterUrl, requestId, "zk_broadcastTransaction", txInput);
74+
return JsonConvert.DeserializeObject<ZkBroadcastTransactionResponse>(response.Result.ToString());
75+
}
76+
7177
// Request
7278

7379
private static async Task<RpcResponseMessage> BundlerRequest(ThirdwebClient client, string url, object requestId, string method, params object[] args)

0 commit comments

Comments
 (0)