-
Notifications
You must be signed in to change notification settings - Fork 676
Expand file tree
/
Copy pathpaymentMachine.ts
More file actions
294 lines (264 loc) · 8.27 KB
/
Copy pathpaymentMachine.ts
File metadata and controls
294 lines (264 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { useCallback, useState } from "react";
import type { Quote } from "../../../bridge/index.js";
import type { Token } from "../../../bridge/types/Token.js";
import type { Address } from "../../../utils/address.js";
import type { AsyncStorage } from "../../../utils/storage/AsyncStorage.js";
import type { Wallet } from "../../../wallets/interfaces/wallet.js";
import type { WindowAdapter } from "../adapters/WindowAdapter.js";
import type {
BridgePrepareRequest,
BridgePrepareResult,
} from "../hooks/useBridgePrepare.js";
import type { CompletedStatusResult } from "../hooks/useStepExecutor.js";
/**
* Payment modes supported by BridgeEmbed
*/
type PaymentMode = "fund_wallet" | "direct_payment" | "transaction";
/**
* Payment method types with their required data
*/
export type PaymentMethod =
| {
type: "wallet";
payerWallet: Wallet;
originToken: Token;
balance: bigint;
quote: Quote;
}
| {
type: "fiat";
payerWallet: Wallet;
currency: string;
onramp: "stripe" | "coinbase" | "transak";
};
/**
* Payment machine context - holds all flow state data
*/
export interface PaymentMachineContext {
// Flow configuration
mode: PaymentMode;
// Target requirements (resolved in init state)
destinationAmount?: string;
destinationToken?: Token;
receiverAddress?: Address;
// User selections (set in methodSelection state)
selectedPaymentMethod?: PaymentMethod;
// Prepared quote data (set in quote state)
quote?: BridgePrepareResult;
request?: BridgePrepareRequest;
// Execution results (set in execute state on completion)
completedStatuses?: CompletedStatusResult[];
// Error handling
currentError?: Error;
retryState?: PaymentMachineState; // State to retry from
// Dependency injection
adapters: {
window: WindowAdapter;
storage: AsyncStorage;
};
}
/**
* Events that can be sent to the payment machine
*/
type PaymentMachineEvent =
| {
type: "DESTINATION_CONFIRMED";
destinationToken: Token;
destinationAmount: string;
receiverAddress: Address;
}
| { type: "PAYMENT_METHOD_SELECTED"; paymentMethod: PaymentMethod }
| {
type: "QUOTE_RECEIVED";
quote: BridgePrepareResult;
request: BridgePrepareRequest;
}
| { type: "ROUTE_CONFIRMED" }
| { type: "EXECUTION_COMPLETE"; completedStatuses: CompletedStatusResult[] }
| { type: "ERROR_OCCURRED"; error: Error }
| { type: "CONTINUE_TO_TRANSACTION" }
| { type: "RETRY" }
| { type: "RESET" }
| { type: "BACK" };
type PaymentMachineState =
| "init"
| "methodSelection"
| "quote"
| "preview"
| "execute"
| "success"
| "post-buy-transaction"
| "error";
/**
* Hook to create and use the payment machine
*/
export function usePaymentMachine(
adapters: PaymentMachineContext["adapters"],
mode: PaymentMode = "fund_wallet",
) {
const [currentState, setCurrentState] = useState<PaymentMachineState>("init");
const [context, setContext] = useState<PaymentMachineContext>({
adapters,
mode,
});
const send = useCallback(
(event: PaymentMachineEvent) => {
setCurrentState((state) => {
setContext((ctx) => {
switch (state) {
case "init":
if (event.type === "DESTINATION_CONFIRMED") {
return {
...ctx,
destinationAmount: event.destinationAmount,
destinationToken: event.destinationToken,
receiverAddress: event.receiverAddress,
};
} else if (event.type === "ERROR_OCCURRED") {
return {
...ctx,
currentError: event.error,
retryState: "init",
};
}
break;
case "methodSelection":
if (event.type === "PAYMENT_METHOD_SELECTED") {
return {
...ctx,
quote: undefined, // reset quote when method changes
selectedPaymentMethod: event.paymentMethod,
};
} else if (event.type === "ERROR_OCCURRED") {
return {
...ctx,
currentError: event.error,
retryState: "methodSelection",
};
}
break;
case "quote":
if (event.type === "QUOTE_RECEIVED") {
return {
...ctx,
quote: event.quote,
request: event.request,
};
} else if (event.type === "ERROR_OCCURRED") {
return {
...ctx,
currentError: event.error,
retryState: "quote",
};
}
break;
case "preview":
if (event.type === "ERROR_OCCURRED") {
return {
...ctx,
currentError: event.error,
retryState: "preview",
};
}
break;
case "execute":
if (event.type === "EXECUTION_COMPLETE") {
return {
...ctx,
completedStatuses: event.completedStatuses,
};
} else if (event.type === "ERROR_OCCURRED") {
return {
...ctx,
currentError: event.error,
retryState: "execute",
};
}
break;
case "error":
if (event.type === "RETRY" || event.type === "RESET") {
return {
...ctx,
currentError: undefined,
retryState: undefined,
};
}
break;
case "success":
if (event.type === "RESET") {
return {
adapters: ctx.adapters,
mode: ctx.mode,
};
}
break;
case "post-buy-transaction":
if (event.type === "RESET") {
return {
adapters: ctx.adapters,
mode: ctx.mode,
};
}
break;
}
return ctx;
});
// State transitions
switch (state) {
case "init":
if (event.type === "DESTINATION_CONFIRMED")
return "methodSelection";
if (event.type === "ERROR_OCCURRED") return "error";
if (event.type === "CONTINUE_TO_TRANSACTION")
return "post-buy-transaction";
break;
case "methodSelection":
if (event.type === "PAYMENT_METHOD_SELECTED") return "quote";
if (event.type === "BACK") return "init";
if (event.type === "ERROR_OCCURRED") return "error";
break;
case "quote":
if (event.type === "QUOTE_RECEIVED") return "preview";
if (event.type === "BACK") return "methodSelection";
if (event.type === "ERROR_OCCURRED") return "error";
break;
case "preview":
if (event.type === "ROUTE_CONFIRMED") return "execute";
if (event.type === "BACK") return "methodSelection";
if (event.type === "ERROR_OCCURRED") return "error";
break;
case "execute":
if (event.type === "EXECUTION_COMPLETE") return "success";
if (event.type === "BACK") return "preview";
if (event.type === "ERROR_OCCURRED") return "error";
break;
case "success":
if (event.type === "CONTINUE_TO_TRANSACTION")
return "post-buy-transaction";
if (event.type === "RESET") return "init";
break;
case "post-buy-transaction":
if (event.type === "RESET") return "init";
break;
case "error":
if (event.type === "RETRY") {
return context.retryState ?? "init";
}
if (event.type === "RESET") {
return "init";
}
break;
}
return state;
});
},
[context.retryState],
);
return [
{
context,
value: currentState,
},
send,
] as const;
}