@@ -20,14 +20,19 @@ import { EvmPlatform } from './platform.js';
20
20
import type { EvmChains } from './types.js' ;
21
21
import { _platform } from './types.js' ;
22
22
23
+ export type EvmSignerOptions = {
24
+ // Whether or not to log messages
25
+ debug ?: boolean ;
26
+ // Do not exceed this gas limit
27
+ maxGasLimit ?: bigint ;
28
+ // Partially override specific transaction request fields
29
+ overrides ?: Partial < TransactionRequest > ;
30
+ } ;
31
+
23
32
export async function getEvmSigner (
24
33
rpc : Provider ,
25
34
key : string | EthersSigner ,
26
- opts ?: {
27
- maxGasLimit ?: bigint ;
28
- chain ?: EvmChains ;
29
- debug ?: boolean ;
30
- } ,
35
+ opts ?: EvmSignerOptions & { chain ?: EvmChains } ,
31
36
) : Promise < Signer > {
32
37
const signer : EthersSigner =
33
38
typeof key === 'string' ? new Wallet ( key , rpc ) : key ;
@@ -75,7 +80,7 @@ export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
75
80
_chain : C ,
76
81
_address : string ,
77
82
_signer : EthersSigner ,
78
- readonly opts ?: { maxGasLimit ?: bigint ; debug ?: boolean } ,
83
+ readonly opts ?: EvmSignerOptions ,
79
84
) {
80
85
super ( _chain , _address , _signer ) ;
81
86
}
@@ -93,37 +98,37 @@ export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
93
98
94
99
const signed = [ ] ;
95
100
96
- // default gas limit
97
- const gasLimit = this . opts ?. maxGasLimit ?? 500_000n ;
98
-
101
+ // Default gas values
102
+ let gasLimit = 500_000n ;
99
103
let gasPrice = 100_000_000_000n ; // 100gwei
100
104
let maxFeePerGas = 1_500_000_000n ; // 1.5gwei
101
105
let maxPriorityFeePerGas = 100_000_000n ; // 0.1gwei
102
106
103
- // Celo does not support this call
104
- if ( chain !== 'Celo' ) {
105
- const feeData = await this . _signer . provider ! . getFeeData ( ) ;
106
- gasPrice = feeData . gasPrice ?? gasPrice ;
107
- maxFeePerGas = feeData . maxFeePerGas ?? maxFeePerGas ;
108
- maxPriorityFeePerGas =
109
- feeData . maxPriorityFeePerGas ?? maxPriorityFeePerGas ;
107
+ // If no overrides were passed, we can get better
108
+ // gas values from the provider
109
+ if ( this . opts ?. overrides === undefined ) {
110
+ // Celo does not support this call
111
+ if ( chain !== 'Celo' ) {
112
+ const feeData = await this . _signer . provider ! . getFeeData ( ) ;
113
+ gasPrice = feeData . gasPrice ?? gasPrice ;
114
+ maxFeePerGas = feeData . maxFeePerGas ?? maxFeePerGas ;
115
+ maxPriorityFeePerGas =
116
+ feeData . maxPriorityFeePerGas ?? maxPriorityFeePerGas ;
117
+ }
118
+ }
119
+
120
+ if ( this . opts ?. maxGasLimit ) {
121
+ // why doesnt math.min work for bigints?
122
+ gasLimit =
123
+ gasLimit > this . opts ?. maxGasLimit ? this . opts ?. maxGasLimit : gasLimit ;
110
124
}
111
125
112
126
// Oasis throws malformed errors unless we
113
127
// set it to use legacy transaction parameters
114
128
const gasOpts =
115
129
chain === 'Oasis'
116
- ? {
117
- gasLimit,
118
- gasPrice : gasPrice ,
119
- // Hardcode type
120
- type : 0 ,
121
- }
122
- : {
123
- maxFeePerGas,
124
- maxPriorityFeePerGas,
125
- gasLimit,
126
- } ;
130
+ ? { gasLimit, gasPrice, type : 0 } // Hardcoded to legacy transaction type
131
+ : { gasLimit, maxFeePerGas, maxPriorityFeePerGas } ;
127
132
128
133
for ( const txn of tx ) {
129
134
const { transaction, description } = txn ;
@@ -135,21 +140,10 @@ export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
135
140
...gasOpts ,
136
141
from : this . address ( ) ,
137
142
nonce : await this . _signer . getNonce ( ) ,
143
+ // Override any existing values with those passed in the constructor
144
+ ...this . opts ?. overrides ,
138
145
} ;
139
146
140
- // try {
141
- // const estimate = await this._signer.provider!.estimateGas(t);
142
- // t.gasLimit = estimate + estimate / 10n; // Add 10% buffer
143
- // if (this.opts?.maxGasLimit && t.gasLimit > this.opts?.maxGasLimit) {
144
- // throw new Error(
145
- // `Gas limit ${t.gasLimit} exceeds maxGasLimit ${this.opts?.maxGasLimit}`,
146
- // );
147
- // }
148
- // } catch (e) {
149
- // console.info('Failed to estimate gas for transaction: ', e);
150
- // console.info('Using gas limit: ', t.gasLimit);
151
- // }
152
-
153
147
signed . push ( await this . _signer . signTransaction ( t ) ) ;
154
148
}
155
149
return signed ;
0 commit comments