Skip to content

Commit a5b5b3e

Browse files
authored
refactor: get orders, types and utils (#815)
1 parent 8723665 commit a5b5b3e

File tree

20 files changed

+1402
-956
lines changed

20 files changed

+1402
-956
lines changed

sdk/src/gateway/client.ts

Lines changed: 53 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import {
77
Hash,
88
Hex,
99
isAddress,
10+
isAddressEqual,
1011
maxUint256,
12+
parseEther,
1113
PublicClient,
14+
toHex,
1215
Transport,
1316
Chain as ViemChain,
1417
WalletClient,
1518
zeroAddress,
16-
parseEther,
17-
toHex,
18-
isAddressEqual,
1919
} from 'viem';
2020
import { bob, bobSepolia } from 'viem/chains';
2121
import { EsploraClient } from '../esplora';
@@ -27,16 +27,19 @@ import { ADDRESS_LOOKUP, getTokenAddress, getTokenDecimals, getTokenSlots } from
2727
import {
2828
BitcoinSigner,
2929
BumpFeeParams,
30+
CrossChainOrder,
3031
EnrichedToken,
31-
LayerZeroSendOrder,
3232
ExecuteQuoteParams,
3333
GatewayCreateOrderRequestPayload,
3434
GatewayCreateOrderResponse,
35+
GatewayOrder,
36+
GatewayOrderType,
3537
GatewayStartOrder,
3638
GatewayStrategy,
3739
GatewayStrategyContract,
3840
GatewayTokensInfo,
3941
GetQuoteParams,
42+
LayerZeroMessagesWalletResponse,
4043
OfframpCreateOrderParams,
4144
OfframpLiquidity,
4245
OfframpOrder,
@@ -47,9 +50,9 @@ import {
4750
OnrampFeeBreakdownRaw,
4851
OnrampOrder,
4952
OnrampOrderResponse,
53+
OnrampOrderStatus,
5054
OnrampQuote,
5155
OrderDetailsRaw,
52-
OrderStatus,
5356
StrategyParams,
5457
Token,
5558
UnlockOrderParams,
@@ -62,6 +65,7 @@ import {
6265
convertOrderDetailsToRaw,
6366
formatBtc,
6467
getChainConfig,
68+
getCrossChainStatus,
6569
parseOrderStatus,
6670
slugify,
6771
stripHexPrefix,
@@ -197,7 +201,7 @@ export class GatewayApiClient {
197201
// NOTE: toChain validation is performed inside `getOnrampQuote` method
198202
const data = await this.getOnrampQuote(params);
199203
return {
200-
type: 'onramp',
204+
type: GatewayOrderType.Onramp,
201205
params,
202206
finalOutputSats: data.outputSatoshis,
203207
finalFeeSats: data.feeBreakdown.overallFeeSats,
@@ -214,7 +218,7 @@ export class GatewayApiClient {
214218
}
215219

216220
return {
217-
type: 'offramp',
221+
type: GatewayOrderType.Offramp,
218222
params,
219223
finalOutputSats: data.amountReceiveInSat,
220224
finalFeeSats: data.feeBreakdown.overallFeeSats,
@@ -1205,7 +1209,7 @@ export class GatewayApiClient {
12051209
getTokens,
12061210
getOutputTokens,
12071211
getConfirmations,
1208-
async getStatus(esploraClient: EsploraClient, latestHeight?: number): Promise<OrderStatus> {
1212+
async getStatus(esploraClient: EsploraClient, latestHeight?: number): Promise<OnrampOrderStatus> {
12091213
const confirmations = await getConfirmations(esploraClient, latestHeight);
12101214
const hasEnoughConfirmations = confirmations >= order.txProofDifficultyFactor;
12111215
const data = { confirmations };
@@ -1386,33 +1390,26 @@ export class GatewayApiClient {
13861390
}
13871391

13881392
/**
1389-
* Retrieves all orders (onramp, offramp, and layerzero sends) for a specific user address.
1393+
* Retrieves all orders (onramp, offramp, and crosschain swaps) for a specific user address.
13901394
*
13911395
* @param userAddress The user's EVM address
13921396
* @returns Promise resolving to array of typed orders
13931397
*/
1394-
async getOrders(
1395-
userAddress: Address
1396-
): Promise<
1397-
Array<
1398-
| { type: 'onramp'; order: OnrampOrder }
1399-
| { type: 'offramp'; order: OfframpOrder }
1400-
| { type: 'layerzero-send'; order: LayerZeroSendOrder }
1401-
>
1402-
> {
1403-
const [onrampOrders, offrampOrders, layerZeroSendOrders] = await Promise.all([
1398+
async getOrders(userAddress: Address): Promise<Array<GatewayOrder>> {
1399+
const [onrampOrders, offrampOrders, crossChainSwapOrders] = await Promise.all([
14041400
this.getOnrampOrders(userAddress),
14051401
this.getOfframpOrders(userAddress),
1406-
this.getLayerZeroSendOrders(userAddress),
1402+
this.getCrossChainSwapOrders(userAddress),
14071403
]);
1404+
14081405
return [
1409-
...onrampOrders.map((order) => ({ type: 'onramp' as const, order })),
1410-
...offrampOrders.map((order) => ({ type: 'offramp' as const, order })),
1411-
...layerZeroSendOrders.map((order) => ({ type: 'layerzero-send' as const, order })),
1406+
...onrampOrders.map((order) => ({ type: GatewayOrderType.Onramp as const, order })),
1407+
...offrampOrders.map((order) => ({ type: GatewayOrderType.Offramp as const, order })),
1408+
...crossChainSwapOrders.map((order) => ({ type: GatewayOrderType.CrossChainSwap as const, order })),
14121409
];
14131410
}
14141411

1415-
async getLayerZeroSendOrders(_userAddress: Address): Promise<LayerZeroSendOrder[]> {
1412+
async getCrossChainSwapOrders(_userAddress: Address): Promise<CrossChainOrder[]> {
14161413
const url = new URL(`https://scan.layerzero-api.com/v1/messages/wallet/${_userAddress}`);
14171414

14181415
const response = await this.safeFetch(url.toString(), undefined, 'Failed to fetch LayerZero send orders');
@@ -1422,59 +1419,43 @@ export class GatewayApiClient {
14221419
throw new Error(errorData?.message || 'Failed to fetch LayerZero send orders');
14231420
}
14241421

1425-
const json = (await response.json()) as {
1426-
data?: Array<{
1427-
pathway?: { srcEid?: number | string; dstEid?: number | string };
1428-
source?: {
1429-
status?: string;
1430-
tx?: { txHash?: string | null; blockTimestamp?: number; payload?: string | null };
1431-
};
1432-
destination?: {
1433-
status?: string;
1434-
tx?: { txHash?: string | null; blockTimestamp?: number };
1435-
lzCompose?: { status?: string };
1436-
};
1437-
}>;
1438-
};
1422+
const json: LayerZeroMessagesWalletResponse = await response.json();
14391423

1440-
const items = (json.data ?? []).filter((item) => item.destination?.lzCompose?.status === 'N/A');
1441-
1442-
return items.map((item) => {
1443-
const sourceStatus = item.source?.status ?? 'UNKNOWN';
1444-
const destinationStatus = item.destination?.status ?? 'UNKNOWN';
1445-
const sourceTxHash = item.source?.tx?.txHash ?? null;
1446-
const destinationTxHash = item.destination?.tx?.txHash ?? null;
1447-
const orderTimestamp = item.source?.tx?.blockTimestamp ?? null;
1448-
const payload = item.source?.tx?.payload ?? null;
1449-
let orderSize = 0n;
1450-
if (payload && typeof payload === 'string') {
1451-
const hex = payload.startsWith('0x') ? payload.slice(2) : payload;
1452-
if (hex.length >= 16) {
1453-
const last16 = hex.slice(-16);
1454-
try {
1455-
orderSize = BigInt('0x' + last16);
1456-
} catch {
1457-
orderSize = 0n;
1458-
}
1424+
const items = json.data.filter((item) => item.destination.lzCompose.status === 'N/A');
1425+
1426+
return items.map((item): CrossChainOrder => {
1427+
const { payload, blockTimestamp, txHash: sourceTxHash } = item.source.tx;
1428+
const { txHash: destinationTxHash } = item.destination.tx;
1429+
1430+
let amount = 0n;
1431+
1432+
const hex = payload.startsWith('0x') ? payload.slice(2) : payload;
1433+
if (hex.length >= 16) {
1434+
const last16 = hex.slice(-16);
1435+
try {
1436+
amount = BigInt('0x' + last16);
1437+
} catch {
1438+
amount = 0n;
14591439
}
14601440
}
1461-
const srcEidRaw = item.pathway?.srcEid ?? null;
1462-
const dstEidRaw = item.pathway?.dstEid ?? null;
1463-
const sourceEid =
1464-
srcEidRaw === null ? null : typeof srcEidRaw === 'string' ? parseInt(srcEidRaw, 10) : srcEidRaw;
1465-
const destinationEid =
1466-
dstEidRaw === null ? null : typeof dstEidRaw === 'string' ? parseInt(dstEidRaw, 10) : dstEidRaw;
14671441

14681442
return {
1469-
orderSize,
1470-
orderTimestamp,
1471-
sourceEid,
1472-
sourceTxHash,
1473-
sourceStatus,
1474-
destinationTxHash,
1475-
destinationEid,
1476-
destinationStatus,
1477-
} as LayerZeroSendOrder;
1443+
amount,
1444+
timestamp: blockTimestamp,
1445+
status: getCrossChainStatus(item),
1446+
source: {
1447+
eid: item.pathway.srcEid,
1448+
1449+
txHash: sourceTxHash,
1450+
token: item.pathway.sender.address as Address,
1451+
},
1452+
destination: {
1453+
eid: item.pathway.dstEid,
1454+
1455+
txHash: destinationTxHash,
1456+
token: item.pathway.receiver.address as Address,
1457+
},
1458+
};
14781459
});
14791460
}
14801461

sdk/src/gateway/index.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1+
export { OkxWalletAdapter } from './adapters/okx-wallet';
2+
export { ReownWalletAdapter } from './adapters/reown';
13
export { GatewayApiClient as GatewaySDK } from './client';
4+
export { LayerZeroGatewayClient } from './layerzero';
25
export {
6+
CrossChainOrder,
7+
CrossChainOrderStatus,
8+
CrossChainSwapQuote,
9+
CrossChainSwapQuoteParams,
10+
ExecuteQuoteParams,
311
GatewayQuoteParams,
4-
OnrampOrder,
512
GatewayStrategyContract,
6-
OrderStatus,
7-
LayerZeroSendQuoteParams,
813
GetQuoteParams,
9-
ExecuteQuoteParams,
14+
OfframpOrder,
15+
OnrampOrder,
16+
OnrampOrderStatus,
1017
} from './types';
1118
export { parseBtc } from './utils';
12-
export { ReownWalletAdapter } from './adapters/reown';
13-
export { OkxWalletAdapter } from './adapters/okx-wallet';
14-
export { LayerZeroGatewayClient, LayerZeroSendClient } from './layerzero';

0 commit comments

Comments
 (0)