@coin-voyage/shared 2.4.3-beta.0 → 2.4.4-beta.0

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.
@@ -1,3 +1,4 @@
1
+ import { CurrencyBase } from "../types";
1
2
  export declare const FIAT_CURRENCIES: readonly ["USD", "EUR"];
2
3
  export type FiatCurrency = (typeof FIAT_CURRENCIES)[number];
3
4
  export interface CurrencyExchangeRate {
@@ -14,3 +15,4 @@ export declare const nonUsdCurrencies: {
14
15
  currency: string;
15
16
  decimals: number;
16
17
  }[];
18
+ export declare function currencyID(currency: CurrencyBase): string;
@@ -25,3 +25,6 @@ export const nonUsdCurrencies = data.map(([name, symbol, currency, decimals]) =>
25
25
  currency,
26
26
  decimals,
27
27
  }));
28
+ export function currencyID(currency) {
29
+ return currency.address ? `${currency.address}:${currency.chain_id}` : currency.chain_id.toString();
30
+ }
@@ -13,7 +13,7 @@ export interface ChainMetadata {
13
13
  name: string;
14
14
  logoURI: string;
15
15
  }
16
- interface ChainEntry {
16
+ export interface ChainEntry {
17
17
  chainId: ChainId;
18
18
  name: string;
19
19
  logoURI: string;
@@ -23,14 +23,14 @@ interface ChainEntry {
23
23
  } | null;
24
24
  tokens: Token[];
25
25
  }
26
- interface TokenListResponse {
26
+ export interface TokenListResponse {
27
27
  chains: ChainEntry[];
28
28
  }
29
29
  export declare function tokenToCurrency(token: Token): Currency;
30
30
  /**
31
31
  * Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
32
32
  */
33
- export declare function fetchTokenList(): Promise<TokenListResponse>;
33
+ export declare function fetchTokenList(url?: string, init?: RequestInit): Promise<TokenListResponse>;
34
34
  /**
35
35
  * Returns all chains from the token list.
36
36
  */
@@ -38,9 +38,8 @@ export declare function getChains(chains: ChainEntry[]): ChainMetadata[];
38
38
  /**
39
39
  * Returns all tokens for the given chainId.
40
40
  */
41
- export declare function tokensByChainId(chains: ChainEntry[], chainId: ChainId): Token[];
41
+ export declare function tokensByChainId(chains: readonly ChainEntry[], chainId: ChainId): Token[];
42
42
  /**
43
43
  * Returns the token for the given chainId and address (undefined for native asset).
44
44
  */
45
- export declare function getToken(chains: ChainEntry[], currency: CurrencyBase): Token | undefined;
46
- export {};
45
+ export declare function getToken(chains: readonly ChainEntry[], currency: CurrencyBase): Token | undefined;
@@ -1,6 +1,9 @@
1
+ import { currencyID } from "./currencies";
1
2
  const TOKEN_LIST_URL = "https://raw.githubusercontent.com/coin-voyage/token-list/main/tokenlist.json";
3
+ const normalizeAddress = (address) => address?.toLowerCase() ?? null;
2
4
  export function tokenToCurrency(token) {
3
5
  return {
6
+ id: currencyID({ chain_id: token.chainId, address: token.address }),
4
7
  chain_id: token.chainId,
5
8
  address: token.address,
6
9
  name: token.name,
@@ -12,33 +15,35 @@ export function tokenToCurrency(token) {
12
15
  /**
13
16
  * Fetches the token list from the TOKEN_LIST_URL and returns it as a structured object.
14
17
  */
15
- export async function fetchTokenList() {
16
- const res = await fetch(TOKEN_LIST_URL);
18
+ export async function fetchTokenList(url = TOKEN_LIST_URL, init) {
19
+ const res = await fetch(url, init);
17
20
  if (!res.ok)
18
- throw new Error(`Failed to fetch token list: ${res.status}`);
19
- return (await res.json());
21
+ throw new Error(`Failed to fetch token list: ${res.status} ${res.statusText}`);
22
+ return res.json();
20
23
  }
21
24
  /**
22
25
  * Returns all chains from the token list.
23
26
  */
24
27
  export function getChains(chains) {
25
- return chains.map((c) => ({
26
- chainId: c.chainId,
27
- name: c.name,
28
- logoURI: c.logoURI,
28
+ return chains.map(({ chainId, name, logoURI }) => ({
29
+ chainId,
30
+ name,
31
+ logoURI,
29
32
  }));
30
33
  }
31
34
  /**
32
35
  * Returns all tokens for the given chainId.
33
36
  */
34
37
  export function tokensByChainId(chains, chainId) {
35
- const chain = chains.find((c) => c.chainId === chainId);
36
- return chain?.tokens ?? [];
38
+ return chains.find((chain) => chain.chainId === chainId)?.tokens ?? [];
37
39
  }
38
40
  /**
39
41
  * Returns the token for the given chainId and address (undefined for native asset).
40
42
  */
41
43
  export function getToken(chains, currency) {
42
- const tokens = tokensByChainId(chains, currency.chain_id);
43
- return tokens.find((t) => !currency.address ? !t.address : t.address?.toLowerCase() === currency.address.toLowerCase());
44
+ const currencyAddress = normalizeAddress(currency.address);
45
+ return tokensByChainId(chains, currency.chain_id).find((token) => {
46
+ const tokenAddress = normalizeAddress(token.address);
47
+ return tokenAddress === currencyAddress;
48
+ });
44
49
  }
@@ -1,4 +1,9 @@
1
- import type { FiatPaymentData, PaymentDataBase, PaymentStep } from "../types/model";
1
+ import type { CryptoPaymentData, DepositStepData, FiatPaymentData, PaymentDataBase, PaymentStep } from "../types/model";
2
+ type PaymentStepData = DepositStepData | CryptoPaymentData | FiatPaymentData;
3
+ export declare function isDepositStepData(data: PaymentStepData): data is DepositStepData;
4
+ export declare function isCryptoPaymentData(data: PaymentStepData): data is CryptoPaymentData;
5
+ export declare function isFiatPaymentData(data: PaymentStepData): data is FiatPaymentData;
2
6
  export declare function getFiatPaymentData(payment?: PaymentDataBase): FiatPaymentData | undefined;
3
7
  export declare function getPaymentStep(payment?: PaymentDataBase, stepIndex?: number): PaymentStep | undefined;
4
8
  export declare function getDepositAddress(payment?: PaymentDataBase, stepIndex?: number): string | undefined;
9
+ export {};
@@ -1,9 +1,18 @@
1
1
  import { PaymentRail, StepKind } from "../types/enums";
2
+ export function isDepositStepData(data) {
3
+ return Boolean("amount" in data && "currency" in data && "deposit_address" in data);
4
+ }
5
+ export function isCryptoPaymentData(data) {
6
+ return Boolean(!isDepositStepData(data) && !isFiatPaymentData(data));
7
+ }
8
+ export function isFiatPaymentData(data) {
9
+ return Boolean("session_id" in data);
10
+ }
2
11
  export function getFiatPaymentData(payment) {
3
12
  const step = getPaymentStep(payment);
4
13
  if (step?.rail !== PaymentRail.FIAT || step.kind !== StepKind.KIND_STRIPE_ONRAMP)
5
14
  return undefined;
6
- return step.data?.fiat;
15
+ return isFiatPaymentData(step.data) ? step.data : undefined;
7
16
  }
8
17
  export function getPaymentStep(payment, stepIndex) {
9
18
  const steps = payment?.steps ?? [];
@@ -14,11 +23,9 @@ export function getPaymentStep(payment, stepIndex) {
14
23
  }
15
24
  export function getDepositAddress(payment, stepIndex) {
16
25
  const step = getPaymentStep(payment, stepIndex);
17
- if (step?.kind === StepKind.KIND_DEPOSIT) {
18
- return step.deposit_address;
19
- }
20
- if (step?.kind === StepKind.KIND_TRANSACTION) {
26
+ if (step?.kind !== StepKind.KIND_DEPOSIT) {
21
27
  return undefined;
22
28
  }
23
- return payment?.deposit_address;
29
+ const data = isDepositStepData(step.data) ? step.data : undefined;
30
+ return data?.deposit_address;
24
31
  }
@@ -55,10 +55,6 @@ export type PayOrderIntent = {
55
55
  * Optional receiving address to fulfill the order to. If not provided, a settlement address will be selected.
56
56
  */
57
57
  receiving_address?: string;
58
- /**
59
- * Optional custom fee in basis points.
60
- */
61
- custom_fee_bps?: number;
62
58
  };
63
59
  export type IntentAmount = {
64
60
  /**
@@ -86,6 +82,7 @@ export type ClaimFeesRequest = {
86
82
  export type ClaimFeesResponse = {
87
83
  claim_order_id: string;
88
84
  source_transaction_hash: string;
85
+ transaction_hash: string;
89
86
  source_chain_id: ChainId;
90
87
  currency: CurrencyAmount;
91
88
  };
@@ -1,7 +1,7 @@
1
1
  import { z } from "zod";
2
2
  import { FiatCurrency } from "../currency/currencies";
3
3
  import { zPayOrderMetadata, zPayOrderSettings } from "../schemas/pay-order";
4
- import { ChainId, PaymentRail, PayOrderMode, PayOrderStatus, ProviderStatus, StepKind } from "./enums";
4
+ import { ChainId, PaymentRail, PayOrderMode, PayOrderStatus, StepKind } from "./enums";
5
5
  export type PayOrder = {
6
6
  id: string;
7
7
  organization_id: string;
@@ -23,7 +23,7 @@ export type ParsedPayOrderSettings = z.output<typeof zPayOrderSettings> & Settin
23
23
  export type PayOrderSettings = z.infer<typeof zPayOrderSettings> & SettingsExtraFields;
24
24
  export type OrganizationSettings = ParsedPayOrderSettings;
25
25
  export interface Currency extends CurrencyBase {
26
- id?: string;
26
+ id: string;
27
27
  name: string;
28
28
  ticker: string;
29
29
  decimals: number;
@@ -45,7 +45,7 @@ export interface QuoteWithCurrency extends CurrencyWithAmount {
45
45
  }
46
46
  export interface CurrencyWithBalance extends CurrencyWithAmount {
47
47
  owner?: string;
48
- balance?: CurrencyAmount;
48
+ balance: CurrencyAmount;
49
49
  }
50
50
  export interface CurrencyAmount {
51
51
  ui_amount: number;
@@ -141,12 +141,21 @@ export type FulfillmentData = {
141
141
  rate_usd?: string;
142
142
  receiving_address?: string;
143
143
  custom_fee_bps?: number;
144
+ swap?: SwapContext;
145
+ };
146
+ export type SwapContext = {
147
+ requested_destination_currency?: CurrencyBase;
148
+ requested_source_currency?: CurrencyBase;
149
+ };
150
+ export type DepositStepData = {
151
+ deposit_address: string;
152
+ currency: CurrencyBase;
153
+ amount: BigIntStr;
144
154
  };
145
155
  export type PaymentDataBase = {
146
- payment_rail: PaymentRail;
156
+ payment_rail?: PaymentRail;
147
157
  src: QuoteWithCurrency;
148
158
  dst: CurrencyWithAmount;
149
- deposit_address: string;
150
159
  receiving_address: string;
151
160
  refund_address?: string;
152
161
  steps: PaymentStep[];
@@ -161,38 +170,11 @@ export type PaymentData = PaymentDataBase & {
161
170
  export type PaymentStep = {
162
171
  rail: PaymentRail;
163
172
  kind: StepKind;
164
- deposit_address?: string;
165
- data?: PaymentStepData;
173
+ data: DepositStepData | CryptoPaymentData | FiatPaymentData;
166
174
  };
167
175
  export type PaymentStepData = {
168
176
  crypto?: CryptoPaymentData;
169
177
  fiat?: FiatPaymentData;
170
178
  };
171
- export type ExecutionStep = {
172
- id: string;
173
- status: ProviderStatus;
174
- provider: string;
175
- receiver: string;
176
- deposit_address: string;
177
- source_tx_hash?: string;
178
- destination_tx_hash?: string;
179
- error?: string;
180
- cleanup_tx_hash?: Record<string, string>;
181
- cleanup_error?: string;
182
- cleanup_recipient?: string;
183
- source_currency: CurrencyWithAmount;
184
- destination_currency: CurrencyWithAmount;
185
- chain_data?: CryptoPaymentData;
186
- gas?: CurrencyWithAmount;
187
- fees?: FeeCurrencyBreakdown;
188
- fee_tx_hash?: string;
189
- fee_error?: string;
190
- fee_plan?: FeePlan;
191
- price_impact?: number;
192
- };
193
- export interface FeePlan extends CurrencyWithAmount {
194
- placement: string;
195
- fee_bps?: number;
196
- }
197
179
  export type BigIntStr = `${bigint}`;
198
180
  export {};
@@ -1,4 +1,3 @@
1
1
  export declare function capitalize(str: string): string;
2
2
  export declare function truncateAddress(address?: string, length?: number): string;
3
3
  export declare function truncateENSName(ensName: string, maxLength: number): string;
4
- export declare function nFormatter(num: number, digits?: number): string;
@@ -13,23 +13,3 @@ export function truncateENSName(ensName, maxLength) {
13
13
  }
14
14
  return ensName;
15
15
  }
16
- export function nFormatter(num, digits = 2) {
17
- if (num < 10000) {
18
- return num.toFixed(2);
19
- }
20
- const lookup = [
21
- { value: 1, symbol: "" },
22
- { value: 1e3, symbol: "k" },
23
- { value: 1e6, symbol: "m" },
24
- { value: 1e9, symbol: "g" },
25
- { value: 1e12, symbol: "t" },
26
- { value: 1e15, symbol: "p" },
27
- { value: 1e18, symbol: "e" },
28
- ];
29
- const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
30
- const item = lookup
31
- .slice()
32
- .reverse()
33
- .find((item) => num >= item.value);
34
- return item ? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol : "0";
35
- }
@@ -1 +1 @@
1
- export declare const withPlural: (totalQuantity: number, singular: string, plural: string) => string;
1
+ export declare const withPlural: (quantity: number, singular: string, plural: string) => string;
@@ -1,6 +1 @@
1
- export const withPlural = (totalQuantity, singular, plural) => {
2
- if (totalQuantity == 1)
3
- return `${totalQuantity} ${singular}`;
4
- else
5
- return `${totalQuantity} ${plural}`;
6
- };
1
+ export const withPlural = (quantity, singular, plural) => `${quantity} ${quantity === 1 ? singular : plural}`;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coin-voyage/shared",
3
3
  "description": "Shared utilities for Coin Voyage",
4
- "version": "2.4.3-beta.0",
4
+ "version": "2.4.4-beta.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "exports": {