Skip to content

Commit

Permalink
Refactor onProgress callback to be an object
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromcunha committed Apr 22, 2024
1 parent 298e718 commit 6c614d7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 78 deletions.
4 changes: 2 additions & 2 deletions demo/pages/sdk/actions/bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}}>
Expand Down
4 changes: 2 additions & 2 deletions demo/pages/sdk/actions/call.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
})
}}>
Expand Down
34 changes: 13 additions & 21 deletions packages/sdk/src/actions/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type {
Execute,
AdaptedWallet,
ExecuteStep,
ExecuteStepItem,
paths,
ProgressData,
} from '../types/index.js'
import {
APIError,
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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.',
)
}

Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -156,7 +148,7 @@ export async function bridge(data: BridgeActionParameters) {
gasLimit: depositGasLimit,
},
}
: undefined
: undefined,
)
return true
}
Expand Down
37 changes: 14 additions & 23 deletions packages/sdk/src/actions/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type {
Execute,
paths,
AdaptedWallet,
ExecuteStep,
ExecuteStepItem,
ProgressData,
} from '../types/index.js'
import { getClient } from '../client.js'
import {
Expand All @@ -19,7 +18,6 @@ import {
zeroAddress,
type WalletClient,
type WriteContractParameters,
type Address,
} from 'viem'
import { isViemWalletClient } from '../utils/viemWallet.js'

Expand All @@ -39,14 +37,7 @@ export type CallActionParameters = {
toChainId: number
options?: Omit<CallBodyOptions, 'user' | 'source'>
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 }
Expand Down Expand Up @@ -95,15 +86,15 @@ 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.',
)
}

try {
const preparedTransactions: CallBody['txs'] = txs.map((tx) => {
if (isSimulateContractRequest(tx)) {
return prepareCallTransaction(
tx as Parameters<typeof prepareCallTransaction>['0']
tx as Parameters<typeof prepareCallTransaction>['0'],
)
}
return tx
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -164,7 +155,7 @@ export async function call(data: CallActionParameters) {
gasLimit: depositGasLimit,
},
}
: undefined
: undefined,
)
return true
}
Expand Down
11 changes: 11 additions & 0 deletions packages/sdk/src/types/Progress.ts
Original file line number Diff line number Diff line change
@@ -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 }[]
}
1 change: 1 addition & 0 deletions packages/sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export * from './SignatureStepItem.js'
export * from './TransactionStepItem.js'
export * from './AdaptedWallet.js'
export * from './RelayChain.js'
export * from './Progress.js'
82 changes: 52 additions & 30 deletions packages/sdk/src/utils/executeSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]: {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}`],
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
{
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 6c614d7

Please sign in to comment.