diff --git a/demo/pages/sdk/actions/bridge.tsx b/demo/pages/sdk/actions/bridge.tsx index 717287ae..45873a6c 100644 --- a/demo/pages/sdk/actions/bridge.tsx +++ b/demo/pages/sdk/actions/bridge.tsx @@ -147,8 +147,8 @@ const BridgeActionPage: NextPage = () => { useExternalLiquidity: canonical, useExactInput: useExactInput }, - onProgress: (steps, fees, currentStep, currentStepItem, txHashes) => { - console.log(steps, fees, currentStep, currentStepItem, txHashes) + onProgress: (data) => { + console.log(data) } }) }}> diff --git a/demo/pages/sdk/actions/call.tsx b/demo/pages/sdk/actions/call.tsx index 0c5be034..329b1cbd 100644 --- a/demo/pages/sdk/actions/call.tsx +++ b/demo/pages/sdk/actions/call.tsx @@ -96,8 +96,8 @@ const CallActionPage: NextPage = () => { txs: txs as any, toChainId, depositGasLimit, - onProgress: (steps, fees, currentStep, currentStepItem, txHashes) => { - console.log(steps, fees, currentStep, currentStepItem, txHashes) + onProgress: (data) => { + console.log(data) }, }) }}> diff --git a/packages/sdk/src/actions/bridge.ts b/packages/sdk/src/actions/bridge.ts index 07c170b9..5351ad1f 100644 --- a/packages/sdk/src/actions/bridge.ts +++ b/packages/sdk/src/actions/bridge.ts @@ -1,9 +1,8 @@ import type { Execute, AdaptedWallet, - ExecuteStep, - ExecuteStepItem, paths, + ProgressData, } from '../types/index.js' import { APIError, @@ -38,14 +37,7 @@ export type BridgeActionParameters = { recipient?: Address options?: BridgeBodyOptions depositGasLimit?: string - onProgress?: ( - steps: Execute['steps'], - fees?: Execute['fees'], - breakdown?: Execute['breakdown'], - currentStep?: ExecuteStep | null, - currentStepItem?: ExecuteStepItem, - txHashes?: { txHash: Address; chainId: number }[] - ) => any + onProgress?: (data: ProgressData) => any } & ( | { precheck: true; wallet?: AdaptedWallet | WalletClient } // When precheck is true, wallet is optional | { precheck?: false; wallet: AdaptedWallet | WalletClient } @@ -94,7 +86,7 @@ export async function bridge(data: BridgeActionParameters) { // Ensure wallet is provided when precheck is false or undefined if (!precheck && !adaptedWallet) { throw new Error( - 'Wallet is required when precheck is false or not provided.' + 'Wallet is required when precheck is false or not provided.', ) } @@ -121,7 +113,11 @@ export async function bridge(data: BridgeActionParameters) { if (res.status !== 200) throw new APIError(res?.data?.message, res.status, res.data) const data = res.data as Execute - onProgress(data['steps'], data['fees'], data['breakdown']) + onProgress({ + steps: data['steps'], + fees: data['fees'], + breakdown: data['breakdown'], + }) return data } else { if (!adaptedWallet) { @@ -132,22 +128,18 @@ export async function bridge(data: BridgeActionParameters) { chainId, request, adaptedWallet, - ( - steps: Execute['steps'], - fees: Execute['fees'], - breakdown: Execute['breakdown'] - ) => { + ({ steps, fees, breakdown }) => { const { currentStep, currentStepItem, txHashes } = getCurrentStepData(steps) - onProgress( + onProgress({ steps, fees, breakdown, currentStep, currentStepItem, - txHashes - ) + txHashes, + }) }, undefined, depositGasLimit @@ -156,7 +148,7 @@ export async function bridge(data: BridgeActionParameters) { gasLimit: depositGasLimit, }, } - : undefined + : undefined, ) return true } diff --git a/packages/sdk/src/actions/call.ts b/packages/sdk/src/actions/call.ts index 6e160a29..7233dd48 100644 --- a/packages/sdk/src/actions/call.ts +++ b/packages/sdk/src/actions/call.ts @@ -2,8 +2,7 @@ import type { Execute, paths, AdaptedWallet, - ExecuteStep, - ExecuteStepItem, + ProgressData, } from '../types/index.js' import { getClient } from '../client.js' import { @@ -19,7 +18,6 @@ import { zeroAddress, type WalletClient, type WriteContractParameters, - type Address, } from 'viem' import { isViemWalletClient } from '../utils/viemWallet.js' @@ -39,14 +37,7 @@ export type CallActionParameters = { toChainId: number options?: Omit depositGasLimit?: string - onProgress?: ( - steps: Execute['steps'], - fees?: Execute['fees'], - breakdown?: Execute['breakdown'], - currentStep?: ExecuteStep | null, - currentStepItem?: ExecuteStepItem, - txHashes?: { txHash: Address; chainId: number }[] - ) => any + onProgress?: (data: ProgressData) => any } & ( | { precheck: true; wallet?: AdaptedWallet | WalletClient } // When precheck is true, wallet is optional | { precheck?: false; wallet: AdaptedWallet | WalletClient } @@ -95,7 +86,7 @@ export async function call(data: CallActionParameters) { // Ensure wallet is provided when precheck is false or undefined if (!precheck && !adaptedWallet) { throw new Error( - 'Wallet is required when precheck is false or not provided.' + 'Wallet is required when precheck is false or not provided.', ) } @@ -103,7 +94,7 @@ export async function call(data: CallActionParameters) { const preparedTransactions: CallBody['txs'] = txs.map((tx) => { if (isSimulateContractRequest(tx)) { return prepareCallTransaction( - tx as Parameters['0'] + tx as Parameters['0'], ) } return tx @@ -129,7 +120,11 @@ export async function call(data: CallActionParameters) { if (res.status !== 200) throw new APIError(res?.data?.message, res.status, res.data) const data = res.data as Execute - onProgress(data['steps'], data['fees'], data['breakdown']) + onProgress({ + steps: data['steps'], + fees: data['fees'], + breakdown: data['breakdown'], + }) return data } else { if (!adaptedWallet) { @@ -140,22 +135,18 @@ export async function call(data: CallActionParameters) { chainId, request, adaptedWallet, - ( - steps: Execute['steps'], - fees: Execute['fees'], - breakdown: Execute['breakdown'] - ) => { + ({ steps, fees, breakdown }) => { const { currentStep, currentStepItem, txHashes } = getCurrentStepData(steps) - onProgress( + onProgress({ steps, fees, breakdown, currentStep, currentStepItem, - txHashes - ) + txHashes, + }) }, undefined, depositGasLimit @@ -164,7 +155,7 @@ export async function call(data: CallActionParameters) { gasLimit: depositGasLimit, }, } - : undefined + : undefined, ) return true } diff --git a/packages/sdk/src/types/Progress.ts b/packages/sdk/src/types/Progress.ts new file mode 100644 index 00000000..34cbda57 --- /dev/null +++ b/packages/sdk/src/types/Progress.ts @@ -0,0 +1,11 @@ +import { type Address } from 'viem' +import type { Execute, ExecuteStep, ExecuteStepItem } from '../types/Execute.js' + +export type ProgressData = { + steps: Execute['steps'] + fees?: Execute['fees'] + breakdown?: Execute['breakdown'] + currentStep?: ExecuteStep | null + currentStepItem?: ExecuteStepItem + txHashes?: { txHash: Address; chainId: number }[] +} diff --git a/packages/sdk/src/types/index.ts b/packages/sdk/src/types/index.ts index f1606ab6..74ed3181 100644 --- a/packages/sdk/src/types/index.ts +++ b/packages/sdk/src/types/index.ts @@ -4,3 +4,4 @@ export * from './SignatureStepItem.js' export * from './TransactionStepItem.js' export * from './AdaptedWallet.js' export * from './RelayChain.js' +export * from './Progress.js' diff --git a/packages/sdk/src/utils/executeSteps.ts b/packages/sdk/src/utils/executeSteps.ts index 2fbec86d..ad0ad669 100644 --- a/packages/sdk/src/utils/executeSteps.ts +++ b/packages/sdk/src/utils/executeSteps.ts @@ -13,6 +13,12 @@ import { getClient } from '../client.js' import { LogLevel } from '../utils/logger.js' import { sendTransactionSafely } from './transaction.js' +export type SetStateData = { + steps: Execute['steps'] + fees?: Execute['fees'] + breakdown?: Execute['breakdown'] +} + // /** // * When attempting to perform actions, such as, bridging or performing a cross chain action // * the user's account needs to meet certain requirements. For @@ -32,11 +38,7 @@ export async function executeSteps( chainId: number, request: AxiosRequestConfig, wallet: AdaptedWallet, - setState: ( - steps: Execute['steps'], - fees?: Execute['fees'], - breakdown?: Execute['breakdown'], - ) => any, + setState: (data: SetStateData) => any, newJson?: Execute, stepOptions?: { [stepId: string]: { @@ -84,7 +86,11 @@ export async function executeSteps( if (json.error || !json.steps) throw json // Update state on first call or recursion - setState([...json?.steps], { ...json?.fees }, json?.breakdown) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) let incompleteStepIndex = -1 let incompleteStepItemIndex = -1 @@ -165,7 +171,11 @@ export async function executeSteps( } stepItems = items stepItem = items[incompleteStepItemIndex] - setState([...json?.steps], { ...json?.fees }, json?.breakdown) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) } client.log( [`Execute Steps: Begin processing step items for: ${step.action}`], @@ -216,21 +226,21 @@ export async function executeSteps( ) stepItem.txHashes = txHashes if (json) { - setState( - [...json.steps], - { ...json?.fees }, - json?.breakdown, - ) + setState({ + steps: [...json.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) } }, (internalTxHashes) => { stepItem.internalTxHashes = internalTxHashes if (json) { - setState( - [...json.steps], - { ...json?.fees }, - json?.breakdown, - ) + setState({ + steps: [...json.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) } }, request, @@ -295,11 +305,11 @@ export async function executeSteps( // If check, poll check until validated if (stepItem?.check) { stepItem.isValidatingSignature = true - setState( - [...json?.steps], - { ...json?.fees }, - json?.breakdown, - ) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) await pollUntilOk( { @@ -374,11 +384,11 @@ export async function executeSteps( }, ] } - setState( - [...json?.steps], - { ...json?.fees }, - json?.breakdown, - ) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) } catch (err) { throw err } @@ -393,7 +403,11 @@ export async function executeSteps( stepItem.status = 'complete' stepItem.isValidatingSignature = false - setState([...json?.steps], { ...json?.fees }, json?.breakdown) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) resolve(stepItem) } catch (e) { const error = e as Error @@ -406,7 +420,11 @@ export async function executeSteps( stepItem.error = errorMessage stepItem.errorData = (e as any)?.response?.data || e stepItem.isValidatingSignature = false - setState([...json?.steps], { ...json?.fees }, json?.breakdown) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) } reject(error) } @@ -434,7 +452,11 @@ export async function executeSteps( if (json) { json.error = err && err?.response?.data ? err.response.data : err - setState([...json?.steps], { ...json?.fees }, json?.breakdown) + setState({ + steps: [...json?.steps], + fees: { ...json?.fees }, + breakdown: json?.breakdown, + }) } else { json = { error: err && err?.response?.data ? err.response.data : err,