Skip to content

feat: add EIP1559 tx type support #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions packages/pkp-ethers/src/lib/pkp-ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ import {

const logger = new Logger(version);

const EIP1559_TX_TYPE = 2;

export class PKPEthersWallet
implements
PKPWallet,
Expand All @@ -81,6 +83,8 @@ export class PKPEthersWallet
// -- manual tx settings --
manualGasPrice?: string;
manualGasLimit?: string;
manualMaxFeePerGas?: string;
manualMaxPriorityFeePerGas?: string;
nonce?: string;
chainId?: number;

Expand Down Expand Up @@ -155,6 +159,14 @@ export class PKPEthersWallet
this.manualGasLimit = gasLimit;
};

setMaxFeePerGas = (maxFeePerGas: string): void => {
this.manualMaxFeePerGas = maxFeePerGas;
};

setMaxPriorityFeePerGas = (maxPriorityFeePerGas: string): void => {
this.manualMaxPriorityFeePerGas = maxPriorityFeePerGas;
};

setNonce = (nonce: string): void => {
this.nonce = nonce;
};
Expand All @@ -166,6 +178,8 @@ export class PKPEthersWallet
resetManualSettings = (): void => {
this.manualGasPrice = undefined;
this.manualGasLimit = undefined;
this.manualMaxFeePerGas = undefined;
this.manualMaxPriorityFeePerGas = undefined;
this.nonce = undefined;
this.chainId = undefined;
};
Expand Down Expand Up @@ -215,6 +229,20 @@ export class PKPEthersWallet
transaction.gasLimit = this.manualGasLimit;
}

if (this.manualMaxFeePerGas && this.manualMaxPriorityFeePerGas) {
transaction.maxFeePerGas = this.manualMaxFeePerGas;
transaction.maxPriorityFeePerGas = this.manualMaxPriorityFeePerGas;
transaction.type = EIP1559_TX_TYPE;
this.pkpBase.log(
'signTransaction => EIP-1559 maxFeePerGas:',
transaction.maxFeePerGas
);
this.pkpBase.log(
'signTransaction => EIP-1559 maxPriorityFeePerGas:',
transaction.maxPriorityFeePerGas
);
}

if (this.nonce) {
transaction.nonce = this.nonce;
}
Expand All @@ -239,9 +267,29 @@ export class PKPEthersWallet
this.pkpBase.log('signTransaction => chainId:', transaction.chainId);
}

if (!transaction['gasPrice']) {
transaction.gasPrice = await this.getGasPrice();
this.pkpBase.log('signTransaction => gasPrice:', transaction.gasPrice);
if (
!transaction.gasPrice &&
(!transaction.maxFeePerGas || !transaction.maxPriorityFeePerGas)
) {
const feeData = await this.getFeeData();
if (feeData.maxFeePerGas && feeData.maxPriorityFeePerGas) {
transaction.maxFeePerGas = feeData.maxFeePerGas;
transaction.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
this.pkpBase.log(
'signTransaction => maxFeePerGas:',
transaction.maxFeePerGas
);
this.pkpBase.log(
'signTransaction => maxPriorityFeePerGas:',
transaction.maxPriorityFeePerGas
);
} else {
transaction.gasPrice = feeData.gasPrice || (await this.getGasPrice());
this.pkpBase.log(
'signTransaction => fallback gasPrice:',
transaction.gasPrice
);
}
}
} catch (err) {
this.pkpBase.log(
Expand Down
Loading