Skip to content

Commit ef5ff09

Browse files
committed
refactor: add wagmi tx step type for to driver
1 parent 287eaca commit ef5ff09

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

packages/arb-token-bridge-ui/src/components/TransferPanel/TransferPanel.tsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Tippy from '@tippyjs/react'
44
import { constants, utils } from 'ethers'
55
import { useLatest } from 'react-use'
66
import { useAccount, useConfig } from 'wagmi'
7+
import { writeContract } from '@wagmi/core'
78
import { TransactionResponse } from '@ethersproject/providers'
89
import { twMerge } from 'tailwind-merge'
910
import { scaleFrom18DecimalsToNativeTokenDecimals } from '@arbitrum/sdk'
@@ -430,6 +431,34 @@ export function TransferPanel() {
430431
return { error: error as unknown as Error }
431432
}
432433
}
434+
435+
case 'tx_wagmi': {
436+
try {
437+
const txHash = await writeContract(
438+
wagmiConfig,
439+
step.payload.txRequest
440+
)
441+
const txReceipt =
442+
await context.transferStarter.sourceChainProvider.waitForTransaction(
443+
txHash
444+
)
445+
446+
return { data: txReceipt }
447+
} catch (error) {
448+
// capture error and show toast for anything that's not user rejecting error
449+
if (!isUserRejectedError(error)) {
450+
handleError({
451+
error,
452+
label: step.payload.txRequestLabel,
453+
category: 'transaction_signing'
454+
})
455+
456+
errorToast(`${(error as Error)?.message ?? error}`)
457+
}
458+
459+
return { error: error as unknown as Error }
460+
}
461+
}
433462
}
434463
}
435464

packages/arb-token-bridge-ui/src/ui-driver/UiDriver.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BigNumber, providers } from 'ethers'
22
import { BridgeTransferStarter } from '@/token-bridge-sdk/BridgeTransferStarter'
3+
import { SimulateContractReturnType } from '@wagmi/core'
34

45
import { DialogType } from '../components/common/Dialog2'
56

@@ -32,6 +33,13 @@ export type UiDriverStep =
3233
txRequestLabel: string
3334
}
3435
}
36+
| {
37+
type: 'tx_wagmi'
38+
payload: {
39+
txRequest: SimulateContractReturnType['request']
40+
txRequestLabel: string
41+
}
42+
}
3543

3644
export type UiDriverStepType = UiDriverStep['type']
3745

@@ -55,7 +63,7 @@ export type UiDriverStepResultFor<TStepType extends UiDriverStepType> =
5563
? boolean
5664
: TStepType extends 'scw_tooltip'
5765
? void
58-
: TStepType extends 'tx_ethers'
66+
: TStepType extends 'tx_ethers' | 'tx_wagmi'
5967
? Result<providers.TransactionReceipt>
6068
: never
6169

packages/arb-token-bridge-ui/src/ui-driver/UiDriverCommon.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,29 @@ export const stepGeneratorForTransactionEthers: UiDriverStepGeneratorForTransact
6161
return data
6262
}
6363
}
64+
65+
export type UiDriverStepGeneratorForTransactionWagmi<
66+
TStep extends UiDriverStep = UiDriverStep
67+
> = (
68+
context: UiDriverContext,
69+
payload: UiDriverStepPayloadFor<'tx_wagmi'>
70+
) => AsyncGenerator<
71+
TStep,
72+
providers.TransactionReceipt | void,
73+
UiDriverStepResultFor<TStep['type']>
74+
>
75+
76+
export const stepGeneratorForTransactionWagmi: UiDriverStepGeneratorForTransactionWagmi =
77+
async function* (context, payload) {
78+
if (context.isSmartContractWallet) {
79+
yield* step({ type: 'scw_tooltip' })
80+
}
81+
82+
const { error, data } = yield* step({ type: 'tx_wagmi', payload })
83+
84+
if (typeof error !== 'undefined') {
85+
yield* step({ type: 'return' })
86+
} else {
87+
return data
88+
}
89+
}

0 commit comments

Comments
 (0)