@esseyonsui/sui-sdk-v3 0.0.0-stage → 0.1.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 +45 -2
- package/dist/config.d.ts +82 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +180 -0
- package/dist/errors.d.ts +38 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +32 -0
- package/dist/essey.d.ts +866 -0
- package/dist/essey.d.ts.map +1 -0
- package/dist/essey.js +3592 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/keeper.d.ts +54 -0
- package/dist/keeper.d.ts.map +1 -0
- package/dist/keeper.js +44 -0
- package/dist/network.d.ts +17 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +62 -0
- package/dist/v3/addr-field.d.ts +3 -0
- package/dist/v3/addr-field.d.ts.map +1 -0
- package/dist/v3/addr-field.js +10 -0
- package/dist/v3/bodies.d.ts +54 -0
- package/dist/v3/bodies.d.ts.map +1 -0
- package/dist/v3/bodies.js +68 -0
- package/dist/v3/body-args.d.ts +29 -0
- package/dist/v3/body-args.d.ts.map +1 -0
- package/dist/v3/body-args.js +78 -0
- package/dist/v3/collateral/bodies.d.ts +78 -0
- package/dist/v3/collateral/bodies.d.ts.map +1 -0
- package/dist/v3/collateral/bodies.js +136 -0
- package/dist/v3/collateral/ext.d.ts +21 -0
- package/dist/v3/collateral/ext.d.ts.map +1 -0
- package/dist/v3/collateral/ext.js +19 -0
- package/dist/v3/collateral/health.d.ts +115 -0
- package/dist/v3/collateral/health.d.ts.map +1 -0
- package/dist/v3/collateral/health.js +255 -0
- package/dist/v3/collateral/index.d.ts +9 -0
- package/dist/v3/collateral/index.d.ts.map +1 -0
- package/dist/v3/collateral/index.js +8 -0
- package/dist/v3/collateral/local-health.d.ts +23 -0
- package/dist/v3/collateral/local-health.d.ts.map +1 -0
- package/dist/v3/collateral/local-health.js +27 -0
- package/dist/v3/collateral/math.d.ts +33 -0
- package/dist/v3/collateral/math.d.ts.map +1 -0
- package/dist/v3/collateral/math.js +38 -0
- package/dist/v3/collateral/openings.d.ts +27 -0
- package/dist/v3/collateral/openings.d.ts.map +1 -0
- package/dist/v3/collateral/openings.js +28 -0
- package/dist/v3/collateral/scan.d.ts +41 -0
- package/dist/v3/collateral/scan.d.ts.map +1 -0
- package/dist/v3/collateral/scan.js +76 -0
- package/dist/v3/collateral/seal.d.ts +31 -0
- package/dist/v3/collateral/seal.d.ts.map +1 -0
- package/dist/v3/collateral/seal.js +59 -0
- package/dist/v3/credential/issue.d.ts +27 -0
- package/dist/v3/credential/issue.d.ts.map +1 -0
- package/dist/v3/credential/issue.js +109 -0
- package/dist/v3/credential/membership.d.ts +37 -0
- package/dist/v3/credential/membership.d.ts.map +1 -0
- package/dist/v3/credential/membership.js +34 -0
- package/dist/v3/credential/pow.d.ts +10 -0
- package/dist/v3/credential/pow.d.ts.map +1 -0
- package/dist/v3/credential/pow.js +22 -0
- package/dist/v3/domains.d.ts +141 -0
- package/dist/v3/domains.d.ts.map +1 -0
- package/dist/v3/domains.js +158 -0
- package/dist/v3/ext.d.ts +29 -0
- package/dist/v3/ext.d.ts.map +1 -0
- package/dist/v3/ext.js +82 -0
- package/dist/v3/faucet.d.ts +66 -0
- package/dist/v3/faucet.d.ts.map +1 -0
- package/dist/v3/faucet.js +64 -0
- package/dist/v3/fee-witness.d.ts +5 -0
- package/dist/v3/fee-witness.d.ts.map +1 -0
- package/dist/v3/fee-witness.js +18 -0
- package/dist/v3/fees.d.ts +78 -0
- package/dist/v3/fees.d.ts.map +1 -0
- package/dist/v3/fees.js +190 -0
- package/dist/v3/index.d.ts +24 -0
- package/dist/v3/index.d.ts.map +1 -0
- package/dist/v3/index.js +23 -0
- package/dist/v3/inputs.d.ts +89 -0
- package/dist/v3/inputs.d.ts.map +1 -0
- package/dist/v3/inputs.js +152 -0
- package/dist/v3/keeper-badge.d.ts +48 -0
- package/dist/v3/keeper-badge.d.ts.map +1 -0
- package/dist/v3/keeper-badge.js +49 -0
- package/dist/v3/keeper-payload.d.ts +36 -0
- package/dist/v3/keeper-payload.d.ts.map +1 -0
- package/dist/v3/keeper-payload.js +116 -0
- package/dist/v3/kinds.d.ts +23 -0
- package/dist/v3/kinds.d.ts.map +1 -0
- package/dist/v3/kinds.js +50 -0
- package/dist/v3/loans/bodies.d.ts +215 -0
- package/dist/v3/loans/bodies.d.ts.map +1 -0
- package/dist/v3/loans/bodies.js +255 -0
- package/dist/v3/loans/offer-plan.d.ts +66 -0
- package/dist/v3/loans/offer-plan.d.ts.map +1 -0
- package/dist/v3/loans/offer-plan.js +141 -0
- package/dist/v3/loans/open-plan.d.ts +62 -0
- package/dist/v3/loans/open-plan.d.ts.map +1 -0
- package/dist/v3/loans/open-plan.js +92 -0
- package/dist/v3/loans/reclaim-plan.d.ts +65 -0
- package/dist/v3/loans/reclaim-plan.d.ts.map +1 -0
- package/dist/v3/loans/reclaim-plan.js +125 -0
- package/dist/v3/loans/refi-plan.d.ts +62 -0
- package/dist/v3/loans/refi-plan.d.ts.map +1 -0
- package/dist/v3/loans/refi-plan.js +157 -0
- package/dist/v3/loans/repay-plan.d.ts +40 -0
- package/dist/v3/loans/repay-plan.d.ts.map +1 -0
- package/dist/v3/loans/repay-plan.js +77 -0
- package/dist/v3/loans/settle-plan.d.ts +152 -0
- package/dist/v3/loans/settle-plan.d.ts.map +1 -0
- package/dist/v3/loans/settle-plan.js +210 -0
- package/dist/v3/market.d.ts +38 -0
- package/dist/v3/market.d.ts.map +1 -0
- package/dist/v3/market.js +30 -0
- package/dist/v3/notes/denominations.d.ts +13 -0
- package/dist/v3/notes/denominations.d.ts.map +1 -0
- package/dist/v3/notes/denominations.js +35 -0
- package/dist/v3/notes/deposit.d.ts +57 -0
- package/dist/v3/notes/deposit.d.ts.map +1 -0
- package/dist/v3/notes/deposit.js +103 -0
- package/dist/v3/notes/derive.d.ts +16 -0
- package/dist/v3/notes/derive.d.ts.map +1 -0
- package/dist/v3/notes/derive.js +34 -0
- package/dist/v3/notes/faucet.d.ts +3 -0
- package/dist/v3/notes/faucet.d.ts.map +1 -0
- package/dist/v3/notes/faucet.js +8 -0
- package/dist/v3/notes/index.d.ts +9 -0
- package/dist/v3/notes/index.d.ts.map +1 -0
- package/dist/v3/notes/index.js +8 -0
- package/dist/v3/notes/keys.d.ts +5 -0
- package/dist/v3/notes/keys.d.ts.map +1 -0
- package/dist/v3/notes/keys.js +11 -0
- package/dist/v3/notes/opening.d.ts +16 -0
- package/dist/v3/notes/opening.d.ts.map +1 -0
- package/dist/v3/notes/opening.js +62 -0
- package/dist/v3/notes/scan.d.ts +32 -0
- package/dist/v3/notes/scan.d.ts.map +1 -0
- package/dist/v3/notes/scan.js +71 -0
- package/dist/v3/notes/tree.d.ts +9 -0
- package/dist/v3/notes/tree.d.ts.map +1 -0
- package/dist/v3/notes/tree.js +31 -0
- package/dist/v3/notes/withdraw.d.ts +12 -0
- package/dist/v3/notes/withdraw.d.ts.map +1 -0
- package/dist/v3/notes/withdraw.js +27 -0
- package/dist/v3/proof-bytes.d.ts +4 -0
- package/dist/v3/proof-bytes.d.ts.map +1 -0
- package/dist/v3/proof-bytes.js +22 -0
- package/dist/v3/prover.d.ts +34 -0
- package/dist/v3/prover.d.ts.map +1 -0
- package/dist/v3/prover.js +76 -0
- package/dist/v3/relay/client.d.ts +57 -0
- package/dist/v3/relay/client.d.ts.map +1 -0
- package/dist/v3/relay/client.js +80 -0
- package/dist/v3/relay/possession.d.ts +21 -0
- package/dist/v3/relay/possession.d.ts.map +1 -0
- package/dist/v3/relay/possession.js +45 -0
- package/dist/v3/sales/auctioneer.d.ts +60 -0
- package/dist/v3/sales/auctioneer.d.ts.map +1 -0
- package/dist/v3/sales/auctioneer.js +130 -0
- package/dist/v3/sales/bcs.d.ts +4 -0
- package/dist/v3/sales/bcs.d.ts.map +1 -0
- package/dist/v3/sales/bcs.js +16 -0
- package/dist/v3/sales/bid.d.ts +51 -0
- package/dist/v3/sales/bid.d.ts.map +1 -0
- package/dist/v3/sales/bid.js +85 -0
- package/dist/v3/sales/bodies.d.ts +83 -0
- package/dist/v3/sales/bodies.d.ts.map +1 -0
- package/dist/v3/sales/bodies.js +78 -0
- package/dist/v3/sales/claim.d.ts +40 -0
- package/dist/v3/sales/claim.d.ts.map +1 -0
- package/dist/v3/sales/claim.js +44 -0
- package/dist/v3/sales/index.d.ts +10 -0
- package/dist/v3/sales/index.d.ts.map +1 -0
- package/dist/v3/sales/index.js +9 -0
- package/dist/v3/sales/open-kind.d.ts +42 -0
- package/dist/v3/sales/open-kind.d.ts.map +1 -0
- package/dist/v3/sales/open-kind.js +43 -0
- package/dist/v3/sales/outcome.d.ts +20 -0
- package/dist/v3/sales/outcome.d.ts.map +1 -0
- package/dist/v3/sales/outcome.js +21 -0
- package/dist/v3/sales/reclaim.d.ts +46 -0
- package/dist/v3/sales/reclaim.d.ts.map +1 -0
- package/dist/v3/sales/reclaim.js +57 -0
- package/dist/v3/sales/rows.d.ts +55 -0
- package/dist/v3/sales/rows.d.ts.map +1 -0
- package/dist/v3/sales/rows.js +98 -0
- package/dist/v3/sales/seller.d.ts +39 -0
- package/dist/v3/sales/seller.d.ts.map +1 -0
- package/dist/v3/sales/seller.js +67 -0
- package/dist/v3/stage1.d.ts +204 -0
- package/dist/v3/stage1.d.ts.map +1 -0
- package/dist/v3/stage1.js +575 -0
- package/package.json +33 -4
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { EsseyError } from "../../errors.js";
|
|
2
|
+
import { address } from "../../network.js";
|
|
3
|
+
import { wireProof } from "../bodies.js";
|
|
4
|
+
import { assertU64 } from "../fees.js";
|
|
5
|
+
import { ENC_BYTES, VK } from "../kinds.js";
|
|
6
|
+
import { K_MAX, K_MIN } from "./math.js";
|
|
7
|
+
const OUTS = ["out0", "out1", "out2", "feeNote"];
|
|
8
|
+
const DELIVERY = ["ephX", "ephY", "feeCt", "lEx", "lEy", "lCt"];
|
|
9
|
+
const invalid = (what) => {
|
|
10
|
+
throw new EsseyError("INVALID_ARGUMENT", what);
|
|
11
|
+
};
|
|
12
|
+
const hex = (bytes) => `0x${Buffer.from(bytes).toString("hex")}`;
|
|
13
|
+
function enc(bytes, what) {
|
|
14
|
+
if (bytes.length !== ENC_BYTES)
|
|
15
|
+
invalid(`${what} is not a 128-byte note ciphertext`);
|
|
16
|
+
return hex(bytes);
|
|
17
|
+
}
|
|
18
|
+
function u64(value, what) {
|
|
19
|
+
try {
|
|
20
|
+
assertU64(what, value);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
invalid(`${what} is not a u64`);
|
|
24
|
+
}
|
|
25
|
+
return value.toString();
|
|
26
|
+
}
|
|
27
|
+
const prefixed = (prefix) => (name) => `${prefix}${name[0].toUpperCase()}${name.slice(1)}`;
|
|
28
|
+
function healthArgs(s, key = (name) => name) {
|
|
29
|
+
if (s.outs.length !== OUTS.length || s.delivery.length !== DELIVERY.length)
|
|
30
|
+
invalid("a health statement has 4 outs and 6 delivery rows");
|
|
31
|
+
return Object.fromEntries([
|
|
32
|
+
...OUTS.map((name, i) => [key(name), s.outs[i].toString()]),
|
|
33
|
+
...DELIVERY.map((name, i) => [key(name), s.delivery[i].toString()]),
|
|
34
|
+
]);
|
|
35
|
+
}
|
|
36
|
+
function lockArgs(lock, noteKey, encs) {
|
|
37
|
+
return {
|
|
38
|
+
root: lock.root.toString(),
|
|
39
|
+
nullifier0: lock.n0.toString(),
|
|
40
|
+
nullifier1: lock.n1.toString(),
|
|
41
|
+
[noteKey]: lock.note.toString(),
|
|
42
|
+
change: lock.change.toString(),
|
|
43
|
+
feeNote: lock.feeNote.toString(),
|
|
44
|
+
ephX: lock.ephX.toString(),
|
|
45
|
+
ephY: lock.ephY.toString(),
|
|
46
|
+
feeCt: lock.feeCt.toString(),
|
|
47
|
+
encChange: enc(encs[0], "encChange"),
|
|
48
|
+
encFee: enc(encs[1], "encFee"),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function body(kind, p, proofs, args) {
|
|
52
|
+
return {
|
|
53
|
+
v: 3,
|
|
54
|
+
kind,
|
|
55
|
+
pool: p.pool,
|
|
56
|
+
proofs,
|
|
57
|
+
args: { loan: address(p.loan, "loan"), market: address(p.market, "market"), ...args },
|
|
58
|
+
relayerFee: "0",
|
|
59
|
+
wait: p.wait,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
const healthProof = (p) => wireProof(VK.health, p.proved);
|
|
63
|
+
/**
|
|
64
|
+
* The `liquidation::lock_collateral` slice of a `loan_open` envelope (`liquidation.move:62-87`): the CollLock
|
|
65
|
+
* spend, the keeper ciphertext, the claimed price and the Health OPEN rows. The request half is the loans module's.
|
|
66
|
+
*/
|
|
67
|
+
export function openLockArgs(p) {
|
|
68
|
+
return {
|
|
69
|
+
...lockArgs(p.lock, "collNote", p.encs),
|
|
70
|
+
keeperCt: hex(p.keeperCt),
|
|
71
|
+
pClaim: u64(p.pClaim, "pClaim"),
|
|
72
|
+
...healthArgs(p.statement, prefixed("open")),
|
|
73
|
+
priceUpdate: hex(p.priceUpdate),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** `encs` pairs with `[out0, fee_note]`. */
|
|
77
|
+
export function release(p) {
|
|
78
|
+
return body("loan_release", p, [healthProof(p)], {
|
|
79
|
+
...healthArgs(p.statement),
|
|
80
|
+
encOut: enc(p.encs[0], "encOut"),
|
|
81
|
+
encFee: enc(p.encs[1], "encFee"),
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
export function heartbeat(p) {
|
|
85
|
+
if (!Number.isInteger(p.k) || p.k < K_MIN || p.k > K_MAX)
|
|
86
|
+
invalid("k is not a heartbeat bucket");
|
|
87
|
+
return body("loan_heartbeat", p, [healthProof(p)], {
|
|
88
|
+
...healthArgs(p.statement),
|
|
89
|
+
encOut: enc(p.encs[0], "encOut"),
|
|
90
|
+
encFee: enc(p.encs[1], "encFee"),
|
|
91
|
+
k: p.k.toString(),
|
|
92
|
+
expiryMs: u64(p.expiryMs, "expiryMs"),
|
|
93
|
+
priceUpdate: hex(p.priceUpdate),
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/** `lockEncs` pairs with the spend's `[change, fee_note]`, `encs` with the re-locked `[out0, fee_note]`. */
|
|
97
|
+
export function topup(p) {
|
|
98
|
+
const toppedUp = prefixed("topup");
|
|
99
|
+
return body("loan_topup", p, [wireProof(VK.collLock, p.lockProved), healthProof(p)], {
|
|
100
|
+
...lockArgs(p.lock, "addNote", p.lockEncs),
|
|
101
|
+
keeperCt: hex(p.keeperCt),
|
|
102
|
+
...healthArgs(p.statement, toppedUp),
|
|
103
|
+
[toppedUp("encOut")]: enc(p.encs[0], "encOut"),
|
|
104
|
+
[toppedUp("encFee")]: enc(p.encs[1], "encFee"),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/** `loan_topup_borrower`: the borrower pass (proof 0, `tag`, `expiryMs`, `passN0`), then `topup` unchanged. */
|
|
108
|
+
export function topupBorrower(p) {
|
|
109
|
+
const t = topup(p);
|
|
110
|
+
return {
|
|
111
|
+
...t,
|
|
112
|
+
kind: "loan_topup_borrower",
|
|
113
|
+
proofs: [wireProof(VK.membership, p.pass.proved), ...t.proofs],
|
|
114
|
+
args: {
|
|
115
|
+
...t.args,
|
|
116
|
+
tag: p.pass.tag.toString(),
|
|
117
|
+
expiryMs: u64(p.pass.expiryMs, "expiryMs"),
|
|
118
|
+
passN0: p.pass.n0.toString(),
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function seizeArgs(p) {
|
|
123
|
+
return {
|
|
124
|
+
...healthArgs(p.statement),
|
|
125
|
+
encLender: enc(p.encs[0], "encLender"),
|
|
126
|
+
encFee: enc(p.encs[1], "encFee"),
|
|
127
|
+
encBorrower: enc(p.encs[2], "encBorrower"),
|
|
128
|
+
encKeeper: enc(p.encs[3], "encKeeper"),
|
|
129
|
+
pSpot: u64(p.pSpot, "pSpot"),
|
|
130
|
+
pEma: u64(p.pEma, "pEma"),
|
|
131
|
+
priceUpdate: hex(p.priceUpdate),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
export const liquidate = (p) => body("loan_liquidate", p, [healthProof(p)], seizeArgs(p));
|
|
135
|
+
export const faultLiquidate = (p) => body("loan_fault_liquidate", p, [healthProof(p)], seizeArgs(p));
|
|
136
|
+
export const claimDefault = (p) => body("loan_claim_default", p, [healthProof(p)], { ...seizeArgs(p), nowS: u64(p.nowS, "nowS") });
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The three ids every Health `ext_hash` binds after the mode byte. */
|
|
2
|
+
export type LoanIds = {
|
|
3
|
+
loan: string;
|
|
4
|
+
poolC: string;
|
|
5
|
+
market: string;
|
|
6
|
+
};
|
|
7
|
+
/** `liquidation::open_ext`: the keeper ciphertext, then the CollLock spend's two note ciphertexts. */
|
|
8
|
+
export declare const openExt: (ids: LoanIds, keeperCt: Uint8Array, encChange: Uint8Array, encFee: Uint8Array) => bigint;
|
|
9
|
+
/** `encs` pairs with `[out0, fee_note]`, the order `room_insert` takes them. */
|
|
10
|
+
export declare const releaseExt: (ids: LoanIds, encs: [Uint8Array, Uint8Array]) => bigint;
|
|
11
|
+
export declare const heartbeatExt: (ids: LoanIds, encs: [Uint8Array, Uint8Array]) => bigint;
|
|
12
|
+
/** `liquidation::topup_ext`: the new keeper ciphertext, the spend's `[change, fee_note]`, then the re-locked pair. */
|
|
13
|
+
export declare const topupExt: (ids: LoanIds, keeperCt: Uint8Array, encChange: Uint8Array, encFee: Uint8Array, encs: [Uint8Array, Uint8Array]) => bigint;
|
|
14
|
+
/** `liquidation::topup_by_borrower_ext`: `topupExt`'s preimage under `HEXT_TOPUP_BY_BORROWER`. */
|
|
15
|
+
export declare const topupByBorrowerExt: (ids: LoanIds, keeperCt: Uint8Array, encChange: Uint8Array, encFee: Uint8Array, encs: [Uint8Array, Uint8Array]) => bigint;
|
|
16
|
+
/** A seizure inserts `[out0, fee_note, out1, out2]`: lender, fee, borrower, keeper (`liquidation.move:437`). */
|
|
17
|
+
export type SeizeEncs = [Uint8Array, Uint8Array, Uint8Array, Uint8Array];
|
|
18
|
+
export declare const liquidateExt: (ids: LoanIds, encs: SeizeEncs) => bigint;
|
|
19
|
+
export declare const defaultExt: (ids: LoanIds, encs: SeizeEncs) => bigint;
|
|
20
|
+
export declare const faultExt: (ids: LoanIds, encs: SeizeEncs) => bigint;
|
|
21
|
+
//# sourceMappingURL=ext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ext.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/ext.ts"],"names":[],"mappings":"AAEA,uEAAuE;AACvE,MAAM,MAAM,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAWtE,sGAAsG;AACtG,eAAO,MAAM,OAAO,GAAI,KAAK,OAAO,EAAE,UAAU,UAAU,EAAE,WAAW,UAAU,EAAE,QAAQ,UAAU,WACjD,CAAC;AAErD,gFAAgF;AAChF,eAAO,MAAM,UAAU,GAAI,KAAK,OAAO,EAAE,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,WAAiC,CAAC;AAEzG,eAAO,MAAM,YAAY,GAAI,KAAK,OAAO,EAAE,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,WAAmC,CAAC;AAE7G,sHAAsH;AACtH,eAAO,MAAM,QAAQ,GACnB,KAAK,OAAO,EACZ,UAAU,UAAU,EACpB,WAAW,UAAU,EACrB,QAAQ,UAAU,EAClB,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,WACiC,CAAC;AAElE,kGAAkG;AAClG,eAAO,MAAM,kBAAkB,GAC7B,KAAK,OAAO,EACZ,UAAU,UAAU,EACpB,WAAW,UAAU,EACrB,QAAQ,UAAU,EAClB,MAAM,CAAC,UAAU,EAAE,UAAU,CAAC,WAC6C,CAAC;AAE9E,gHAAgH;AAChH,MAAM,MAAM,SAAS,GAAG,CAAC,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,CAAC,CAAC;AAEzE,eAAO,MAAM,YAAY,GAAI,KAAK,OAAO,EAAE,MAAM,SAAS,WAAmC,CAAC;AAE9F,eAAO,MAAM,UAAU,GAAI,KAAK,OAAO,EAAE,MAAM,SAAS,WAAiC,CAAC;AAE1F,eAAO,MAAM,QAAQ,GAAI,KAAK,OAAO,EAAE,MAAM,SAAS,WAA+B,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { HEXT_TOPUP_BY_BORROWER, MODE_OPEN, MODE_TOPUP, healthExt } from "../ext.js";
|
|
2
|
+
const MODE_HEARTBEAT = 2;
|
|
3
|
+
const MODE_LIQUIDATE = 4;
|
|
4
|
+
const MODE_DEFAULT = 5;
|
|
5
|
+
const MODE_RELEASE = 6;
|
|
6
|
+
const MODE_FAULT = 7;
|
|
7
|
+
const ext = (mode, ids, cts) => healthExt(mode, ids.loan, ids.poolC, ids.market, cts);
|
|
8
|
+
/** `liquidation::open_ext`: the keeper ciphertext, then the CollLock spend's two note ciphertexts. */
|
|
9
|
+
export const openExt = (ids, keeperCt, encChange, encFee) => ext(MODE_OPEN, ids, [keeperCt, encChange, encFee]);
|
|
10
|
+
/** `encs` pairs with `[out0, fee_note]`, the order `room_insert` takes them. */
|
|
11
|
+
export const releaseExt = (ids, encs) => ext(MODE_RELEASE, ids, encs);
|
|
12
|
+
export const heartbeatExt = (ids, encs) => ext(MODE_HEARTBEAT, ids, encs);
|
|
13
|
+
/** `liquidation::topup_ext`: the new keeper ciphertext, the spend's `[change, fee_note]`, then the re-locked pair. */
|
|
14
|
+
export const topupExt = (ids, keeperCt, encChange, encFee, encs) => ext(MODE_TOPUP, ids, [keeperCt, encChange, encFee, ...encs]);
|
|
15
|
+
/** `liquidation::topup_by_borrower_ext`: `topupExt`'s preimage under `HEXT_TOPUP_BY_BORROWER`. */
|
|
16
|
+
export const topupByBorrowerExt = (ids, keeperCt, encChange, encFee, encs) => ext(HEXT_TOPUP_BY_BORROWER, ids, [keeperCt, encChange, encFee, ...encs]);
|
|
17
|
+
export const liquidateExt = (ids, encs) => ext(MODE_LIQUIDATE, ids, encs);
|
|
18
|
+
export const defaultExt = (ids, encs) => ext(MODE_DEFAULT, ids, encs);
|
|
19
|
+
export const faultExt = (ids, encs) => ext(MODE_FAULT, ids, encs);
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
2
|
+
import type { CircuitInput, OutNote } from "../inputs.ts";
|
|
3
|
+
import type { KeeperTerms } from "../keeper-payload.ts";
|
|
4
|
+
import type { Rates } from "../market.ts";
|
|
5
|
+
import { type Reading } from "./math.ts";
|
|
6
|
+
import { type CollOpening, type ReqOpening } from "./openings.ts";
|
|
7
|
+
export declare const MODE: {
|
|
8
|
+
readonly OPEN: 1;
|
|
9
|
+
readonly HEARTBEAT: 2;
|
|
10
|
+
readonly TOPUP: 3;
|
|
11
|
+
readonly LIQUIDATE: 4;
|
|
12
|
+
readonly DEFAULT: 5;
|
|
13
|
+
readonly RELEASE: 6;
|
|
14
|
+
readonly FAULT: 7;
|
|
15
|
+
};
|
|
16
|
+
export declare const HEALTH_VERSION = 1n;
|
|
17
|
+
/** What `health::rows` folds for the loan: its market's decimals, its stored rates, and `loan::keeper_bps(mode)`. */
|
|
18
|
+
export type HealthRisk = {
|
|
19
|
+
cDec: bigint;
|
|
20
|
+
lDec: bigint;
|
|
21
|
+
ltvBps: bigint;
|
|
22
|
+
liqBps: bigint;
|
|
23
|
+
bonusBps: bigint;
|
|
24
|
+
keeperBps: bigint;
|
|
25
|
+
};
|
|
26
|
+
type Common = {
|
|
27
|
+
opening: CollOpening;
|
|
28
|
+
risk: HealthRisk;
|
|
29
|
+
/** The claimed prices the entry folds: both `p_claim` in OPEN, both the floor in HEARTBEAT, all 0 in TOPUP and RELEASE. */
|
|
30
|
+
price: Reading;
|
|
31
|
+
/** HEARTBEAT: `expiry_ms / 1000`. DEFAULT: `now_s`. Every other entry folds 0. */
|
|
32
|
+
timeS: bigint;
|
|
33
|
+
extHash: bigint;
|
|
34
|
+
fee: FeeCommitInput;
|
|
35
|
+
eph: bigint;
|
|
36
|
+
};
|
|
37
|
+
export type HealthParams = Common & ({
|
|
38
|
+
mode: typeof MODE.OPEN;
|
|
39
|
+
request: ReqOpening;
|
|
40
|
+
} | {
|
|
41
|
+
mode: typeof MODE.HEARTBEAT | typeof MODE.RELEASE;
|
|
42
|
+
terms: KeeperTerms;
|
|
43
|
+
} | {
|
|
44
|
+
mode: typeof MODE.TOPUP;
|
|
45
|
+
terms: KeeperTerms;
|
|
46
|
+
add: {
|
|
47
|
+
amount: bigint;
|
|
48
|
+
blinding: bigint;
|
|
49
|
+
ephL: bigint;
|
|
50
|
+
};
|
|
51
|
+
} | {
|
|
52
|
+
mode: typeof MODE.LIQUIDATE | typeof MODE.DEFAULT | typeof MODE.FAULT;
|
|
53
|
+
terms: KeeperTerms;
|
|
54
|
+
keeper: {
|
|
55
|
+
pk: bigint;
|
|
56
|
+
blinding: bigint;
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
export type HealthStatement = {
|
|
60
|
+
rows: {
|
|
61
|
+
s: bigint[];
|
|
62
|
+
a: bigint[];
|
|
63
|
+
d: bigint[];
|
|
64
|
+
};
|
|
65
|
+
/** The entry's `outs`: out0, out1, out2, fee_note. */
|
|
66
|
+
outs: [bigint, bigint, bigint, bigint];
|
|
67
|
+
/** The entry's `delivery`: ephX, ephY, fee_ct, lEx, lEy, lCt. */
|
|
68
|
+
delivery: [bigint, bigint, bigint, bigint, bigint, bigint];
|
|
69
|
+
};
|
|
70
|
+
/** `notes` are the openings of out0, out1 and out2. They never go on the wire. */
|
|
71
|
+
export type Health = {
|
|
72
|
+
input: CircuitInput;
|
|
73
|
+
publics: [bigint, bigint, bigint];
|
|
74
|
+
statement: HealthStatement;
|
|
75
|
+
notes: [OutNote, OutNote, OutNote];
|
|
76
|
+
fee: bigint;
|
|
77
|
+
};
|
|
78
|
+
/** What the lender reads out of a TOPUP's `delivery[3..6]` with the key behind `lenderEnc`: the re-locked amount. */
|
|
79
|
+
export declare function lenderAmount(encSk: bigint, [lEx, lEy, lCt]: [bigint, bigint, bigint]): Promise<bigint>;
|
|
80
|
+
/**
|
|
81
|
+
* A seizure's split of the lock (B §5.5, `health.circom:303-346`): what the seizer proves, and what the lender and the
|
|
82
|
+
* borrower recompute afterwards from the claimed `p_spot` the seizing call took. FAULT seizes the whole lock and runs
|
|
83
|
+
* the price chain on (debt 0, price 1), so pass `owed` 0 and `pSpot` 1 with it.
|
|
84
|
+
*/
|
|
85
|
+
export declare function seizeSplit(p: {
|
|
86
|
+
owed: bigint;
|
|
87
|
+
avail: bigint;
|
|
88
|
+
pSpot: bigint;
|
|
89
|
+
expo: bigint;
|
|
90
|
+
risk: HealthRisk;
|
|
91
|
+
fee: Pick<FeeCommitInput, "bps" | "min" | "max">;
|
|
92
|
+
fault?: boolean;
|
|
93
|
+
}): {
|
|
94
|
+
prodT: bigint;
|
|
95
|
+
target: bigint;
|
|
96
|
+
seizeR: {
|
|
97
|
+
q: bigint;
|
|
98
|
+
r: bigint;
|
|
99
|
+
};
|
|
100
|
+
seize: bigint;
|
|
101
|
+
pf: bigint;
|
|
102
|
+
prodK: bigint;
|
|
103
|
+
kf: bigint;
|
|
104
|
+
toLender: bigint;
|
|
105
|
+
surplus: bigint;
|
|
106
|
+
};
|
|
107
|
+
/** `loan::keeper_bps`: the liquidation rate in LIQUIDATE and FAULT, the default rate in DEFAULT, or 0. */
|
|
108
|
+
export declare function loanRisk(rates: Rates, decimals: {
|
|
109
|
+
cDec: bigint;
|
|
110
|
+
lDec: bigint;
|
|
111
|
+
}, mode: number): HealthRisk;
|
|
112
|
+
/** Witness for `circuits/v3/health/health.circom`. Public signals, in order: hS, hA, hD. */
|
|
113
|
+
export declare function health(p: HealthParams): Promise<Health>;
|
|
114
|
+
export {};
|
|
115
|
+
//# sourceMappingURL=health.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"health.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/health.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { EsseyError } from "../../errors.js";
|
|
3
|
+
import { MODE_OPEN, MODE_TOPUP } from "../ext.js";
|
|
4
|
+
import { R } from "../domains.js";
|
|
5
|
+
import { BPS_DENOM, assertU64, ecdhBlind, feeLane, feeOf, fieldAdd } from "../fees.js";
|
|
6
|
+
import { noteCommitment } from "../notes/keys.js";
|
|
7
|
+
import { GRACE_S, collFor, overLine, valueAt } from "./math.js";
|
|
8
|
+
import { collLockPk, outputBlind, requestCommit, termsCommit, } from "./openings.js";
|
|
9
|
+
export const MODE = {
|
|
10
|
+
OPEN: MODE_OPEN,
|
|
11
|
+
HEARTBEAT: 2,
|
|
12
|
+
TOPUP: MODE_TOPUP,
|
|
13
|
+
LIQUIDATE: 4,
|
|
14
|
+
DEFAULT: 5,
|
|
15
|
+
RELEASE: 6,
|
|
16
|
+
FAULT: 7,
|
|
17
|
+
};
|
|
18
|
+
export const HEALTH_VERSION = 1n;
|
|
19
|
+
/**
|
|
20
|
+
* Outside TOPUP the lender delivery runs on Base8 and its rows are the identity (`health.circom:383-394`), so
|
|
21
|
+
* the scalar hides nothing. This is the one every stored fixture proves with (`circuits/v3/health/ref.mjs:49`).
|
|
22
|
+
*/
|
|
23
|
+
const IDLE_EPH_L = 5n;
|
|
24
|
+
const MAX_BPS = 10000n;
|
|
25
|
+
const NO_TERMS = {
|
|
26
|
+
principal: 0n,
|
|
27
|
+
rateBps: 0n,
|
|
28
|
+
owed: 0n,
|
|
29
|
+
maturityS: 0n,
|
|
30
|
+
lenderPk: 0n,
|
|
31
|
+
lenderEncX: 0n,
|
|
32
|
+
lenderEncY: 0n,
|
|
33
|
+
salt: 0n,
|
|
34
|
+
};
|
|
35
|
+
const NO_REQUEST = { principal: 0n, maxRateBps: 0n, termMs: 0n, salt: 0n };
|
|
36
|
+
const NO_NOTE = { amount: 0n, pk: 0n, blinding: 0n };
|
|
37
|
+
const refuse = (code, what) => {
|
|
38
|
+
throw new EsseyError(code, what);
|
|
39
|
+
};
|
|
40
|
+
const lenderMask = (sx) => poseidonHash([sx, 2n]);
|
|
41
|
+
/** What the lender reads out of a TOPUP's `delivery[3..6]` with the key behind `lenderEnc`: the re-locked amount. */
|
|
42
|
+
export async function lenderAmount(encSk, [lEx, lEy, lCt]) {
|
|
43
|
+
const { sx } = await ecdhBlind(lEx, lEy, encSk);
|
|
44
|
+
return (lCt - lenderMask(sx) + R) % R;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* A seizure's split of the lock (B §5.5, `health.circom:303-346`): what the seizer proves, and what the lender and the
|
|
48
|
+
* borrower recompute afterwards from the claimed `p_spot` the seizing call took. FAULT seizes the whole lock and runs
|
|
49
|
+
* the price chain on (debt 0, price 1), so pass `owed` 0 and `pSpot` 1 with it.
|
|
50
|
+
*/
|
|
51
|
+
export function seizeSplit(p) {
|
|
52
|
+
const prodT = p.owed * (BPS_DENOM + p.risk.bonusBps);
|
|
53
|
+
const target = prodT / BPS_DENOM;
|
|
54
|
+
const seizeR = collFor(target, p.pSpot, { cDec: p.risk.cDec, lDec: p.risk.lDec, expo: p.expo });
|
|
55
|
+
const seize = p.fault || seizeR.q >= p.avail ? p.avail : seizeR.q;
|
|
56
|
+
const pf = feeOf({ base: seize, bps: p.fee.bps, min: p.fee.min, max: p.fee.max }).fee;
|
|
57
|
+
const prodK = seize * p.risk.keeperBps;
|
|
58
|
+
const kf = prodK / BPS_DENOM;
|
|
59
|
+
return { prodT, target, seizeR, seize, pf, prodK, kf, toLender: seize - pf - kf, surplus: p.avail - seize };
|
|
60
|
+
}
|
|
61
|
+
/** `loan::keeper_bps`: the liquidation rate in LIQUIDATE and FAULT, the default rate in DEFAULT, or 0. */
|
|
62
|
+
export function loanRisk(rates, decimals, mode) {
|
|
63
|
+
let keeperBps = 0n;
|
|
64
|
+
if (mode === MODE.LIQUIDATE || mode === MODE.FAULT)
|
|
65
|
+
keeperBps = rates.keeperLiqBps;
|
|
66
|
+
if (mode === MODE.DEFAULT)
|
|
67
|
+
keeperBps = rates.keeperDefaultBps;
|
|
68
|
+
return { ...decimals, ltvBps: rates.ltvBps, liqBps: rates.liqBps, bonusBps: rates.bonusBps, keeperBps };
|
|
69
|
+
}
|
|
70
|
+
function assertRanges(p, principal, terms, addAmount) {
|
|
71
|
+
const u64s = {
|
|
72
|
+
collAmt: p.opening.amount,
|
|
73
|
+
nonce: p.opening.nonce,
|
|
74
|
+
principal,
|
|
75
|
+
owed: terms.owed,
|
|
76
|
+
maturityS: terms.maturityS,
|
|
77
|
+
addAmt: addAmount,
|
|
78
|
+
spot: p.price.spot,
|
|
79
|
+
ema: p.price.ema,
|
|
80
|
+
timeS: p.timeS,
|
|
81
|
+
};
|
|
82
|
+
for (const [name, value] of Object.entries(u64s))
|
|
83
|
+
assertU64(name, value);
|
|
84
|
+
const { ltvBps, liqBps, bonusBps, keeperBps } = p.risk;
|
|
85
|
+
for (const [name, value] of Object.entries({ ltvBps, liqBps, bonusBps, keeperBps }))
|
|
86
|
+
if (value < 0n || value > MAX_BPS)
|
|
87
|
+
throw new Error(`${name} is not in 0..10000`);
|
|
88
|
+
}
|
|
89
|
+
/** The per-mode constraints of `health.circom:281-299`: a witness that breaks one has no proof. */
|
|
90
|
+
function assertProvable(p, principal, terms, spot, ema) {
|
|
91
|
+
const { risk, mode } = p;
|
|
92
|
+
const due = terms.maturityS + GRACE_S;
|
|
93
|
+
if (mode === MODE.OPEN && principal === 0n)
|
|
94
|
+
refuse("INVALID_ARGUMENT", "an open needs a principal");
|
|
95
|
+
if (mode === MODE.OPEN && overLine(spot, principal, risk.ltvBps))
|
|
96
|
+
refuse("INVALID_ARGUMENT", "the principal is above the LTV line at this price");
|
|
97
|
+
if (mode === MODE.HEARTBEAT && overLine(spot, terms.owed, risk.liqBps))
|
|
98
|
+
refuse("HEALTH_NO_BUCKET", "the loan is over the liquidation line at this floor");
|
|
99
|
+
if (mode === MODE.HEARTBEAT && p.timeS > due)
|
|
100
|
+
refuse("INVALID_ARGUMENT", "a heartbeat cannot cover past maturity plus grace");
|
|
101
|
+
const over = overLine(spot, terms.owed, risk.liqBps) && overLine(ema, terms.owed, risk.liqBps);
|
|
102
|
+
if (mode === MODE.LIQUIDATE && !over)
|
|
103
|
+
refuse("HEALTH_NOT_UNDERWATER", "the loan is not over the liquidation line at both prices");
|
|
104
|
+
if (mode === MODE.DEFAULT && p.timeS < due)
|
|
105
|
+
refuse("INVALID_ARGUMENT", "maturity plus grace has not passed");
|
|
106
|
+
}
|
|
107
|
+
function feeInputs(fee, eph, lane) {
|
|
108
|
+
return {
|
|
109
|
+
fee_bps: fee.bps.toString(),
|
|
110
|
+
fee_min: fee.min.toString(),
|
|
111
|
+
fee_max: fee.max.toString(),
|
|
112
|
+
fee_pk: fee.pk.toString(),
|
|
113
|
+
tX: fee.tx.toString(),
|
|
114
|
+
tY: fee.ty.toString(),
|
|
115
|
+
fee_salt: fee.salt.toString(),
|
|
116
|
+
cfg_version: fee.version.toString(),
|
|
117
|
+
eph: eph.toString(),
|
|
118
|
+
q: lane.q.toString(),
|
|
119
|
+
r: lane.r.toString(),
|
|
120
|
+
fee_commit: lane.fee_commit.toString(),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/** Witness for `circuits/v3/health/health.circom`. Public signals, in order: hS, hA, hD. */
|
|
124
|
+
export async function health(p) {
|
|
125
|
+
const { opening: o, risk, price, mode } = p;
|
|
126
|
+
const priced = p.mode === MODE.LIQUIDATE || p.mode === MODE.DEFAULT;
|
|
127
|
+
const seizing = priced || p.mode === MODE.FAULT;
|
|
128
|
+
const terms = p.mode === MODE.OPEN ? NO_TERMS : p.terms;
|
|
129
|
+
const request = p.mode === MODE.OPEN ? p.request : NO_REQUEST;
|
|
130
|
+
const principal = p.mode === MODE.OPEN ? request.principal : terms.principal;
|
|
131
|
+
const add = p.mode === MODE.TOPUP ? p.add : { amount: 0n, blinding: 0n, ephL: IDLE_EPH_L };
|
|
132
|
+
const keeper = seizing ? p.keeper : { pk: 0n, blinding: 0n };
|
|
133
|
+
assertRanges(p, principal, terms, add.amount);
|
|
134
|
+
if (priced && price.spot === 0n)
|
|
135
|
+
refuse("INVALID_ARGUMENT", "a seizure needs a spot price");
|
|
136
|
+
const scale = { cDec: risk.cDec, lDec: risk.lDec, expo: price.expo };
|
|
137
|
+
const spot = valueAt(o.amount, price.spot, scale);
|
|
138
|
+
const ema = valueAt(o.amount, price.ema, scale);
|
|
139
|
+
assertProvable(p, principal, terms, spot.q, ema.q);
|
|
140
|
+
if (seizing && p.fee.bps + risk.keeperBps > risk.bonusBps)
|
|
141
|
+
refuse("INVALID_ARGUMENT", "the fee and keeper rates exceed the bonus");
|
|
142
|
+
const lockPk = collLockPk(o.loanKey, o.borrowerPk);
|
|
143
|
+
const note = noteCommitment(o.amount, lockPk, o.blinding);
|
|
144
|
+
const commitIn = mode === MODE.OPEN ? requestCommit(request, o.borrowerPk, note) : termsCommit(terms, o.borrowerPk);
|
|
145
|
+
const { prodT, target, seizeR, seize, prodK, kf } = seizeSplit({
|
|
146
|
+
owed: priced ? terms.owed : 0n,
|
|
147
|
+
avail: seizing ? o.amount : 0n,
|
|
148
|
+
pSpot: priced ? price.spot : 1n,
|
|
149
|
+
expo: price.expo,
|
|
150
|
+
risk,
|
|
151
|
+
fee: p.fee,
|
|
152
|
+
fault: mode === MODE.FAULT,
|
|
153
|
+
});
|
|
154
|
+
const lane = await feeLane({ ...p.fee, base: seize, eph: p.eph });
|
|
155
|
+
const toLender = seize - lane.fee - kf;
|
|
156
|
+
if (toLender < 0n)
|
|
157
|
+
refuse("INVALID_ARGUMENT", "the fee and the keeper fee exceed the seized amount");
|
|
158
|
+
const avail = o.amount + add.amount;
|
|
159
|
+
assertU64("collAmt + addAmt", avail);
|
|
160
|
+
const bD0 = outputBlind(o.blinding, 0, o.nonce);
|
|
161
|
+
let notes = [NO_NOTE, NO_NOTE, NO_NOTE];
|
|
162
|
+
if (mode === MODE.HEARTBEAT || mode === MODE.TOPUP)
|
|
163
|
+
notes = [{ amount: avail, pk: lockPk, blinding: bD0 }, NO_NOTE, NO_NOTE];
|
|
164
|
+
if (mode === MODE.RELEASE)
|
|
165
|
+
notes = [{ amount: avail, pk: o.borrowerPk, blinding: bD0 }, NO_NOTE, NO_NOTE];
|
|
166
|
+
if (seizing)
|
|
167
|
+
notes = [
|
|
168
|
+
{ amount: toLender, pk: terms.lenderPk, blinding: bD0 },
|
|
169
|
+
{ amount: avail - seize, pk: o.borrowerPk, blinding: outputBlind(o.blinding, 1, o.nonce) },
|
|
170
|
+
{ amount: kf, pk: keeper.pk, blinding: keeper.blinding },
|
|
171
|
+
];
|
|
172
|
+
const [out0, out1, out2] = notes.map((n) => noteCommitment(n.amount, n.pk, n.blinding));
|
|
173
|
+
const addCommit = mode === MODE.TOPUP ? noteCommitment(add.amount, lockPk, add.blinding) : 0n;
|
|
174
|
+
let lender = [0n, 1n, 0n];
|
|
175
|
+
if (mode === MODE.TOPUP) {
|
|
176
|
+
const k = await ecdhBlind(terms.lenderEncX, terms.lenderEncY, add.ephL);
|
|
177
|
+
lender = [k.ephX, k.ephY, fieldAdd(notes[0].amount, lenderMask(k.sx))];
|
|
178
|
+
}
|
|
179
|
+
const s = [
|
|
180
|
+
BigInt(mode),
|
|
181
|
+
o.loanKey,
|
|
182
|
+
note,
|
|
183
|
+
commitIn,
|
|
184
|
+
addCommit,
|
|
185
|
+
risk.cDec,
|
|
186
|
+
risk.lDec,
|
|
187
|
+
risk.ltvBps,
|
|
188
|
+
risk.liqBps,
|
|
189
|
+
risk.bonusBps,
|
|
190
|
+
lane.fee_commit,
|
|
191
|
+
risk.keeperBps,
|
|
192
|
+
p.fee.version,
|
|
193
|
+
p.extHash,
|
|
194
|
+
p.timeS,
|
|
195
|
+
o.nonce,
|
|
196
|
+
];
|
|
197
|
+
const outs = [out0, out1, out2, lane.fee_note];
|
|
198
|
+
const delivery = [lane.ephX, lane.ephY, lane.fee_ct, ...lender];
|
|
199
|
+
const a = [price.spot, price.ema, price.expo, ...outs, HEALTH_VERSION];
|
|
200
|
+
const d = [...delivery, 0n, 0n];
|
|
201
|
+
const input = {
|
|
202
|
+
mode: mode.toString(),
|
|
203
|
+
loanKey: o.loanKey.toString(),
|
|
204
|
+
collAmt: o.amount.toString(),
|
|
205
|
+
collBlind: o.blinding.toString(),
|
|
206
|
+
borrowerPk: o.borrowerPk.toString(),
|
|
207
|
+
principal: principal.toString(),
|
|
208
|
+
rate_bps: terms.rateBps.toString(),
|
|
209
|
+
owed: terms.owed.toString(),
|
|
210
|
+
maturity_s: terms.maturityS.toString(),
|
|
211
|
+
lenderPk: terms.lenderPk.toString(),
|
|
212
|
+
lenderEncX: terms.lenderEncX.toString(),
|
|
213
|
+
lenderEncY: terms.lenderEncY.toString(),
|
|
214
|
+
salt_t: terms.salt.toString(),
|
|
215
|
+
max_rate_bps: request.maxRateBps.toString(),
|
|
216
|
+
term_ms: request.termMs.toString(),
|
|
217
|
+
salt_req: request.salt.toString(),
|
|
218
|
+
addAmt: add.amount.toString(),
|
|
219
|
+
addBlind: add.blinding.toString(),
|
|
220
|
+
addCommit: addCommit.toString(),
|
|
221
|
+
c_dec: risk.cDec.toString(),
|
|
222
|
+
l_dec: risk.lDec.toString(),
|
|
223
|
+
expo: price.expo.toString(),
|
|
224
|
+
ltv_bps: risk.ltvBps.toString(),
|
|
225
|
+
liq_bps: risk.liqBps.toString(),
|
|
226
|
+
bonus_bps: risk.bonusBps.toString(),
|
|
227
|
+
keeper_bps: risk.keeperBps.toString(),
|
|
228
|
+
p_spot: price.spot.toString(),
|
|
229
|
+
p_ema: price.ema.toString(),
|
|
230
|
+
time_s: p.timeS.toString(),
|
|
231
|
+
nonce: o.nonce.toString(),
|
|
232
|
+
keeperPk: keeper.pk.toString(),
|
|
233
|
+
keeperBlind: keeper.blinding.toString(),
|
|
234
|
+
extHash: p.extHash.toString(),
|
|
235
|
+
eL: add.ephL.toString(),
|
|
236
|
+
q_spot: spot.q.toString(),
|
|
237
|
+
r_spot: spot.r.toString(),
|
|
238
|
+
q_ema: ema.q.toString(),
|
|
239
|
+
r_ema: ema.r.toString(),
|
|
240
|
+
q_target: target.toString(),
|
|
241
|
+
r_target: (prodT % BPS_DENOM).toString(),
|
|
242
|
+
q_seize: seizeR.q.toString(),
|
|
243
|
+
r_seize: seizeR.r.toString(),
|
|
244
|
+
q_kf: kf.toString(),
|
|
245
|
+
r_kf: (prodK % BPS_DENOM).toString(),
|
|
246
|
+
...feeInputs(p.fee, p.eph, lane),
|
|
247
|
+
};
|
|
248
|
+
return {
|
|
249
|
+
input,
|
|
250
|
+
publics: [poseidonHash(s), poseidonHash(a), poseidonHash(d)],
|
|
251
|
+
statement: { rows: { s, a, d }, outs, delivery },
|
|
252
|
+
notes,
|
|
253
|
+
fee: lane.fee,
|
|
254
|
+
};
|
|
255
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * as bodies from "./bodies.ts";
|
|
2
|
+
export * from "./ext.ts";
|
|
3
|
+
export * from "./health.ts";
|
|
4
|
+
export * from "./local-health.ts";
|
|
5
|
+
export * from "./math.ts";
|
|
6
|
+
export * from "./openings.ts";
|
|
7
|
+
export * from "./scan.ts";
|
|
8
|
+
export * from "./seal.ts";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type Reading } from "./math.ts";
|
|
2
|
+
/** The loan's stored liquidation rate and its market's decimals. */
|
|
3
|
+
export type LineRisk = {
|
|
4
|
+
cDec: bigint;
|
|
5
|
+
lDec: bigint;
|
|
6
|
+
liqBps: bigint;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* `underwater` is the LIQUIDATE statement: over the line at both spot and EMA (`health.circom:282-283`).
|
|
10
|
+
* `liquidationPrice` is the lowest price not over the line (`value.circom:33-34,124,244-253`), null when none is.
|
|
11
|
+
* `bestBucketK` is the deepest floor of `liquidation.move:451-455` still healthy (`health.circom:281`), or null.
|
|
12
|
+
* `ltvBps` is floor(owed · 10000 / value) at min(spot, EMA), the cap `open_price_ok` puts on an OPEN's price
|
|
13
|
+
* (`move/v3/market/sources/market.move:241-245`), null at value 0. It is on owed and no on-chain line computes
|
|
14
|
+
* it: `health.circom:271-281` compares principal, at OPEN only.
|
|
15
|
+
*/
|
|
16
|
+
export type LocalHealth = {
|
|
17
|
+
ltvBps: bigint | null;
|
|
18
|
+
underwater: boolean;
|
|
19
|
+
liquidationPrice: bigint | null;
|
|
20
|
+
bestBucketK: number | null;
|
|
21
|
+
};
|
|
22
|
+
export declare function localHealth(collAmt: bigint, owed: bigint, risk: LineRisk, r: Reading): LocalHealth;
|
|
23
|
+
//# sourceMappingURL=local-health.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-health.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/local-health.ts"],"names":[],"mappings":"AAEA,OAAO,EAAmD,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAE1F,oEAAoE;AACpE,MAAM,MAAM,QAAQ,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtE;;;;;;;GAOG;AACH,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,UAAU,EAAE,OAAO,CAAC;IACpB,gBAAgB,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC5B,CAAC;AAWF,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,GAAG,WAAW,CAclG"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { BPS_DENOM } from "../fees.js";
|
|
2
|
+
import { K_MAX, K_MIN, heartbeatFloor, overLine, valueAt } from "./math.js";
|
|
3
|
+
const ceilDiv = (a, b) => (a + b - 1n) / b;
|
|
4
|
+
function healthyFrom(collAmt, owed, risk, expo) {
|
|
5
|
+
if (owed === 0n)
|
|
6
|
+
return 0n;
|
|
7
|
+
if (collAmt === 0n || risk.liqBps === 0n)
|
|
8
|
+
return null;
|
|
9
|
+
const value = ceilDiv(owed * BPS_DENOM, risk.liqBps);
|
|
10
|
+
return ceilDiv(value * 10n ** (risk.cDec + expo), collAmt * 10n ** risk.lDec);
|
|
11
|
+
}
|
|
12
|
+
export function localHealth(collAmt, owed, risk, r) {
|
|
13
|
+
const scale = { cDec: risk.cDec, lDec: risk.lDec, expo: r.expo };
|
|
14
|
+
const over = (price) => overLine(valueAt(collAmt, price, scale).q, owed, risk.liqBps);
|
|
15
|
+
const low = r.spot < r.ema ? r.spot : r.ema;
|
|
16
|
+
const value = valueAt(collAmt, low, scale).q;
|
|
17
|
+
let bestBucketK = null;
|
|
18
|
+
for (let k = K_MAX; k >= K_MIN && bestBucketK === null; k--)
|
|
19
|
+
if (!over(heartbeatFloor(r, k)))
|
|
20
|
+
bestBucketK = k;
|
|
21
|
+
return {
|
|
22
|
+
ltvBps: value === 0n ? null : (owed * BPS_DENOM) / value,
|
|
23
|
+
underwater: over(r.spot) && over(r.ema),
|
|
24
|
+
liquidationPrice: healthyFrom(collAmt, owed, risk, r.expo),
|
|
25
|
+
bestBucketK,
|
|
26
|
+
};
|
|
27
|
+
}
|