Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Provider, TransactionRequest } from '@ethersproject/providers'
import { BigNumber, ContractTransaction, Signer } from 'ethers'
import { Config } from 'wagmi'
import { SimulateContractReturnType } from '@wagmi/core'

import { MergedTransaction } from '../state/app/state'
import {
Expand Down Expand Up @@ -58,6 +59,14 @@ export type TransferOverrides = {
excessFeeRefundAddress?: string
}

export type TransferPrepareTxRequestProps = {
amount: BigNumber
from: string
destinationAddress?: string
overrides?: TransferOverrides
wagmiConfig?: Config
}

export type TransferProps = {
amount: BigNumber
signer: Signer
Expand Down Expand Up @@ -169,6 +178,16 @@ export abstract class BridgeTransferStarter {
props: ApproveTokenProps
): Promise<ContractTransaction | void>

// not marking this as abstract for now, as we need a dummy implementation for every class
// only cctp is going to override it for now, and we'll do the same for others one by one
// finally, once we have all implementations we'll mark it as abstract
public async transferPrepareTxRequest(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
props?: TransferPrepareTxRequestProps
): Promise<TransactionRequest | SimulateContractReturnType> {
return {} as TransactionRequest | SimulateContractReturnType
}

public abstract transferEstimateGas(
props: TransferEstimateGasProps
): Promise<TransferEstimateGasResult>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
ApproveTokenProps,
BridgeTransferStarter,
RequiresTokenApprovalProps,
TransferPrepareTxRequestProps,
TransferProps,
TransferType
} from './BridgeTransferStarter'
Expand Down Expand Up @@ -85,16 +86,14 @@ export class CctpTransferStarter extends BridgeTransferStarter {
return undefined
}

public async transfer({
signer,
public async transferPrepareTxRequest({
from,
amount,
destinationAddress,
wagmiConfig
}: TransferProps & { wagmiConfig: Config }) {
}: TransferPrepareTxRequestProps & { wagmiConfig: Config }) {
const sourceChainId = await this.getSourceChainId()

const address = await getAddressFromSigner(signer)

// cctp has an upper limit for transfer
const burnLimit = await fetchPerMessageBurnLimit({
sourceChainId,
Expand All @@ -111,10 +110,8 @@ export class CctpTransferStarter extends BridgeTransferStarter {
)
}

const recipient = destinationAddress ?? address

const recipient = destinationAddress ?? from
// burn token on the selected chain to be transferred from cctp contracts to the other chain

// CCTP uses 32 bytes addresses, while EVEM uses 20 bytes addresses
const mintRecipient = utils.hexlify(utils.zeroPad(recipient, 32)) as Address

Expand All @@ -126,7 +123,7 @@ export class CctpTransferStarter extends BridgeTransferStarter {
sourceChainId
})

const { request } = await simulateContract(wagmiConfig, {
return simulateContract(wagmiConfig, {
address: tokenMessengerContractAddress,
abi: TokenMessengerAbi,
functionName: 'depositForBurn',
Expand All @@ -137,6 +134,20 @@ export class CctpTransferStarter extends BridgeTransferStarter {
usdcContractAddress
]
})
}

async transfer({
signer,
amount,
destinationAddress,
wagmiConfig
}: TransferProps & { wagmiConfig: Config }) {
const { request } = await this.transferPrepareTxRequest({
from: await getAddressFromSigner(signer),
amount,
destinationAddress,
wagmiConfig
})

const depositForBurnTx = await writeContract(wagmiConfig, request)

Expand Down
Loading