Skip to content

Commit a27ac04

Browse files
committed
chore: add method to get orders
1 parent 623aebd commit a27ac04

File tree

6 files changed

+118
-63
lines changed

6 files changed

+118
-63
lines changed

sdk/src/gateway/cross-chain-swap.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ import { bob } from 'viem/chains';
22
import { AllWalletClientParams } from './client';
33
import { ExecuteQuoteParams, GetQuoteParams } from './types/quote';
44
import { resolveChainId, getChainConfig } from './utils/common';
5-
import { GatewayOrderType } from './types/order';
5+
import { GatewayOrder, GatewayOrderType } from './types/order';
66
import { LayerZeroGatewayClient } from './layerzero';
77
import { GatewayApiClient } from './client';
88
import {
99
CrossChainSwapQuoteParamsExt,
1010
OnrampWithSwapsExecuteQuoteParams,
1111
OfframpWithSwapsExecuteQuoteParams,
1212
ActionsParams,
13-
TransactionParams,
1413
} from './types';
1514
import { SwapsClient } from './swaps';
1615
import { getTokenAddress } from './tokens';
@@ -162,8 +161,38 @@ export class CrossChainSwapGatewayClient extends LayerZeroGatewayClient {
162161
}
163162
}
164163

165-
getTransactions(params: TransactionParams) {
166-
return this.swapsClient.getTransactions(params);
164+
/**
165+
* Retrieves all orders (onramp, offramp, and evm-to-evm swaps) for a specific user address.
166+
*
167+
* @param {TransactionParams} params query params for the swaps api
168+
* @returns Promise resolving to array of typed orders
169+
*/
170+
async getOrders(userAddress: Address): Promise<Array<GatewayOrder>> {
171+
const [swapOrders, gatewayOrders] = await Promise.all([
172+
this.swapsClient.getTransactions(userAddress),
173+
super.getOrders(userAddress),
174+
]);
175+
176+
const orders = swapOrders.txs.map<GatewayOrder>((order) => ({
177+
type: GatewayOrderType.EVMToEVMWithSwaps as const,
178+
order: {
179+
amount: BigInt(order.actionResponse.amountOut.amount),
180+
timestamp: new Date(order.srcTx.timestamp).getTime(),
181+
status: order.status,
182+
source: {
183+
chainId: order.srcTx.chainId,
184+
txHash: order.srcTxHash,
185+
token: order.srcTx.paymentToken.address,
186+
},
187+
destination: {
188+
chainId: order.dstTx.chainId,
189+
txHash: order.dstTxHash,
190+
token: order.dstTx.paymentToken.address,
191+
},
192+
},
193+
}));
194+
195+
return [...gatewayOrders, ...orders];
167196
}
168197

169198
private async getSwapsOnrampQuote(

sdk/src/gateway/layerzero.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -969,43 +969,50 @@ export class LayerZeroGatewayClient extends GatewayApiClient {
969969

970970
const items = json.data.filter((item) => item.destination.lzCompose.status === 'N/A');
971971

972-
return items.map((item): EVMToEVMWithLayerZeroOrder => {
973-
const { payload, blockTimestamp, txHash: sourceTxHash } = item.source.tx;
974-
const { txHash: destinationTxHash } = item.destination.tx;
975-
976-
let amount = 0n;
977-
978-
if (payload && typeof payload === 'string') {
979-
// LayerZero payload format: the order size is encoded in the last 8 bytes (16 hex chars)
980-
const hex = payload.startsWith('0x') ? payload.slice(2) : payload;
981-
// Validate minimum expected payload length
982-
if (hex.length >= 16 && hex.length % 2 === 0) {
983-
const last16 = hex.slice(-16);
984-
try {
985-
amount = BigInt('0x' + last16);
986-
} catch {
987-
console.warn('Failed to parse order size from LayerZero payload');
988-
amount = 0n;
972+
return Promise.all(
973+
items.map(async (item): Promise<EVMToEVMWithLayerZeroOrder> => {
974+
const { payload, blockTimestamp, txHash: sourceTxHash } = item.source.tx;
975+
const { txHash: destinationTxHash } = item.destination.tx;
976+
977+
let amount = 0n;
978+
979+
if (payload && typeof payload === 'string') {
980+
// LayerZero payload format: the order size is encoded in the last 8 bytes (16 hex chars)
981+
const hex = payload.startsWith('0x') ? payload.slice(2) : payload;
982+
// Validate minimum expected payload length
983+
if (hex.length >= 16 && hex.length % 2 === 0) {
984+
const last16 = hex.slice(-16);
985+
try {
986+
amount = BigInt('0x' + last16);
987+
} catch {
988+
console.warn('Failed to parse order size from LayerZero payload');
989+
amount = 0n;
990+
}
989991
}
990992
}
991-
}
992993

993-
return {
994-
amount,
995-
timestamp: blockTimestamp,
996-
status: getEVMToEVMWithLayerZeroStatus(item),
997-
source: {
998-
eid: item.pathway.srcEid,
999-
txHash: sourceTxHash,
1000-
token: item.pathway.sender.address as Address,
1001-
},
1002-
destination: {
1003-
eid: item.pathway.dstEid,
1004-
txHash: destinationTxHash,
1005-
token: item.pathway.receiver.address as Address,
1006-
},
1007-
};
1008-
});
994+
const [fromChain, toChain] = (await Promise.all([
995+
this.getChainIdForEid(item.pathway.srcEid),
996+
this.getChainIdForEid(item.pathway.dstEid),
997+
])) as [number, number];
998+
999+
return {
1000+
amount,
1001+
timestamp: blockTimestamp,
1002+
status: getEVMToEVMWithLayerZeroStatus(item),
1003+
source: {
1004+
chainId: fromChain,
1005+
txHash: sourceTxHash,
1006+
token: item.pathway.sender.address as Address,
1007+
},
1008+
destination: {
1009+
chainId: toChain,
1010+
txHash: destinationTxHash,
1011+
token: item.pathway.receiver.address as Address,
1012+
},
1013+
};
1014+
})
1015+
);
10091016
}
10101017

10111018
/**

sdk/src/gateway/swaps.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Chain } from 'viem';
1+
import { Address, Chain } from 'viem';
22
import {
33
ActionsParams,
44
ActionsResponse,
55
PathsParams,
66
PathsResponse,
77
StatusParams,
88
StatusResponse,
9-
TransactionParams,
109
TransactionResponse,
1110
} from './types';
1211

@@ -45,9 +44,9 @@ export class SwapsClient {
4544
return this.getJson(url.toString());
4645
}
4746

48-
async getTransactions(params: TransactionParams): Promise<TransactionResponse> {
47+
async getTransactions(userAddress: Address): Promise<TransactionResponse> {
4948
const url = new URL('getTransactions', this.basePath);
50-
url.search = new URLSearchParams(params as unknown as Record<string, string>).toString();
49+
url.searchParams.set('walletAddress', userAddress);
5150
return this.getJson(url.toString());
5251
}
5352

sdk/src/gateway/types/layerzero.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ export interface EVMToEVMWithLayerZeroOrder {
117117
timestamp: number;
118118
status: EVMToEVMWithLayerZeroOrderStatus;
119119
source: {
120-
eid: number;
120+
chainId: number;
121121
txHash: string;
122122
token: Address;
123123
};
124124
destination: {
125-
eid: number;
125+
chainId: number;
126126
txHash: string;
127127
token: Address;
128128
};

sdk/src/gateway/types/order.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Address, Hex } from 'viem';
22
import { OnrampOrder } from './onramp';
33
import { OfframpOrder } from './offramp';
44
import { EVMToEVMWithLayerZeroOrder } from './layerzero';
5+
import { EVMToEVMWithSwapsOrder } from './swaps';
56

67
export type OrderDetailsRaw = {
78
version: string;
@@ -101,5 +102,5 @@ export type GatewayOrder =
101102
| { type: GatewayOrderType.OfframpWithLayerZero; order: OfframpOrder }
102103
// | { type: GatewayOrderType.OnrampWithSwaps; order: OnrampOrder };
103104
// | { type: GatewayOrderType.OfframpWithSwaps; order: OfframpOrder };
104-
| { type: GatewayOrderType.EVMToEVMWithLayerZero; order: EVMToEVMWithLayerZeroOrder };
105-
// | { type: GatewayOrderType.EVMToEVMWithSwaps; order: EVMToEVMOrder };
105+
| { type: GatewayOrderType.EVMToEVMWithLayerZero; order: EVMToEVMWithLayerZeroOrder }
106+
| { type: GatewayOrderType.EVMToEVMWithSwaps; order: EVMToEVMWithSwapsOrder };

sdk/src/gateway/types/swaps.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ export enum BridgeProtocolId {
491491
type Period = 'day' | 'week' | 'month' | 'allTime';
492492

493493
export interface TransactionParams {
494-
appIds: string; //Comma-separated list of your app IDs. By default, a request to this endpoint will return transactions across all appIds in your org. Each API key has a unique app ID. Multiple app IDs can exist within a single org ID.
494+
appIds: string; // Comma-separated list of your app IDs. By default, a request to this endpoint will return transactions across all appIds in your org. Each API key has a unique app ID. Multiple app IDs can exist within a single org ID.
495495
walletAddress: Address;
496496
chainIds: string; // Comma-separated list of chain IDs to filter by Example: "1,42161,10"
497497
page: number;
@@ -502,15 +502,15 @@ export interface TransactionParams {
502502
}
503503

504504
interface Transaction {
505-
status: 'success';
505+
status: Status;
506506
sender: Address;
507507
srcChainId: number;
508-
dstChainId: 42161;
508+
dstChainId: number;
509509
srcTxHash: Hex;
510510
dstTxHash: Hex;
511511
bridgeDetails: {
512-
isBridge: true;
513-
bridgeTime: 420;
512+
isBridge: boolean;
513+
bridgeTime: number;
514514
txPath: [
515515
{
516516
chainId: number;
@@ -716,35 +716,35 @@ interface Transaction {
716716
];
717717
};
718718
org: {
719-
appId: 'app_123';
720-
affiliateId: 'affiliate_123';
719+
appId: string;
720+
affiliateId: string;
721721
};
722722
usdValue: number;
723723
srcTx: {
724724
toAddress: Address;
725725
txHash: Hex;
726726
chainId: number;
727-
value: '1000000000000000000n';
727+
value: `${number}n`;
728728
timestamp: `${number}`;
729729
paymentToken: {
730-
name: 'Ethereum';
731-
symbol: 'ETH';
732-
decimals: 18;
733-
amount: '1000000000000000000n';
730+
name: string;
731+
symbol: string;
732+
decimals: number;
733+
amount: `${number}n`;
734734
address: Address;
735735
};
736736
};
737737
dstTx: {
738738
toAddress: Address;
739739
txHash: Hex;
740740
chainId: number;
741-
value: '1000000000000000000n';
741+
value: `${number}n`;
742742
timestamp: `${number}`;
743743
paymentToken: {
744-
name: 'Ethereum';
745-
symbol: 'ETH';
746-
decimals: 18;
747-
amount: '1000000000000000000n';
744+
name: string;
745+
symbol: string;
746+
decimals: number;
747+
amount: `${number}n`;
748748
address: Address;
749749
};
750750
};
@@ -816,3 +816,22 @@ export type OfframpWithSwapsExecuteQuoteParams<T = {}> = BaseExecuteQuoteParams<
816816
tx: SwapsTransaction; // Transaction from Swaps API to execute
817817
};
818818
};
819+
820+
// success, pending, requires refund, refunded, failed
821+
export type EVMToEVMWithSwapsOrderStatus = 'pending' | 'success' | 'requires refund' | 'refunded' | 'failed';
822+
823+
export interface EVMToEVMWithSwapsOrder {
824+
amount: bigint;
825+
timestamp: number;
826+
status: EVMToEVMWithSwapsOrderStatus;
827+
source: {
828+
chainId: number;
829+
txHash: string;
830+
token: Address;
831+
};
832+
destination: {
833+
chainId: number;
834+
txHash: string;
835+
token: Address;
836+
};
837+
}

0 commit comments

Comments
 (0)