@orderly.network/ui-transfer 3.1.0 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -279,25 +279,29 @@ declare const useConvertFormScript: (options: ConvertFormScriptOptions) => {
279
279
  address: string | undefined;
280
280
  quantity: string;
281
281
  onQuantityChange: (qty: string) => void;
282
- token: (_orderly_network_types.API.Chain & {
282
+ token: (_orderly_network_types.API.Token & {
283
283
  contract_address: string;
284
284
  quoteChainId: string;
285
285
  precision: number;
286
+ symbol: string;
286
287
  }) | undefined;
287
- sourceTokens: (_orderly_network_types.API.Chain & {
288
+ sourceTokens: (_orderly_network_types.API.Token & {
288
289
  contract_address: string;
289
290
  quoteChainId: string;
290
291
  precision: number;
292
+ symbol: string;
291
293
  })[];
292
- onSourceTokenChange: react.Dispatch<react.SetStateAction<(_orderly_network_types.API.Chain & {
294
+ onSourceTokenChange: react.Dispatch<react.SetStateAction<(_orderly_network_types.API.Token & {
293
295
  contract_address: string;
294
296
  quoteChainId: string;
295
297
  precision: number;
298
+ symbol: string;
296
299
  }) | undefined>>;
297
- targetToken: (_orderly_network_types.API.Chain & {
300
+ targetToken: (_orderly_network_types.API.Token & {
298
301
  contract_address: string;
299
302
  quoteChainId: string;
300
303
  precision: number;
304
+ symbol: string;
301
305
  }) | undefined;
302
306
  balanceRevalidating: boolean;
303
307
  maxQuantity: number;
@@ -317,16 +321,16 @@ declare const useConvertFormScript: (options: ConvertFormScriptOptions) => {
317
321
  currentLTV: number;
318
322
  nextLTV: number;
319
323
  targetChainInfo: {
324
+ contract_address: string;
320
325
  precision: number | undefined;
321
- chain_id?: string | undefined;
322
- contract_address?: string | undefined;
323
- decimals?: number | undefined;
326
+ chain_id: string;
327
+ decimals: number;
324
328
  exclusive_deposit_supported?: boolean;
325
- withdrawal_fee?: number | undefined;
329
+ withdrawal_fee: number;
326
330
  chain_name?: string;
327
- cross_chain_withdrawal_fee?: number | undefined;
328
- display_name?: string | undefined;
329
- };
331
+ cross_chain_withdrawal_fee: number;
332
+ display_name: string;
333
+ } | undefined;
330
334
  };
331
335
 
332
336
  type ConvertFormProps = ConvertFormScriptReturn;
package/dist/index.d.ts CHANGED
@@ -279,25 +279,29 @@ declare const useConvertFormScript: (options: ConvertFormScriptOptions) => {
279
279
  address: string | undefined;
280
280
  quantity: string;
281
281
  onQuantityChange: (qty: string) => void;
282
- token: (_orderly_network_types.API.Chain & {
282
+ token: (_orderly_network_types.API.Token & {
283
283
  contract_address: string;
284
284
  quoteChainId: string;
285
285
  precision: number;
286
+ symbol: string;
286
287
  }) | undefined;
287
- sourceTokens: (_orderly_network_types.API.Chain & {
288
+ sourceTokens: (_orderly_network_types.API.Token & {
288
289
  contract_address: string;
289
290
  quoteChainId: string;
290
291
  precision: number;
292
+ symbol: string;
291
293
  })[];
292
- onSourceTokenChange: react.Dispatch<react.SetStateAction<(_orderly_network_types.API.Chain & {
294
+ onSourceTokenChange: react.Dispatch<react.SetStateAction<(_orderly_network_types.API.Token & {
293
295
  contract_address: string;
294
296
  quoteChainId: string;
295
297
  precision: number;
298
+ symbol: string;
296
299
  }) | undefined>>;
297
- targetToken: (_orderly_network_types.API.Chain & {
300
+ targetToken: (_orderly_network_types.API.Token & {
298
301
  contract_address: string;
299
302
  quoteChainId: string;
300
303
  precision: number;
304
+ symbol: string;
301
305
  }) | undefined;
302
306
  balanceRevalidating: boolean;
303
307
  maxQuantity: number;
@@ -317,16 +321,16 @@ declare const useConvertFormScript: (options: ConvertFormScriptOptions) => {
317
321
  currentLTV: number;
318
322
  nextLTV: number;
319
323
  targetChainInfo: {
324
+ contract_address: string;
320
325
  precision: number | undefined;
321
- chain_id?: string | undefined;
322
- contract_address?: string | undefined;
323
- decimals?: number | undefined;
326
+ chain_id: string;
327
+ decimals: number;
324
328
  exclusive_deposit_supported?: boolean;
325
- withdrawal_fee?: number | undefined;
329
+ withdrawal_fee: number;
326
330
  chain_name?: string;
327
- cross_chain_withdrawal_fee?: number | undefined;
328
- display_name?: string | undefined;
329
- };
331
+ cross_chain_withdrawal_fee: number;
332
+ display_name: string;
333
+ } | undefined;
330
334
  };
331
335
 
332
336
  type ConvertFormProps = ConvertFormScriptReturn;
package/dist/index.js CHANGED
@@ -7177,6 +7177,7 @@ var SwapCoin = (props) => {
7177
7177
  }
7178
7178
  );
7179
7179
  };
7180
+ var nativeTokenAddress2 = "0x0000000000000000000000000000000000000000";
7180
7181
  var splitTokenBySymbol = (items) => {
7181
7182
  return items.reduce(
7182
7183
  (result, item) => {
@@ -7190,14 +7191,13 @@ var splitTokenBySymbol = (items) => {
7190
7191
  { usdc: [], others: [] }
7191
7192
  );
7192
7193
  };
7193
- var findChainInfo = (tokenInfo) => {
7194
+ var findMainnetQuoteChainInfo = (tokenInfo) => {
7194
7195
  const arbitrumChainInfo = tokenInfo.chain_details.find(
7195
7196
  (item) => parseInt(item.chain_id) === types.Arbitrum.id
7196
7197
  );
7197
7198
  const nativeTokenChainInfo = tokenInfo.chain_details.find(
7198
7199
  (item) => !item.contract_address
7199
7200
  );
7200
- const nativeTokenAddress2 = "0x0000000000000000000000000000000000000000";
7201
7201
  if (arbitrumChainInfo) {
7202
7202
  return {
7203
7203
  contract_address: arbitrumChainInfo.contract_address || nativeTokenAddress2,
@@ -7218,23 +7218,49 @@ var findChainInfo = (tokenInfo) => {
7218
7218
  decimals: tokenInfo.chain_details[0]?.decimals
7219
7219
  };
7220
7220
  };
7221
+ var getMainnetConvertTokenInfo = (tokenInfo) => {
7222
+ const chainInfo = findMainnetQuoteChainInfo(tokenInfo);
7223
+ if (!chainInfo) {
7224
+ return void 0;
7225
+ }
7226
+ return {
7227
+ ...tokenInfo,
7228
+ symbol: tokenInfo.token,
7229
+ precision: tokenInfo.decimals ?? 6,
7230
+ ...chainInfo
7231
+ };
7232
+ };
7233
+ var findQuoteTargetChainInfo = (targetToken, quoteChainId) => {
7234
+ const info = targetToken?.chain_details?.find(
7235
+ (item) => item.chain_id === quoteChainId
7236
+ );
7237
+ if (!info) {
7238
+ return void 0;
7239
+ }
7240
+ return {
7241
+ ...info,
7242
+ contract_address: info.contract_address || nativeTokenAddress2,
7243
+ precision: targetToken?.precision
7244
+ };
7245
+ };
7221
7246
  var useToken3 = (options2) => {
7222
7247
  const { defaultValue } = options2;
7223
7248
  const [sourceToken, setSourceToken] = react.useState();
7224
7249
  const [targetToken, setTargetToken] = react.useState();
7225
7250
  const [sourceTokens, setSourceTokens] = react.useState([]);
7226
- const tokensInfo = hooks.useTokensInfo();
7251
+ const tokensInfo = hooks.useMainTokenStore((state) => state.data);
7227
7252
  const newTokensInfo = react.useMemo(() => {
7228
- const filteredTokensInfo = tokensInfo.filter((item) => item.on_chain_swap);
7229
- return filteredTokensInfo.map((item) => {
7230
- const chainInfo = findChainInfo(item);
7231
- return {
7232
- ...item,
7233
- symbol: item.token,
7234
- precision: item.decimals ?? 6,
7235
- ...chainInfo
7236
- };
7237
- });
7253
+ const filteredTokensInfo = (tokensInfo ?? []).filter(
7254
+ (item) => item.on_chain_swap
7255
+ );
7256
+ return filteredTokensInfo.reduce((result, item) => {
7257
+ const tokenInfo = getMainnetConvertTokenInfo(item);
7258
+ if (!tokenInfo) {
7259
+ return result;
7260
+ }
7261
+ result.push(tokenInfo);
7262
+ return result;
7263
+ }, []);
7238
7264
  }, [tokensInfo]);
7239
7265
  react.useEffect(() => {
7240
7266
  const { usdc, others } = splitTokenBySymbol(newTokensInfo);
@@ -7249,13 +7275,7 @@ var useToken3 = (options2) => {
7249
7275
  setTargetToken(usdc[0]);
7250
7276
  }, [defaultValue, newTokensInfo]);
7251
7277
  const targetChainInfo = react.useMemo(() => {
7252
- const info = targetToken?.chain_details?.find(
7253
- (item) => item.chain_id === sourceToken?.quoteChainId
7254
- );
7255
- return {
7256
- ...info,
7257
- precision: targetToken?.precision
7258
- };
7278
+ return findQuoteTargetChainInfo(targetToken, sourceToken?.quoteChainId);
7259
7279
  }, [sourceToken, targetToken]);
7260
7280
  return {
7261
7281
  sourceToken,
@@ -7269,6 +7289,7 @@ var useToken3 = (options2) => {
7269
7289
  // src/components/convertForm/convertForm.script.tsx
7270
7290
  var { calcMinimumReceived } = perp.account;
7271
7291
  var ORDERLY_CONVERT_SLIPPAGE_KEY = "orderly_convert_slippage";
7292
+ var ODOS_QUOTE_DEBOUNCE_MS = 300;
7272
7293
  var normalizeAmount = (amount, decimals) => {
7273
7294
  return new utils.Decimal(amount).mul(new utils.Decimal(10).pow(decimals)).toFixed(0);
7274
7295
  };
@@ -7326,16 +7347,26 @@ var useConvertFormScript = (options2) => {
7326
7347
  setLoading(false);
7327
7348
  });
7328
7349
  };
7329
- const [postQuote, { data: quoteData, isMutating: isQuoteLoading }] = hooks.useOdosQuote();
7330
- react.useEffect(() => {
7350
+ const [
7351
+ postQuote,
7352
+ { data: quoteData, reset: resetQuote, isMutating: isQuoteLoading }
7353
+ ] = hooks.useOdosQuote();
7354
+ const quoteRequest = react.useMemo(() => {
7331
7355
  const { quoteChainId, contract_address, decimals } = sourceToken || {};
7332
7356
  const targetAddress = targetChainInfo?.contract_address;
7333
- if (quantity && quoteChainId && contract_address && targetAddress) {
7334
- postQuote({
7357
+ if (!quantity || new utils.Decimal(quantity).lte(0) || !quoteChainId || !contract_address || typeof decimals === "undefined" || !targetAddress || !address) {
7358
+ return null;
7359
+ }
7360
+ const inputAmount = normalizeAmount(quantity, decimals);
7361
+ return {
7362
+ inputAmount,
7363
+ inputTokenAddress: contract_address.toLowerCase(),
7364
+ outputTokenAddress: targetAddress.toLowerCase(),
7365
+ body: {
7335
7366
  chainId: parseInt(quoteChainId),
7336
7367
  inputTokens: [
7337
7368
  {
7338
- amount: normalizeAmount(quantity, decimals),
7369
+ amount: inputAmount,
7339
7370
  tokenAddress: contract_address
7340
7371
  }
7341
7372
  ],
@@ -7344,40 +7375,98 @@ var useConvertFormScript = (options2) => {
7344
7375
  proportion: 1,
7345
7376
  tokenAddress: targetAddress
7346
7377
  }
7347
- ]
7378
+ ],
7379
+ userAddr: address
7348
7380
  // simple: true,
7381
+ }
7382
+ };
7383
+ }, [
7384
+ quantity,
7385
+ sourceToken?.quoteChainId,
7386
+ sourceToken?.contract_address,
7387
+ sourceToken?.decimals,
7388
+ targetChainInfo?.contract_address,
7389
+ targetChainInfo?.decimals,
7390
+ address
7391
+ ]);
7392
+ react.useEffect(() => {
7393
+ resetQuote();
7394
+ if (!quoteRequest) {
7395
+ return;
7396
+ }
7397
+ let active = true;
7398
+ const timer = window.setTimeout(() => {
7399
+ postQuote(quoteRequest.body).catch((error) => {
7400
+ if (!active) {
7401
+ return;
7402
+ }
7403
+ let message = t("transfer.convert.failed");
7404
+ if (error instanceof Error) {
7405
+ message = error.message;
7406
+ } else if (error) {
7407
+ message = String(error);
7408
+ }
7409
+ if (message === "Failed to fetch") {
7410
+ message = t("transfer.convert.failed");
7411
+ }
7412
+ console.error("[convertForm] Odos quote failed:", error);
7413
+ ui.toast.error(message);
7414
+ resetQuote();
7349
7415
  });
7416
+ }, ODOS_QUOTE_DEBOUNCE_MS);
7417
+ return () => {
7418
+ active = false;
7419
+ window.clearTimeout(timer);
7420
+ };
7421
+ }, [postQuote, quoteRequest, resetQuote, t]);
7422
+ const isQuoteDataMatched = react.useMemo(() => {
7423
+ if (!quoteData || !quoteRequest) {
7424
+ return false;
7425
+ }
7426
+ const inAmount = quoteData?.inAmounts?.[0]?.toString();
7427
+ const inToken = quoteData?.inTokens?.[0]?.toLowerCase();
7428
+ const outToken = quoteData?.outTokens?.[0]?.toLowerCase();
7429
+ return inAmount === quoteRequest.inputAmount && inToken === quoteRequest.inputTokenAddress && outToken === quoteRequest.outputTokenAddress;
7430
+ }, [quoteData, quoteRequest]);
7431
+ react.useEffect(() => {
7432
+ if (quoteData && !isQuoteDataMatched) {
7433
+ resetQuote();
7350
7434
  }
7351
- }, [quantity, sourceToken, targetChainInfo, postQuote]);
7435
+ }, [isQuoteDataMatched, quoteData, resetQuote]);
7352
7436
  const memoizedOutAmounts = react.useMemo(() => {
7353
- if (!quoteData || isQuoteLoading) {
7354
- return "-";
7437
+ if (quoteData && !isQuoteLoading && isQuoteDataMatched) {
7438
+ return quoteData?.outAmounts[0];
7355
7439
  }
7356
- return quoteData?.outAmounts[0];
7357
- }, [quoteData, isQuoteLoading]);
7440
+ return "-";
7441
+ }, [quoteData, isQuoteDataMatched, isQuoteLoading]);
7358
7442
  const memoizedConvertRate = react.useMemo(() => {
7359
- if (!quoteData || isQuoteLoading) {
7360
- return "-";
7443
+ if (quoteData && !isQuoteLoading && isQuoteDataMatched) {
7444
+ return new utils.Decimal(
7445
+ unnormalizeAmount(
7446
+ quoteData.outAmounts[0],
7447
+ targetChainInfo?.decimals ?? 6
7448
+ )
7449
+ ).div(
7450
+ unnormalizeAmount(quoteData.inAmounts[0], sourceToken?.decimals ?? 6)
7451
+ ).toString();
7361
7452
  }
7362
- const rate = new utils.Decimal(
7363
- unnormalizeAmount(
7364
- quoteData.outAmounts[0],
7365
- targetChainInfo?.decimals ?? 6
7366
- )
7367
- ).div(
7368
- unnormalizeAmount(quoteData.inAmounts[0], sourceToken?.decimals ?? 6)
7369
- ).toString();
7370
- return rate;
7371
- }, [isQuoteLoading, quoteData, sourceToken, targetChainInfo]);
7453
+ return "-";
7454
+ }, [
7455
+ isQuoteDataMatched,
7456
+ isQuoteLoading,
7457
+ quoteData,
7458
+ sourceToken,
7459
+ targetChainInfo
7460
+ ]);
7372
7461
  const memoizedMinimumReceived = react.useMemo(() => {
7373
- if (!quoteData || isQuoteLoading) {
7462
+ if (!quoteData || isQuoteLoading || !isQuoteDataMatched) {
7374
7463
  return 0;
7375
7464
  }
7376
7465
  return calcMinimumReceived({
7377
7466
  amount: quoteData?.outAmounts[0],
7378
7467
  slippage: Number(slippage)
7379
7468
  });
7380
- }, [quoteData, isQuoteLoading, slippage]);
7469
+ }, [quoteData, isQuoteDataMatched, isQuoteLoading, slippage]);
7381
7470
  const currentLtv = hooks.useComputedLTV();
7382
7471
  const nextLTV = hooks.useComputedLTV({
7383
7472
  input: Number(quantity),