@@ -38,6 +38,8 @@ public struct GasCosts
38
38
public class Transaction
39
39
{
40
40
private readonly Contract contract ;
41
+ private readonly string fnName ;
42
+ private object [ ] fnArgs ;
41
43
42
44
/// <summary>
43
45
/// Gets the transaction input.
@@ -49,10 +51,12 @@ public class Transaction
49
51
/// </summary>
50
52
/// <param name="contract">The contract associated with the transaction.</param>
51
53
/// <param name="txInput">The transaction input.</param>
52
- public Transaction ( Contract contract , TransactionInput txInput )
54
+ public Transaction ( Contract contract , TransactionInput txInput , string fnName , object [ ] fnArgs )
53
55
{
54
56
this . contract = contract ;
55
57
this . Input = txInput ;
58
+ this . fnName = fnName ;
59
+ this . fnArgs = fnArgs ;
56
60
}
57
61
58
62
/// <summary>
@@ -192,9 +196,16 @@ public Transaction SetNonce(string nonce)
192
196
/// <returns>The modified <see cref="Transaction"/> object.</returns>
193
197
public Transaction SetArgs ( params object [ ] args )
194
198
{
195
- var web3 = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
196
- var function = web3 . Eth . GetContract ( contract . abi , contract . address ) . GetFunction ( Input . To ) ;
197
- Input . Data = function . GetData ( args ) ;
199
+ if ( Utils . IsWebGLBuild ( ) )
200
+ {
201
+ this . fnArgs = args ;
202
+ }
203
+ else
204
+ {
205
+ var web3 = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
206
+ var function = web3 . Eth . GetContract ( contract . abi , contract . address ) . GetFunction ( Input . To ) ;
207
+ Input . Data = function . GetData ( args ) ;
208
+ }
198
209
return this ;
199
210
}
200
211
@@ -204,12 +215,19 @@ public Transaction SetArgs(params object[] args)
204
215
/// <returns>The gas price for the transaction as a <see cref="BigInteger"/>.</returns>
205
216
public async Task < BigInteger > GetGasPrice ( )
206
217
{
207
- var web3 = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
208
- var gasPrice = await web3 . Eth . GasPrice . SendRequestAsync ( ) ;
209
- var maxGasPrice = BigInteger . Parse ( "300000000000" ) ; // 300 Gwei in Wei
210
- var extraTip = gasPrice . Value / 10 ; // +10%
211
- var txGasPrice = gasPrice . Value + extraTip ;
212
- return txGasPrice > maxGasPrice ? maxGasPrice : txGasPrice ;
218
+ if ( Utils . IsWebGLBuild ( ) )
219
+ {
220
+ return await Bridge . InvokeRoute < BigInteger > ( GetTxBuilderRoute ( "getGasPrice" ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ;
221
+ }
222
+ else
223
+ {
224
+ var web3 = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
225
+ var gasPrice = await web3 . Eth . GasPrice . SendRequestAsync ( ) ;
226
+ var maxGasPrice = BigInteger . Parse ( "300000000000" ) ; // 300 Gwei in Wei
227
+ var extraTip = gasPrice . Value / 10 ; // +10%
228
+ var txGasPrice = gasPrice . Value + extraTip ;
229
+ return txGasPrice > maxGasPrice ? maxGasPrice : txGasPrice ;
230
+ }
213
231
}
214
232
215
233
/// <summary>
@@ -218,9 +236,16 @@ public async Task<BigInteger> GetGasPrice()
218
236
/// <returns>The estimated gas limit for the transaction as a <see cref="BigInteger"/>.</returns>
219
237
public async Task < BigInteger > EstimateGasLimit ( )
220
238
{
221
- var gasEstimator = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
222
- var gas = await gasEstimator . Eth . Transactions . EstimateGas . SendRequestAsync ( Input ) ;
223
- return gas . Value ;
239
+ if ( Utils . IsWebGLBuild ( ) )
240
+ {
241
+ return await Bridge . InvokeRoute < BigInteger > ( GetTxBuilderRoute ( "estimateGasLimit" ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ;
242
+ }
243
+ else
244
+ {
245
+ var gasEstimator = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
246
+ var gas = await gasEstimator . Eth . Transactions . EstimateGas . SendRequestAsync ( Input ) ;
247
+ return gas . Value ;
248
+ }
224
249
}
225
250
226
251
/// <summary>
@@ -229,11 +254,17 @@ public async Task<BigInteger> EstimateGasLimit()
229
254
/// <returns>The estimated gas costs for the transaction as a <see cref="GasCosts"/> struct.</returns>
230
255
public async Task < GasCosts > EstimateGasCosts ( )
231
256
{
232
- var gasLimit = await EstimateGasLimit ( ) ;
233
- var gasPrice = await GetGasPrice ( ) ;
234
- var gasCost = gasLimit * gasPrice ;
235
-
236
- return new GasCosts { ether = gasPrice . ToString ( ) . ToEth ( 18 , false ) , wei = gasCost } ;
257
+ if ( Utils . IsWebGLBuild ( ) )
258
+ {
259
+ return await Bridge . InvokeRoute < GasCosts > ( GetTxBuilderRoute ( "estimateGasCosts" ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ;
260
+ }
261
+ else
262
+ {
263
+ var gasLimit = await EstimateGasLimit ( ) ;
264
+ var gasPrice = await GetGasPrice ( ) ;
265
+ var gasCost = gasLimit * gasPrice ;
266
+ return new GasCosts { ether = gasCost . ToString ( ) . ToEth ( 18 , false ) , wei = gasCost } ;
267
+ }
237
268
}
238
269
239
270
/// <summary>
@@ -255,8 +286,15 @@ public async Task<Transaction> EstimateAndSetGasLimitAsync(string minimumGas = "
255
286
/// <returns>The result of the transaction simulation as a string.</returns>
256
287
public async Task < string > Simulate ( )
257
288
{
258
- var web3 = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
259
- return await web3 . Eth . Transactions . Call . SendRequestAsync ( Input ) ;
289
+ if ( Utils . IsWebGLBuild ( ) )
290
+ {
291
+ return JsonConvert . SerializeObject ( await Bridge . InvokeRoute < object > ( GetTxBuilderRoute ( "simulate" ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ) ;
292
+ }
293
+ else
294
+ {
295
+ var web3 = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
296
+ return await web3 . Eth . Transactions . Call . SendRequestAsync ( Input ) ;
297
+ }
260
298
}
261
299
262
300
/// <summary>
@@ -266,21 +304,28 @@ public async Task<string> Simulate()
266
304
/// <returns>The transaction hash as a string.</returns>
267
305
public async Task < string > Send ( bool ? gasless = null )
268
306
{
269
- if ( Input . Gas == null )
270
- await EstimateAndSetGasLimitAsync ( ) ;
271
-
272
- if ( Input . Value == null )
273
- Input . Value = new HexBigInteger ( 0 ) ;
274
-
275
- bool isGaslessSetup = ThirdwebManager . Instance . SDK . session . Options . gasless . HasValue && ThirdwebManager . Instance . SDK . session . Options . gasless . Value . openzeppelin . HasValue ;
276
- if ( gasless != null && gasless . Value && ! isGaslessSetup )
277
- throw new UnityException ( "Gasless transactions are not enabled. Please enable them in the SDK options." ) ;
278
-
279
- bool sendGaslessly = gasless == null ? isGaslessSetup : gasless . Value ;
280
- if ( sendGaslessly )
281
- return await SendGasless ( ) ;
307
+ if ( Utils . IsWebGLBuild ( ) )
308
+ {
309
+ if ( gasless == null || gasless == false )
310
+ return await Send ( ) ;
311
+ else
312
+ return await SendGasless ( ) ;
313
+ }
282
314
else
283
- return await Send ( ) ;
315
+ {
316
+ if ( Input . Gas == null )
317
+ await EstimateAndSetGasLimitAsync ( ) ;
318
+ if ( Input . Value == null )
319
+ Input . Value = new HexBigInteger ( 0 ) ;
320
+ bool isGaslessSetup = ThirdwebManager . Instance . SDK . session . Options . gasless . HasValue && ThirdwebManager . Instance . SDK . session . Options . gasless . Value . openzeppelin . HasValue ;
321
+ if ( gasless != null && gasless . Value && ! isGaslessSetup )
322
+ throw new UnityException ( "Gasless transactions are not enabled. Please enable them in the SDK options." ) ;
323
+ bool sendGaslessly = gasless == null ? isGaslessSetup : gasless . Value ;
324
+ if ( sendGaslessly )
325
+ return await SendGasless ( ) ;
326
+ else
327
+ return await Send ( ) ;
328
+ }
284
329
}
285
330
286
331
/// <summary>
@@ -290,8 +335,21 @@ public async Task<string> Send(bool? gasless = null)
290
335
/// <returns>The transaction result as a <see cref="TransactionResult"/> object.</returns>
291
336
public async Task < TransactionResult > SendAndWaitForTransactionResult ( bool ? gasless = null )
292
337
{
293
- var txHash = await Send ( gasless ) ;
294
- return await WaitForTransactionResult ( txHash ) ;
338
+ if ( Utils . IsWebGLBuild ( ) )
339
+ {
340
+ string action ;
341
+ if ( gasless == null || gasless == false )
342
+ action = "execute" ;
343
+ else
344
+ action = "executeGasless" ;
345
+
346
+ return await Bridge . InvokeRoute < TransactionResult > ( GetTxBuilderRoute ( action ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ;
347
+ }
348
+ else
349
+ {
350
+ var txHash = await Send ( gasless ) ;
351
+ return await WaitForTransactionResult ( txHash ) ;
352
+ }
295
353
}
296
354
297
355
/// <summary>
@@ -301,6 +359,9 @@ public async Task<TransactionResult> SendAndWaitForTransactionResult(bool? gasle
301
359
/// <returns>The transaction result as a <see cref="TransactionResult"/> object.</returns>
302
360
public static async Task < TransactionResult > WaitForTransactionResult ( string txHash )
303
361
{
362
+ if ( Utils . IsWebGLBuild ( ) )
363
+ throw new UnityException ( "WaitForTransactionResult is not supported in WebGL builds." ) ;
364
+
304
365
var receiptPoller = new Web3 ( ThirdwebManager . Instance . SDK . session . RPC ) ;
305
366
var receipt = await receiptPoller . TransactionReceiptPolling . PollForReceiptAsync ( txHash ) ;
306
367
return receipt . ToTransactionResult ( ) ;
@@ -310,10 +371,7 @@ private async Task<string> Send()
310
371
{
311
372
if ( Utils . IsWebGLBuild ( ) )
312
373
{
313
- throw new UnityException ( "This functionality is not yet available on your current platform." ) ;
314
- // string route = contract.abi != null ? $"{contract.address}{Routable.subSeparator}{contract.abi}" : contract.address;
315
- // string sendRoute = $"{route}{Routable.separator}send";
316
- // return await Bridge.InvokeRoute<string>(sendRoute, new string[] { JsonConvert.SerializeObject(Input) });
374
+ return await Bridge . InvokeRoute < string > ( GetTxBuilderRoute ( "send" ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ;
317
375
}
318
376
else
319
377
{
@@ -336,7 +394,7 @@ private async Task<string> SendGasless()
336
394
{
337
395
if ( Utils . IsWebGLBuild ( ) )
338
396
{
339
- throw new UnityException ( "This functionality is not yet available on your current platform." ) ;
397
+ return await Bridge . InvokeRoute < string > ( GetTxBuilderRoute ( "sendGasless" ) , Utils . ToJsonStringArray ( Input , fnName , fnArgs ) ) ;
340
398
}
341
399
else
342
400
{
@@ -398,5 +456,11 @@ private async Task<string> SendGasless()
398
456
}
399
457
}
400
458
}
459
+
460
+ private string GetTxBuilderRoute ( string action )
461
+ {
462
+ string route = contract . abi != null ? $ "{ contract . address } { Routable . subSeparator } { contract . abi } " : contract . address ;
463
+ return $ "{ route } { Routable . separator } tx{ Routable . separator } { action } ";
464
+ }
401
465
}
402
466
}
0 commit comments