@coin-voyage/shared 0.0.1 → 0.0.3

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.
Files changed (97) hide show
  1. package/dist/api/config.d.ts +1 -0
  2. package/dist/api/config.js +3 -0
  3. package/dist/api/config.js.map +1 -0
  4. package/dist/api/fetcher.d.ts +7 -0
  5. package/{src/api/fetcher.ts → dist/api/fetcher.js} +10 -17
  6. package/dist/api/fetcher.js.map +1 -0
  7. package/dist/api/index.d.ts +3 -0
  8. package/dist/api/index.js +4 -0
  9. package/dist/api/index.js.map +1 -0
  10. package/{src/api/pay-order.ts → dist/api/pay-order.d.ts} +114 -230
  11. package/dist/api/pay-order.js +238 -0
  12. package/dist/api/pay-order.js.map +1 -0
  13. package/dist/common/assert.d.ts +3 -0
  14. package/dist/common/assert.js +14 -0
  15. package/dist/common/assert.js.map +1 -0
  16. package/dist/common/chainExplorer.d.ts +2 -0
  17. package/dist/common/chainExplorer.js +26 -0
  18. package/dist/common/chainExplorer.js.map +1 -0
  19. package/dist/common/chains.d.ts +31 -0
  20. package/dist/common/chains.js +121 -0
  21. package/dist/common/chains.js.map +1 -0
  22. package/dist/common/currencies.d.ts +14 -0
  23. package/dist/common/currencies.js +31 -0
  24. package/dist/common/currencies.js.map +1 -0
  25. package/dist/common/debug.d.ts +1 -0
  26. package/dist/common/debug.js +11 -0
  27. package/dist/common/debug.js.map +1 -0
  28. package/dist/common/format.d.ts +1 -0
  29. package/dist/common/format.js +4 -0
  30. package/dist/common/format.js.map +1 -0
  31. package/dist/common/i18n/index.d.ts +6 -0
  32. package/dist/common/i18n/index.js +12 -0
  33. package/dist/common/i18n/index.js.map +1 -0
  34. package/dist/common/i18n/languages/en.d.ts +18 -0
  35. package/dist/common/i18n/languages/en.js +21 -0
  36. package/dist/common/i18n/languages/en.js.map +1 -0
  37. package/dist/common/i18n/languages/es.d.ts +2 -0
  38. package/dist/common/i18n/languages/es.js +21 -0
  39. package/dist/common/i18n/languages/es.js.map +1 -0
  40. package/dist/common/index.d.ts +12 -0
  41. package/dist/common/index.js +13 -0
  42. package/dist/common/index.js.map +1 -0
  43. package/dist/common/model.d.ts +22 -0
  44. package/dist/common/model.js +17 -0
  45. package/dist/common/model.js.map +1 -0
  46. package/{src/common/organization.ts → dist/common/organization.d.ts} +1 -2
  47. package/dist/common/organization.js +2 -0
  48. package/dist/common/organization.js.map +1 -0
  49. package/dist/common/pay.d.ts +284 -0
  50. package/dist/common/pay.js +82 -0
  51. package/dist/common/pay.js.map +1 -0
  52. package/dist/common/retryBackoff.d.ts +1 -0
  53. package/dist/common/retryBackoff.js +22 -0
  54. package/dist/common/retryBackoff.js.map +1 -0
  55. package/dist/common/time.d.ts +11 -0
  56. package/dist/common/time.js +62 -0
  57. package/dist/common/time.js.map +1 -0
  58. package/dist/hooks/index.js +2 -0
  59. package/dist/hooks/index.js.map +1 -0
  60. package/dist/hooks/use-is-mobile.d.ts +1 -0
  61. package/dist/hooks/use-is-mobile.js +14 -0
  62. package/dist/hooks/use-is-mobile.js.map +1 -0
  63. package/dist/utils/account.d.ts +1 -0
  64. package/dist/utils/account.js +4 -0
  65. package/dist/utils/account.js.map +1 -0
  66. package/dist/utils/browser.d.ts +6 -0
  67. package/dist/utils/browser.js +22 -0
  68. package/dist/utils/browser.js.map +1 -0
  69. package/dist/utils/index.d.ts +3 -0
  70. package/dist/utils/index.js +4 -0
  71. package/dist/utils/index.js.map +1 -0
  72. package/dist/utils/plurar.d.ts +1 -0
  73. package/dist/utils/plurar.js +7 -0
  74. package/dist/utils/plurar.js.map +1 -0
  75. package/package.json +8 -8
  76. package/src/api/config.ts +0 -2
  77. package/src/api/index.ts +0 -3
  78. package/src/common/assert.ts +0 -21
  79. package/src/common/chainExplorer.ts +0 -27
  80. package/src/common/chains.ts +0 -130
  81. package/src/common/currencies.ts +0 -42
  82. package/src/common/debug.ts +0 -11
  83. package/src/common/format.ts +0 -3
  84. package/src/common/i18n/index.ts +0 -18
  85. package/src/common/i18n/languages/en.ts +0 -38
  86. package/src/common/i18n/languages/es.ts +0 -38
  87. package/src/common/index.ts +0 -12
  88. package/src/common/model.ts +0 -43
  89. package/src/common/pay.ts +0 -194
  90. package/src/common/retryBackoff.ts +0 -24
  91. package/src/common/time.ts +0 -78
  92. package/src/hooks/use-is-mobile.ts +0 -16
  93. package/src/utils/account.ts +0 -3
  94. package/src/utils/browser.ts +0 -24
  95. package/src/utils/index.ts +0 -3
  96. package/src/utils/plurar.ts +0 -5
  97. /package/{src/hooks/index.ts → dist/hooks/index.d.ts} +0 -0
@@ -0,0 +1,4 @@
1
+ export * from "./account";
2
+ export * from "./browser";
3
+ export * from "./plurar";
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA"}
@@ -0,0 +1 @@
1
+ export declare const withPlural: (totalQuantity: number, singular: string, plural: string) => string;
@@ -0,0 +1,7 @@
1
+ export const withPlural = (totalQuantity, singular, plural) => {
2
+ if (totalQuantity == 1)
3
+ return `${totalQuantity} ${singular}`;
4
+ else
5
+ return `${totalQuantity} ${plural}`;
6
+ };
7
+ //# sourceMappingURL=plurar.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plurar.js","sourceRoot":"","sources":["../../src/utils/plurar.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,aAAqB,EAAE,QAAgB,EAAE,MAAc,EAAE,EAAE;IAClF,IAAI,aAAa,IAAI,CAAC;QAAE,OAAO,GAAG,aAAa,IAAI,QAAQ,EAAE,CAAC;;QACzD,OAAO,GAAG,aAAa,IAAI,MAAM,EAAE,CAAC;AAC3C,CAAC,CAAC"}
package/package.json CHANGED
@@ -1,22 +1,22 @@
1
1
  {
2
2
  "name": "@coin-voyage/shared",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "private": false,
5
5
  "sideEffects": false,
6
6
  "exports": {
7
- ".": "./src/index.ts",
8
- "./*": [
9
- "./src/*/index.ts",
10
- "./src/*/index.tsx"
11
- ]
7
+ "./api": "./dist/api/index.ts",
8
+ "./common": "./dist/common/index.ts",
9
+ "./hooks": "./dist/hooks/index.ts",
10
+ "./utils": "./dist/utils/index.ts"
12
11
  },
13
12
  "files": [
14
- "src"
13
+ "dist"
15
14
  ],
16
15
  "typesVersions": {
17
16
  "*": {
18
17
  "*": [
19
- "src/*/index.d.ts"
18
+ "dist/*",
19
+ "dist/*/index.d.ts"
20
20
  ]
21
21
  }
22
22
  },
package/src/api/config.ts DELETED
@@ -1,2 +0,0 @@
1
- export const API_URL = "https://dev-api.coinvoyage.io"
2
- // export const API_URL = "http://localhost:8000"
package/src/api/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from "./config"
2
- export * from "./fetcher"
3
- export * from "./pay-order"
@@ -1,21 +0,0 @@
1
- import { debugJson } from "./debug"
2
-
3
- export function assert(condition: boolean, ...args: any[]): asserts condition {
4
- if (!condition) {
5
- throw new Error(
6
- `Assertion failed: ${args.map((a) => debugJson(a)).join(", ")}`
7
- )
8
- }
9
- }
10
-
11
- export function assertNotNull<T>(
12
- value: T | null | undefined,
13
- ...args: any[]
14
- ): T {
15
- assert(value !== null && value !== undefined, ...args)
16
- return value
17
- }
18
-
19
- export function assertEqual<T>(a: T, b: T, ...args: any[]): void {
20
- assert(a === b, ...args)
21
- }
@@ -1,27 +0,0 @@
1
- import { ChainId } from "./chains"
2
-
3
- const chainExplorerUrls: Record<ChainId, string> = {
4
- [ChainId.ETH]: "https://etherscan.io",
5
- [ChainId.ARB]: "https://arbiscan.io",
6
- [ChainId.BASE]: "https://basescan.org",
7
- [ChainId.BLAST]: "https://blastexplorer.io",
8
- [ChainId.OP]: "https://optimistic.etherscan.io",
9
- [ChainId.POL]: "https://polygonscan.com",
10
- [ChainId.AVAX]: "https://snowtrace.io",
11
- [ChainId.SEPOLIA]: "https://sepolia.etherscan.io",
12
- [ChainId.SEPOLIA_BASE]: "https://sepolia.basescan.org",
13
- [ChainId.SEPOLIA_ARB]: "https://sepolia.arbiscan.io",
14
- [ChainId.SEPOLIA_OP]: "https://sepolia-optimism.etherscan.io",
15
- [ChainId.BSC]: "https://bscscan.com",
16
- [ChainId.ZKSYNC]: "https://explorer.zksync.io",
17
- [ChainId.FTM]: "https://ftmscan.com",
18
- [ChainId.SOL]: "https://solscan.io",
19
- [ChainId.SUI]: "https://suiscan.xyz",
20
- [ChainId.TRX]: "https://tronscan.org",
21
- [ChainId.BTC]: "https://mempool.space",
22
- }
23
-
24
- export function getChainExplorerTxUrl(chainId: ChainId, txHash?: string) {
25
- const explorer = chainExplorerUrls[chainId]
26
- return `${explorer}/tx/${txHash}`
27
- }
@@ -1,130 +0,0 @@
1
- import * as chains from "viem/chains"
2
-
3
- export enum ChainType {
4
- EVM = "EVM",
5
- // Solana virtual machine
6
- SOL = "SOL",
7
- SUI = "SUI",
8
- TRON = "TRON",
9
- // Unspent transaction output (e.g. Bitcoin, Litecoin, Dogecoin)
10
- UTXO = "UTXO",
11
- }
12
-
13
- export function getChainTypeName(chainType?: ChainType): string {
14
- switch (chainType) {
15
- case ChainType.EVM:
16
- return "Ethereum"
17
- case ChainType.SOL:
18
- return "Solana"
19
- case ChainType.SUI:
20
- return "Sui"
21
- case ChainType.TRON:
22
- return "Tron"
23
- case ChainType.UTXO:
24
- return "Bitcoin"
25
- default:
26
- return ""
27
- }
28
- }
29
-
30
- export enum ChainId {
31
- ETH = 1,
32
- OP = 10,
33
- BSC = 56,
34
- POL = 137,
35
- FTM = 250,
36
- ZKSYNC = 324,
37
- BASE = 8453,
38
- ARB = 42161,
39
- AVAX = 43114,
40
- BLAST = 81457,
41
-
42
- // testnet
43
- SEPOLIA = 11155111,
44
- SEPOLIA_BASE = 84532,
45
- SEPOLIA_ARB = 421614,
46
- SEPOLIA_OP = 11155420,
47
-
48
- // UTXO (IDs are made up)
49
- BTC = 20000000000001,
50
- // BCH = 20000000000002,
51
- // LTC = 20000000000003,
52
- // DGE = 20000000000004,
53
-
54
- // None-EVM (IDs are made up)
55
- SOL = 30000000000001,
56
- SUI = 30000000000002,
57
- TRX = 30000000000003,
58
- // XRPL = 30000000000004,
59
- }
60
-
61
- export function getChainTypeByChainId(chainId?: ChainId): ChainType {
62
- switch (chainId) {
63
- case ChainId.ETH:
64
- case ChainId.OP:
65
- case ChainId.BSC:
66
- case ChainId.POL:
67
- case ChainId.FTM:
68
- case ChainId.ZKSYNC:
69
- case ChainId.BASE:
70
- case ChainId.ARB:
71
- case ChainId.AVAX:
72
- case ChainId.BLAST:
73
- return ChainType.EVM
74
- case ChainId.SOL:
75
- return ChainType.SOL
76
- case ChainId.SUI:
77
- return ChainType.SUI
78
- case ChainId.TRX:
79
- return ChainType.TRON
80
- case ChainId.BTC:
81
- return ChainType.UTXO
82
- default:
83
- return ChainType.EVM
84
- }
85
- }
86
-
87
- export function isTestnetChain(chainId: number): boolean {
88
- return Boolean(
89
- Object.values(chains).find((chain) => chain.id === chainId)?.testnet
90
- )
91
- }
92
-
93
- export function getChainName(chainId?: ChainId): string | undefined {
94
- if (!chainId) {
95
- return
96
- }
97
-
98
- switch (chainId) {
99
- case ChainId.ETH:
100
- return "Ethereum"
101
- case ChainId.OP:
102
- return "Optimism"
103
- case ChainId.BSC:
104
- return "Binance Smart Chain"
105
- case ChainId.POL:
106
- return "Polygon"
107
- case ChainId.FTM:
108
- return "Fantom"
109
- case ChainId.ZKSYNC:
110
- return "zkSync"
111
- case ChainId.BASE:
112
- return "Base"
113
- case ChainId.ARB:
114
- return "Arbitrum"
115
- case ChainId.AVAX:
116
- return "Avalanche"
117
- case ChainId.BLAST:
118
- return "Blast"
119
- case ChainId.SOL:
120
- return "Solana"
121
- case ChainId.SUI:
122
- return "Sui"
123
- case ChainId.TRX:
124
- return "Tron"
125
- case ChainId.BTC:
126
- return "Bitcoin"
127
- default:
128
- return
129
- }
130
- }
@@ -1,42 +0,0 @@
1
- export interface CurrencyExchangeRate {
2
- name: string
3
- symbol: string
4
- currency: string
5
- decimals: number
6
- rateUSD: number
7
- }
8
-
9
- export const currencyRateUSD: CurrencyExchangeRate = {
10
- name: "US Dollar",
11
- symbol: "$",
12
- currency: "USD",
13
- decimals: 2,
14
- rateUSD: 1,
15
- }
16
-
17
- const data: [string, string, string, number][] = [
18
- ["Euro", "€", "EUR", 2],
19
- ["Argentine Peso", "ARS", "ARS", 0],
20
- ["Bolivian Boliviano", "$Bs", "BOB", 0],
21
- ["Turkish Lira", "₺", "TRY", 0],
22
- ["New Taiwan Dollar", "NT$", "TWD", 0],
23
- ["Ukrainian Hryvnia", "₴", "UAH", 0],
24
- ["Naira", "₦", "NGN", 0],
25
- ["Swiss Franc", "₣", "CHF", 2],
26
- ["Japanese Yen", "¥", "JPY", 0],
27
- ["Korean Won", "₩", "KRW", 0],
28
- ["Yuan", "¥", "CNY", 0],
29
- ["Pound", "£", "GBP", 2],
30
- ["Canadian Dollar", "C$", "CAD", 2],
31
- ["Australian Dollar", "A$", "AUD", 2],
32
- ["Singapore Dollar", "S$", "SGD", 2],
33
- ]
34
-
35
- export const nonUsdCurrencies = data.map(
36
- ([name, symbol, currency, decimals]) => ({
37
- name,
38
- symbol,
39
- currency,
40
- decimals,
41
- })
42
- )
@@ -1,11 +0,0 @@
1
- // Return compact JSON, 1000 chars max. Never throws.
2
- export function debugJson(obj: any) {
3
- try {
4
- const serialized = JSON.stringify(obj, (_, value) =>
5
- typeof value === "bigint" ? value.toString() : value
6
- )
7
- return serialized.slice(0, 1000)
8
- } catch (e: any) {
9
- return `<JSON error: ${e.message}>`
10
- }
11
- }
@@ -1,3 +0,0 @@
1
- export function capitalize(str: string) {
2
- return str.charAt(0).toUpperCase() + str.slice(1)
3
- }
@@ -1,18 +0,0 @@
1
- import { en } from "./languages/en"
2
- import { es } from "./languages/es"
3
-
4
- export interface Locale {
5
- languageCode?: string
6
- }
7
-
8
- export type I18NTranslation = typeof en
9
-
10
- // Return i18n translation based on locale
11
- export const i18n = (locale: Locale | undefined): I18NTranslation => {
12
- switch (locale?.languageCode) {
13
- case "es":
14
- return es
15
- default:
16
- return en
17
- }
18
- }
@@ -1,38 +0,0 @@
1
- export const en = {
2
- // format.ts
3
- format: {
4
- fee: () => "Fee:",
5
- },
6
-
7
- // op.ts
8
- op: {
9
- acceptedInbound: (
10
- readableAmount: string,
11
- otherCoinSymbol: string,
12
- homeCoinSymbol: string,
13
- chain?: string
14
- ) =>
15
- `Accepted ${readableAmount} ${otherCoinSymbol}${
16
- chain ? ` on ${chain}` : ""
17
- } as ${homeCoinSymbol}`,
18
- sentOutbound: (
19
- readableAmount: string,
20
- coinSymbol: string,
21
- chain?: string
22
- ) => `Sent ${readableAmount} ${coinSymbol}${chain ? ` on ${chain}` : ""}`,
23
- },
24
-
25
- // time.ts
26
- time: {
27
- soon: () => "soon",
28
- now: (long?: boolean) => `${long ? "just now" : "now"}`,
29
- minutesAgo: (minutes: number, long?: boolean) =>
30
- `${minutes}m ${long ? "ago" : ""}`,
31
- hoursAgo: (hours: number, long?: boolean) =>
32
- `${hours}h ${long ? "ago" : ""}`,
33
- daysAgo: (days: number, long?: boolean) => `${days}d ${long ? "ago" : ""}`,
34
- inDays: (days: number, long?: boolean) => `${long ? "in" : ""} ${days}d`,
35
- },
36
- }
37
-
38
- export type LanguageDefinition = typeof en
@@ -1,38 +0,0 @@
1
- import type { LanguageDefinition } from "./en"
2
- export const es: LanguageDefinition = {
3
- // format.ts
4
- format: {
5
- fee: () => "Tasas:",
6
- },
7
-
8
- // op.ts
9
- op: {
10
- acceptedInbound: (
11
- readableAmount: string,
12
- otherCoinSymbol: string,
13
- homeCoinSymbol: string,
14
- chain?: string
15
- ) =>
16
- `Aceptado ${readableAmount} ${otherCoinSymbol}${
17
- chain ? ` en ${chain}` : ""
18
- } como ${homeCoinSymbol}`,
19
- sentOutbound: (
20
- readableAmount: string,
21
- coinSymbol: string,
22
- chain?: string
23
- ) =>
24
- `Envidado ${readableAmount} ${coinSymbol}${chain ? ` en ${chain}` : ""}`,
25
- },
26
-
27
- // time.ts
28
- time: {
29
- soon: () => "pronto",
30
- now: () => "ahora",
31
- minutesAgo: (minutes: number, long?: boolean) =>
32
- `${long ? "hace" : ""} ${minutes}m`,
33
- hoursAgo: (hours: number, long?: boolean) =>
34
- `${long ? "hace" : ""} ${hours}h`,
35
- daysAgo: (days: number, long?: boolean) => `${long ? "hace" : ""} ${days}d`,
36
- inDays: (days: number, long?: boolean) => `${long ? "en" : ""} ${days}d`,
37
- },
38
- }
@@ -1,12 +0,0 @@
1
- export * from "./assert"
2
- export * from "./chainExplorer"
3
- export * from "./chains"
4
- export * from "./currencies"
5
- export * from "./organization"
6
- export * from "./pay"
7
- export * from "./debug"
8
- export * from "./format"
9
- export * from "./model"
10
- export * from "./retryBackoff"
11
- export * from "./time"
12
- export * from "./i18n"
@@ -1,43 +0,0 @@
1
- import type { Address } from "viem"
2
- import { z } from "zod"
3
- import { ChainId } from "./chains"
4
-
5
- export const zSolanaPublicKey = z.string().regex(/^[1-9A-HJ-NP-Za-km-z]+$/)
6
-
7
- export type SolanaPublicKey = z.infer<typeof zSolanaPublicKey>
8
-
9
- export interface Currency extends CurrencyBase {
10
- id?: string
11
- name: string
12
- ticker: string
13
- decimals: number
14
- image_uri?: string
15
- price_usd?: number
16
- }
17
-
18
- export interface CurrencyBase {
19
- chain_id: ChainId
20
- address: string | null
21
- }
22
-
23
- export const zAddress = z
24
- .string()
25
- .regex(/^0x[0-9a-f]{40}$/i)
26
- .refine((s): s is Address => true)
27
-
28
- export const zBigIntStr = z
29
- .string()
30
- .regex(/^[0-9]+$/i)
31
- .refine((s): s is BigIntStr => true)
32
-
33
- export type BigIntStr = `${bigint}`
34
-
35
- export type PlatformType = "ios" | "android" | "other"
36
-
37
- export function dateToUnix(d: Date): number {
38
- return Math.floor(d.getTime() / 1000)
39
- }
40
-
41
- export function unixToDate(unix: number): Date {
42
- return new Date(unix * 1000)
43
- }
package/src/common/pay.ts DELETED
@@ -1,194 +0,0 @@
1
- import { Address, Hex, zeroAddress } from "viem"
2
- import z from "zod"
3
- import { Currency, zAddress, zBigIntStr } from "./model"
4
-
5
-
6
- export enum PayOrderMode {
7
- SALE = "SALE", // product or item sale, value out can only be adjusted by the merchant
8
- DEPOSIT = "DEPOSIT", // deposit into user account, let the user specify the value out
9
- }
10
-
11
- export const zBridgeTokenOutOptions = z.array(
12
- z.object({
13
- token: zAddress,
14
- amount: zBigIntStr.transform((a) => BigInt(a)),
15
- })
16
- )
17
-
18
- export type BridgeTokenOutOptions = z.infer<typeof zBridgeTokenOutOptions>
19
-
20
- // NOTE: be careful to modify this type only in backward-compatible ways.
21
- // Add OPTIONAL fields, etc. Anything else requires a migration.
22
- export const zPayOrderMetadata = z.object({
23
- items: z
24
- .array(
25
- z.object({
26
- name: z.string(),
27
- description: z.string().optional(),
28
- image: z.string().optional(),
29
- quantity: z.number().int().optional(),
30
- unit_price: z.number().optional(),
31
- currency: z.string().optional(),
32
- })
33
- )
34
- .describe("Details about what's being ordered, donated, deposited, etc."),
35
- })
36
-
37
- // also validate not both destination_value_usd and destination_amount are present
38
- export const zDepositPayOrder = z.object({
39
- metadata: zPayOrderMetadata.optional(),
40
- destination_value_usd: z.number().optional()
41
- .refine((val) => val === undefined || Number(val) > 0, {
42
- message: "destination_value_usd must be greater than zero if defined",
43
- }),
44
- destination_currency: z.object({
45
- chain_id: z.number(),
46
- address: z.string().nullable(),
47
- }),
48
- destination_amount: z.string().optional()
49
- .refine((val) => val === undefined || Number(val) > 0, {
50
- message: "destination_amount must be greater than zero if defined",
51
- }),
52
- receiving_address: z.string().min(1),
53
- }).refine(
54
- (data) =>
55
- !(data.destination_value_usd && data.destination_amount), // Ensure not both are present
56
- {
57
- message: "Only one of destination_value_usd or destination_amount should be present.",
58
- path: ["destination_value_usd", "destination_amount"],
59
- }
60
- );
61
-
62
- export type PayOrderMetadata = z.infer<typeof zPayOrderMetadata>
63
-
64
- export enum PayOrderStatus {
65
- PENDING = "PENDING",
66
- FAILED = "FAILED",
67
- AWAITING_PAYMENT = "AWAITING_PAYMENT",
68
- AWAITING_CONFIRMATION = "AWAITING_CONFIRMATION",
69
- EXECUTING_ORDER = "EXECUTING_ORDER",
70
- COMPLETED = "COMPLETED",
71
- EXPIRED = "EXPIRED",
72
- REFUNDED = "REFUNDED",
73
- }
74
-
75
- export type PayOrder = {
76
- id: string
77
-
78
- mode: PayOrderMode.SALE | PayOrderMode.DEPOSIT
79
- status: PayOrderStatus
80
- metadata?: PayOrderMetadata
81
- expires_at?: Date
82
-
83
- source_currency?: Currency
84
- source_amount?: CurrencyAmount
85
- deposit_tx_hash?: string
86
-
87
- destination_currency?: Currency
88
- destination_amount?: CurrencyAmount
89
- receiving_tx_hash?: string
90
-
91
- receiving_address?: string
92
- refund_address?: string
93
- deposit_address?: string
94
-
95
- refund_tx_hash?: string
96
- }
97
-
98
- export interface CurrencyWithAmount extends Currency {
99
- currency_amount: CurrencyAmount
100
- }
101
-
102
- export interface CurrencyWithBalance extends CurrencyWithAmount {
103
- balance?: CurrencyAmount
104
- }
105
-
106
- export interface CurrencyAmount {
107
- ui_amount: number
108
- raw_amount: bigint
109
- value_usd: number
110
- }
111
-
112
- export type OnChainCall = {
113
- to: Address
114
- data: Hex
115
- value: bigint
116
- }
117
-
118
- export const emptyOnChainCall: OnChainCall = {
119
- to: zeroAddress,
120
- data: "0x",
121
- value: BigInt(0),
122
- }
123
-
124
- export const zUUID = z.string().uuid()
125
-
126
- export type UUID = z.infer<typeof zUUID>
127
-
128
- export type PaymentCreationErrorEvent = {
129
- type: "payment_creation_error"
130
- errorMessage: string
131
- }
132
-
133
- export type PaymentStartedEvent = {
134
- type: "payment_started"
135
- paymentId: string
136
- chainId: number
137
- txHash: Hex | string | null
138
- }
139
-
140
- export type PaymentCompletedEvent = {
141
- type: "payment_completed"
142
- paymentId: UUID
143
- chainId: number
144
- txHash: Hex | string
145
- }
146
-
147
- export type PaymentBouncedEvent = {
148
- type: "payment_bounced"
149
- paymentId: UUID
150
- chainId: number
151
- txHash: Hex | string
152
- }
153
-
154
- export type PayEvent =
155
- | PaymentStartedEvent
156
- | PaymentCompletedEvent
157
- | PaymentBouncedEvent
158
-
159
- export interface WebhookEndpoint {
160
- id: string
161
- organization_id: string
162
- active: boolean
163
- url: string
164
- webhook_secret: string
165
- subscription_events: WebhookEventStatus[]
166
- created_at: Date
167
- }
168
-
169
- export enum WebhookEventStatus {
170
- ORDER_CREATED = "ORDER_CREATED",
171
- ORDER_AWAITING_PAYMENT = "ORDER_AWAITING_PAYMENT",
172
- ORDER_CONFIRMING = "ORDER_CONFIRMING",
173
- ORDER_EXECUTING = "ORDER_EXECUTING",
174
- ORDER_COMPLETED = "ORDER_COMPLETED",
175
- ORDER_ERROR = "ORDER_ERROR",
176
- ORDER_REFUNDED = "ORDER_REFUNDED"
177
- }
178
-
179
- export interface WebhookEvent {
180
- id: UUID
181
- endpoint: WebhookEndpoint
182
- body: PayEvent
183
- status: WebhookEventStatus
184
- //deliveries: WebhookDelivery[]
185
- createdAt: Date
186
- }
187
-
188
- // export interface WebhookDelivery {
189
- // id: UUID
190
- // eventId: UUID
191
- // httpStatus: number | null
192
- // body: string | null
193
- // createdAt: Date
194
- // }
@@ -1,24 +0,0 @@
1
- // Retries a function up to a maximum number of times, with exponential backoff.
2
- // Current settings, max total wait time is ~10 seconds.
3
- export async function retryBackoff<T>(
4
- name: string,
5
- fn: () => Promise<T>,
6
- maxRetries = 5,
7
- backoffFn: (i: number) => number = (i) => Math.min(2000, 250 * 2 ** i)
8
- ): Promise<T> {
9
- for (let i = 1; ; i++) {
10
- try {
11
- return await fn()
12
- } catch (e) {
13
- if (i <= maxRetries) {
14
- const sleepMs = backoffFn(i)
15
- await new Promise((r) => setTimeout(r, sleepMs))
16
- } else {
17
- console.warn(`[RETRY] ${name} QUITTING after try ${i}, error: ${e}`)
18
- break
19
- }
20
- }
21
- }
22
- // TODO: add performance logging
23
- throw new Error(`too many retries: ${name}`)
24
- }