Skip to content

Commit

Permalink
Merge pull request #99 from reservoirprotocol/ted/add-swap-extra-txs-…
Browse files Browse the repository at this point in the history
…support

Make txs a first class parameter in swap action
  • Loading branch information
ted-palmer authored May 24, 2024
2 parents 62d166d + 820021e commit 707728d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/poor-masks-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@reservoir0x/relay-sdk': patch
---

Make txs a first class parameter in swap action
10 changes: 4 additions & 6 deletions demo/pages/sdk/actions/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

})
}}
>
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/actions/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
27 changes: 24 additions & 3 deletions packages/sdk/src/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -41,8 +47,9 @@ export type SwapActionParameters = {
toCurrency: string
amount: string
recipient?: Address
options?: Omit<SwapBodyOptions, 'user' | 'source'>
options?: Omit<SwapBodyOptions, 'user' | 'source' | 'txs'>
depositGasLimit?: string
txs?: (NonNullable<CallBody['txs']>[0] | SimulateContractRequest)[]
onProgress?: (data: SwapProgressData) => any
} & (
| { precheck: true; wallet?: AdaptedWallet | WalletClient } // When precheck is true, wallet is optional
Expand Down Expand Up @@ -70,7 +77,8 @@ export async function swap(data: SwapActionParameters) {
options,
onProgress = () => {},
precheck,
depositGasLimit
depositGasLimit,
txs
} = data
const client = getClient()

Expand All @@ -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<typeof prepareCallTransaction>['0']
)
}
return tx
})
}

const data: SwapBody = {
user: caller || zeroAddress,
destinationCurrency: toCurrency,
Expand All @@ -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
}

Expand Down

0 comments on commit 707728d

Please sign in to comment.