Skip to content

Commit

Permalink
Upgrade to v2 or call/bridge actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromcunha committed May 28, 2024
1 parent e3f5f53 commit 00663fe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
24 changes: 14 additions & 10 deletions packages/sdk/src/actions/bridge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import type { Execute, AdaptedWallet, paths, CallFees } from '../types/index.js'
import type {
Execute,
AdaptedWallet,
paths,
ProgressData
} from '../types/index.js'
import {
APIError,
adaptViemWallet,
Expand All @@ -10,10 +15,9 @@ import { isViemWalletClient } from '../utils/viemWallet.js'
import { getClient } from '../client.js'
import type { AxiosRequestConfig } from 'axios'
import axios from 'axios'
import type { CallProgressData } from './call.js'

export type BridgeBody = NonNullable<
paths['/execute/bridge']['post']['requestBody']['content']['application/json']
paths['/execute/bridge/v2']['post']['requestBody']['content']['application/json']
>
export type BridgeBodyOptions = Omit<
BridgeBody,
Expand All @@ -33,7 +37,7 @@ export type BridgeActionParameters = {
recipient?: Address
options?: BridgeBodyOptions
depositGasLimit?: string
onProgress?: (data: CallProgressData) => 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 @@ -100,7 +104,7 @@ export async function bridge(data: BridgeActionParameters) {
}

const request: AxiosRequestConfig = {
url: `${client.baseApiUrl}/execute/bridge`,
url: `${client.baseApiUrl}/execute/bridge/v2`,
method: 'post',
data
}
Expand All @@ -109,10 +113,10 @@ export async function bridge(data: BridgeActionParameters) {
const res = await axios.request(request)
if (res.status !== 200)
throw new APIError(res?.data?.message, res.status, res.data)
const data = res.data as Omit<Execute, 'fees'> & { fees: CallFees }
const data = res.data as Execute
onProgress({
steps: data['steps'],
fees: data['fees'] as CallFees,
fees: data['fees'],
breakdown: data['breakdown']
})
return data
Expand All @@ -121,7 +125,7 @@ export async function bridge(data: BridgeActionParameters) {
throw new Error('AdaptedWallet is required to execute steps')
}

return (await executeSteps(
return await executeSteps(
chainId,
request,
adaptedWallet,
Expand All @@ -131,7 +135,7 @@ export async function bridge(data: BridgeActionParameters) {

onProgress({
steps,
fees: fees as CallFees,
fees,
breakdown,
currentStep,
currentStepItem,
Expand All @@ -146,7 +150,7 @@ export async function bridge(data: BridgeActionParameters) {
}
}
: undefined
)) as Omit<Execute, 'fees'> & { fees: CallFees }
)
}
} catch (err: any) {
console.error(err)
Expand Down
18 changes: 7 additions & 11 deletions packages/sdk/src/actions/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,13 @@ import {
import { isViemWalletClient } from '../utils/viemWallet.js'

export type CallBody = NonNullable<
paths['/execute/call']['post']['requestBody']['content']['application/json']
paths['/execute/call/v2']['post']['requestBody']['content']['application/json']
>
export type CallBodyOptions = Omit<
CallBody,
'txs' | 'destinationChainId' | 'originChainId'
>

export type CallProgressData = Omit<ProgressData, 'fees'> & {
fees: CallFees
}

export type SimulateContractRequest = WriteContractParameters<any>

export type CallActionParameters = {
Expand All @@ -42,7 +38,7 @@ export type CallActionParameters = {
toChainId: number
options?: Omit<CallBodyOptions, 'user' | 'source'>
depositGasLimit?: string
onProgress?: (data: CallProgressData) => 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 @@ -118,7 +114,7 @@ export async function call(data: CallActionParameters) {
}

const request: AxiosRequestConfig = {
url: `${client.baseApiUrl}/execute/call`,
url: `${client.baseApiUrl}/execute/call/v2`,
method: 'post',
data
}
Expand All @@ -127,7 +123,7 @@ export async function call(data: CallActionParameters) {
const res = await axios.request(request)
if (res.status !== 200)
throw new APIError(res?.data?.message, res.status, res.data)
const data = res.data as Omit<Execute, 'fees'> & { fees: CallFees }
const data = res.data as Execute
onProgress({
steps: data['steps'],
fees: data['fees'] as CallFees,
Expand All @@ -139,7 +135,7 @@ export async function call(data: CallActionParameters) {
throw new Error('AdaptedWallet is required to execute steps')
}

return (await executeSteps(
return await executeSteps(
chainId,
request,
adaptedWallet,
Expand All @@ -149,7 +145,7 @@ export async function call(data: CallActionParameters) {

onProgress({
steps,
fees: fees as CallFees,
fees,
breakdown,
currentStep,
currentStepItem,
Expand All @@ -164,7 +160,7 @@ export async function call(data: CallActionParameters) {
}
}
: undefined
)) as Omit<Execute, 'fees'> & { fees: CallFees }
)
}
} catch (err: any) {
console.error(err)
Expand Down
11 changes: 4 additions & 7 deletions packages/sdk/src/actions/swap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type {
Execute,
paths,
AdaptedWallet,
ProgressData,
SwapFees
ProgressData
} from '../types/index.js'
import { getClient } from '../client.js'
import {
Expand Down Expand Up @@ -36,9 +35,7 @@ export type SwapBodyOptions = Omit<
| 'recipient'
>

type SwapProgressData = Omit<ProgressData, 'fees'> & {
fees: SwapFees
}
type SwapProgressData = ProgressData

export type SwapActionParameters = {
chainId: number
Expand Down Expand Up @@ -142,7 +139,7 @@ export async function swap(data: SwapActionParameters) {
const data = res.data as Execute
onProgress({
steps: data['steps'],
fees: data['fees'] as SwapFees,
fees: data['fees'],
breakdown: data['breakdown'],
details: data['details']
})
Expand All @@ -162,7 +159,7 @@ export async function swap(data: SwapActionParameters) {

onProgress({
steps,
fees: fees as SwapFees,
fees,
breakdown,
details,
currentStep,
Expand Down
10 changes: 4 additions & 6 deletions packages/sdk/src/types/Execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import type { Address } from 'viem'
import type { paths } from './api.js'

export type CallFees =
paths['/execute/call']['post']['responses']['200']['content']['application/json']['fees']
export type SwapFees =
paths['/execute/swap']['post']['responses']['200']['content']['application/json']['fees']
paths['/execute/call/v2']['post']['responses']['200']['content']['application/json']['fees']
export type CallBreakdown =
paths['/execute/call']['post']['responses']['200']['content']['application/json']['breakdown']
paths['/execute/call/v2']['post']['responses']['200']['content']['application/json']['breakdown']
export type CheckApi = NonNullable<
NonNullable<
paths['/execute/call']['post']['responses']['200']['content']['application/json']['steps']
paths['/execute/call/v2']['post']['responses']['200']['content']['application/json']['steps']
>['0']['items']
>[0]['check']
export type ExecuteDetails = NonNullable<
Expand All @@ -25,7 +23,7 @@ export type SignatureStepState =

export type Execute = {
errors?: { message?: string; orderId?: string }[]
fees?: CallFees | SwapFees
fees?: CallFees
breakdown?: CallBreakdown
details?: ExecuteDetails
error?: any // Manually added client error
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2329,8 +2329,10 @@ export interface paths {
200: {
content: {
"application/json": {
AVAX?: number;
ETH?: number;
DEGEN?: number;
MATIC?: number;
USDC?: number;
XAI?: number;
};
Expand Down

0 comments on commit 00663fe

Please sign in to comment.