@kasufinance/kasu-sdk 2.6.0 → 2.7.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 +167 -4
- package/dist/bundle.cjs.js +1389 -257
- package/dist/bundle.esm.js +1375 -258
- package/dist/domain/index.d.ts +5 -2
- package/dist/domain/index.js +3 -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/revert-errors.d.ts +59 -0
- package/dist/domain/revert-errors.js +190 -0
- package/dist/domain/revert-errors.js.map +1 -0
- 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 +332 -0
- package/dist/flows/deposit-flow.js +436 -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 +10 -1
- package/src/domain/requests.ts +57 -4
- package/src/domain/revert-errors.ts +248 -0
- 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 +884 -0
- package/src/flows/flow.ts +108 -0
- package/src/flows/index.ts +47 -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
package/dist/domain/index.d.ts
CHANGED
|
@@ -30,11 +30,14 @@ export { asContractType, buildContractVersionType, buildFullNameRequestMessage,
|
|
|
30
30
|
export type { ContractListItem, ContractSection, ContractType, ExemptLoanContract, GenerateContractResponse, LoanContractFormatted, ResolvedContractResponse, RetailLoanContract, } from './loan-contract';
|
|
31
31
|
export { maxNetRateCeiling, pickHighestYieldTranche, poolMaxApy, selectVisiblePools, } from './pools';
|
|
32
32
|
export type { BestTranche } from './pools';
|
|
33
|
-
export { countSubmissions, deriveRequestState, firstSubmissionTimestamp, isCycleClosed, submissionEvents, } from './requests';
|
|
33
|
+
export { countSubmissions, deriveRequestState, firstSubmissionTimestamp, isCycleClosed, lastEventTimestamp, submissionEvents, } from './requests';
|
|
34
34
|
export type { RequestKind, RequestState, RequestStatusCode, } from './requests';
|
|
35
|
+
export { decodeRevert, extractRevertData } from './revert-errors';
|
|
36
|
+
export type { DecodedRevert, RevertFamily } from './revert-errors';
|
|
35
37
|
export { CLEARING_WINDOW_SECONDS, computeSettlementWindow, deriveCycleDates, nextCycleBoundary, } from './settlement';
|
|
36
38
|
export type { CycleDates, SettlementWindowInput, SettlementWindowState, } from './settlement';
|
|
37
39
|
export { getTrancheDisplayName, UPPER_MEZZANINE } from './tranche-display-name';
|
|
38
40
|
export { compareTrancheSeniority, derivePoolStatus, MIN_TRANCHE_CAPACITY, netTrancheApyBounds, pickDefaultTrancheId, poolAllTranchesFull, trancheApyBounds, trancheHasCapacity, trancheRiskRank, } from './tranches';
|
|
39
41
|
export type { ApyBounds, PoolStatus, TrancheCapacitySignal, } from './tranches';
|
|
40
|
-
export { isUnpredictableGas, isUserRejected } from './wallet-errors';
|
|
42
|
+
export { classifyWalletFailure, isUnpredictableGas, isUserRejected, } from './wallet-errors';
|
|
43
|
+
export type { WalletFailure } from './wallet-errors';
|
package/dist/domain/index.js
CHANGED
|
@@ -25,9 +25,10 @@ export { ceilToCents, floorToCents, isBelowMinimumCapacity, MAX_LENDING_AMOUNT_F
|
|
|
25
25
|
export { APXIUM, getCreditOriginator, getInstitutionalLender, INVOICEMATE, RIXON_CAPITAL, } from './partners';
|
|
26
26
|
export { asContractType, buildContractVersionType, buildFullNameRequestMessage, buildLegacyContractRequestMessage, buildLoanAgreementSignMessage, encodeDepositData, formatSignTimestampUtc, parseFormattedMessage, } from './loan-contract';
|
|
27
27
|
export { maxNetRateCeiling, pickHighestYieldTranche, poolMaxApy, selectVisiblePools, } from './pools';
|
|
28
|
-
export { countSubmissions, deriveRequestState, firstSubmissionTimestamp, isCycleClosed, submissionEvents, } from './requests';
|
|
28
|
+
export { countSubmissions, deriveRequestState, firstSubmissionTimestamp, isCycleClosed, lastEventTimestamp, submissionEvents, } from './requests';
|
|
29
|
+
export { decodeRevert, extractRevertData } from './revert-errors';
|
|
29
30
|
export { CLEARING_WINDOW_SECONDS, computeSettlementWindow, deriveCycleDates, nextCycleBoundary, } from './settlement';
|
|
30
31
|
export { getTrancheDisplayName, UPPER_MEZZANINE } from './tranche-display-name';
|
|
31
32
|
export { compareTrancheSeniority, derivePoolStatus, MIN_TRANCHE_CAPACITY, netTrancheApyBounds, pickDefaultTrancheId, poolAllTranchesFull, trancheApyBounds, trancheHasCapacity, trancheRiskRank, } from './tranches';
|
|
32
|
-
export { isUnpredictableGas, isUserRejected } from './wallet-errors';
|
|
33
|
+
export { classifyWalletFailure, isUnpredictableGas, isUserRejected, } from './wallet-errors';
|
|
33
34
|
//# sourceMappingURL=index.js.map
|
package/dist/domain/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/domain/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EACH,cAAc,EACd,cAAc,EACd,cAAc,EACd,eAAe,GAClB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACH,SAAS,EACT,2BAA2B,EAC3B,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,eAAe,GAClB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACH,WAAW,EACX,YAAY,EACZ,sBAAsB,EACtB,2BAA2B,EAC3B,2BAA2B,EAC3B,iBAAiB,EACjB,qBAAqB,EACrB,oBAAoB,GACvB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACH,MAAM,EACN,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,EACX,aAAa,GAChB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACH,cAAc,EACd,wBAAwB,EACxB,2BAA2B,EAC3B,iCAAiC,EACjC,6BAA6B,EAC7B,iBAAiB,EACjB,sBAAsB,EACtB,qBAAqB,GACxB,MAAM,iBAAiB,CAAC;AAYzB,OAAO,EACH,iBAAiB,EACjB,uBAAuB,EACvB,UAAU,EACV,kBAAkB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACH,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,aAAa,EACb,gBAAgB,GACnB,MAAM,YAAY,CAAC;AAOpB,OAAO,EACH,uBAAuB,EACvB,uBAAuB,EACvB,gBAAgB,EAChB,iBAAiB,GACpB,MAAM,cAAc,CAAC;AAOtB,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEhF,OAAO,EACH,uBAAuB,EACvB,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,GAClB,MAAM,YAAY,CAAC;AAOpB,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/domain/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,EACH,cAAc,EACd,cAAc,EACd,cAAc,EACd,eAAe,GAClB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACH,SAAS,EACT,2BAA2B,EAC3B,kBAAkB,EAClB,cAAc,EACd,iBAAiB,EACjB,eAAe,EACf,eAAe,GAClB,MAAM,cAAc,CAAC;AAGtB,OAAO,EACH,WAAW,EACX,YAAY,EACZ,sBAAsB,EACtB,2BAA2B,EAC3B,2BAA2B,EAC3B,iBAAiB,EACjB,qBAAqB,EACrB,oBAAoB,GACvB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACH,MAAM,EACN,mBAAmB,EACnB,sBAAsB,EACtB,WAAW,EACX,aAAa,GAChB,MAAM,YAAY,CAAC;AAGpB,OAAO,EACH,cAAc,EACd,wBAAwB,EACxB,2BAA2B,EAC3B,iCAAiC,EACjC,6BAA6B,EAC7B,iBAAiB,EACjB,sBAAsB,EACtB,qBAAqB,GACxB,MAAM,iBAAiB,CAAC;AAYzB,OAAO,EACH,iBAAiB,EACjB,uBAAuB,EACvB,UAAU,EACV,kBAAkB,GACrB,MAAM,SAAS,CAAC;AAGjB,OAAO,EACH,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,aAAa,EACb,kBAAkB,EAClB,gBAAgB,GACnB,MAAM,YAAY,CAAC;AAOpB,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGlE,OAAO,EACH,uBAAuB,EACvB,uBAAuB,EACvB,gBAAgB,EAChB,iBAAiB,GACpB,MAAM,cAAc,CAAC;AAOtB,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,MAAM,wBAAwB,CAAC;AAEhF,OAAO,EACH,uBAAuB,EACvB,gBAAgB,EAChB,oBAAoB,EACpB,mBAAmB,EACnB,oBAAoB,EACpB,mBAAmB,EACnB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,GAClB,MAAM,YAAY,CAAC;AAOpB,OAAO,EACH,qBAAqB,EACrB,kBAAkB,EAClB,cAAc,GACjB,MAAM,iBAAiB,CAAC"}
|
|
@@ -87,6 +87,36 @@ export interface RequestState {
|
|
|
87
87
|
* here.
|
|
88
88
|
*/
|
|
89
89
|
initiatedAmount: number | null;
|
|
90
|
+
/**
|
|
91
|
+
* The rejected figure the subgraph reported, parsed with the same `num`
|
|
92
|
+
* rule as the rest: an absent or unparseable value is 0. Unlike
|
|
93
|
+
* `acceptedAmount` this is NOT nullable — it feeds the partial-vs-complete
|
|
94
|
+
* branch, where "no figure" and "nothing rejected" mean the same thing.
|
|
95
|
+
*/
|
|
96
|
+
rejectedAmount: number;
|
|
97
|
+
/**
|
|
98
|
+
* `assetAmount` of the reallocation event, or 0 when the request was not
|
|
99
|
+
* reallocated. The amount that LEFT the requested tranche — which is not
|
|
100
|
+
* necessarily `acceptedAmount`, because a partly-filled request can be
|
|
101
|
+
* reallocated for only part of itself.
|
|
102
|
+
*/
|
|
103
|
+
reallocatedOutAmount: number;
|
|
104
|
+
/**
|
|
105
|
+
* RAW name of the tranche a reallocated deposit was accepted into, or
|
|
106
|
+
* `null` when there was no reallocation. RAW for the same reason
|
|
107
|
+
* `trancheName` is: the app calls `getTrancheDisplayName` on it at the
|
|
108
|
+
* view boundary, and a renamed value must never reach matching code.
|
|
109
|
+
*/
|
|
110
|
+
reallocationTargetTrancheName: string | null;
|
|
111
|
+
/**
|
|
112
|
+
* The row's highest-resolution timestamp: the LATEST event, or
|
|
113
|
+
* `request.timestamp` when the timeline is empty or older. Unix seconds.
|
|
114
|
+
*
|
|
115
|
+
* `firstSubmissionTimestamp` says when the lender asked; this says when
|
|
116
|
+
* anything last happened to the request, which is what a "last updated"
|
|
117
|
+
* column and a recency sort need.
|
|
118
|
+
*/
|
|
119
|
+
lastTimestamp: number;
|
|
90
120
|
/**
|
|
91
121
|
* The signed figure the row shows, chosen by the same branch logic
|
|
92
122
|
* kasu-ui uses: positive for an inflow, negative for an outflow, 0 for a
|
|
@@ -123,6 +153,14 @@ export declare function countSubmissions(events: Pick<UserRequestEvent, 'request
|
|
|
123
153
|
* that is (kasu-ui passes the request's own timestamp).
|
|
124
154
|
*/
|
|
125
155
|
export declare function firstSubmissionTimestamp(events: Pick<UserRequestEvent, 'requestType' | 'timestamp'>[], fallback: number): number;
|
|
156
|
+
/**
|
|
157
|
+
* The LATEST event timestamp, or `fallback` when the timeline is empty — or
|
|
158
|
+
* when every event predates it, because `fallback` seeds the reduction. That
|
|
159
|
+
* seeding is deliberate and is kasu-ui's behaviour: `request.timestamp` is a
|
|
160
|
+
* fact about the request, and an event indexed with an earlier clock must not
|
|
161
|
+
* make the row look older than the request itself.
|
|
162
|
+
*/
|
|
163
|
+
export declare function lastEventTimestamp(events: Pick<UserRequestEvent, 'timestamp'>[], fallback: number): number;
|
|
126
164
|
/**
|
|
127
165
|
* Has THIS request's cycle closed? — the single open/closed signal behind the
|
|
128
166
|
* status vocabulary and behind Cancel.
|
|
@@ -170,10 +208,10 @@ export declare function isCycleClosed(request: Pick<UserRequest, 'canCancel'>):
|
|
|
170
208
|
* status would strand it.
|
|
171
209
|
*
|
|
172
210
|
* WHAT THE APPLICATION STILL OWNS: the status word and the detail line beneath
|
|
173
|
-
* it; the tranche display rename (`getTrancheDisplayName` on `trancheName
|
|
174
|
-
* and on
|
|
175
|
-
* split; the amount format. The "view loan agreement" affordance is
|
|
176
|
-
* it follows from two fields already here —
|
|
211
|
+
* it; the tranche display rename (`getTrancheDisplayName` on `trancheName`
|
|
212
|
+
* and on `reallocationTargetTrancheName`, both of which are RAW here); the
|
|
213
|
+
* pool-name split; the amount format. The "view loan agreement" affordance is
|
|
214
|
+
* a fact, and it follows from two fields already here —
|
|
177
215
|
* `requestType === 'Deposit' && statusCode !== 'cancelled' && statusCode !== 'rejected'`
|
|
178
216
|
* — because neither a cancelled nor a fully-rejected deposit ever issued one,
|
|
179
217
|
* and withdrawals sign no agreement at all.
|
package/dist/domain/requests.js
CHANGED
|
@@ -29,6 +29,16 @@ export function firstSubmissionTimestamp(events, fallback) {
|
|
|
29
29
|
return fallback;
|
|
30
30
|
return submissions.reduce((min, e) => (e.timestamp < min ? e.timestamp : min), Infinity);
|
|
31
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* The LATEST event timestamp, or `fallback` when the timeline is empty — or
|
|
34
|
+
* when every event predates it, because `fallback` seeds the reduction. That
|
|
35
|
+
* seeding is deliberate and is kasu-ui's behaviour: `request.timestamp` is a
|
|
36
|
+
* fact about the request, and an event indexed with an earlier clock must not
|
|
37
|
+
* make the row look older than the request itself.
|
|
38
|
+
*/
|
|
39
|
+
export function lastEventTimestamp(events, fallback) {
|
|
40
|
+
return events.reduce((max, e) => (e.timestamp > max ? e.timestamp : max), fallback);
|
|
41
|
+
}
|
|
32
42
|
/**
|
|
33
43
|
* Has THIS request's cycle closed? — the single open/closed signal behind the
|
|
34
44
|
* status vocabulary and behind Cancel.
|
|
@@ -112,10 +122,10 @@ const numOrNull = (str) => {
|
|
|
112
122
|
* status would strand it.
|
|
113
123
|
*
|
|
114
124
|
* WHAT THE APPLICATION STILL OWNS: the status word and the detail line beneath
|
|
115
|
-
* it; the tranche display rename (`getTrancheDisplayName` on `trancheName
|
|
116
|
-
* and on
|
|
117
|
-
* split; the amount format. The "view loan agreement" affordance is
|
|
118
|
-
* it follows from two fields already here —
|
|
125
|
+
* it; the tranche display rename (`getTrancheDisplayName` on `trancheName`
|
|
126
|
+
* and on `reallocationTargetTrancheName`, both of which are RAW here); the
|
|
127
|
+
* pool-name split; the amount format. The "view loan agreement" affordance is
|
|
128
|
+
* a fact, and it follows from two fields already here —
|
|
119
129
|
* `requestType === 'Deposit' && statusCode !== 'cancelled' && statusCode !== 'rejected'`
|
|
120
130
|
* — because neither a cancelled nor a fully-rejected deposit ever issued one,
|
|
121
131
|
* and withdrawals sign no agreement at all.
|
|
@@ -150,6 +160,12 @@ export function deriveRequestState(request) {
|
|
|
150
160
|
initiatedAmount: initiated
|
|
151
161
|
? initiatedAmountOf(request.events)
|
|
152
162
|
: null,
|
|
163
|
+
rejectedAmount: rejected,
|
|
164
|
+
reallocatedOutAmount: reallocation ? num(reallocation.assetAmount) : 0,
|
|
165
|
+
reallocationTargetTrancheName: reallocation
|
|
166
|
+
? reallocation.trancheName
|
|
167
|
+
: null,
|
|
168
|
+
lastTimestamp: lastEventTimestamp(request.events, request.timestamp),
|
|
153
169
|
submissionCount: countSubmissions(request.events),
|
|
154
170
|
// `firstSubmissionTimestamp` needs a fallback it will never use here:
|
|
155
171
|
// the bundle is non-empty on every path that reaches the call.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requests.js","sourceRoot":"","sources":["../../src/domain/requests.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;
|
|
1
|
+
{"version":3,"file":"requests.js","sourceRoot":"","sources":["../../src/domain/requests.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAC;AA8J3E;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAiD,IAAI,GAAG,CAC3E,CAAC,WAAW,EAAE,WAAW,CAAC,CAC7B,CAAC;AAEF;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAE9B,MAAW;IACT,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC;AACtE,CAAC;AAED,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAC5B,MAA+C;IAE/C,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;AAC3C,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,wBAAwB,CACpC,MAA6D,EAC7D,QAAgB;IAEhB,MAAM,WAAW,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC7C,IAAI,WAAW,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,QAAQ,CAAC;IAC9C,OAAO,WAAW,CAAC,MAAM,CACrB,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,EACnD,QAAQ,CACX,CAAC;AACN,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,kBAAkB,CAC9B,MAA6C,EAC7C,QAAgB;IAEhB,OAAO,MAAM,CAAC,MAAM,CAChB,CAAC,GAA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AI,YAAY,EAAE,CAAC;YACf,MAAM,MAAM,GAAG,QAAQ,GAAG,SAAS,CAAC;YACpC,uCACO,IAAI,KACP,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,CAAC,QAAQ,EACjB,UAAU,EAAE,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,IAC7C;QACN,CAAC;QAED,kDAAkD;QAClD,IAAI,QAAQ,KAAK,CAAC,EAAE,CAAC;YACjB,uCACO,IAAI,KACP,IAAI,EAAE,SAAS,EACf,MAAM,EAAE,CAAC,EACT,UAAU,EAAE,UAAU,IACxB;QACN,CAAC;QACD,IAAI,QAAQ,GAAG,CAAC,EAAE,CAAC;YACf,uCACO,IAAI,KACP,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,QAAQ,EAChB,UAAU,EAAE,SAAS,IACvB;QACN,CAAC;QACD,uCACO,IAAI,KACP,IAAI,EAAE,QAAQ,EACd,MAAM,EAAE,QAAQ,EAChB,UAAU,EAAE,UAAU,IACxB;IACN,CAAC;IAED,sEAAsE;IACtE,sEAAsE;IACtE,uCACO,IAAI,KACP,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,EACzC,MAAM,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,EAC7C,UAAU,EAAE,SAAS,IACvB;AACN,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Revert decoding — turn the bytes a failed call came back with into the name
|
|
3
|
+
* the contract actually reverted with.
|
|
4
|
+
*
|
|
5
|
+
* `isUnpredictableGas` says only THAT the call would revert. That was enough
|
|
6
|
+
* while the answer was always the same one: a `transferFrom` failing on a
|
|
7
|
+
* balance that cannot cover the deposit. It is not enough any more, because
|
|
8
|
+
* `requestDepositWithKyc` reverts for a whole family of reasons the ABI
|
|
9
|
+
* declares and the lender can do nothing about — the pool is stopped, the pool
|
|
10
|
+
* is mid-clearing, their KYC lapsed, they are not on the allow list, the
|
|
11
|
+
* allow-list signature's block window closed. Every one of those was reported
|
|
12
|
+
* as "insufficient balance", so a fully funded lender was told to top up a
|
|
13
|
+
* wallet that was never short.
|
|
14
|
+
*
|
|
15
|
+
* The revert data carries the answer. This module extracts it from whatever
|
|
16
|
+
* envelope the provider wrapped it in and matches the 4-byte selector against
|
|
17
|
+
* the custom errors the protocol declares.
|
|
18
|
+
*
|
|
19
|
+
* Pure, like everything else in `domain/`: bytes in, a name out. It never
|
|
20
|
+
* decides what a lender is shown — `DepositFlow` maps the name to a code and
|
|
21
|
+
* the application maps the code to its own words.
|
|
22
|
+
*/
|
|
23
|
+
/**
|
|
24
|
+
* Which family a recognised revert belongs to, because the two are acted on
|
|
25
|
+
* differently.
|
|
26
|
+
*
|
|
27
|
+
* `erc20` is the case `insufficient-balance` was named for and still means:
|
|
28
|
+
* the token transfer itself failed, so the lender genuinely has to fund or
|
|
29
|
+
* re-approve. `protocol` is everything else the protocol declares — a
|
|
30
|
+
* condition of the pool or the lender's standing, which topping up cannot fix.
|
|
31
|
+
*/
|
|
32
|
+
export type RevertFamily = 'protocol' | 'erc20';
|
|
33
|
+
export interface DecodedRevert {
|
|
34
|
+
/** The custom error's name, exactly as the ABI declares it. */
|
|
35
|
+
name: string;
|
|
36
|
+
family: RevertFamily;
|
|
37
|
+
/** The reason of a `require(..., "…")` revert, when it was one. */
|
|
38
|
+
reason?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Find the revert data on an error, wherever the provider left it.
|
|
42
|
+
*
|
|
43
|
+
* ethers v5 raises `UNPREDICTABLE_GAS_LIMIT` with the provider's own error
|
|
44
|
+
* attached, and each provider nests the bytes one layer differently:
|
|
45
|
+
* `error.data`, `error.error.data`, `error.data.originalError.data`, or only
|
|
46
|
+
* inside the JSON `body` of a `SERVER_ERROR`. Rather than enumerate the
|
|
47
|
+
* products, walk a fixed set of keys to a fixed depth and take the first value
|
|
48
|
+
* that is shaped like revert data.
|
|
49
|
+
*/
|
|
50
|
+
export declare function extractRevertData(err: unknown): string | null;
|
|
51
|
+
/**
|
|
52
|
+
* Decode a failed call's revert into the error the contract named, or `null`
|
|
53
|
+
* when there is nothing recognisable to name.
|
|
54
|
+
*
|
|
55
|
+
* `null` is the honest answer for an unknown selector, an unrelated `require`
|
|
56
|
+
* string, or an error with no revert data at all — the caller keeps whatever
|
|
57
|
+
* it did before rather than inventing a diagnosis from bytes it cannot read.
|
|
58
|
+
*/
|
|
59
|
+
export declare function decodeRevert(err: unknown): DecodedRevert | null;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { IKasuAllowListAbi__factory } from '../contracts/factories/IKasuAllowListAbi__factory';
|
|
3
|
+
import { ILendingPoolManagerAbi__factory } from '../contracts/factories/ILendingPoolManagerAbi__factory';
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// The selector tables
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
/**
|
|
8
|
+
* OpenZeppelin v5's ERC-20 errors. They are not in either Kasu ABI because no
|
|
9
|
+
* Kasu contract declares them — they arrive from the TOKEN, through the
|
|
10
|
+
* manager's `transferFrom`, and they are the modern spelling of the string
|
|
11
|
+
* reverts below.
|
|
12
|
+
*/
|
|
13
|
+
const ERC20_ERROR_ABI = [
|
|
14
|
+
'error ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed)',
|
|
15
|
+
'error ERC20InsufficientAllowance(address spender, uint256 allowance, uint256 needed)',
|
|
16
|
+
];
|
|
17
|
+
/**
|
|
18
|
+
* The protocol errors that mean the ERC-20 transfer failed rather than the
|
|
19
|
+
* protocol refusing.
|
|
20
|
+
*
|
|
21
|
+
* `SafeERC20FailedOperation` is OpenZeppelin's wrapper around a token call
|
|
22
|
+
* that returned false or reverted, which on this path IS the `transferFrom`;
|
|
23
|
+
* `AddressInsufficientBalance` is its native-value sibling. Both belong with
|
|
24
|
+
* the balance case, not with the pool conditions — a lender who sees one of
|
|
25
|
+
* them really does need to fund or re-approve.
|
|
26
|
+
*/
|
|
27
|
+
const ERC20_FAMILY_NAMES = new Set([
|
|
28
|
+
'AddressInsufficientBalance',
|
|
29
|
+
'ERC20InsufficientAllowance',
|
|
30
|
+
'ERC20InsufficientBalance',
|
|
31
|
+
'SafeERC20FailedOperation',
|
|
32
|
+
]);
|
|
33
|
+
/** Solidity's `Error(string)`, the selector every `require` reason arrives under. */
|
|
34
|
+
const STRING_REVERT_SELECTOR = '0x08c379a0';
|
|
35
|
+
/**
|
|
36
|
+
* The `require` reasons that are a balance or an allowance, across the token
|
|
37
|
+
* implementations in circulation: OpenZeppelin v4's sentences, and the
|
|
38
|
+
* shorthand the minimal implementations use.
|
|
39
|
+
*/
|
|
40
|
+
const ERC20_REASON_PATTERNS = [
|
|
41
|
+
/exceeds\s+balance/,
|
|
42
|
+
/exceeds\s+allowance/,
|
|
43
|
+
/insufficient\s+balance/,
|
|
44
|
+
/insufficient\s+allowance/,
|
|
45
|
+
/transfer_from_failed/,
|
|
46
|
+
/transferfrom\s+failed/,
|
|
47
|
+
/low-level\s+call\s+failed/,
|
|
48
|
+
];
|
|
49
|
+
/**
|
|
50
|
+
* selector → error name, built once on first use.
|
|
51
|
+
*
|
|
52
|
+
* Lazily, because a consumer that never sees a revert should not pay for three
|
|
53
|
+
* `Interface` constructions at import. The two Kasu tables are the GENERATED
|
|
54
|
+
* typechain ABIs, so this cannot drift from the contracts: regenerate the ABIs
|
|
55
|
+
* and the new errors are recognised with no edit here.
|
|
56
|
+
*/
|
|
57
|
+
let selectorTable = null;
|
|
58
|
+
function selectors() {
|
|
59
|
+
if (selectorTable)
|
|
60
|
+
return selectorTable;
|
|
61
|
+
const table = new Map();
|
|
62
|
+
const interfaces = [
|
|
63
|
+
// The manager the deposit is called against, and the allow list it
|
|
64
|
+
// delegates the KYC and block-window checks to.
|
|
65
|
+
ILendingPoolManagerAbi__factory.createInterface(),
|
|
66
|
+
IKasuAllowListAbi__factory.createInterface(),
|
|
67
|
+
new ethers.utils.Interface(ERC20_ERROR_ABI),
|
|
68
|
+
];
|
|
69
|
+
for (const iface of interfaces) {
|
|
70
|
+
for (const fragment of Object.values(iface.errors)) {
|
|
71
|
+
// First writer wins: the same name declared by both ABIs is the
|
|
72
|
+
// same selector, so the duplicate would be a no-op anyway.
|
|
73
|
+
const sighash = iface.getSighash(fragment);
|
|
74
|
+
if (!table.has(sighash))
|
|
75
|
+
table.set(sighash, fragment.name);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
selectorTable = table;
|
|
79
|
+
return table;
|
|
80
|
+
}
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// Getting the bytes out of the envelope
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
/**
|
|
85
|
+
* How deep to look for the revert data. Providers nest it differently —
|
|
86
|
+
* ethers' own error wraps the provider's, which wraps the RPC body — and a
|
|
87
|
+
* bound is what keeps a self-referencing error object from being walked
|
|
88
|
+
* forever.
|
|
89
|
+
*/
|
|
90
|
+
const MAX_DEPTH = 6;
|
|
91
|
+
/** The keys ethers and the providers under it put a wrapped error on. */
|
|
92
|
+
const NESTED_KEYS = [
|
|
93
|
+
'data',
|
|
94
|
+
'error',
|
|
95
|
+
'originalError',
|
|
96
|
+
'info',
|
|
97
|
+
'cause',
|
|
98
|
+
'body',
|
|
99
|
+
];
|
|
100
|
+
/** At least a 4-byte selector, and a whole number of bytes. */
|
|
101
|
+
function isRevertData(value) {
|
|
102
|
+
return (value.length >= 10 &&
|
|
103
|
+
value.length % 2 === 0 &&
|
|
104
|
+
/^0x[0-9a-fA-F]+$/.test(value));
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Find the revert data on an error, wherever the provider left it.
|
|
108
|
+
*
|
|
109
|
+
* ethers v5 raises `UNPREDICTABLE_GAS_LIMIT` with the provider's own error
|
|
110
|
+
* attached, and each provider nests the bytes one layer differently:
|
|
111
|
+
* `error.data`, `error.error.data`, `error.data.originalError.data`, or only
|
|
112
|
+
* inside the JSON `body` of a `SERVER_ERROR`. Rather than enumerate the
|
|
113
|
+
* products, walk a fixed set of keys to a fixed depth and take the first value
|
|
114
|
+
* that is shaped like revert data.
|
|
115
|
+
*/
|
|
116
|
+
export function extractRevertData(err) {
|
|
117
|
+
return walk(err, 0);
|
|
118
|
+
}
|
|
119
|
+
function walk(node, depth) {
|
|
120
|
+
if (node == null || depth > MAX_DEPTH)
|
|
121
|
+
return null;
|
|
122
|
+
if (typeof node === 'string') {
|
|
123
|
+
if (isRevertData(node))
|
|
124
|
+
return node;
|
|
125
|
+
// A `SERVER_ERROR`'s body: the RPC's JSON reply, verbatim.
|
|
126
|
+
if (!node.startsWith('{'))
|
|
127
|
+
return null;
|
|
128
|
+
try {
|
|
129
|
+
return walk(JSON.parse(node), depth + 1);
|
|
130
|
+
}
|
|
131
|
+
catch (_a) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (typeof node !== 'object')
|
|
136
|
+
return null;
|
|
137
|
+
const record = node;
|
|
138
|
+
for (const key of NESTED_KEYS) {
|
|
139
|
+
const found = walk(record[key], depth + 1);
|
|
140
|
+
if (found)
|
|
141
|
+
return found;
|
|
142
|
+
}
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
// ---------------------------------------------------------------------------
|
|
146
|
+
// Decoding
|
|
147
|
+
// ---------------------------------------------------------------------------
|
|
148
|
+
/**
|
|
149
|
+
* Decode a failed call's revert into the error the contract named, or `null`
|
|
150
|
+
* when there is nothing recognisable to name.
|
|
151
|
+
*
|
|
152
|
+
* `null` is the honest answer for an unknown selector, an unrelated `require`
|
|
153
|
+
* string, or an error with no revert data at all — the caller keeps whatever
|
|
154
|
+
* it did before rather than inventing a diagnosis from bytes it cannot read.
|
|
155
|
+
*/
|
|
156
|
+
export function decodeRevert(err) {
|
|
157
|
+
const data = extractRevertData(err);
|
|
158
|
+
if (!data)
|
|
159
|
+
return null;
|
|
160
|
+
const selector = data.slice(0, 10).toLowerCase();
|
|
161
|
+
if (selector === STRING_REVERT_SELECTOR) {
|
|
162
|
+
const reason = decodeStringRevert(data);
|
|
163
|
+
if (reason === null)
|
|
164
|
+
return null;
|
|
165
|
+
return isErc20Reason(reason)
|
|
166
|
+
? { name: 'Error', family: 'erc20', reason }
|
|
167
|
+
: null;
|
|
168
|
+
}
|
|
169
|
+
const name = selectors().get(selector);
|
|
170
|
+
if (!name)
|
|
171
|
+
return null;
|
|
172
|
+
return {
|
|
173
|
+
name,
|
|
174
|
+
family: ERC20_FAMILY_NAMES.has(name) ? 'erc20' : 'protocol',
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
function decodeStringRevert(data) {
|
|
178
|
+
try {
|
|
179
|
+
const [reason] = ethers.utils.defaultAbiCoder.decode(['string'], `0x${data.slice(10)}`);
|
|
180
|
+
return reason;
|
|
181
|
+
}
|
|
182
|
+
catch (_a) {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function isErc20Reason(reason) {
|
|
187
|
+
const lower = reason.toLowerCase();
|
|
188
|
+
return ERC20_REASON_PATTERNS.some((pattern) => pattern.test(lower));
|
|
189
|
+
}
|
|
190
|
+
//# sourceMappingURL=revert-errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"revert-errors.js","sourceRoot":"","sources":["../../src/domain/revert-errors.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,0BAA0B,EAAE,MAAM,mDAAmD,CAAC;AAC/F,OAAO,EAAE,+BAA+B,EAAE,MAAM,wDAAwD,CAAC;AAgDzG,8EAA8E;AAC9E,sBAAsB;AACtB,8EAA8E;AAE9E;;;;;GAKG;AACH,MAAM,eAAe,GAAG;IACpB,iFAAiF;IACjF,sFAAsF;CACzF,CAAC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IAClB,CAAC;IAAC,WAAM,CAAC;QACL,OAAO,IAAI,CAAC;IAChB,CAAC;AACL,CAAC;AAED,SAAS,aAAa,CAAC,MAAc;IACjC,MAAM,KAAK,GAAG,MAAM,CAAC,WAAW,EAAE,CAAC;IACnC,OAAO,qBAAqB,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;AACxE,CAAC"}
|
|
@@ -8,21 +8,51 @@
|
|
|
8
8
|
* enumerated once here.
|
|
9
9
|
*
|
|
10
10
|
* Lifted from kasu-ui's `src/lib/web3/is-user-rejected.ts` and kasu-mobile's
|
|
11
|
-
* `src/features/lending/lib/errors.ts
|
|
11
|
+
* `src/features/lending/lib/errors.ts` — and now the UNION of the two, so
|
|
12
|
+
* neither app has to keep a wrapper on top of this one.
|
|
12
13
|
*/
|
|
13
14
|
/**
|
|
14
15
|
* Did the lender reject the request in their wallet?
|
|
15
16
|
*
|
|
16
|
-
* Providers surface a rejection in different shapes
|
|
17
|
+
* Providers surface a rejection in different shapes, and the union of them is
|
|
18
|
+
* the point of this function existing once:
|
|
17
19
|
* - MetaMask and most EIP-1193 wallets: `code: 4001`
|
|
20
|
+
* - Coinbase Wallet and ethers v5: `code: 'ACTION_REJECTED'`
|
|
21
|
+
* - viem: a `UserRejectedRequestError` name
|
|
18
22
|
* - WalletConnect and some Privy paths: `Error('User rejected the request')`
|
|
19
|
-
* -
|
|
20
|
-
*
|
|
23
|
+
* - a provider error WRAPPED by another layer, carrying the real code and
|
|
24
|
+
* message on a nested `error` — the shape Privy's embedded wallet
|
|
25
|
+
* surfaces on Expo, where the outer object says nothing useful
|
|
26
|
+
* - ethers' own `reason` field, which is often the only place the text lands
|
|
27
|
+
* - a PLAIN OBJECT carrying `message`, which `String(err)` would read as
|
|
28
|
+
* `"[object Object]"`
|
|
21
29
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
30
|
+
* Previously kasu-ui's implementation verbatim, with kasu-mobile keeping its
|
|
31
|
+
* own superset on top. That is precisely the drift this layer exists to stop —
|
|
32
|
+
* a rejection kasu-mobile recognised and kasu-ui did not was reported to the
|
|
33
|
+
* same lender as a failure on one app and a cancellation on the other. The
|
|
34
|
+
* union lives here; the mobile wrapper goes.
|
|
35
|
+
*
|
|
36
|
+
* ## The text is read for a SUBJECT, not for a keyword
|
|
37
|
+
*
|
|
38
|
+
* The words alone are not the signal. "Declined" and "request rejected" are
|
|
39
|
+
* also what a rate limiter, a risk engine and a KYC decision say, and those
|
|
40
|
+
* arrive wrapped in exactly the same envelope a wallet error does — ethers'
|
|
41
|
+
* `SERVER_ERROR` around `-32603` carries the upstream body on a nested
|
|
42
|
+
* `error.message`, so `{ error: { message: 'request rejected: rate limit
|
|
43
|
+
* exceeded' } }` is an RPC refusing to serve us, not a lender refusing to
|
|
44
|
+
* sign. Reporting one as the other tells a lender they cancelled something
|
|
45
|
+
* they never saw, and DROPS the real error on the floor.
|
|
46
|
+
*
|
|
47
|
+
* So a rejection is a machine-readable wallet code, or a sentence that names
|
|
48
|
+
* the party who did it: "user rejected", "declined by the user", "cancelled by
|
|
49
|
+
* the wallet". A bare "declined" is not a rejection, whatever else is on the
|
|
50
|
+
* envelope.
|
|
51
|
+
*
|
|
52
|
+
* Within that rule, being generous is the safe direction: calling a genuine
|
|
53
|
+
* fault a cancellation costs a lender one retry, while calling a deliberate
|
|
54
|
+
* rejection a failure sends them to support to report a bug that does not
|
|
55
|
+
* exist.
|
|
26
56
|
*/
|
|
27
57
|
export declare function isUserRejected(err: unknown): boolean;
|
|
28
58
|
/**
|
|
@@ -35,3 +65,28 @@ export declare function isUserRejected(err: unknown): boolean;
|
|
|
35
65
|
* re-check its preconditions rather than invite a retry.
|
|
36
66
|
*/
|
|
37
67
|
export declare function isUnpredictableGas(err: unknown): boolean;
|
|
68
|
+
/** A step that failed, as a code plus whether the lender chose it. */
|
|
69
|
+
export type WalletFailure<Step> = {
|
|
70
|
+
step: Step;
|
|
71
|
+
reason: 'cancelled';
|
|
72
|
+
} | {
|
|
73
|
+
step: Step;
|
|
74
|
+
reason: 'failed';
|
|
75
|
+
error: unknown;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* The `cancelled` / `failed` split, for a WALLET call on any step of any flow.
|
|
79
|
+
*
|
|
80
|
+
* A lender who pressed Reject is not a fault. Reporting one as the other is
|
|
81
|
+
* how a support queue fills with people who did exactly what they meant to.
|
|
82
|
+
*
|
|
83
|
+
* Generic in the step so both flows share one implementation — it was
|
|
84
|
+
* duplicated byte-for-byte in each of them, which is the same drift this
|
|
85
|
+
* layer exists to stop, one level up.
|
|
86
|
+
*
|
|
87
|
+
* Only pass it an error a WALLET produced. An HTTP port's throw is always a
|
|
88
|
+
* `failed`: the lender's wallet was not involved in it, so a backend that
|
|
89
|
+
* happens to word a refusal "declined" must never be shown to them as
|
|
90
|
+
* something they did.
|
|
91
|
+
*/
|
|
92
|
+
export declare function classifyWalletFailure<Step>(step: Step, err: unknown): WalletFailure<Step>;
|