Skip to content

Commit 287eaca

Browse files
committed
refactor: add cctp prepare transfer tx request
1 parent 3399fbc commit 287eaca

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

packages/arb-token-bridge-ui/src/token-bridge-sdk/BridgeTransferStarter.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Provider, TransactionRequest } from '@ethersproject/providers'
22
import { BigNumber, ContractTransaction, Signer } from 'ethers'
33
import { Config } from 'wagmi'
4+
import { SimulateContractReturnType } from '@wagmi/core'
45

56
import { MergedTransaction } from '../state/app/state'
67
import {
@@ -58,6 +59,13 @@ export type TransferOverrides = {
5859
excessFeeRefundAddress?: string
5960
}
6061

62+
export type TransferPrepareTxRequestProps = {
63+
amount: BigNumber
64+
from: string
65+
destinationAddress?: string
66+
overrides?: TransferOverrides
67+
}
68+
6169
export type TransferProps = {
6270
amount: BigNumber
6371
signer: Signer
@@ -169,6 +177,16 @@ export abstract class BridgeTransferStarter {
169177
props: ApproveTokenProps
170178
): Promise<ContractTransaction | void>
171179

180+
// not marking this as abstract for now, as we need a dummy implementation for every class
181+
// only cctp is going to override it for now, and we'll do the same for others one by one
182+
// finally, once we have all implementations we'll mark it as abstract
183+
public async transferPrepareTxRequest(
184+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
185+
props?: TransferPrepareTxRequestProps
186+
): Promise<TransactionRequest | SimulateContractReturnType['request']> {
187+
return {} as TransactionRequest | SimulateContractReturnType['request']
188+
}
189+
172190
public abstract transferEstimateGas(
173191
props: TransferEstimateGasProps
174192
): Promise<TransferEstimateGasResult>

packages/arb-token-bridge-ui/src/token-bridge-sdk/CctpTransferStarter.ts

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { Config, simulateContract, writeContract } from '@wagmi/core'
1+
import {
2+
Config,
3+
simulateContract,
4+
SimulateContractReturnType,
5+
writeContract
6+
} from '@wagmi/core'
27
import { BigNumber, constants, utils } from 'ethers'
38
import { TransactionRequest } from '@ethersproject/providers'
49
import { ERC20__factory } from '@arbitrum/sdk/dist/lib/abi/factories/ERC20__factory'
@@ -8,6 +13,7 @@ import {
813
ApproveTokenProps,
914
BridgeTransferStarter,
1015
RequiresTokenApprovalProps,
16+
TransferPrepareTxRequestProps,
1117
TransferProps,
1218
TransferType
1319
} from './BridgeTransferStarter'
@@ -85,16 +91,14 @@ export class CctpTransferStarter extends BridgeTransferStarter {
8591
return undefined
8692
}
8793

88-
public async transfer({
89-
signer,
94+
public async transferPrepareTxRequest({
95+
from,
9096
amount,
9197
destinationAddress,
9298
wagmiConfig
93-
}: TransferProps & { wagmiConfig: Config }) {
99+
}: TransferPrepareTxRequestProps & { wagmiConfig: Config }) {
94100
const sourceChainId = await this.getSourceChainId()
95101

96-
const address = await getAddressFromSigner(signer)
97-
98102
// cctp has an upper limit for transfer
99103
const burnLimit = await fetchPerMessageBurnLimit({
100104
sourceChainId,
@@ -111,10 +115,8 @@ export class CctpTransferStarter extends BridgeTransferStarter {
111115
)
112116
}
113117

114-
const recipient = destinationAddress ?? address
115-
118+
const recipient = destinationAddress ?? from
116119
// burn token on the selected chain to be transferred from cctp contracts to the other chain
117-
118120
// CCTP uses 32 bytes addresses, while EVEM uses 20 bytes addresses
119121
const mintRecipient = utils.hexlify(utils.zeroPad(recipient, 32)) as Address
120122

@@ -138,6 +140,22 @@ export class CctpTransferStarter extends BridgeTransferStarter {
138140
]
139141
})
140142

143+
return request as unknown as SimulateContractReturnType['request']
144+
}
145+
146+
async transfer({
147+
signer,
148+
amount,
149+
destinationAddress,
150+
wagmiConfig
151+
}: TransferProps & { wagmiConfig: Config }) {
152+
const request = await this.transferPrepareTxRequest({
153+
from: await getAddressFromSigner(signer),
154+
amount,
155+
destinationAddress,
156+
wagmiConfig
157+
})
158+
141159
const depositForBurnTx = await writeContract(wagmiConfig, request)
142160

143161
return {

0 commit comments

Comments
 (0)