Skip to content

Commit

Permalink
feat(wallet,provider): add paymaster support for transfer and withdra…
Browse files Browse the repository at this point in the history
…wal tx
  • Loading branch information
danijelTxFusion committed Feb 5, 2024
1 parent 8cfbfcd commit 5cab446
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
FinalizeWithdrawalParams,
FullDepositFee,
MessageProof,
PaymasterParams,
PriorityOpResponse,
TransactionResponse,
} from "./types";
Expand Down Expand Up @@ -816,6 +817,7 @@ export function AdapterL2<TBase extends Constructor<TxSender>>(Base: TBase) {
amount: BigNumberish;
to?: Address;
bridgeAddress?: Address;
paymasterParamas?: PaymasterParams;
overrides?: ethers.Overrides;
}): Promise<TransactionResponse> {
const withdrawTx = await this._providerL2().getWithdrawTx({
Expand All @@ -829,6 +831,7 @@ export function AdapterL2<TBase extends Constructor<TxSender>>(Base: TBase) {
to: Address;
amount: BigNumberish;
token?: Address;
paymasterParamas?: PaymasterParams;
overrides?: ethers.Overrides;
}): Promise<TransactionResponse> {
const transferTx = await this._providerL2().getTransferTx({
Expand Down
51 changes: 48 additions & 3 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ import {
Fee,
Transaction,
RawBlockTransaction,
PaymasterParams,
TransactionLike,
} from "./types";
import {
isETH,
Expand Down Expand Up @@ -273,6 +275,7 @@ export function JsonRpcApiProvider<TBase extends Constructor<ethers.JsonRpcApiPr
from?: Address;
to?: Address;
bridgeAddress?: Address;
paymasterParamas?: PaymasterParams;
overrides?: ethers.Overrides;
}): Promise<EthersTransactionRequest> {
const { ...tx } = transaction;
Expand All @@ -299,7 +302,16 @@ export function JsonRpcApiProvider<TBase extends Constructor<ethers.JsonRpcApiPr
}

const ethL2Token = IEthToken__factory.connect(L2_ETH_TOKEN_ADDRESS, this);
return ethL2Token.withdraw.populateTransaction(tx.to as Address, tx.overrides);
const populatedTx = await ethL2Token.withdraw.populateTransaction(tx.to as Address, tx.overrides);
if (tx.paymasterParamas) {
return {
...populatedTx,
customData: {
paymasterParams: tx.paymasterParamas,
},
}
}
return populatedTx;
}

if (tx.bridgeAddress == null) {
Expand All @@ -314,12 +326,21 @@ export function JsonRpcApiProvider<TBase extends Constructor<ethers.JsonRpcApiPr
}

const bridge = IL2Bridge__factory.connect(tx.bridgeAddress!, this);
return bridge.withdraw.populateTransaction(
const populatedTx = await bridge.withdraw.populateTransaction(
tx.to as Address,
tx.token,
tx.amount,
tx.overrides,
);
if (tx.paymasterParamas) {
return {
...populatedTx,
customData: {
paymasterParams: tx.paymasterParamas,
},
}
}
return populatedTx;
}

async estimateGasWithdraw(transaction: {
Expand All @@ -328,6 +349,7 @@ export function JsonRpcApiProvider<TBase extends Constructor<ethers.JsonRpcApiPr
from?: Address;
to?: Address;
bridgeAddress?: Address;
paymasterParamas?: PaymasterParams;
overrides?: ethers.Overrides;
}): Promise<bigint> {
const withdrawTx = await this.getWithdrawTx(transaction);
Expand All @@ -339,21 +361,43 @@ export function JsonRpcApiProvider<TBase extends Constructor<ethers.JsonRpcApiPr
amount: BigNumberish;
from?: Address;
token?: Address;
paymasterParamas?: PaymasterParams;
overrides?: ethers.Overrides;
}): Promise<EthersTransactionRequest> {
const { ...tx } = transaction;
tx.overrides ??= {};
tx.overrides.from ??= tx.from;

if (tx.token == null || tx.token == ETH_ADDRESS) {
if (tx.paymasterParamas) {
return {
...tx.overrides,
type: EIP712_TX_TYPE,
to: tx.to,
value: tx.amount,
customData: {
paymasterParams: tx.paymasterParamas,
},
};
}

return {
...tx.overrides,
to: tx.to,
value: tx.amount,
};
} else {
const token = IERC20__factory.connect(tx.token, this);
return await token.transfer.populateTransaction(tx.to, tx.amount, tx.overrides);
const populatedTx = await token.transfer.populateTransaction(tx.to, tx.amount, tx.overrides);
if (tx.paymasterParamas) {
return {
...populatedTx,
customData: {
paymasterParams: tx.paymasterParamas,
},
}
}
return populatedTx;
}
}

Expand All @@ -362,6 +406,7 @@ export function JsonRpcApiProvider<TBase extends Constructor<ethers.JsonRpcApiPr
amount: BigNumberish;
from?: Address;
token?: Address;
paymasterParamas?: PaymasterParams;
overrides?: ethers.Overrides;
}): Promise<bigint> {
const transferTx = await this.getTransferTx(transaction);
Expand Down

0 comments on commit 5cab446

Please sign in to comment.