@kasufinance/kasu-sdk 2.6.0 → 2.7.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.
- package/README.md +128 -3
- package/dist/bundle.cjs.js +1119 -254
- package/dist/bundle.esm.js +1109 -255
- package/dist/domain/index.d.ts +3 -2
- package/dist/domain/index.js +2 -2
- package/dist/domain/index.js.map +1 -1
- package/dist/domain/requests.d.ts +42 -4
- package/dist/domain/requests.js +20 -4
- package/dist/domain/requests.js.map +1 -1
- package/dist/domain/wallet-errors.d.ts +63 -8
- package/dist/domain/wallet-errors.js +115 -16
- package/dist/domain/wallet-errors.js.map +1 -1
- package/dist/facade/deposits.js +2 -1
- package/dist/facade/deposits.js.map +1 -1
- package/dist/facade/flows.d.ts +95 -0
- package/dist/facade/flows.js +116 -0
- package/dist/facade/flows.js.map +1 -0
- package/dist/facade/index.d.ts +3 -0
- package/dist/facade/index.js +3 -0
- package/dist/facade/index.js.map +1 -1
- package/dist/facade/kasu.d.ts +6 -2
- package/dist/facade/kasu.js +28 -3
- package/dist/facade/kasu.js.map +1 -1
- package/dist/facade/read-only.d.ts +12 -0
- package/dist/facade/read-only.js +13 -0
- package/dist/facade/read-only.js.map +1 -0
- package/dist/flows/deposit-flow.d.ts +301 -0
- package/dist/flows/deposit-flow.js +358 -0
- package/dist/flows/deposit-flow.js.map +1 -0
- package/dist/flows/flow.d.ts +72 -0
- package/dist/flows/flow.js +110 -0
- package/dist/flows/flow.js.map +1 -0
- package/dist/flows/index.d.ts +19 -0
- package/dist/flows/index.js +17 -0
- package/dist/flows/index.js.map +1 -0
- package/dist/flows/observable.d.ts +59 -0
- package/dist/flows/observable.js +81 -0
- package/dist/flows/observable.js.map +1 -0
- package/dist/flows/withdraw-flow.d.ts +99 -0
- package/dist/flows/withdraw-flow.js +83 -0
- package/dist/flows/withdraw-flow.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/services/DataService/data-service.js +0 -1
- package/dist/services/DataService/data-service.js.map +1 -1
- package/package.json +8 -2
- package/src/domain/index.ts +7 -1
- package/src/domain/requests.ts +57 -4
- package/src/domain/wallet-errors.ts +135 -18
- package/src/facade/deposits.ts +2 -3
- package/src/facade/flows.ts +172 -0
- package/src/facade/index.ts +8 -0
- package/src/facade/kasu.ts +43 -3
- package/src/facade/read-only.ts +13 -0
- package/src/flows/deposit-flow.ts +775 -0
- package/src/flows/flow.ts +108 -0
- package/src/flows/index.ts +45 -0
- package/src/flows/observable.ts +97 -0
- package/src/flows/withdraw-flow.ts +210 -0
- package/src/index.ts +7 -0
- package/src/services/DataService/data-service.ts +0 -1
- package/dist/domain/au-minimum.test.d.ts +0 -1
- package/dist/domain/au-minimum.test.js +0 -202
- package/dist/domain/au-minimum.test.js.map +0 -1
- package/dist/domain/deposit-bounds.test.d.ts +0 -1
- package/dist/domain/deposit-bounds.test.js +0 -135
- package/dist/domain/deposit-bounds.test.js.map +0 -1
- package/dist/domain/loan-contract.test.d.ts +0 -1
- package/dist/domain/loan-contract.test.js +0 -255
- package/dist/domain/loan-contract.test.js.map +0 -1
- package/dist/domain/partners.test.d.ts +0 -1
- package/dist/domain/partners.test.js +0 -53
- package/dist/domain/partners.test.js.map +0 -1
- package/dist/domain/pools.test.d.ts +0 -1
- package/dist/domain/pools.test.js +0 -184
- package/dist/domain/pools.test.js.map +0 -1
- package/dist/domain/rates.test.d.ts +0 -1
- package/dist/domain/rates.test.js +0 -181
- package/dist/domain/rates.test.js.map +0 -1
- package/dist/domain/requests.test.d.ts +0 -1
- package/dist/domain/requests.test.js +0 -470
- package/dist/domain/requests.test.js.map +0 -1
- package/dist/domain/settlement.test.d.ts +0 -1
- package/dist/domain/settlement.test.js +0 -152
- package/dist/domain/settlement.test.js.map +0 -1
- package/dist/domain/tranche-display-name.test.d.ts +0 -1
- package/dist/domain/tranche-display-name.test.js +0 -58
- package/dist/domain/tranche-display-name.test.js.map +0 -1
- package/dist/domain/tranches.test.d.ts +0 -1
- package/dist/domain/tranches.test.js +0 -206
- package/dist/domain/tranches.test.js.map +0 -1
- package/dist/domain/wallet-errors.test.d.ts +0 -1
- package/dist/domain/wallet-errors.test.js +0 -71
- package/dist/domain/wallet-errors.test.js.map +0 -1
- package/dist/facade/config.test.d.ts +0 -1
- package/dist/facade/config.test.js +0 -216
- package/dist/facade/config.test.js.map +0 -1
- package/dist/facade/facade.test.d.ts +0 -1
- package/dist/facade/facade.test.js +0 -278
- package/dist/facade/facade.test.js.map +0 -1
- package/dist/services/Locking/calculate-apy.test.d.ts +0 -1
- package/dist/services/Locking/calculate-apy.test.js +0 -41
- package/dist/services/Locking/calculate-apy.test.js.map +0 -1
- package/dist/tests/sample.test.d.ts +0 -1
- package/dist/tests/sample.test.js +0 -59
- package/dist/tests/sample.test.js.map +0 -1
- package/src/domain/au-minimum.test.ts +0 -371
- package/src/domain/deposit-bounds.test.ts +0 -200
- package/src/domain/loan-contract.test.ts +0 -343
- package/src/domain/partners.test.ts +0 -83
- package/src/domain/pools.test.ts +0 -260
- package/src/domain/rates.test.ts +0 -254
- package/src/domain/requests.test.ts +0 -653
- package/src/domain/settlement.test.ts +0 -198
- package/src/domain/tranche-display-name.test.ts +0 -96
- package/src/domain/tranches.test.ts +0 -300
- package/src/domain/wallet-errors.test.ts +0 -100
- package/src/facade/config.test.ts +0 -265
- package/src/facade/facade.test.ts +0 -374
- package/src/services/Locking/calculate-apy.test.ts +0 -52
- package/src/tests/sample.test.ts +0 -63
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { FlowStore } from './observable';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The run lifecycle both flows share: one run at a time, every transition
|
|
5
|
+
* published, and a `reset()` that is safe to call in the middle of one.
|
|
6
|
+
*
|
|
7
|
+
* `DepositFlow` and `WithdrawFlow` each carried their own copy of this — the
|
|
8
|
+
* re-entrancy guard, the `try/finally` around the run, `state`, `isRunning`,
|
|
9
|
+
* `subscribe`, `reset`. Two copies of a guard is two places for a guard to be
|
|
10
|
+
* wrong, on the money path, which is the same reason the pipelines themselves
|
|
11
|
+
* moved out of the applications.
|
|
12
|
+
*
|
|
13
|
+
* ## The guard and the run token are separate, and both matter
|
|
14
|
+
*
|
|
15
|
+
* `_runToken` is the re-entrancy guard: claimed synchronously, before the
|
|
16
|
+
* first `await`, so a double tap cannot launch two pipelines. The store's
|
|
17
|
+
* `generation` is the abandonment mechanism: an in-flight run holds the value
|
|
18
|
+
* it claimed, and every `patch` and `isCurrent` checkpoint it makes after
|
|
19
|
+
* `reset()` is dropped.
|
|
20
|
+
*
|
|
21
|
+
* `reset()` releases the guard IMMEDIATELY rather than waiting for the
|
|
22
|
+
* abandoned run to notice. It has to: a run parked on a wallet prompt that
|
|
23
|
+
* never answers is a promise that may never settle, and a consumer that has
|
|
24
|
+
* left that screen must be able to start a new run in the same tick. The
|
|
25
|
+
* abandoned run's `finally` therefore releases the guard only if it still
|
|
26
|
+
* holds it — by then a NEW run may own it, and clearing it there would let a
|
|
27
|
+
* third run start alongside the second.
|
|
28
|
+
*/
|
|
29
|
+
export abstract class Flow<S, I> {
|
|
30
|
+
protected readonly _store: FlowStore<S>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The token of the run currently holding the guard, or `null` when none
|
|
34
|
+
* does. Not a boolean: releasing the guard has to be attributable to the
|
|
35
|
+
* run that claimed it.
|
|
36
|
+
*/
|
|
37
|
+
private _runToken: number | null = null;
|
|
38
|
+
|
|
39
|
+
protected constructor(initial: S) {
|
|
40
|
+
this._store = new FlowStore<S>(initial);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The current state. Every transition is also published to `subscribe`. */
|
|
44
|
+
get state(): S {
|
|
45
|
+
return this._store.state;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** True while a run is in flight, including while parked. */
|
|
49
|
+
get isRunning(): boolean {
|
|
50
|
+
return this._runToken !== null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Observe every transition. Returns the unsubscribe function.
|
|
55
|
+
*
|
|
56
|
+
* The listener is not called on subscribe; read `state` for the value it
|
|
57
|
+
* starts from.
|
|
58
|
+
*/
|
|
59
|
+
subscribe(listener: (state: S) => void): () => void {
|
|
60
|
+
return this._store.subscribe(listener);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Run the pipeline. Resolves when it reaches a terminal phase — it does
|
|
65
|
+
* not reject, because every outcome a consumer can act on is in `state`.
|
|
66
|
+
*
|
|
67
|
+
* A second call while one is in flight is a no-op: one submission per
|
|
68
|
+
* intent, however many times the button is pressed.
|
|
69
|
+
*/
|
|
70
|
+
async start(input: I): Promise<void> {
|
|
71
|
+
if (this._runToken !== null) return;
|
|
72
|
+
this._store.reset();
|
|
73
|
+
const token = this._store.beginRun();
|
|
74
|
+
this._runToken = token;
|
|
75
|
+
try {
|
|
76
|
+
await this._run(input, token);
|
|
77
|
+
} finally {
|
|
78
|
+
if (this._runToken === token) this._runToken = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Back to `idle`, abandoning any run in flight: its remaining transitions
|
|
84
|
+
* are dropped, whatever it is parked on is unparked, and the guard is
|
|
85
|
+
* released at once so `start()` is accepted in the SAME tick. Subscribers
|
|
86
|
+
* are kept — the consumer watching the last run is the one watching the
|
|
87
|
+
* next.
|
|
88
|
+
*/
|
|
89
|
+
reset(): void {
|
|
90
|
+
this._runToken = null;
|
|
91
|
+
// Bump the generation BEFORE unparking, so the abandoned run's next
|
|
92
|
+
// checkpoint already reads as superseded.
|
|
93
|
+
this._store.reset();
|
|
94
|
+
this._onAbandon();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Hook for a flow that parks: unpark it. Called by `reset()` after the
|
|
99
|
+
* generation has been bumped, so anything the abandoned run does with the
|
|
100
|
+
* result is dropped.
|
|
101
|
+
*/
|
|
102
|
+
protected _onAbandon(): void {
|
|
103
|
+
// A flow with nothing parked has nothing to unpark.
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The pipeline itself. `token` is this run's; check it after every await. */
|
|
107
|
+
protected abstract _run(input: I, token: number): Promise<void>;
|
|
108
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flows: the headless money-path state machines.
|
|
3
|
+
*
|
|
4
|
+
* `domain/` holds rules that are pure functions. These are the same idea one
|
|
5
|
+
* level up — a rule that unfolds over time, with side effects the consumer
|
|
6
|
+
* injects as ports. They own the ORDER of a deposit and a withdrawal, the
|
|
7
|
+
* guards around them, and the codes that describe where a run got to. They own
|
|
8
|
+
* no UI, no framework, no network and no words.
|
|
9
|
+
*
|
|
10
|
+
* Every application drove its own copy of the deposit pipeline before this, and
|
|
11
|
+
* the copies had already started to differ.
|
|
12
|
+
*/
|
|
13
|
+
export { Flow } from './flow';
|
|
14
|
+
export { FlowStore } from './observable';
|
|
15
|
+
export type { WaitableTransaction } from './observable';
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
CONTRACT_TTL_MS,
|
|
19
|
+
DepositFlow,
|
|
20
|
+
NO_SPENDER_MESSAGE,
|
|
21
|
+
} from './deposit-flow';
|
|
22
|
+
export type {
|
|
23
|
+
ContractMessageRequest,
|
|
24
|
+
DepositFailure,
|
|
25
|
+
DepositFlowInput,
|
|
26
|
+
DepositFlowOptions,
|
|
27
|
+
DepositPhase,
|
|
28
|
+
DepositPorts,
|
|
29
|
+
DepositState,
|
|
30
|
+
DepositStep,
|
|
31
|
+
GenerateContractRequest,
|
|
32
|
+
KycSignature,
|
|
33
|
+
LegacyContractRequest,
|
|
34
|
+
LoanAgreementRequest,
|
|
35
|
+
} from './deposit-flow';
|
|
36
|
+
|
|
37
|
+
export { NO_KYC_PARAMS_MESSAGE, WithdrawFlow } from './withdraw-flow';
|
|
38
|
+
export type {
|
|
39
|
+
WithdrawFailure,
|
|
40
|
+
WithdrawFlowInput,
|
|
41
|
+
WithdrawPhase,
|
|
42
|
+
WithdrawPorts,
|
|
43
|
+
WithdrawState,
|
|
44
|
+
WithdrawStep,
|
|
45
|
+
} from './withdraw-flow';
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tiny observable-state kernel both flows are built on.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately not a framework store: no React, no signals, no dependency.
|
|
5
|
+
* A flow is a long asynchronous run that has to publish every transition to
|
|
6
|
+
* whatever is rendering it, and has to be able to abandon a run that the
|
|
7
|
+
* consumer walked away from. That is the whole requirement, and it is 60 lines.
|
|
8
|
+
*
|
|
9
|
+
* The `generation` counter is the abandonment mechanism. `reset()` bumps it;
|
|
10
|
+
* an in-flight run captured the previous value, so every later `patch` it
|
|
11
|
+
* attempts is dropped and every `isCurrent` checkpoint tells it to stop. It is
|
|
12
|
+
* what makes `reset()` safe to call mid-flight — the run cannot resurrect a
|
|
13
|
+
* dead state or, worse, drive the UI back into a phase the consumer has left.
|
|
14
|
+
*/
|
|
15
|
+
export class FlowStore<S> {
|
|
16
|
+
private _state: S;
|
|
17
|
+
private _generation = 0;
|
|
18
|
+
private readonly _listeners = new Set<(state: S) => void>();
|
|
19
|
+
|
|
20
|
+
constructor(private readonly _initial: S) {
|
|
21
|
+
this._state = _initial;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
get state(): S {
|
|
25
|
+
return this._state;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The run token an in-flight pipeline captures at `start()`. */
|
|
29
|
+
get generation(): number {
|
|
30
|
+
return this._generation;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** False once `reset()` (or a later `start()`) has superseded `token`. */
|
|
34
|
+
isCurrent(token: number): boolean {
|
|
35
|
+
return this._generation === token;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Subscribe to every transition. Returns the unsubscribe function; calling
|
|
40
|
+
* it twice is harmless.
|
|
41
|
+
*
|
|
42
|
+
* Listeners are NOT called on subscribe — read `state` for the current
|
|
43
|
+
* value. A listener that throws is not allowed to break the pipeline or
|
|
44
|
+
* starve the listeners registered after it, so throws are swallowed.
|
|
45
|
+
*/
|
|
46
|
+
subscribe(listener: (state: S) => void): () => void {
|
|
47
|
+
this._listeners.add(listener);
|
|
48
|
+
return () => {
|
|
49
|
+
this._listeners.delete(listener);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Merge `next` into the state and notify, unless `token` names a
|
|
55
|
+
* superseded run.
|
|
56
|
+
*/
|
|
57
|
+
patch(next: Partial<S>, token?: number): void {
|
|
58
|
+
if (token !== undefined && !this.isCurrent(token)) return;
|
|
59
|
+
this._state = { ...this._state, ...next };
|
|
60
|
+
for (const listener of [...this._listeners]) {
|
|
61
|
+
try {
|
|
62
|
+
listener(this._state);
|
|
63
|
+
} catch {
|
|
64
|
+
// A consumer's rendering fault is its own problem; the pipeline
|
|
65
|
+
// owns real money and keeps going.
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Back to the initial state, abandoning any in-flight run.
|
|
72
|
+
*
|
|
73
|
+
* Subscribers are kept: the consumer that was watching the last run is the
|
|
74
|
+
* one that will watch the next.
|
|
75
|
+
*/
|
|
76
|
+
reset(): void {
|
|
77
|
+
this._generation += 1;
|
|
78
|
+
this.patch(this._initial);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Claim the next run token. Any earlier run is abandoned. */
|
|
82
|
+
beginRun(): number {
|
|
83
|
+
this._generation += 1;
|
|
84
|
+
return this._generation;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Anything with a `wait()` — an ethers `ContractTransaction`, or a fake.
|
|
90
|
+
*
|
|
91
|
+
* Lives beside the store rather than in either flow: both submit transactions
|
|
92
|
+
* through their own ports, and neither of them should have to import the
|
|
93
|
+
* other's file to say so.
|
|
94
|
+
*/
|
|
95
|
+
export interface WaitableTransaction {
|
|
96
|
+
wait(): Promise<unknown>;
|
|
97
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { BigNumber } from 'ethers';
|
|
2
|
+
|
|
3
|
+
import { classifyWalletFailure } from '../domain/wallet-errors';
|
|
4
|
+
import { KycParams } from '../facade/types';
|
|
5
|
+
|
|
6
|
+
import { Flow } from './flow';
|
|
7
|
+
import { WaitableTransaction } from './observable';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The withdrawal pipeline, headless — the small sibling of `DepositFlow`.
|
|
11
|
+
*
|
|
12
|
+
* Much less happens here, and that is the point of it being separate: no loan
|
|
13
|
+
* agreement, no approval (the lender is handing tranche shares back, not
|
|
14
|
+
* granting a spend), no on-chain KYC payload. What survives is the shape both
|
|
15
|
+
* applications need — an observable phase, one `'max'`-aware submission, and
|
|
16
|
+
* the same `cancelled` / `failed` split, so a lender who pressed Reject is
|
|
17
|
+
* never told something broke.
|
|
18
|
+
*
|
|
19
|
+
* The run lifecycle — the re-entrancy guard, `state`, `isRunning`,
|
|
20
|
+
* `subscribe`, `reset` — is `Flow`'s, shared with the deposit pipeline. It was
|
|
21
|
+
* a second copy here until 2.7.0, which is two places for one guard to be
|
|
22
|
+
* wrong.
|
|
23
|
+
*
|
|
24
|
+
* Same house rules as the deposit flow: no React, no copy, no I/O of its own.
|
|
25
|
+
* `state` carries codes; the consumer owns every word a lender reads.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* const flow = new WithdrawFlow(ports);
|
|
29
|
+
* flow.subscribe(render);
|
|
30
|
+
* await flow.start({ poolId, trancheId, amount: 'max', userAddress });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
// Codes
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
|
|
38
|
+
export type WithdrawPhase =
|
|
39
|
+
| 'idle'
|
|
40
|
+
| 'checking-kyc'
|
|
41
|
+
| 'request-sign'
|
|
42
|
+
| 'request-confirm'
|
|
43
|
+
| 'success'
|
|
44
|
+
| 'error';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* `kyc` only exists when the consumer supplies `getKycSignature`. kasu-mobile
|
|
48
|
+
* does, to fail early and legibly when a lender's KYC has lapsed rather than
|
|
49
|
+
* let the on-chain call revert; kasu-ui does not.
|
|
50
|
+
*/
|
|
51
|
+
export type WithdrawStep = 'kyc' | 'request';
|
|
52
|
+
|
|
53
|
+
export type WithdrawFailure =
|
|
54
|
+
| { step: WithdrawStep; reason: 'cancelled' }
|
|
55
|
+
| { step: WithdrawStep; reason: 'failed'; error: unknown };
|
|
56
|
+
|
|
57
|
+
// ---------------------------------------------------------------------------
|
|
58
|
+
// Ports, input, state
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
|
|
61
|
+
export interface WithdrawPorts {
|
|
62
|
+
/**
|
|
63
|
+
* Build the Nexera KYC params for the pre-check. Defaults to
|
|
64
|
+
* `kasu.deposits.buildKycParams`, exactly as the deposit flow's does.
|
|
65
|
+
*/
|
|
66
|
+
buildKycParams?(
|
|
67
|
+
userAddress: `0x${string}`,
|
|
68
|
+
): KycParams | Promise<KycParams>;
|
|
69
|
+
/**
|
|
70
|
+
* Exchange those params for a signature at the consumer's own backend, and
|
|
71
|
+
* so verify the lender's KYC BEFORE the wallet is opened — a lapsed one
|
|
72
|
+
* surfaces as `{ step: 'kyc' }` instead of an opaque revert. The
|
|
73
|
+
* withdrawal call itself does not carry the signature.
|
|
74
|
+
*
|
|
75
|
+
* Supplying this port is what turns the pre-check on; there is no default,
|
|
76
|
+
* because the SDK does not know the application's backend. This is the
|
|
77
|
+
* same pair the deposit flow uses, rather than the opaque `ensureKyc` of
|
|
78
|
+
* earlier drafts, so one KYC pre-check exists across both money paths.
|
|
79
|
+
*/
|
|
80
|
+
getKycSignature?(params: KycParams): Promise<unknown>;
|
|
81
|
+
/** `requestWithdrawalInAsset`. Defaults to `kasu.deposits.withdraw`. */
|
|
82
|
+
withdraw(params: {
|
|
83
|
+
poolId: string;
|
|
84
|
+
trancheId: string;
|
|
85
|
+
amount: BigNumber;
|
|
86
|
+
}): Promise<WaitableTransaction>;
|
|
87
|
+
/** `requestWithdrawalMax`. Defaults to `kasu.deposits.withdrawMax`. */
|
|
88
|
+
withdrawMax(
|
|
89
|
+
poolId: string,
|
|
90
|
+
trancheId: string,
|
|
91
|
+
userAddress: string,
|
|
92
|
+
): Promise<WaitableTransaction>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface WithdrawFlowInput {
|
|
96
|
+
poolId: string;
|
|
97
|
+
trancheId: string;
|
|
98
|
+
/**
|
|
99
|
+
* The amount in BASE units, or the literal `'max'`.
|
|
100
|
+
*
|
|
101
|
+
* `'max'` is not "the balance as I last read it": it routes to the
|
|
102
|
+
* all-shares contract call, which resolves the balance on chain at
|
|
103
|
+
* execution. Deciding max-ness from a number the consumer read a moment ago
|
|
104
|
+
* is how dust gets stranded in a tranche, so the decision is a code, made
|
|
105
|
+
* by the consumer, and carried here intact.
|
|
106
|
+
*/
|
|
107
|
+
amount: BigNumber | 'max';
|
|
108
|
+
userAddress: `0x${string}`;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface WithdrawState {
|
|
112
|
+
phase: WithdrawPhase;
|
|
113
|
+
step: WithdrawStep | null;
|
|
114
|
+
/** True when this run routed through the all-shares call. */
|
|
115
|
+
isMax: boolean;
|
|
116
|
+
failure: WithdrawFailure | null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const INITIAL: WithdrawState = {
|
|
120
|
+
phase: 'idle',
|
|
121
|
+
step: null,
|
|
122
|
+
isMax: false,
|
|
123
|
+
failure: null,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
/** What a KYC pre-check with no `buildKycParams` to call fails with. */
|
|
127
|
+
export const NO_KYC_PARAMS_MESSAGE =
|
|
128
|
+
'WithdrawFlow: getKycSignature was supplied without buildKycParams; build the flow with kasu.flows.withdraw() or pass both';
|
|
129
|
+
|
|
130
|
+
// ---------------------------------------------------------------------------
|
|
131
|
+
// The flow
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
|
|
134
|
+
export class WithdrawFlow extends Flow<WithdrawState, WithdrawFlowInput> {
|
|
135
|
+
constructor(private readonly _ports: WithdrawPorts) {
|
|
136
|
+
super(INITIAL);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
protected async _run(
|
|
140
|
+
input: WithdrawFlowInput,
|
|
141
|
+
token: number,
|
|
142
|
+
): Promise<void> {
|
|
143
|
+
const ports = this._ports;
|
|
144
|
+
const isMax = input.amount === 'max';
|
|
145
|
+
|
|
146
|
+
// 1. The optional KYC pre-check: build the params, exchange them for a
|
|
147
|
+
// signature. Both ports reach the application's own backend, so a
|
|
148
|
+
// throw here is ALWAYS `failed` — running it through the wallet
|
|
149
|
+
// rejection classifier would let a backend's "Declined" be reported
|
|
150
|
+
// to a lender as something they did, with the real error dropped.
|
|
151
|
+
if (ports.getKycSignature) {
|
|
152
|
+
this._store.patch(
|
|
153
|
+
{ phase: 'checking-kyc', step: 'kyc', isMax },
|
|
154
|
+
token,
|
|
155
|
+
);
|
|
156
|
+
try {
|
|
157
|
+
if (!ports.buildKycParams) {
|
|
158
|
+
throw new Error(NO_KYC_PARAMS_MESSAGE);
|
|
159
|
+
}
|
|
160
|
+
const params = await ports.buildKycParams(input.userAddress);
|
|
161
|
+
await ports.getKycSignature(params);
|
|
162
|
+
} catch (err) {
|
|
163
|
+
this._store.patch(
|
|
164
|
+
{
|
|
165
|
+
phase: 'error',
|
|
166
|
+
failure: { step: 'kyc', reason: 'failed', error: err },
|
|
167
|
+
},
|
|
168
|
+
token,
|
|
169
|
+
);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
if (!this._store.isCurrent(token)) return;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 2. The submission. This one IS a wallet call, so it keeps the
|
|
176
|
+
// `cancelled` / `failed` split.
|
|
177
|
+
this._store.patch(
|
|
178
|
+
{ phase: 'request-sign', step: 'request', isMax },
|
|
179
|
+
token,
|
|
180
|
+
);
|
|
181
|
+
try {
|
|
182
|
+
const tx =
|
|
183
|
+
input.amount === 'max'
|
|
184
|
+
? await ports.withdrawMax(
|
|
185
|
+
input.poolId,
|
|
186
|
+
input.trancheId,
|
|
187
|
+
input.userAddress.toLowerCase(),
|
|
188
|
+
)
|
|
189
|
+
: await ports.withdraw({
|
|
190
|
+
poolId: input.poolId,
|
|
191
|
+
trancheId: input.trancheId,
|
|
192
|
+
amount: input.amount,
|
|
193
|
+
});
|
|
194
|
+
this._store.patch({ phase: 'request-confirm' }, token);
|
|
195
|
+
await tx.wait();
|
|
196
|
+
} catch (err) {
|
|
197
|
+
this._store.patch(
|
|
198
|
+
{
|
|
199
|
+
phase: 'error',
|
|
200
|
+
failure: classifyWalletFailure('request', err),
|
|
201
|
+
},
|
|
202
|
+
token,
|
|
203
|
+
);
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (!this._store.isCurrent(token)) return;
|
|
207
|
+
|
|
208
|
+
this._store.patch({ phase: 'success' }, token);
|
|
209
|
+
}
|
|
210
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -67,3 +67,10 @@ export * from './facade';
|
|
|
67
67
|
// only: no copy, no locale. See `src/domain/index.ts`.
|
|
68
68
|
// ---------------------------------------------------------------------------
|
|
69
69
|
export * from './domain';
|
|
70
|
+
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
// Flows — the headless deposit / withdraw state machines. No React, no copy,
|
|
73
|
+
// no I/O of their own: every side effect is an injected port and every
|
|
74
|
+
// observable state is a code. See `src/flows/index.ts`.
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
export * from './flows';
|
|
@@ -547,7 +547,6 @@ export class DataService {
|
|
|
547
547
|
const poolDelegateProfileAndHistoryDirectus: PoolDelegateProfileAndHistoryDirectus[] =
|
|
548
548
|
(await this._directus.request(
|
|
549
549
|
readItems('PoolDelegateProfileAndHistory', {
|
|
550
|
-
// @ts-ignore Directus' deep-fields typing is fragile across versions
|
|
551
550
|
fields: ['*', { otherPools: ['*'] }],
|
|
552
551
|
}),
|
|
553
552
|
)) as unknown as PoolDelegateProfileAndHistoryDirectus[];
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|