diff --git a/.changeset/poor-masks-chew.md b/.changeset/poor-masks-chew.md new file mode 100644 index 00000000..fa236699 --- /dev/null +++ b/.changeset/poor-masks-chew.md @@ -0,0 +1,5 @@ +--- +'@reservoir0x/relay-sdk': patch +--- + +Make txs a first class parameter in swap action diff --git a/demo/pages/sdk/actions/swap.tsx b/demo/pages/sdk/actions/swap.tsx index 2bac0b6c..58fb301c 100644 --- a/demo/pages/sdk/actions/swap.tsx +++ b/demo/pages/sdk/actions/swap.tsx @@ -194,17 +194,15 @@ const SwapActionPage: NextPage = () => { currency: fromCurrency, recipient: recipient ? (recipient as Address) : undefined, depositGasLimit, + txs: [ + ...txs as any + ], options: { - tradeType, - txs: [ - ...txs as any - ] + tradeType }, - onProgress: (data) => { console.log(data) } - }) }} > diff --git a/packages/sdk/src/actions/call.ts b/packages/sdk/src/actions/call.ts index f40ae4f6..c1b6f239 100644 --- a/packages/sdk/src/actions/call.ts +++ b/packages/sdk/src/actions/call.ts @@ -48,7 +48,9 @@ export type CallActionParameters = { | { precheck?: false; wallet: AdaptedWallet | WalletClient } ) -function isSimulateContractRequest(tx: any): tx is SimulateContractRequest { +export function isSimulateContractRequest( + tx: any +): tx is SimulateContractRequest { return (tx as SimulateContractRequest).abi !== undefined } diff --git a/packages/sdk/src/actions/swap.ts b/packages/sdk/src/actions/swap.ts index f7848839..d6d0bcd0 100644 --- a/packages/sdk/src/actions/swap.ts +++ b/packages/sdk/src/actions/swap.ts @@ -10,12 +10,18 @@ import { executeSteps, APIError, adaptViemWallet, - getCurrentStepData + getCurrentStepData, + prepareCallTransaction } from '../utils/index.js' import axios from 'axios' import type { AxiosRequestConfig } from 'axios' import { zeroAddress, type Address, type WalletClient } from 'viem' import { isViemWalletClient } from '../utils/viemWallet.js' +import { + isSimulateContractRequest, + type CallBody, + type SimulateContractRequest +} from './call.js' export type SwapBody = NonNullable< paths['/execute/swap']['post']['requestBody']['content']['application/json'] @@ -41,8 +47,9 @@ export type SwapActionParameters = { toCurrency: string amount: string recipient?: Address - options?: Omit + options?: Omit depositGasLimit?: string + txs?: (NonNullable[0] | SimulateContractRequest)[] onProgress?: (data: SwapProgressData) => any } & ( | { precheck: true; wallet?: AdaptedWallet | WalletClient } // When precheck is true, wallet is optional @@ -70,7 +77,8 @@ export async function swap(data: SwapActionParameters) { options, onProgress = () => {}, precheck, - depositGasLimit + depositGasLimit, + txs } = data const client = getClient() @@ -95,6 +103,18 @@ export async function swap(data: SwapActionParameters) { } try { + let preparedTransactions: CallBody['txs'] + if (txs && txs.length > 0) { + preparedTransactions = txs.map((tx) => { + if (isSimulateContractRequest(tx)) { + return prepareCallTransaction( + tx as Parameters['0'] + ) + } + return tx + }) + } + const data: SwapBody = { user: caller || zeroAddress, destinationCurrency: toCurrency, @@ -105,6 +125,7 @@ export async function swap(data: SwapActionParameters) { recipient: recipient ? (recipient as string) : caller ?? zeroAddress, tradeType: options?.tradeType ?? 'EXACT_INPUT', source: client.source || undefined, + txs: preparedTransactions, ...options }