Skip to content

Commit 3c7c756

Browse files
authored
Evm: add additional overrides to evm signer for better gas settings (#583)
1 parent ad65589 commit 3c7c756

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

examples/src/helpers/helpers.ts

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
TransferState,
1010
TxHash,
1111
Wormhole,
12+
amount,
1213
api,
1314
tasks,
1415
} from "@wormhole-foundation/sdk";
@@ -62,13 +63,20 @@ export async function getSigner<N extends Network, C extends Chain>(
6263
max: 1000,
6364
},
6465
});
66+
6567
break;
6668
case "Cosmwasm":
6769
signer = await cosmwasm.getSigner(await chain.getRpc(), getEnv("COSMOS_MNEMONIC"));
6870
break;
6971
case "Evm":
7072
signer = await evm.getSigner(await chain.getRpc(), getEnv("ETH_PRIVATE_KEY"), {
7173
debug: true,
74+
maxGasLimit: amount.units(amount.parse("0.01", 18)),
75+
// overrides is a Partial<TransactionRequest>, so any fields can be overriden
76+
//overrides: {
77+
// maxFeePerGas: amount.units(amount.parse("1.5", 9)),
78+
// maxPriorityFeePerGas: amount.units(amount.parse("0.1", 9)),
79+
//},
7280
});
7381
break;
7482
case "Algorand":

platforms/evm/src/signer.ts

+34-40
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,19 @@ import { EvmPlatform } from './platform.js';
2020
import type { EvmChains } from './types.js';
2121
import { _platform } from './types.js';
2222

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+
2332
export async function getEvmSigner(
2433
rpc: Provider,
2534
key: string | EthersSigner,
26-
opts?: {
27-
maxGasLimit?: bigint;
28-
chain?: EvmChains;
29-
debug?: boolean;
30-
},
35+
opts?: EvmSignerOptions & { chain?: EvmChains },
3136
): Promise<Signer> {
3237
const signer: EthersSigner =
3338
typeof key === 'string' ? new Wallet(key, rpc) : key;
@@ -75,7 +80,7 @@ export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
7580
_chain: C,
7681
_address: string,
7782
_signer: EthersSigner,
78-
readonly opts?: { maxGasLimit?: bigint; debug?: boolean },
83+
readonly opts?: EvmSignerOptions,
7984
) {
8085
super(_chain, _address, _signer);
8186
}
@@ -93,37 +98,37 @@ export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
9398

9499
const signed = [];
95100

96-
// default gas limit
97-
const gasLimit = this.opts?.maxGasLimit ?? 500_000n;
98-
101+
// Default gas values
102+
let gasLimit = 500_000n;
99103
let gasPrice = 100_000_000_000n; // 100gwei
100104
let maxFeePerGas = 1_500_000_000n; // 1.5gwei
101105
let maxPriorityFeePerGas = 100_000_000n; // 0.1gwei
102106

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;
110124
}
111125

112126
// Oasis throws malformed errors unless we
113127
// set it to use legacy transaction parameters
114128
const gasOpts =
115129
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 };
127132

128133
for (const txn of tx) {
129134
const { transaction, description } = txn;
@@ -135,21 +140,10 @@ export class EvmNativeSigner<N extends Network, C extends EvmChains = EvmChains>
135140
...gasOpts,
136141
from: this.address(),
137142
nonce: await this._signer.getNonce(),
143+
// Override any existing values with those passed in the constructor
144+
...this.opts?.overrides,
138145
};
139146

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-
153147
signed.push(await this._signer.signTransaction(t));
154148
}
155149
return signed;

0 commit comments

Comments
 (0)