1
1
using System . Numerics ;
2
2
using Nethereum . Hex . HexTypes ;
3
3
using Nethereum . RPC . Eth . DTOs ;
4
- using Nethereum . RPC . Eth . Transactions ;
5
4
using Newtonsoft . Json ;
6
5
using Nethereum . Contracts ;
7
6
using Nethereum . ABI . FunctionEncoding ;
@@ -34,6 +33,7 @@ public static async Task<ThirdwebTransaction> Create(ThirdwebClient client, IThi
34
33
{
35
34
var address = await wallet . GetAddress ( ) ;
36
35
txInput . From ??= address ;
36
+ txInput . Data ??= "0x" ;
37
37
return address != txInput . From
38
38
? throw new ArgumentException ( "Transaction sender (from) must match wallet address" )
39
39
: client == null
@@ -89,54 +89,50 @@ public ThirdwebTransaction SetNonce(BigInteger nonce)
89
89
public static async Task < TotalCosts > EstimateGasCosts ( ThirdwebTransaction transaction )
90
90
{
91
91
var gasPrice = transaction . Input . GasPrice ? . Value ?? await EstimateGasPrice ( transaction ) ;
92
- var gasLimit = transaction . Input . Gas ? . Value ?? await EstimateGasLimit ( transaction , true ) ;
92
+ var gasLimit = transaction . Input . Gas ? . Value ?? await EstimateGasLimit ( transaction ) ;
93
93
var gasCost = BigInteger . Multiply ( gasLimit , gasPrice ) ;
94
94
return new TotalCosts { ether = gasCost . ToString ( ) . ToEth ( 18 , false ) , wei = gasCost } ;
95
95
}
96
96
97
97
public static async Task < TotalCosts > EstimateTotalCosts ( ThirdwebTransaction transaction )
98
98
{
99
- var gasPrice = transaction . Input . GasPrice ? . Value ?? await EstimateGasPrice ( transaction ) ;
100
- var gasLimit = transaction . Input . Gas ? . Value ?? await EstimateGasLimit ( transaction , true ) ;
101
- var gasCost = BigInteger . Multiply ( gasLimit , gasPrice ) ;
102
- var gasCostWithValue = BigInteger . Add ( gasCost , transaction . Input . Value ? . Value ?? 0 ) ;
103
- return new TotalCosts { ether = gasCostWithValue . ToString ( ) . ToEth ( 18 , false ) , wei = gasCostWithValue } ;
99
+ var gasCosts = await EstimateGasCosts ( transaction ) ;
100
+ var value = transaction . Input . Value ? . Value ?? 0 ;
101
+ return new TotalCosts { ether = ( value + gasCosts . wei ) . ToString ( ) . ToEth ( 18 , false ) , wei = value + gasCosts . wei } ;
104
102
}
105
103
106
104
public static async Task < BigInteger > EstimateGasPrice ( ThirdwebTransaction transaction , bool withBump = true )
107
105
{
108
- {
109
- var rpc = ThirdwebRPC . GetRpcInstance ( transaction . _client , transaction . Input . ChainId . Value ) ;
110
- var hex = new HexBigInteger ( await rpc . SendRequestAsync < string > ( "eth_gasPrice" ) ) ;
111
- return withBump ? hex . Value * 10 / 9 : hex . Value ;
112
- }
106
+ var rpc = ThirdwebRPC . GetRpcInstance ( transaction . _client , transaction . Input . ChainId . Value ) ;
107
+ var hex = new HexBigInteger ( await rpc . SendRequestAsync < string > ( "eth_gasPrice" ) ) ;
108
+ return withBump ? hex . Value * 10 / 9 : hex . Value ;
113
109
}
114
110
115
- public static async Task < BigInteger > Simulate ( ThirdwebTransaction transaction )
111
+ public static async Task < string > Simulate ( ThirdwebTransaction transaction )
116
112
{
117
- return await EstimateGasLimit ( transaction , false ) ;
113
+ var rpc = ThirdwebRPC . GetRpcInstance ( transaction . _client , transaction . Input . ChainId . Value ) ;
114
+ var data = await rpc . SendRequestAsync < string > ( "eth_call" , transaction . Input , "latest" ) ;
115
+ return data ;
118
116
}
119
117
120
- public static async Task < BigInteger > EstimateGasLimit ( ThirdwebTransaction transaction , bool overrideBalance = true )
118
+ public static async Task < BigInteger > EstimateGasLimit ( ThirdwebTransaction transaction )
121
119
{
122
- var rpc = ThirdwebRPC . GetRpcInstance ( transaction . _client , transaction . Input . ChainId . Value ) ;
123
- var from = transaction . Input . From ;
124
- var hex = overrideBalance
125
- ? await rpc . SendRequestAsync < string > (
126
- "eth_estimateGas" ,
127
- transaction . Input ,
128
- "latest" ,
129
- new Dictionary < string , Dictionary < string , string > > ( )
130
- {
131
- {
132
- from ,
133
- new ( ) { { "balance" , "0xFFFFFFFFFFFFFFFFFFFF" } }
134
- }
135
- }
136
- )
137
- : await rpc . SendRequestAsync < string > ( "eth_estimateGas" , transaction . Input , "latest" ) ;
138
-
139
- return new HexBigInteger ( hex ) . Value ;
120
+ if ( transaction . _wallet . AccountType == ThirdwebAccountType . SmartAccount )
121
+ {
122
+ var smartAccount = transaction . _wallet as SmartWallet ;
123
+ return await smartAccount . EstimateUserOperationGas ( transaction . Input , transaction . Input . ChainId . Value ) ;
124
+ }
125
+ else
126
+ {
127
+ var rpc = ThirdwebRPC . GetRpcInstance ( transaction . _client , transaction . Input . ChainId . Value ) ;
128
+ var hex = await rpc . SendRequestAsync < string > ( "eth_estimateGas" , transaction . Input , "latest" ) ;
129
+ return new HexBigInteger ( hex ) . Value ;
130
+ }
131
+ }
132
+
133
+ public static async Task < string > Sign ( ThirdwebTransaction transaction )
134
+ {
135
+ return await transaction . _wallet . SignTransaction ( transaction . Input , transaction . Input . ChainId . Value ) ;
140
136
}
141
137
142
138
public static async Task < string > Send ( ThirdwebTransaction transaction )
@@ -149,18 +145,18 @@ public static async Task<string> Send(ThirdwebTransaction transaction)
149
145
transaction . Input . From ??= await transaction . _wallet . GetAddress ( ) ;
150
146
transaction . Input . Value ??= new HexBigInteger ( 0 ) ;
151
147
transaction . Input . Data ??= "0x" ;
152
- transaction . Input . GasPrice ??= new HexBigInteger ( await EstimateGasPrice ( transaction ) ) ;
153
148
transaction . Input . MaxFeePerGas = null ;
154
149
transaction . Input . MaxPriorityFeePerGas = null ;
155
150
transaction . Input . Gas ??= new HexBigInteger ( await EstimateGasLimit ( transaction ) ) ;
151
+ transaction . Input . GasPrice ??= new HexBigInteger ( await EstimateGasPrice ( transaction ) ) ;
156
152
157
153
var rpc = ThirdwebRPC . GetRpcInstance ( transaction . _client , transaction . Input . ChainId . Value ) ;
158
154
string hash ;
159
155
switch ( transaction . _wallet . AccountType )
160
156
{
161
157
case ThirdwebAccountType . PrivateKeyAccount :
162
158
transaction . Input . Nonce ??= new HexBigInteger ( await rpc . SendRequestAsync < string > ( "eth_getTransactionCount" , await transaction . _wallet . GetAddress ( ) , "latest" ) ) ;
163
- var signedTx = await transaction . _wallet . SignTransaction ( transaction . Input , transaction . Input . ChainId . Value ) ;
159
+ var signedTx = await Sign ( transaction ) ;
164
160
hash = await rpc . SendRequestAsync < string > ( "eth_sendRawTransaction" , signedTx ) ;
165
161
break ;
166
162
case ThirdwebAccountType . SmartAccount :
0 commit comments