@binopartner/pay-sdk 0.1.1 → 0.3.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/README.md +145 -2
- package/dist/core/api-base.d.ts +49 -0
- package/dist/core/api-base.js +196 -0
- package/dist/core/appearance.d.ts +17 -0
- package/dist/core/appearance.js +122 -0
- package/dist/core/auth.d.ts +32 -0
- package/dist/core/auth.js +233 -0
- package/dist/core/client.d.ts +71 -0
- package/dist/core/client.js +307 -0
- package/dist/core/clock.d.ts +7 -0
- package/dist/core/clock.js +17 -0
- package/dist/core/errors.d.ts +29 -0
- package/dist/core/errors.js +87 -0
- package/dist/core/events.d.ts +50 -0
- package/dist/core/events.js +1 -0
- package/dist/core/flows/deposit.d.ts +63 -0
- package/dist/core/flows/deposit.js +502 -0
- package/dist/core/flows/withdraw.d.ts +51 -0
- package/dist/core/flows/withdraw.js +413 -0
- package/dist/core/i18n/ar.d.ts +2 -0
- package/dist/core/i18n/ar.js +314 -0
- package/dist/core/i18n/base.d.ts +14 -0
- package/dist/core/i18n/base.js +86 -0
- package/dist/core/i18n/cs.d.ts +2 -0
- package/dist/core/i18n/cs.js +314 -0
- package/dist/core/i18n/de.d.ts +2 -0
- package/dist/core/i18n/de.js +314 -0
- package/dist/core/i18n/en-ui.d.ts +39 -0
- package/dist/core/i18n/en-ui.js +39 -0
- package/dist/core/i18n/en.d.ts +316 -0
- package/dist/core/i18n/en.js +280 -0
- package/dist/core/i18n/es.d.ts +2 -0
- package/dist/core/i18n/es.js +314 -0
- package/dist/core/i18n/fr.d.ts +2 -0
- package/dist/core/i18n/fr.js +314 -0
- package/dist/core/i18n/hi.d.ts +2 -0
- package/dist/core/i18n/hi.js +314 -0
- package/dist/core/i18n/index.d.ts +16 -0
- package/dist/core/i18n/index.js +39 -0
- package/dist/core/i18n/it.d.ts +2 -0
- package/dist/core/i18n/it.js +314 -0
- package/dist/core/i18n/ru.d.ts +2 -0
- package/dist/core/i18n/ru.js +314 -0
- package/dist/core/i18n/tr.d.ts +2 -0
- package/dist/core/i18n/tr.js +314 -0
- package/dist/core/i18n/ua.d.ts +2 -0
- package/dist/core/i18n/ua.js +314 -0
- package/dist/core/index.d.ts +37 -0
- package/dist/core/index.js +19 -0
- package/dist/core/intent.d.ts +13 -0
- package/dist/core/intent.js +45 -0
- package/dist/core/money.d.ts +22 -0
- package/dist/core/money.js +178 -0
- package/dist/core/poll.d.ts +16 -0
- package/dist/core/poll.js +100 -0
- package/dist/core/requisites-frame.d.ts +32 -0
- package/dist/core/requisites-frame.js +202 -0
- package/dist/core/rules/address.d.ts +3 -0
- package/dist/core/rules/address.js +29 -0
- package/dist/core/rules/limits.d.ts +17 -0
- package/dist/core/rules/limits.js +19 -0
- package/dist/core/rules/paying.d.ts +10 -0
- package/dist/core/rules/paying.js +32 -0
- package/dist/core/rules/receipt.d.ts +6 -0
- package/dist/core/rules/receipt.js +40 -0
- package/dist/core/rules/requisites.d.ts +42 -0
- package/dist/core/rules/requisites.js +68 -0
- package/dist/core/rules/slippage.d.ts +1 -0
- package/dist/core/rules/slippage.js +7 -0
- package/dist/core/store.d.ts +15 -0
- package/dist/core/store.js +53 -0
- package/dist/core/types.d.ts +244 -0
- package/dist/core/types.js +1 -0
- package/dist/core/wire.d.ts +178 -0
- package/dist/core/wire.js +602 -0
- package/dist/elements/Root.d.ts +37 -0
- package/dist/elements/Root.js +143 -0
- package/dist/elements/chains.d.ts +1 -0
- package/dist/elements/chains.js +1 -0
- package/dist/elements/components/Qr.d.ts +17 -0
- package/dist/elements/components/Qr.js +28 -0
- package/dist/elements/components/basic.d.ts +42 -0
- package/dist/elements/components/basic.js +129 -0
- package/dist/elements/components/deposit.d.ts +61 -0
- package/dist/elements/components/deposit.js +272 -0
- package/dist/elements/components/inputs.d.ts +24 -0
- package/dist/elements/components/inputs.js +92 -0
- package/dist/elements/components/pickers.d.ts +32 -0
- package/dist/elements/components/pickers.js +267 -0
- package/dist/elements/components/status.d.ts +25 -0
- package/dist/elements/components/status.js +221 -0
- package/dist/elements/context.d.ts +47 -0
- package/dist/elements/context.js +33 -0
- package/dist/elements/hooks/deposit.d.ts +52 -0
- package/dist/elements/hooks/deposit.js +215 -0
- package/dist/elements/hooks/root.d.ts +70 -0
- package/dist/elements/hooks/root.js +125 -0
- package/dist/elements/hooks/withdraw.d.ts +30 -0
- package/dist/elements/hooks/withdraw.js +145 -0
- package/dist/elements/index.d.ts +28 -0
- package/dist/elements/index.js +14 -0
- package/dist/elements/part.d.ts +2 -0
- package/dist/elements/part.js +1 -0
- package/dist/elements/presets/DepositForm.d.ts +32 -0
- package/dist/elements/presets/DepositForm.js +97 -0
- package/dist/elements/presets/WithdrawForm.d.ts +25 -0
- package/dist/elements/presets/WithdrawForm.js +44 -0
- package/dist/elements/presets/focus.d.ts +2 -0
- package/dist/elements/presets/focus.js +26 -0
- package/dist/elements.css +2777 -0
- package/dist/fonts.css +31 -0
- package/dist/frame.d.ts +1 -0
- package/dist/frame.js +37 -6
- package/dist/types.d.ts +1 -0
- package/dist/ui/chains.d.ts +3 -0
- package/dist/ui/chains.js +42 -0
- package/dist/ui/context.d.ts +51 -0
- package/dist/ui/context.js +63 -0
- package/dist/ui/crypto.d.ts +67 -0
- package/dist/ui/crypto.js +68 -0
- package/dist/ui/entry.d.ts +25 -0
- package/dist/ui/entry.js +26 -0
- package/dist/ui/fiat.d.ts +94 -0
- package/dist/ui/fiat.js +149 -0
- package/dist/ui/fields.d.ts +76 -0
- package/dist/ui/fields.js +71 -0
- package/dist/ui/fonts.d.ts +4 -0
- package/dist/ui/fonts.js +31 -0
- package/dist/ui/icons.d.ts +39 -0
- package/dist/ui/icons.js +116 -0
- package/dist/ui/index.d.ts +31 -0
- package/dist/ui/index.js +17 -0
- package/dist/ui/layout.d.ts +36 -0
- package/dist/ui/layout.js +44 -0
- package/dist/ui/logos.d.ts +14 -0
- package/dist/ui/logos.js +61 -0
- package/dist/ui/part.d.ts +32 -0
- package/dist/ui/part.js +33 -0
- package/dist/ui/pickers.d.ts +71 -0
- package/dist/ui/pickers.js +167 -0
- package/dist/ui/qr-encode.d.ts +30 -0
- package/dist/ui/qr-encode.js +340 -0
- package/dist/ui/summary.d.ts +31 -0
- package/dist/ui/summary.js +36 -0
- package/dist/ui/types.d.ts +67 -0
- package/dist/ui/types.js +1 -0
- package/dist/ui/urls.d.ts +4 -0
- package/dist/ui/urls.js +41 -0
- package/dist/ui/withdraw.d.ts +38 -0
- package/dist/ui/withdraw.js +30 -0
- package/dist/ui.css +2699 -0
- package/dist/widget-url.d.ts +1 -0
- package/dist/widget-url.js +16 -0
- package/package.json +46 -5
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { createIntentKeys } from "../../core/intent.js";
|
|
4
|
+
import { toCreateDepositBody, toCryptoDepositBody } from "../../core/wire.js";
|
|
5
|
+
import { isAbortError, toPayError } from "../../core/errors.js";
|
|
6
|
+
import { isTerminalDepositStatus, getPayView, shownPayStatus } from "../../core/rules/requisites.js";
|
|
7
|
+
import { createPoller } from "../../core/poll.js";
|
|
8
|
+
import { useDepositFlowStore, usePayRoot } from "../context.js";
|
|
9
|
+
export const QUOTE_DEBOUNCE_MS = 400;
|
|
10
|
+
export function useDepositQuote(methodId, amount) {
|
|
11
|
+
const { client } = usePayRoot("useDepositQuote()");
|
|
12
|
+
const [result, setResult] = useState({
|
|
13
|
+
quote: null,
|
|
14
|
+
loading: false,
|
|
15
|
+
error: null,
|
|
16
|
+
});
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
if (!methodId || !amount || !(Number(amount) > 0)) {
|
|
19
|
+
setResult({ quote: null, loading: false, error: null });
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
setResult((previous) => ({ ...previous, loading: true, error: null }));
|
|
23
|
+
const controller = new AbortController();
|
|
24
|
+
const handle = setTimeout(() => {
|
|
25
|
+
client
|
|
26
|
+
.quoteDeposit({ methodId, amount }, controller.signal)
|
|
27
|
+
.then((quote) => {
|
|
28
|
+
if (!controller.signal.aborted)
|
|
29
|
+
setResult({ quote, loading: false, error: null });
|
|
30
|
+
})
|
|
31
|
+
.catch((error) => {
|
|
32
|
+
if (controller.signal.aborted || isAbortError(error))
|
|
33
|
+
return;
|
|
34
|
+
setResult({ quote: null, loading: false, error: toPayError(error) });
|
|
35
|
+
});
|
|
36
|
+
}, QUOTE_DEBOUNCE_MS);
|
|
37
|
+
return () => {
|
|
38
|
+
clearTimeout(handle);
|
|
39
|
+
controller.abort();
|
|
40
|
+
};
|
|
41
|
+
}, [client, methodId, amount]);
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
export function useCryptoDeposit(input) {
|
|
45
|
+
const { client, cryptoCache } = usePayRoot("useCryptoDeposit()");
|
|
46
|
+
const { chainId, currency, methodId } = input;
|
|
47
|
+
const key = chainId && currency ? `${chainId}/${currency}` : null;
|
|
48
|
+
const reusableFor = useCallback((routeKey) => {
|
|
49
|
+
const deposit = routeKey ? cryptoCache.get(routeKey) : undefined;
|
|
50
|
+
return deposit &&
|
|
51
|
+
deposit.status === "active" &&
|
|
52
|
+
(deposit.expiresAt === null || deposit.expiresAt > client.clock.now())
|
|
53
|
+
? deposit
|
|
54
|
+
: null;
|
|
55
|
+
}, [client, cryptoCache]);
|
|
56
|
+
const [state, setState] = useState(() => ({ key, deposit: reusableFor(key), loading: false, error: null }));
|
|
57
|
+
const inflight = useRef(null);
|
|
58
|
+
const intents = useRef(createIntentKeys());
|
|
59
|
+
if (state.key !== key) {
|
|
60
|
+
setState({ key, deposit: reusableFor(key), loading: false, error: null });
|
|
61
|
+
}
|
|
62
|
+
const request = useCallback(() => {
|
|
63
|
+
if (!chainId || !currency || !key)
|
|
64
|
+
return Promise.resolve(null);
|
|
65
|
+
const current = reusableFor(key);
|
|
66
|
+
if (current) {
|
|
67
|
+
setState({ key, deposit: current, loading: false, error: null });
|
|
68
|
+
return Promise.resolve(current);
|
|
69
|
+
}
|
|
70
|
+
if (inflight.current?.key === key)
|
|
71
|
+
return inflight.current.promise;
|
|
72
|
+
setState({ key, deposit: null, loading: true, error: null });
|
|
73
|
+
const input = { chainId, currency };
|
|
74
|
+
const promise = client
|
|
75
|
+
.createCryptoDeposit(input, { idempotencyKey: intents.current.keyFor(toCryptoDepositBody(input)) })
|
|
76
|
+
.then((deposit) => {
|
|
77
|
+
intents.current.settle({ ok: true });
|
|
78
|
+
cryptoCache.set(key, deposit);
|
|
79
|
+
client.emit({
|
|
80
|
+
type: "deposit.created",
|
|
81
|
+
depositId: deposit.id,
|
|
82
|
+
kind: "crypto",
|
|
83
|
+
methodId: methodId ?? `crypto:${chainId}:${currency.toLowerCase()}`,
|
|
84
|
+
});
|
|
85
|
+
setState((previous) => previous.key === key ? { key, deposit, loading: false, error: null } : previous);
|
|
86
|
+
return deposit;
|
|
87
|
+
})
|
|
88
|
+
.catch((error) => {
|
|
89
|
+
intents.current.settle({ ok: false, error });
|
|
90
|
+
setState((previous) => previous.key === key
|
|
91
|
+
? { key, deposit: null, loading: false, error: toPayError(error) }
|
|
92
|
+
: previous);
|
|
93
|
+
return null;
|
|
94
|
+
})
|
|
95
|
+
.finally(() => {
|
|
96
|
+
if (inflight.current?.key === key)
|
|
97
|
+
inflight.current = null;
|
|
98
|
+
});
|
|
99
|
+
inflight.current = { key, promise };
|
|
100
|
+
return promise;
|
|
101
|
+
}, [client, cryptoCache, chainId, currency, key, methodId, reusableFor]);
|
|
102
|
+
return { deposit: state.deposit, loading: state.loading, error: state.error, request };
|
|
103
|
+
}
|
|
104
|
+
export function useCreateDeposit() {
|
|
105
|
+
const { client } = usePayRoot("useCreateDeposit()");
|
|
106
|
+
const [state, setState] = useState({
|
|
107
|
+
pending: false,
|
|
108
|
+
error: null,
|
|
109
|
+
deposit: null,
|
|
110
|
+
});
|
|
111
|
+
const intents = useRef(createIntentKeys());
|
|
112
|
+
const busy = useRef(false);
|
|
113
|
+
const create = useCallback(async (input) => {
|
|
114
|
+
if (busy.current)
|
|
115
|
+
return null;
|
|
116
|
+
busy.current = true;
|
|
117
|
+
const idempotencyKey = intents.current.keyFor(toCreateDepositBody(input));
|
|
118
|
+
setState((previous) => ({ ...previous, pending: true, error: null }));
|
|
119
|
+
try {
|
|
120
|
+
const deposit = await client.createDeposit(input, { idempotencyKey });
|
|
121
|
+
intents.current.settle({ ok: true });
|
|
122
|
+
client.emit({ type: "deposit.created", depositId: deposit.id, kind: "fiat", methodId: input.methodId });
|
|
123
|
+
setState({ pending: false, error: null, deposit });
|
|
124
|
+
return deposit;
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
intents.current.settle({ ok: false, error });
|
|
128
|
+
setState((previous) => ({ ...previous, pending: false, error: toPayError(error) }));
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
busy.current = false;
|
|
133
|
+
}
|
|
134
|
+
}, [client]);
|
|
135
|
+
return { ...state, create };
|
|
136
|
+
}
|
|
137
|
+
export function useDeposit(id) {
|
|
138
|
+
const { client } = usePayRoot("useDeposit()");
|
|
139
|
+
const [state, setState] = useState({
|
|
140
|
+
deposit: null,
|
|
141
|
+
view: null,
|
|
142
|
+
shownStatus: null,
|
|
143
|
+
error: null,
|
|
144
|
+
loading: !!id,
|
|
145
|
+
});
|
|
146
|
+
const known = useRef(null);
|
|
147
|
+
const pollerRef = useRef(null);
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
known.current = null;
|
|
150
|
+
if (!id) {
|
|
151
|
+
setState({ deposit: null, view: null, shownStatus: null, error: null, loading: false });
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
setState((previous) => ({ ...previous, loading: true }));
|
|
155
|
+
const poller = createPoller(async (signal) => {
|
|
156
|
+
try {
|
|
157
|
+
const deposit = await client.getDeposit(id, signal);
|
|
158
|
+
if (deposit.kind === "fiat") {
|
|
159
|
+
const view = getPayView(deposit, known.current, client.clock.now());
|
|
160
|
+
if (view.kind === "requisites")
|
|
161
|
+
known.current = view.requisites;
|
|
162
|
+
setState({ deposit, view, shownStatus: shownPayStatus(deposit, view), error: null, loading: false });
|
|
163
|
+
return isTerminalDepositStatus(deposit.status) ? "stop" : "continue";
|
|
164
|
+
}
|
|
165
|
+
setState({ deposit, view: null, shownStatus: null, error: null, loading: false });
|
|
166
|
+
return deposit.status === "active" ? "continue" : "stop";
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
if (!isAbortError(error)) {
|
|
170
|
+
setState((previous) => ({ ...previous, error: toPayError(error), loading: false }));
|
|
171
|
+
}
|
|
172
|
+
throw error;
|
|
173
|
+
}
|
|
174
|
+
}, { intervalMs: 5000, maxIntervalMs: 30000 });
|
|
175
|
+
pollerRef.current = poller;
|
|
176
|
+
poller.start();
|
|
177
|
+
return () => {
|
|
178
|
+
poller.stop();
|
|
179
|
+
pollerRef.current = null;
|
|
180
|
+
};
|
|
181
|
+
}, [client, id]);
|
|
182
|
+
const refresh = useCallback(() => pollerRef.current?.now(), []);
|
|
183
|
+
return { ...state, refresh };
|
|
184
|
+
}
|
|
185
|
+
export function useAttachReceipt(id) {
|
|
186
|
+
const { client } = usePayRoot("useAttachReceipt()");
|
|
187
|
+
const [state, setState] = useState({ pending: false, error: null, attached: false });
|
|
188
|
+
const attach = useCallback(async (file) => {
|
|
189
|
+
if (!id)
|
|
190
|
+
return false;
|
|
191
|
+
setState({ pending: true, error: null, attached: false });
|
|
192
|
+
try {
|
|
193
|
+
await client.attachReceipt(id, file);
|
|
194
|
+
setState({ pending: false, error: null, attached: true });
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
catch (error) {
|
|
198
|
+
setState({ pending: false, error: toPayError(error), attached: false });
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
}, [client, id]);
|
|
202
|
+
return { ...state, attach };
|
|
203
|
+
}
|
|
204
|
+
export function useOnramp() {
|
|
205
|
+
const { flow, state } = useDepositFlowStore("useOnramp()");
|
|
206
|
+
if (!flow || !state)
|
|
207
|
+
throw new Error("useOnramp() needs <Pay.Root mode=\"deposit\">");
|
|
208
|
+
return {
|
|
209
|
+
url: state.onrampUrl,
|
|
210
|
+
loading: state.onrampLoading,
|
|
211
|
+
error: state.onrampError,
|
|
212
|
+
load: flow.actions.loadOnramp,
|
|
213
|
+
retry: flow.actions.retryOnramp,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ClientStatus, PayClient } from "../../core/client.js";
|
|
2
|
+
import type { PayError } from "../../core/errors.js";
|
|
3
|
+
import type { PayEvent } from "../../core/events.js";
|
|
4
|
+
import type { DepositFlowActions, DepositFlowState } from "../../core/flows/deposit.js";
|
|
5
|
+
import type { WithdrawFlowActions, WithdrawFlowState } from "../../core/flows/withdraw.js";
|
|
6
|
+
import { type PayLocale, type Translate } from "../../core/i18n/index.js";
|
|
7
|
+
import type { Balance, PayContext, PayMethods, PayMode } from "../../core/types.js";
|
|
8
|
+
export interface UsePayResult {
|
|
9
|
+
client: PayClient;
|
|
10
|
+
mode: PayMode;
|
|
11
|
+
status: ClientStatus;
|
|
12
|
+
error: PayError | null;
|
|
13
|
+
open: boolean;
|
|
14
|
+
setOpen: (open: boolean) => void;
|
|
15
|
+
restart: () => void;
|
|
16
|
+
}
|
|
17
|
+
export declare function usePay(): UsePayResult;
|
|
18
|
+
export interface UsePayContextResult {
|
|
19
|
+
data: PayContext | null;
|
|
20
|
+
error: PayError | null;
|
|
21
|
+
loading: boolean;
|
|
22
|
+
refresh: () => Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
export declare function usePayContext(): UsePayContextResult;
|
|
25
|
+
export interface UseBalanceResult {
|
|
26
|
+
balance: Balance | null;
|
|
27
|
+
available: string | null;
|
|
28
|
+
withdrawable: string | null;
|
|
29
|
+
currency: string;
|
|
30
|
+
loading: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare function useBalance(): UseBalanceResult;
|
|
33
|
+
export interface UsePaymentMethodsResult {
|
|
34
|
+
methods: PayMethods["methods"];
|
|
35
|
+
ordering: PayMethods["ordering"];
|
|
36
|
+
unavailableRails: string[];
|
|
37
|
+
suggestedCountry: string | null;
|
|
38
|
+
maxWithdrawUsd: string | null;
|
|
39
|
+
loading: boolean;
|
|
40
|
+
error: PayError | null;
|
|
41
|
+
}
|
|
42
|
+
export declare function usePaymentMethods(): UsePaymentMethodsResult;
|
|
43
|
+
export declare function useDepositFlow(): {
|
|
44
|
+
state: DepositFlowState;
|
|
45
|
+
actions: DepositFlowActions;
|
|
46
|
+
};
|
|
47
|
+
export declare function useWithdrawFlow(): {
|
|
48
|
+
state: WithdrawFlowState;
|
|
49
|
+
actions: WithdrawFlowActions;
|
|
50
|
+
};
|
|
51
|
+
export declare function usePayEvents(handler: (event: PayEvent) => void): void;
|
|
52
|
+
export interface UsePayMessagesResult {
|
|
53
|
+
t: Translate;
|
|
54
|
+
locale: PayLocale;
|
|
55
|
+
intl: string;
|
|
56
|
+
dir: "ltr" | "rtl";
|
|
57
|
+
money: (value: string | number | null | undefined, currency: string) => string;
|
|
58
|
+
usd: (value: string | number | null | undefined) => string;
|
|
59
|
+
eta: (seconds: number | null) => string | null;
|
|
60
|
+
error: (error: Pick<PayError, "code" | "details">) => string;
|
|
61
|
+
}
|
|
62
|
+
export declare function usePayMessages(): UsePayMessagesResult;
|
|
63
|
+
export interface UseCountdownResult {
|
|
64
|
+
remainingMs: number | null;
|
|
65
|
+
expired: boolean;
|
|
66
|
+
text: string | null;
|
|
67
|
+
}
|
|
68
|
+
export declare const formatClock: (ms: number) => string;
|
|
69
|
+
export declare function useCountdown(expiresAt: number | null | undefined): UseCountdownResult;
|
|
70
|
+
export declare function usePayError(): PayError | null;
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
+
import { errorMessage, formatEta, } from "../../core/i18n/index.js";
|
|
4
|
+
import { formatMoney, formatUsd } from "../../core/money.js";
|
|
5
|
+
import { useDepositFlowStore, useFlowError, usePayRoot, useStoreValue, useWithdrawFlowStore, } from "../context.js";
|
|
6
|
+
export function usePay() {
|
|
7
|
+
const root = usePayRoot("usePay()");
|
|
8
|
+
const state = useStoreValue(root.client.state);
|
|
9
|
+
return {
|
|
10
|
+
client: root.client,
|
|
11
|
+
mode: root.mode,
|
|
12
|
+
status: state.status,
|
|
13
|
+
error: state.error,
|
|
14
|
+
open: root.open,
|
|
15
|
+
setOpen: root.setOpen,
|
|
16
|
+
restart: root.restart,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const useFlowShared = (owner) => {
|
|
20
|
+
const root = usePayRoot(owner);
|
|
21
|
+
const state = useStoreValue(root.flow);
|
|
22
|
+
const refresh = useCallback(() => root.flow.actions.refreshContext(), [root.flow]);
|
|
23
|
+
return { ...state, refresh };
|
|
24
|
+
};
|
|
25
|
+
export function usePayContext() {
|
|
26
|
+
const shared = useFlowShared("usePayContext()");
|
|
27
|
+
return {
|
|
28
|
+
data: shared.context,
|
|
29
|
+
error: shared.error,
|
|
30
|
+
loading: shared.step === "loading",
|
|
31
|
+
refresh: shared.refresh,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export function useBalance() {
|
|
35
|
+
const shared = useFlowShared("useBalance()");
|
|
36
|
+
const balance = shared.context?.balance ?? null;
|
|
37
|
+
return {
|
|
38
|
+
balance,
|
|
39
|
+
available: balance?.available ?? null,
|
|
40
|
+
withdrawable: balance ? (balance.withdrawable ?? balance.available) : null,
|
|
41
|
+
currency: balance?.currency ?? "USD",
|
|
42
|
+
loading: shared.step === "loading",
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export function usePaymentMethods() {
|
|
46
|
+
const shared = useFlowShared("usePaymentMethods()");
|
|
47
|
+
const methods = shared.methods;
|
|
48
|
+
return {
|
|
49
|
+
methods: methods?.methods ?? [],
|
|
50
|
+
ordering: methods?.ordering ?? { currencies: [], chains: [] },
|
|
51
|
+
unavailableRails: methods?.unavailableRails ?? [],
|
|
52
|
+
suggestedCountry: methods?.suggestedCountry ?? null,
|
|
53
|
+
maxWithdrawUsd: methods?.maxWithdrawUsd ?? null,
|
|
54
|
+
loading: shared.step === "loading",
|
|
55
|
+
error: shared.error,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
export function useDepositFlow() {
|
|
59
|
+
const { flow, state } = useDepositFlowStore("useDepositFlow()");
|
|
60
|
+
if (!flow || !state)
|
|
61
|
+
throw new Error("useDepositFlow() needs <Pay.Root mode=\"deposit\">");
|
|
62
|
+
return { state, actions: flow.actions };
|
|
63
|
+
}
|
|
64
|
+
export function useWithdrawFlow() {
|
|
65
|
+
const { flow, state } = useWithdrawFlowStore("useWithdrawFlow()");
|
|
66
|
+
if (!flow || !state)
|
|
67
|
+
throw new Error("useWithdrawFlow() needs <Pay.Root mode=\"withdraw\">");
|
|
68
|
+
return { state, actions: flow.actions };
|
|
69
|
+
}
|
|
70
|
+
export function usePayEvents(handler) {
|
|
71
|
+
const root = usePayRoot("usePayEvents()");
|
|
72
|
+
const latest = useRef(handler);
|
|
73
|
+
useEffect(() => {
|
|
74
|
+
latest.current = handler;
|
|
75
|
+
});
|
|
76
|
+
useEffect(() => root.client.onEvent((event) => latest.current(event)), [root.client]);
|
|
77
|
+
}
|
|
78
|
+
export function usePayMessages() {
|
|
79
|
+
const root = usePayRoot("usePayMessages()");
|
|
80
|
+
return useMemo(() => ({
|
|
81
|
+
t: root.t,
|
|
82
|
+
locale: root.locale,
|
|
83
|
+
intl: root.intl,
|
|
84
|
+
dir: root.dir,
|
|
85
|
+
money: (value, currency) => formatMoney(value, currency, root.intl),
|
|
86
|
+
usd: (value) => formatUsd(value),
|
|
87
|
+
eta: (seconds) => formatEta(root.t, seconds),
|
|
88
|
+
error: (error) => errorMessage(root.t, error, root.locale),
|
|
89
|
+
}), [root.t, root.locale, root.intl, root.dir]);
|
|
90
|
+
}
|
|
91
|
+
export const formatClock = (ms) => {
|
|
92
|
+
const total = Math.max(0, Math.ceil(ms / 1000));
|
|
93
|
+
const hours = Math.floor(total / 3600);
|
|
94
|
+
const minutes = Math.floor((total % 3600) / 60);
|
|
95
|
+
const seconds = total % 60;
|
|
96
|
+
const mm = String(minutes).padStart(2, "0");
|
|
97
|
+
const ss = String(seconds).padStart(2, "0");
|
|
98
|
+
return hours > 0 ? `${hours}:${mm}:${ss}` : `${mm}:${ss}`;
|
|
99
|
+
};
|
|
100
|
+
export function useCountdown(expiresAt) {
|
|
101
|
+
const root = usePayRoot("useCountdown()");
|
|
102
|
+
const clock = root.client.clock;
|
|
103
|
+
const compute = useCallback(() => (typeof expiresAt === "number" ? Math.max(0, expiresAt - clock.now()) : null), [expiresAt, clock]);
|
|
104
|
+
const [remainingMs, setRemaining] = useState(compute);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
setRemaining(compute());
|
|
107
|
+
if (typeof expiresAt !== "number")
|
|
108
|
+
return;
|
|
109
|
+
const handle = setInterval(() => {
|
|
110
|
+
const next = compute();
|
|
111
|
+
setRemaining(next);
|
|
112
|
+
if (next === 0)
|
|
113
|
+
clearInterval(handle);
|
|
114
|
+
}, 1000);
|
|
115
|
+
return () => clearInterval(handle);
|
|
116
|
+
}, [compute, expiresAt]);
|
|
117
|
+
return {
|
|
118
|
+
remainingMs,
|
|
119
|
+
expired: remainingMs === 0,
|
|
120
|
+
text: remainingMs === null ? null : formatClock(remainingMs),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
export function usePayError() {
|
|
124
|
+
return useFlowError("usePayError()");
|
|
125
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type PayError } from "../../core/errors.js";
|
|
2
|
+
import type { CreateWithdrawalInput, SlippageWarning, Withdrawal, WithdrawalQuote } from "../../core/types.js";
|
|
3
|
+
export interface UseWithdrawQuoteResult {
|
|
4
|
+
quote: WithdrawalQuote | null;
|
|
5
|
+
slippageWarning: SlippageWarning | null;
|
|
6
|
+
acceptedSlippagePct: number | null;
|
|
7
|
+
loading: boolean;
|
|
8
|
+
error: PayError | null;
|
|
9
|
+
accept: () => void;
|
|
10
|
+
}
|
|
11
|
+
export declare function useWithdrawQuote(input: {
|
|
12
|
+
methodId: string | null | undefined;
|
|
13
|
+
address: string | null | undefined;
|
|
14
|
+
amount: string | null | undefined;
|
|
15
|
+
}): UseWithdrawQuoteResult;
|
|
16
|
+
export interface UseCreateWithdrawalResult {
|
|
17
|
+
create: (input: CreateWithdrawalInput) => Promise<Withdrawal | null>;
|
|
18
|
+
pending: boolean;
|
|
19
|
+
error: PayError | null;
|
|
20
|
+
withdrawal: Withdrawal | null;
|
|
21
|
+
twoFactorRequired: boolean;
|
|
22
|
+
inProgressId: string | null;
|
|
23
|
+
}
|
|
24
|
+
export declare function useCreateWithdrawal(): UseCreateWithdrawalResult;
|
|
25
|
+
export interface UseWithdrawalResult {
|
|
26
|
+
withdrawal: Withdrawal | null;
|
|
27
|
+
error: PayError | null;
|
|
28
|
+
loading: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare function useWithdrawal(id: string | null | undefined): UseWithdrawalResult;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { createIntentKeys } from "../../core/intent.js";
|
|
4
|
+
import { toCreateWithdrawalBody } from "../../core/wire.js";
|
|
5
|
+
import { isAbortError, toPayError } from "../../core/errors.js";
|
|
6
|
+
import { isTerminalWithdrawal } from "../../core/flows/withdraw.js";
|
|
7
|
+
import { createPoller } from "../../core/poll.js";
|
|
8
|
+
import { acceptSlippagePct } from "../../core/rules/slippage.js";
|
|
9
|
+
import { usePayRoot } from "../context.js";
|
|
10
|
+
import { QUOTE_DEBOUNCE_MS } from "./deposit.js";
|
|
11
|
+
export function useWithdrawQuote(input) {
|
|
12
|
+
const { client } = usePayRoot("useWithdrawQuote()");
|
|
13
|
+
const { methodId, amount } = input;
|
|
14
|
+
const address = input.address?.trim() ?? "";
|
|
15
|
+
const [accepted, setAccepted] = useState(null);
|
|
16
|
+
const fingerprint = `${methodId ?? ""}|${address}|${amount ?? ""}`;
|
|
17
|
+
const acceptedPct = accepted?.fingerprint === fingerprint ? accepted.pct : null;
|
|
18
|
+
const [state, setState] = useState({ quote: null, slippageWarning: null, loading: false, error: null });
|
|
19
|
+
useEffect(() => {
|
|
20
|
+
if (!methodId || !address || !amount || !(Number(amount) > 0)) {
|
|
21
|
+
setState({ quote: null, slippageWarning: null, loading: false, error: null });
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
setState({ quote: null, slippageWarning: null, loading: true, error: null });
|
|
25
|
+
const controller = new AbortController();
|
|
26
|
+
const handle = setTimeout(() => {
|
|
27
|
+
client
|
|
28
|
+
.quoteWithdrawal({
|
|
29
|
+
methodId,
|
|
30
|
+
address,
|
|
31
|
+
amount,
|
|
32
|
+
...(acceptedPct !== null ? { acceptSlippagePct: acceptedPct } : {}),
|
|
33
|
+
}, controller.signal)
|
|
34
|
+
.then((result) => {
|
|
35
|
+
if (controller.signal.aborted)
|
|
36
|
+
return;
|
|
37
|
+
setState({
|
|
38
|
+
quote: result.quote,
|
|
39
|
+
slippageWarning: result.slippageWarning,
|
|
40
|
+
loading: false,
|
|
41
|
+
error: null,
|
|
42
|
+
});
|
|
43
|
+
})
|
|
44
|
+
.catch((error) => {
|
|
45
|
+
if (controller.signal.aborted || isAbortError(error))
|
|
46
|
+
return;
|
|
47
|
+
setState({ quote: null, slippageWarning: null, loading: false, error: toPayError(error) });
|
|
48
|
+
});
|
|
49
|
+
}, acceptedPct !== null ? 0 : QUOTE_DEBOUNCE_MS);
|
|
50
|
+
return () => {
|
|
51
|
+
clearTimeout(handle);
|
|
52
|
+
controller.abort();
|
|
53
|
+
};
|
|
54
|
+
}, [client, methodId, address, amount, acceptedPct]);
|
|
55
|
+
const warning = state.slippageWarning;
|
|
56
|
+
const accept = useCallback(() => {
|
|
57
|
+
if (!warning?.slippagePct)
|
|
58
|
+
return;
|
|
59
|
+
const pct = acceptSlippagePct(warning.slippagePct);
|
|
60
|
+
if (pct !== null)
|
|
61
|
+
setAccepted({ fingerprint, pct });
|
|
62
|
+
}, [warning, fingerprint]);
|
|
63
|
+
return { ...state, acceptedSlippagePct: acceptedPct, accept };
|
|
64
|
+
}
|
|
65
|
+
export function useCreateWithdrawal() {
|
|
66
|
+
const { client } = usePayRoot("useCreateWithdrawal()");
|
|
67
|
+
const [state, setState] = useState({
|
|
68
|
+
pending: false,
|
|
69
|
+
error: null,
|
|
70
|
+
withdrawal: null,
|
|
71
|
+
twoFactorRequired: false,
|
|
72
|
+
inProgressId: null,
|
|
73
|
+
});
|
|
74
|
+
const intents = useRef(createIntentKeys());
|
|
75
|
+
const busy = useRef(false);
|
|
76
|
+
const create = useCallback(async (input) => {
|
|
77
|
+
if (busy.current)
|
|
78
|
+
return null;
|
|
79
|
+
busy.current = true;
|
|
80
|
+
const idempotencyKey = intents.current.keyFor(toCreateWithdrawalBody(input));
|
|
81
|
+
setState((previous) => ({ ...previous, pending: true, error: null }));
|
|
82
|
+
try {
|
|
83
|
+
const withdrawal = await client.createWithdrawal(input, { idempotencyKey });
|
|
84
|
+
intents.current.settle({ ok: true });
|
|
85
|
+
client.emit({ type: "withdrawal.created", withdrawalId: withdrawal.id });
|
|
86
|
+
setState({
|
|
87
|
+
pending: false,
|
|
88
|
+
error: null,
|
|
89
|
+
withdrawal,
|
|
90
|
+
twoFactorRequired: false,
|
|
91
|
+
inProgressId: null,
|
|
92
|
+
});
|
|
93
|
+
return withdrawal;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
intents.current.settle({ ok: false, error });
|
|
97
|
+
const payError = toPayError(error);
|
|
98
|
+
setState((previous) => ({
|
|
99
|
+
...previous,
|
|
100
|
+
pending: false,
|
|
101
|
+
error: payError,
|
|
102
|
+
twoFactorRequired: previous.twoFactorRequired || payError.code.startsWith("TWO_FA_"),
|
|
103
|
+
inProgressId: payError.code === "WITHDRAWAL_IN_PROGRESS"
|
|
104
|
+
? (payError.details.withdrawalId ?? null)
|
|
105
|
+
: previous.inProgressId,
|
|
106
|
+
}));
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
finally {
|
|
110
|
+
busy.current = false;
|
|
111
|
+
}
|
|
112
|
+
}, [client]);
|
|
113
|
+
return { ...state, create };
|
|
114
|
+
}
|
|
115
|
+
export function useWithdrawal(id) {
|
|
116
|
+
const { client } = usePayRoot("useWithdrawal()");
|
|
117
|
+
const [state, setState] = useState({
|
|
118
|
+
withdrawal: null,
|
|
119
|
+
error: null,
|
|
120
|
+
loading: !!id,
|
|
121
|
+
});
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
if (!id) {
|
|
124
|
+
setState({ withdrawal: null, error: null, loading: false });
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
setState((previous) => ({ ...previous, loading: true }));
|
|
128
|
+
const poller = createPoller(async (signal) => {
|
|
129
|
+
try {
|
|
130
|
+
const withdrawal = await client.getWithdrawal(id, signal);
|
|
131
|
+
setState({ withdrawal, error: null, loading: false });
|
|
132
|
+
return isTerminalWithdrawal(withdrawal) ? "stop" : "continue";
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
if (!isAbortError(error)) {
|
|
136
|
+
setState((previous) => ({ ...previous, error: toPayError(error), loading: false }));
|
|
137
|
+
}
|
|
138
|
+
throw error;
|
|
139
|
+
}
|
|
140
|
+
}, { intervalMs: 4000, maxIntervalMs: 30000 });
|
|
141
|
+
poller.start();
|
|
142
|
+
return () => poller.stop();
|
|
143
|
+
}, [client, id]);
|
|
144
|
+
return state;
|
|
145
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export { Root } from "./Root.js";
|
|
2
|
+
export type { PayFallback, RootProps } from "./Root.js";
|
|
3
|
+
export { BackButton, Balance, Close, ErrorMessage, Loader, SubmitButton, Trigger, } from "./components/basic.js";
|
|
4
|
+
export type { BalanceProps, ErrorMessageProps } from "./components/basic.js";
|
|
5
|
+
export { CurrencyPicker, MethodPicker, NetworkPicker } from "./components/pickers.js";
|
|
6
|
+
export type { CurrencyPickerProps, MethodOption, MethodPickerProps, NetworkPickerProps, } from "./components/pickers.js";
|
|
7
|
+
export { AddressInput, AmountInput, PhoneInput, TwoFactorInput } from "./components/inputs.js";
|
|
8
|
+
export { Countdown, CryptoAddress, MethodNotice, Onramp, ReceiptUpload, RedirectButton, Requisites, RequisitesFrame, RestartButton, } from "./components/deposit.js";
|
|
9
|
+
export type { OnrampProps, RequisitesFrameProps, RequisitesProps } from "./components/deposit.js";
|
|
10
|
+
export { Quote, SlippageWarning, Status } from "./components/status.js";
|
|
11
|
+
export type { StatusProps } from "./components/status.js";
|
|
12
|
+
export { Qr } from "./components/Qr.js";
|
|
13
|
+
export type { QrProps } from "./components/Qr.js";
|
|
14
|
+
export { DepositForm } from "./presets/DepositForm.js";
|
|
15
|
+
export type { DepositFormComponents, DepositFormProps } from "./presets/DepositForm.js";
|
|
16
|
+
export { WithdrawForm } from "./presets/WithdrawForm.js";
|
|
17
|
+
export type { WithdrawFormComponents, WithdrawFormProps } from "./presets/WithdrawForm.js";
|
|
18
|
+
export { useBalance, useCountdown, useDepositFlow, usePay, usePayContext, usePayError, usePayEvents, usePayMessages, usePaymentMethods, useWithdrawFlow, } from "./hooks/root.js";
|
|
19
|
+
export type { UseBalanceResult, UseCountdownResult, UsePayContextResult, UsePayMessagesResult, UsePaymentMethodsResult, UsePayResult, } from "./hooks/root.js";
|
|
20
|
+
export { useAttachReceipt, useCreateDeposit, useCryptoDeposit, useDeposit, useDepositQuote, useOnramp, } from "./hooks/deposit.js";
|
|
21
|
+
export type { UseAttachReceiptResult, UseCreateDepositResult, UseCryptoDepositResult, UseDepositQuoteResult, UseDepositResult, UseOnrampResult, } from "./hooks/deposit.js";
|
|
22
|
+
export { useCreateWithdrawal, useWithdrawQuote, useWithdrawal } from "./hooks/withdraw.js";
|
|
23
|
+
export type { UseCreateWithdrawalResult, UseWithdrawQuoteResult, UseWithdrawalResult, } from "./hooks/withdraw.js";
|
|
24
|
+
export type { Appearance, AppearanceSkin, AppearanceTheme, TokenName } from "../core/appearance.js";
|
|
25
|
+
export type { PayErrorCode } from "../core/errors.js";
|
|
26
|
+
export { PayError } from "../core/errors.js";
|
|
27
|
+
export type { PayEvent, PayEventType } from "../core/events.js";
|
|
28
|
+
export type { MessageKey, PayLocale } from "../core/i18n/index.js";
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
export { Root } from "./Root.js";
|
|
3
|
+
export { BackButton, Balance, Close, ErrorMessage, Loader, SubmitButton, Trigger, } from "./components/basic.js";
|
|
4
|
+
export { CurrencyPicker, MethodPicker, NetworkPicker } from "./components/pickers.js";
|
|
5
|
+
export { AddressInput, AmountInput, PhoneInput, TwoFactorInput } from "./components/inputs.js";
|
|
6
|
+
export { Countdown, CryptoAddress, MethodNotice, Onramp, ReceiptUpload, RedirectButton, Requisites, RequisitesFrame, RestartButton, } from "./components/deposit.js";
|
|
7
|
+
export { Quote, SlippageWarning, Status } from "./components/status.js";
|
|
8
|
+
export { Qr } from "./components/Qr.js";
|
|
9
|
+
export { DepositForm } from "./presets/DepositForm.js";
|
|
10
|
+
export { WithdrawForm } from "./presets/WithdrawForm.js";
|
|
11
|
+
export { useBalance, useCountdown, useDepositFlow, usePay, usePayContext, usePayError, usePayEvents, usePayMessages, usePaymentMethods, useWithdrawFlow, } from "./hooks/root.js";
|
|
12
|
+
export { useAttachReceipt, useCreateDeposit, useCryptoDeposit, useDeposit, useDepositQuote, useOnramp, } from "./hooks/deposit.js";
|
|
13
|
+
export { useCreateWithdrawal, useWithdrawQuote, useWithdrawal } from "./hooks/withdraw.js";
|
|
14
|
+
export { PayError } from "../core/errors.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SENSITIVE_CLASS, SENSITIVE_PROPS, assignRef, cx, partProps } from "../ui/part.js";
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type ComponentType, type HTMLAttributes } from "react";
|
|
2
|
+
import * as ui from "../../ui/index.js";
|
|
3
|
+
import { type PartStyleProps } from "../part.js";
|
|
4
|
+
export interface DepositFormComponents {
|
|
5
|
+
Loader: ComponentType;
|
|
6
|
+
ErrorMessage: ComponentType;
|
|
7
|
+
Balance: ComponentType;
|
|
8
|
+
BackButton: ComponentType;
|
|
9
|
+
Close: ComponentType;
|
|
10
|
+
MethodPicker: ComponentType;
|
|
11
|
+
CurrencyPicker: ComponentType;
|
|
12
|
+
NetworkPicker: ComponentType;
|
|
13
|
+
AmountInput: ComponentType;
|
|
14
|
+
PhoneInput: ComponentType;
|
|
15
|
+
MethodNotice: ComponentType;
|
|
16
|
+
Quote: ComponentType;
|
|
17
|
+
SubmitButton: ComponentType;
|
|
18
|
+
CryptoAddress: ComponentType;
|
|
19
|
+
Requisites: ComponentType;
|
|
20
|
+
Countdown: ComponentType;
|
|
21
|
+
ReceiptUpload: ComponentType;
|
|
22
|
+
RedirectButton: ComponentType;
|
|
23
|
+
RestartButton: ComponentType;
|
|
24
|
+
Onramp: ComponentType;
|
|
25
|
+
Status: ComponentType;
|
|
26
|
+
}
|
|
27
|
+
export type DepositFormProps = Omit<HTMLAttributes<HTMLDivElement>, "className" | "style" | "children"> & PartStyleProps & {
|
|
28
|
+
components?: Partial<DepositFormComponents>;
|
|
29
|
+
};
|
|
30
|
+
export declare const DepositForm: import("react").ForwardRefExoticComponent<Omit<HTMLAttributes<HTMLDivElement>, "style" | "children" | "className"> & ui.PartStyleProps & {
|
|
31
|
+
components?: Partial<DepositFormComponents>;
|
|
32
|
+
} & import("react").RefAttributes<HTMLDivElement>>;
|