@esseyonsui/sui-sdk-v3 0.0.0-stage → 0.1.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 +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 +3593 -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,157 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { valueAt } from "../collateral/math.js";
|
|
3
|
+
import { termsCommit as termsCommitOf } from "../collateral/openings.js";
|
|
4
|
+
import { DOM_A6, DOM_A6D, DOM_A6O, DOM_A6P, DOM_LBLIND, DOM_LNULL, DOM_PRICE, DOM_TSALT, R, } from "../domains.js";
|
|
5
|
+
import { feeWitness } from "../fee-witness.js";
|
|
6
|
+
import { ecdhBlind, feeLane, fieldAdd } from "../fees.js";
|
|
7
|
+
import { noteCommitment } from "../notes/keys.js";
|
|
8
|
+
import { lock4, lockField, offerCommit } from "./offer-plan.js";
|
|
9
|
+
import { SETTLE_SLOTS, interest, lowestEligible } from "./settle-plan.js";
|
|
10
|
+
export const LOAN_REFI_KIND = 15;
|
|
11
|
+
/** Witness and Move arguments of `circuits/v3/loan_refi/loan_refi.circom` (`ref.mjs` loanRefiCase). */
|
|
12
|
+
export async function loanRefi(p) {
|
|
13
|
+
const { request: q, slots, old, price } = p;
|
|
14
|
+
if (slots.length > SETTLE_SLOTS)
|
|
15
|
+
throw new Error("more than 16 offers");
|
|
16
|
+
const w = lowestEligible(slots, q.maxRateBps);
|
|
17
|
+
if (w < 0)
|
|
18
|
+
throw new Error("no eligible offer");
|
|
19
|
+
const win = slots[w].open;
|
|
20
|
+
const field = lockField(p.loanKey, p.round);
|
|
21
|
+
for (const s of slots) {
|
|
22
|
+
if (!s.active || !s.open)
|
|
23
|
+
continue;
|
|
24
|
+
const o = s.open;
|
|
25
|
+
if (offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY) !== s.commit)
|
|
26
|
+
throw new Error("an offer does not open its commit");
|
|
27
|
+
if (lock4(field, o.lenderPk, o.ns, o.amount, o.b) !== s.lock)
|
|
28
|
+
throw new Error("an offer does not open its lock");
|
|
29
|
+
}
|
|
30
|
+
const pad = (pick, zero) => Array.from({ length: SETTLE_SLOTS }, (_, i) => (slots[i] ? pick(slots[i]) : zero));
|
|
31
|
+
const opened = (pick) => pad((s) => (s.open ? pick(s.open) : 0n), 0n);
|
|
32
|
+
const commits = pad((s) => (s.active ? s.commit : 0n), 0n);
|
|
33
|
+
const locks = pad((s) => s.lock, 0n);
|
|
34
|
+
const actMask = slots.reduce((m, s, i) => (s.active ? m | (1n << BigInt(i)) : m), 0n);
|
|
35
|
+
const term = interest(q.principal, win.rateBps, q.termMs);
|
|
36
|
+
const terms = {
|
|
37
|
+
principal: q.principal,
|
|
38
|
+
rateBps: win.rateBps,
|
|
39
|
+
owed: term.owed,
|
|
40
|
+
maturityS: p.nowS + term.qTerm,
|
|
41
|
+
lenderPk: win.lenderPk,
|
|
42
|
+
lenderEncX: win.encX,
|
|
43
|
+
lenderEncY: win.encY,
|
|
44
|
+
salt: poseidonHash([DOM_TSALT, field, win.ns]),
|
|
45
|
+
};
|
|
46
|
+
const termsCommit = termsCommitOf(terms, q.borrowerPk);
|
|
47
|
+
const value = valueAt(q.collAmt, price.pClaim, { cDec: price.cDec, lDec: price.lDec, expo: price.expo });
|
|
48
|
+
if (term.owed * 10000n > value.q * price.ltvBps)
|
|
49
|
+
throw new Error("the new debt is over the ltv bound");
|
|
50
|
+
const lane = await feeLane({ ...p.fee, base: q.principal, eph: p.eph });
|
|
51
|
+
const rest = q.principal - old.owed - lane.fee;
|
|
52
|
+
if (rest < 0n)
|
|
53
|
+
throw new Error("the principal does not pay the old debt and the fee");
|
|
54
|
+
const borrowerNote = noteCommitment(rest, p.borrowerNote.pk, p.borrowerNote.blinding);
|
|
55
|
+
const kP = await ecdhBlind(old.lenderEncX, old.lenderEncY, p.eP);
|
|
56
|
+
const payoffNote = noteCommitment(old.owed, old.lenderPk, poseidonHash([DOM_LBLIND, kP.sx]));
|
|
57
|
+
const padNote = noteCommitment(0n, p.pad.pk, p.pad.blinding);
|
|
58
|
+
const winNull = poseidonHash([DOM_LNULL, win.ns, slots[w].lock]);
|
|
59
|
+
const k = await ecdhBlind(win.encX, win.encY, p.e);
|
|
60
|
+
const d = [
|
|
61
|
+
k.ephX,
|
|
62
|
+
k.ephY,
|
|
63
|
+
fieldAdd(q.collAmt, poseidonHash([k.sx, 1n])),
|
|
64
|
+
fieldAdd(q.collBlind, poseidonHash([k.sx, 2n])),
|
|
65
|
+
];
|
|
66
|
+
const ephP = [kP.ephX, kP.ephY];
|
|
67
|
+
const reqCommit = poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]);
|
|
68
|
+
const priceDigest = poseidonHash([
|
|
69
|
+
DOM_PRICE,
|
|
70
|
+
price.pClaim,
|
|
71
|
+
price.expo,
|
|
72
|
+
price.cDec,
|
|
73
|
+
price.lDec,
|
|
74
|
+
price.ltvBps,
|
|
75
|
+
]);
|
|
76
|
+
const publics = [
|
|
77
|
+
poseidonHash(commits),
|
|
78
|
+
poseidonHash(locks),
|
|
79
|
+
poseidonHash([DOM_A6, reqCommit, field, actMask, p.nowS, lane.fee_commit, p.fee.version, p.extHash, 0n]),
|
|
80
|
+
poseidonHash([DOM_A6O, termsCommit, borrowerNote, lane.fee_note, winNull, payoffNote, padNote]),
|
|
81
|
+
poseidonHash([DOM_A6D, ...d, lane.ephX, lane.ephY, lane.fee_ct, ...ephP]),
|
|
82
|
+
poseidonHash([DOM_A6P, termsCommitOf(old, q.borrowerPk), q.collNote, priceDigest]),
|
|
83
|
+
];
|
|
84
|
+
const s = (xs) => xs.map(String);
|
|
85
|
+
const input = {
|
|
86
|
+
commits: s(commits),
|
|
87
|
+
locks: s(locks),
|
|
88
|
+
rate: s(opened((o) => o.rateBps)),
|
|
89
|
+
salt: s(opened((o) => o.salt)),
|
|
90
|
+
lenderPk: s(opened((o) => o.lenderPk)),
|
|
91
|
+
encX: s(opened((o) => o.encX)),
|
|
92
|
+
encY: s(opened((o) => o.encY)),
|
|
93
|
+
actMask: actMask.toString(),
|
|
94
|
+
winner: String(w),
|
|
95
|
+
winRate: win.rateBps.toString(),
|
|
96
|
+
round: p.round.toString(),
|
|
97
|
+
ns_w: win.ns.toString(),
|
|
98
|
+
b_w: win.b.toString(),
|
|
99
|
+
q_term: term.qTerm.toString(),
|
|
100
|
+
r_term: term.rTerm.toString(),
|
|
101
|
+
q_intr: term.qIntr.toString(),
|
|
102
|
+
r_intr: term.rIntr.toString(),
|
|
103
|
+
principal: q.principal.toString(),
|
|
104
|
+
max_rate_bps: q.maxRateBps.toString(),
|
|
105
|
+
term_ms: q.termMs.toString(),
|
|
106
|
+
borrowerPk: q.borrowerPk.toString(),
|
|
107
|
+
collAmt: q.collAmt.toString(),
|
|
108
|
+
collBlind: q.collBlind.toString(),
|
|
109
|
+
salt_req: q.salt.toString(),
|
|
110
|
+
loanKey: p.loanKey.toString(),
|
|
111
|
+
pkB: p.borrowerNote.pk.toString(),
|
|
112
|
+
blindB: p.borrowerNote.blinding.toString(),
|
|
113
|
+
e: p.e.toString(),
|
|
114
|
+
now_s: p.nowS.toString(),
|
|
115
|
+
extHash: p.extHash.toString(),
|
|
116
|
+
relayer_fee: "0",
|
|
117
|
+
principalOld: old.principal.toString(),
|
|
118
|
+
rateOld: old.rateBps.toString(),
|
|
119
|
+
owedOld: old.owed.toString(),
|
|
120
|
+
maturityOld: old.maturityS.toString(),
|
|
121
|
+
lenderPkOld: old.lenderPk.toString(),
|
|
122
|
+
encXOld: old.lenderEncX.toString(),
|
|
123
|
+
encYOld: old.lenderEncY.toString(),
|
|
124
|
+
saltOld: old.salt.toString(),
|
|
125
|
+
eP: p.eP.toString(),
|
|
126
|
+
pClaim: price.pClaim.toString(),
|
|
127
|
+
expo: price.expo.toString(),
|
|
128
|
+
c_dec: price.cDec.toString(),
|
|
129
|
+
l_dec: price.lDec.toString(),
|
|
130
|
+
ltv_bps: price.ltvBps.toString(),
|
|
131
|
+
q_v: value.q.toString(),
|
|
132
|
+
r_v: value.r.toString(),
|
|
133
|
+
padPk: p.pad.pk.toString(),
|
|
134
|
+
padBlind: p.pad.blinding.toString(),
|
|
135
|
+
...feeWitness(p.fee, q.principal, p.eph, lane),
|
|
136
|
+
};
|
|
137
|
+
if (publics.some((x) => x >= R))
|
|
138
|
+
throw new Error("a public signal is not a field element");
|
|
139
|
+
return {
|
|
140
|
+
input,
|
|
141
|
+
publics,
|
|
142
|
+
winner: w,
|
|
143
|
+
terms,
|
|
144
|
+
termsCommit,
|
|
145
|
+
borrowerNote,
|
|
146
|
+
borrowerValue: rest,
|
|
147
|
+
feeNote: lane.fee_note,
|
|
148
|
+
winNull,
|
|
149
|
+
d,
|
|
150
|
+
ephX: lane.ephX,
|
|
151
|
+
ephY: lane.ephY,
|
|
152
|
+
feeCt: lane.fee_ct,
|
|
153
|
+
payoffNote,
|
|
154
|
+
pad: padNote,
|
|
155
|
+
ephP,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
2
|
+
import type { CircuitInput, SpendNote } from "../inputs.ts";
|
|
3
|
+
import type { KeeperTerms } from "../keeper-payload.ts";
|
|
4
|
+
export declare const LOAN_REPAY_KIND = 14;
|
|
5
|
+
export type LoanRepayParams = {
|
|
6
|
+
root: bigint;
|
|
7
|
+
ins: [SpendNote, SpendNote];
|
|
8
|
+
terms: KeeperTerms;
|
|
9
|
+
borrowerPk: bigint;
|
|
10
|
+
eL: bigint;
|
|
11
|
+
change: {
|
|
12
|
+
pk: bigint;
|
|
13
|
+
blinding: bigint;
|
|
14
|
+
};
|
|
15
|
+
pad: {
|
|
16
|
+
pk: bigint;
|
|
17
|
+
blinding: bigint;
|
|
18
|
+
};
|
|
19
|
+
relayerFee: bigint;
|
|
20
|
+
extHash: bigint;
|
|
21
|
+
fee: FeeCommitInput;
|
|
22
|
+
eph: bigint;
|
|
23
|
+
};
|
|
24
|
+
/** Witness for `circuits/v3/loan_repay/loan_repay.circom` (`ref.mjs` loanRepayCase). Publics: lender_note, change, fee_note, pad, hCtx, root, n0, n1. */
|
|
25
|
+
export declare function loanRepay(p: LoanRepayParams): Promise<{
|
|
26
|
+
input: CircuitInput;
|
|
27
|
+
publics: bigint[];
|
|
28
|
+
n0: bigint;
|
|
29
|
+
n1: bigint;
|
|
30
|
+
lenderNote: bigint;
|
|
31
|
+
change: bigint;
|
|
32
|
+
changeAmount: bigint;
|
|
33
|
+
pad: bigint;
|
|
34
|
+
ephL: [bigint, bigint];
|
|
35
|
+
feeNote: bigint;
|
|
36
|
+
ephX: bigint;
|
|
37
|
+
ephY: bigint;
|
|
38
|
+
feeCt: bigint;
|
|
39
|
+
}>;
|
|
40
|
+
//# sourceMappingURL=repay-plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"repay-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/repay-plan.ts"],"names":[],"mappings":"AAKA,OAAO,EAAsB,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AACrE,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC5D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAGxD,eAAO,MAAM,eAAe,KAAK,CAAC;AAElC,MAAM,MAAM,eAAe,GAAG;IAC5B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IAC5B,KAAK,EAAE,WAAW,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC;IACnB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACzC,GAAG,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACtC,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,yJAAyJ;AACzJ,wBAAsB,SAAS,CAAC,CAAC,EAAE,eAAe;;;;;;;;;UA6DpB,CAAC,MAAM,EAAE,MAAM,CAAC;;;;;GAM7C"}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { termsCommit } from "../collateral/openings.js";
|
|
3
|
+
import { DOM_A3, DOM_LBLIND } from "../domains.js";
|
|
4
|
+
import { feeWitness } from "../fee-witness.js";
|
|
5
|
+
import { ecdhBlind, feeLane } from "../fees.js";
|
|
6
|
+
import { noteCommitment, noteNullifier, notePk } from "../notes/keys.js";
|
|
7
|
+
export const LOAN_REPAY_KIND = 14;
|
|
8
|
+
/** Witness for `circuits/v3/loan_repay/loan_repay.circom` (`ref.mjs` loanRepayCase). Publics: lender_note, change, fee_note, pad, hCtx, root, n0, n1. */
|
|
9
|
+
export async function loanRepay(p) {
|
|
10
|
+
const t = p.terms;
|
|
11
|
+
const lane = await feeLane({ ...p.fee, base: t.owed, eph: p.eph });
|
|
12
|
+
const changeAmount = p.ins[0].amount + p.ins[1].amount - t.owed - lane.fee - p.relayerFee;
|
|
13
|
+
if (changeAmount < 0n)
|
|
14
|
+
throw new Error("inputs do not cover what is owed and its fee");
|
|
15
|
+
const commitments = p.ins.map((n) => noteCommitment(n.amount, notePk(n.sk), n.blinding));
|
|
16
|
+
const [n0, n1] = p.ins.map((n, i) => noteNullifier(commitments[i], n.sk, n.pathIndex));
|
|
17
|
+
const k = await ecdhBlind(t.lenderEncX, t.lenderEncY, p.eL);
|
|
18
|
+
const lenderNote = noteCommitment(t.owed, t.lenderPk, poseidonHash([DOM_LBLIND, k.sx]));
|
|
19
|
+
const change = noteCommitment(changeAmount, p.change.pk, p.change.blinding);
|
|
20
|
+
const pad = noteCommitment(0n, p.pad.pk, p.pad.blinding);
|
|
21
|
+
const commit = termsCommit(t, p.borrowerPk);
|
|
22
|
+
const hCtx = poseidonHash([
|
|
23
|
+
DOM_A3,
|
|
24
|
+
commit,
|
|
25
|
+
lane.fee_commit,
|
|
26
|
+
k.ephX,
|
|
27
|
+
k.ephY,
|
|
28
|
+
lane.ephX,
|
|
29
|
+
lane.ephY,
|
|
30
|
+
lane.fee_ct,
|
|
31
|
+
p.extHash,
|
|
32
|
+
p.relayerFee,
|
|
33
|
+
]);
|
|
34
|
+
const input = {
|
|
35
|
+
root: p.root.toString(),
|
|
36
|
+
n0: n0.toString(),
|
|
37
|
+
n1: n1.toString(),
|
|
38
|
+
inAmount: p.ins.map((n) => n.amount.toString()),
|
|
39
|
+
inPrivateKey: p.ins.map((n) => n.sk.toString()),
|
|
40
|
+
inBlinding: p.ins.map((n) => n.blinding.toString()),
|
|
41
|
+
inPathIndices: p.ins.map((n) => n.pathIndex.toString()),
|
|
42
|
+
inPathElements: p.ins.map((n) => n.pathElements.map(String)),
|
|
43
|
+
principal: t.principal.toString(),
|
|
44
|
+
rate: t.rateBps.toString(),
|
|
45
|
+
owed: t.owed.toString(),
|
|
46
|
+
maturity_s: t.maturityS.toString(),
|
|
47
|
+
borrowerPk: p.borrowerPk.toString(),
|
|
48
|
+
lenderPk: t.lenderPk.toString(),
|
|
49
|
+
encX: t.lenderEncX.toString(),
|
|
50
|
+
encY: t.lenderEncY.toString(),
|
|
51
|
+
salt_t: t.salt.toString(),
|
|
52
|
+
eL: p.eL.toString(),
|
|
53
|
+
cAmt: changeAmount.toString(),
|
|
54
|
+
cPk: p.change.pk.toString(),
|
|
55
|
+
cBlind: p.change.blinding.toString(),
|
|
56
|
+
padPk: p.pad.pk.toString(),
|
|
57
|
+
padBlind: p.pad.blinding.toString(),
|
|
58
|
+
relayer_fee: p.relayerFee.toString(),
|
|
59
|
+
extHash: p.extHash.toString(),
|
|
60
|
+
...feeWitness(p.fee, t.owed, p.eph, lane),
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
input,
|
|
64
|
+
publics: [lenderNote, change, lane.fee_note, pad, hCtx, p.root, n0, n1],
|
|
65
|
+
n0,
|
|
66
|
+
n1,
|
|
67
|
+
lenderNote,
|
|
68
|
+
change,
|
|
69
|
+
changeAmount,
|
|
70
|
+
pad,
|
|
71
|
+
ephL: [k.ephX, k.ephY],
|
|
72
|
+
feeNote: lane.fee_note,
|
|
73
|
+
ephX: lane.ephX,
|
|
74
|
+
ephY: lane.ephY,
|
|
75
|
+
feeCt: lane.fee_ct,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
2
|
+
import type { CircuitInput } from "../inputs.ts";
|
|
3
|
+
import { type OfferPlain } from "./offer-plan.ts";
|
|
4
|
+
export declare const SETTLE_SLOTS = 16;
|
|
5
|
+
export declare const LOAN_SETTLE_KIND = 13;
|
|
6
|
+
/** The 208-byte offer plaintext `offerPlaintext` writes. */
|
|
7
|
+
export declare function decodeOffer(plain: Uint8Array): OfferPlain;
|
|
8
|
+
/** `crypto.mjs` interest: whole seconds of the term, and interest rounded up over 365 days of 10_000 bps. */
|
|
9
|
+
export declare function interest(principal: bigint, rateBps: bigint, termMs: bigint): {
|
|
10
|
+
qTerm: bigint;
|
|
11
|
+
rTerm: bigint;
|
|
12
|
+
qIntr: bigint;
|
|
13
|
+
rIntr: bigint;
|
|
14
|
+
owed: bigint;
|
|
15
|
+
};
|
|
16
|
+
/** One stored offer slot: its commit and lock as the round holds them, and its opening when active. */
|
|
17
|
+
export type SettleSlot = {
|
|
18
|
+
commit: bigint;
|
|
19
|
+
lock: bigint;
|
|
20
|
+
active: boolean;
|
|
21
|
+
open?: OfferPlain;
|
|
22
|
+
};
|
|
23
|
+
export type LoanSettleParams = {
|
|
24
|
+
slots: SettleSlot[];
|
|
25
|
+
request: {
|
|
26
|
+
principal: bigint;
|
|
27
|
+
maxRateBps: bigint;
|
|
28
|
+
termMs: bigint;
|
|
29
|
+
borrowerPk: bigint;
|
|
30
|
+
collAmt: bigint;
|
|
31
|
+
collBlind: bigint;
|
|
32
|
+
collNote: bigint;
|
|
33
|
+
salt: bigint;
|
|
34
|
+
};
|
|
35
|
+
loanKey: bigint;
|
|
36
|
+
borrowerNote: {
|
|
37
|
+
pk: bigint;
|
|
38
|
+
blinding: bigint;
|
|
39
|
+
};
|
|
40
|
+
e: bigint;
|
|
41
|
+
nowS: bigint;
|
|
42
|
+
extHash: bigint;
|
|
43
|
+
fee: FeeCommitInput;
|
|
44
|
+
eph: bigint;
|
|
45
|
+
};
|
|
46
|
+
/** The winner: the lowest rate of an active slot at or under the request's maximum, earliest on a tie. */
|
|
47
|
+
export declare function lowestEligible(slots: SettleSlot[], maxRate: bigint): number;
|
|
48
|
+
/** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (`ref.mjs` loanSettleCase, round 0). */
|
|
49
|
+
export declare function loanSettle(p: LoanSettleParams): Promise<{
|
|
50
|
+
input: CircuitInput;
|
|
51
|
+
publics: bigint[];
|
|
52
|
+
winner: number;
|
|
53
|
+
terms: {
|
|
54
|
+
principal: bigint;
|
|
55
|
+
rateBps: bigint;
|
|
56
|
+
owed: bigint;
|
|
57
|
+
maturityS: bigint;
|
|
58
|
+
lenderPk: bigint;
|
|
59
|
+
lenderEncX: bigint;
|
|
60
|
+
lenderEncY: bigint;
|
|
61
|
+
salt: bigint;
|
|
62
|
+
};
|
|
63
|
+
termsCommit: bigint;
|
|
64
|
+
principalNote: bigint;
|
|
65
|
+
principalValue: bigint;
|
|
66
|
+
winNull: bigint;
|
|
67
|
+
d: [bigint, bigint, bigint, bigint];
|
|
68
|
+
feeNote: bigint;
|
|
69
|
+
ephX: bigint;
|
|
70
|
+
ephY: bigint;
|
|
71
|
+
feeCt: bigint;
|
|
72
|
+
}>;
|
|
73
|
+
/** `loan::action_hash`: blake2b256("ess3/act/" ‖ kind ‖ "/" ‖ bcs(loan_key) ‖ bcs(nonce) ‖ args) mod R. */
|
|
74
|
+
export declare function actionHash(blake: (b: Uint8Array) => Uint8Array, loanKey: bigint, nonce: bigint, kind: string, args: Uint8Array): bigint;
|
|
75
|
+
/** `settle::settle_args`: every argument in declaration order, shared objects by id, vectors and the proof as bcs. */
|
|
76
|
+
export declare function settleArgs(a: {
|
|
77
|
+
loan: string;
|
|
78
|
+
poolL: string;
|
|
79
|
+
proof: Uint8Array;
|
|
80
|
+
termsCommit: bigint;
|
|
81
|
+
principalNote: bigint;
|
|
82
|
+
feeNote: bigint;
|
|
83
|
+
winNull: bigint;
|
|
84
|
+
d: bigint[];
|
|
85
|
+
ephX: bigint;
|
|
86
|
+
ephY: bigint;
|
|
87
|
+
feeCt: bigint;
|
|
88
|
+
keeperCt: Uint8Array;
|
|
89
|
+
encs: Uint8Array[];
|
|
90
|
+
nowS: bigint;
|
|
91
|
+
}): Uint8Array;
|
|
92
|
+
/** `membership::seal_offer(room, round, seq, viewer).bind`. */
|
|
93
|
+
export declare const sealOfferBind: (blake: (b: Uint8Array) => Uint8Array, room: string, round: bigint, seq: bigint, viewer: string) => Uint8Array<ArrayBufferLike>;
|
|
94
|
+
/** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request as bcs, `close_ms`. */
|
|
95
|
+
export declare const openRefiArgs: (a: {
|
|
96
|
+
loan: string;
|
|
97
|
+
poolL: string;
|
|
98
|
+
reqCommit: bigint;
|
|
99
|
+
reqSealed: Uint8Array;
|
|
100
|
+
closeMs: bigint;
|
|
101
|
+
}) => Uint8Array;
|
|
102
|
+
/** `liquidation::topup_args`: every `topup_by_borrower` argument after the pass, in order, by `settle_args`' rule. */
|
|
103
|
+
export declare function topupArgs(a: {
|
|
104
|
+
loan: string;
|
|
105
|
+
poolC: string;
|
|
106
|
+
market: string;
|
|
107
|
+
lockProof: Uint8Array;
|
|
108
|
+
fields: bigint[];
|
|
109
|
+
encChange: Uint8Array;
|
|
110
|
+
encFee: Uint8Array;
|
|
111
|
+
keeperCt: Uint8Array;
|
|
112
|
+
proof: Uint8Array;
|
|
113
|
+
outs: bigint[];
|
|
114
|
+
delivery: bigint[];
|
|
115
|
+
encs: Uint8Array[];
|
|
116
|
+
}): Uint8Array;
|
|
117
|
+
/** `refi::cancel_refi_args`: the loan and pool by id. */
|
|
118
|
+
export declare const cancelRefiArgs: (loan: string, poolL: string) => Uint8Array;
|
|
119
|
+
/** `refi::settle_refi_args`: `settle_args`' rule with the market, the reading's bcs, the payoff pair and `p_claim`. */
|
|
120
|
+
export declare function settleRefiArgs(a: {
|
|
121
|
+
loan: string;
|
|
122
|
+
poolL: string;
|
|
123
|
+
market: string;
|
|
124
|
+
reading: Uint8Array;
|
|
125
|
+
proof: Uint8Array;
|
|
126
|
+
termsCommit: bigint;
|
|
127
|
+
borrowerNote: bigint;
|
|
128
|
+
feeNote: bigint;
|
|
129
|
+
winNull: bigint;
|
|
130
|
+
d: bigint[];
|
|
131
|
+
ephX: bigint;
|
|
132
|
+
ephY: bigint;
|
|
133
|
+
feeCt: bigint;
|
|
134
|
+
payoffNote: bigint;
|
|
135
|
+
pad: bigint;
|
|
136
|
+
ephP: bigint[];
|
|
137
|
+
pClaim: bigint;
|
|
138
|
+
keeperCt: Uint8Array;
|
|
139
|
+
encs: Uint8Array[];
|
|
140
|
+
encsP: Uint8Array[];
|
|
141
|
+
nowS: bigint;
|
|
142
|
+
}): Uint8Array;
|
|
143
|
+
/** The bcs of `price::Reading`: the oracle type name, the feed id, spot, EMA, the exponent and `publish_s`. */
|
|
144
|
+
export declare const readingBytes: (r: {
|
|
145
|
+
oracle: string;
|
|
146
|
+
feedId: string;
|
|
147
|
+
spot: bigint;
|
|
148
|
+
ema: bigint;
|
|
149
|
+
expo: bigint;
|
|
150
|
+
publishS: bigint;
|
|
151
|
+
}) => Uint8Array;
|
|
152
|
+
//# sourceMappingURL=settle-plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settle-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/settle-plan.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgC,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjD,OAAO,EAAiC,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAEjF,eAAO,MAAM,YAAY,KAAK,CAAC;AAC/B,eAAO,MAAM,gBAAgB,KAAK,CAAC;AASnC,4DAA4D;AAC5D,wBAAgB,WAAW,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAYzD;AAED,6GAA6G;AAC7G,wBAAgB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CAAC;CAClB,KAAG,UAQD,CAAC"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { DOM_A2, DOM_A2D, DOM_A2O, DOM_LNULL, DOM_TSALT, R } from "../domains.js";
|
|
3
|
+
import { feeWitness } from "../fee-witness.js";
|
|
4
|
+
import { ecdhBlind, feeLane, fieldAdd } from "../fees.js";
|
|
5
|
+
import { noteCommitment } from "../notes/keys.js";
|
|
6
|
+
import { lock4, lockField, offerCommit } from "./offer-plan.js";
|
|
7
|
+
export const SETTLE_SLOTS = 16;
|
|
8
|
+
export const LOAN_SETTLE_KIND = 13;
|
|
9
|
+
const INTEREST_DENOM = 315360000000n;
|
|
10
|
+
function readLe(bytes, at, size) {
|
|
11
|
+
let v = 0n;
|
|
12
|
+
for (let i = size - 1; i >= 0; i--)
|
|
13
|
+
v = (v << 8n) | BigInt(bytes[at + i]);
|
|
14
|
+
return v;
|
|
15
|
+
}
|
|
16
|
+
/** The 208-byte offer plaintext `offerPlaintext` writes. */
|
|
17
|
+
export function decodeOffer(plain) {
|
|
18
|
+
if (plain.length !== 208)
|
|
19
|
+
throw new Error("the offer plaintext is not 208 bytes");
|
|
20
|
+
return {
|
|
21
|
+
rateBps: readLe(plain, 0, 8),
|
|
22
|
+
salt: readLe(plain, 8, 32),
|
|
23
|
+
lenderPk: readLe(plain, 40, 32),
|
|
24
|
+
encX: readLe(plain, 72, 32),
|
|
25
|
+
encY: readLe(plain, 104, 32),
|
|
26
|
+
ns: readLe(plain, 136, 32),
|
|
27
|
+
b: readLe(plain, 168, 32),
|
|
28
|
+
amount: readLe(plain, 200, 8),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** `crypto.mjs` interest: whole seconds of the term, and interest rounded up over 365 days of 10_000 bps. */
|
|
32
|
+
export function interest(principal, rateBps, termMs) {
|
|
33
|
+
const qTerm = termMs / 1000n;
|
|
34
|
+
const num = principal * rateBps * qTerm + INTEREST_DENOM - 1n;
|
|
35
|
+
return {
|
|
36
|
+
qTerm,
|
|
37
|
+
rTerm: termMs % 1000n,
|
|
38
|
+
qIntr: num / INTEREST_DENOM,
|
|
39
|
+
rIntr: num % INTEREST_DENOM,
|
|
40
|
+
owed: principal + num / INTEREST_DENOM,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/** The winner: the lowest rate of an active slot at or under the request's maximum, earliest on a tie. */
|
|
44
|
+
export function lowestEligible(slots, maxRate) {
|
|
45
|
+
let w = -1;
|
|
46
|
+
slots.forEach((s, i) => {
|
|
47
|
+
if (!s.active || !s.open || s.open.rateBps > maxRate)
|
|
48
|
+
return;
|
|
49
|
+
if (w < 0 || s.open.rateBps < slots[w].open.rateBps)
|
|
50
|
+
w = i;
|
|
51
|
+
});
|
|
52
|
+
return w;
|
|
53
|
+
}
|
|
54
|
+
/** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (`ref.mjs` loanSettleCase, round 0). */
|
|
55
|
+
export async function loanSettle(p) {
|
|
56
|
+
const { request: q, slots } = p;
|
|
57
|
+
if (slots.length > SETTLE_SLOTS)
|
|
58
|
+
throw new Error("more than 16 offers");
|
|
59
|
+
const w = lowestEligible(slots, q.maxRateBps);
|
|
60
|
+
if (w < 0)
|
|
61
|
+
throw new Error("no eligible offer");
|
|
62
|
+
const win = slots[w].open;
|
|
63
|
+
const field = lockField(p.loanKey, 0n);
|
|
64
|
+
for (const s of slots) {
|
|
65
|
+
if (!s.active || !s.open)
|
|
66
|
+
continue;
|
|
67
|
+
const o = s.open;
|
|
68
|
+
if (offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY) !== s.commit)
|
|
69
|
+
throw new Error("an offer does not open its commit");
|
|
70
|
+
if (lock4(field, o.lenderPk, o.ns, o.amount, o.b) !== s.lock)
|
|
71
|
+
throw new Error("an offer does not open its lock");
|
|
72
|
+
}
|
|
73
|
+
const pad = (pick, zero) => Array.from({ length: SETTLE_SLOTS }, (_, i) => (slots[i] ? pick(slots[i]) : zero));
|
|
74
|
+
const opened = (pick) => pad((s) => (s.open ? pick(s.open) : 0n), 0n);
|
|
75
|
+
const commits = pad((s) => (s.active ? s.commit : 0n), 0n);
|
|
76
|
+
const locks = pad((s) => s.lock, 0n);
|
|
77
|
+
const actMask = slots.reduce((m, s, i) => (s.active ? m | (1n << BigInt(i)) : m), 0n);
|
|
78
|
+
const term = interest(q.principal, win.rateBps, q.termMs);
|
|
79
|
+
const maturityS = p.nowS + term.qTerm;
|
|
80
|
+
const tSalt = poseidonHash([DOM_TSALT, field, win.ns]);
|
|
81
|
+
const termsCommit = poseidonHash([
|
|
82
|
+
q.principal,
|
|
83
|
+
win.rateBps,
|
|
84
|
+
term.owed,
|
|
85
|
+
maturityS,
|
|
86
|
+
q.borrowerPk,
|
|
87
|
+
win.lenderPk,
|
|
88
|
+
poseidonHash([win.encX, win.encY]),
|
|
89
|
+
tSalt,
|
|
90
|
+
]);
|
|
91
|
+
const lane = await feeLane({ ...p.fee, base: q.principal, eph: p.eph });
|
|
92
|
+
const principalNote = noteCommitment(q.principal - lane.fee, p.borrowerNote.pk, p.borrowerNote.blinding);
|
|
93
|
+
const winNull = poseidonHash([DOM_LNULL, win.ns, slots[w].lock]);
|
|
94
|
+
const k = await ecdhBlind(win.encX, win.encY, p.e);
|
|
95
|
+
const ct1 = fieldAdd(q.collAmt, poseidonHash([k.sx, 1n]));
|
|
96
|
+
const ct2 = fieldAdd(q.collBlind, poseidonHash([k.sx, 2n]));
|
|
97
|
+
const reqCommit = poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]);
|
|
98
|
+
const d = [k.ephX, k.ephY, ct1, ct2];
|
|
99
|
+
const publics = [
|
|
100
|
+
poseidonHash(commits),
|
|
101
|
+
poseidonHash(locks),
|
|
102
|
+
poseidonHash([DOM_A2, reqCommit, field, actMask, p.nowS, lane.fee_commit, p.fee.version, p.extHash, 0n]),
|
|
103
|
+
poseidonHash([DOM_A2O, termsCommit, principalNote, lane.fee_note, winNull]),
|
|
104
|
+
poseidonHash([DOM_A2D, ...d, lane.ephX, lane.ephY, lane.fee_ct]),
|
|
105
|
+
];
|
|
106
|
+
const s = (xs) => xs.map(String);
|
|
107
|
+
const input = {
|
|
108
|
+
commits: s(commits),
|
|
109
|
+
locks: s(locks),
|
|
110
|
+
rate: s(opened((o) => o.rateBps)),
|
|
111
|
+
salt: s(opened((o) => o.salt)),
|
|
112
|
+
lenderPk: s(opened((o) => o.lenderPk)),
|
|
113
|
+
encX: s(opened((o) => o.encX)),
|
|
114
|
+
encY: s(opened((o) => o.encY)),
|
|
115
|
+
actMask: actMask.toString(),
|
|
116
|
+
winner: String(w),
|
|
117
|
+
winRate: win.rateBps.toString(),
|
|
118
|
+
ns_w: win.ns.toString(),
|
|
119
|
+
b_w: win.b.toString(),
|
|
120
|
+
q_term: term.qTerm.toString(),
|
|
121
|
+
r_term: term.rTerm.toString(),
|
|
122
|
+
q_intr: term.qIntr.toString(),
|
|
123
|
+
r_intr: term.rIntr.toString(),
|
|
124
|
+
principal: q.principal.toString(),
|
|
125
|
+
max_rate_bps: q.maxRateBps.toString(),
|
|
126
|
+
term_ms: q.termMs.toString(),
|
|
127
|
+
borrowerPk: q.borrowerPk.toString(),
|
|
128
|
+
collAmt: q.collAmt.toString(),
|
|
129
|
+
collBlind: q.collBlind.toString(),
|
|
130
|
+
salt_req: q.salt.toString(),
|
|
131
|
+
loanKey: p.loanKey.toString(),
|
|
132
|
+
pkB: p.borrowerNote.pk.toString(),
|
|
133
|
+
blindB: p.borrowerNote.blinding.toString(),
|
|
134
|
+
e: p.e.toString(),
|
|
135
|
+
now_s: p.nowS.toString(),
|
|
136
|
+
extHash: p.extHash.toString(),
|
|
137
|
+
relayer_fee: "0",
|
|
138
|
+
...feeWitness(p.fee, q.principal, p.eph, lane),
|
|
139
|
+
};
|
|
140
|
+
if (publics.some((x) => x >= R))
|
|
141
|
+
throw new Error("a public signal is not a field element");
|
|
142
|
+
return {
|
|
143
|
+
input,
|
|
144
|
+
publics,
|
|
145
|
+
winner: w,
|
|
146
|
+
terms: {
|
|
147
|
+
principal: q.principal,
|
|
148
|
+
rateBps: win.rateBps,
|
|
149
|
+
owed: term.owed,
|
|
150
|
+
maturityS,
|
|
151
|
+
lenderPk: win.lenderPk,
|
|
152
|
+
lenderEncX: win.encX,
|
|
153
|
+
lenderEncY: win.encY,
|
|
154
|
+
salt: tSalt,
|
|
155
|
+
},
|
|
156
|
+
termsCommit,
|
|
157
|
+
principalNote,
|
|
158
|
+
principalValue: q.principal - lane.fee,
|
|
159
|
+
winNull,
|
|
160
|
+
d,
|
|
161
|
+
feeNote: lane.fee_note,
|
|
162
|
+
ephX: lane.ephX,
|
|
163
|
+
ephY: lane.ephY,
|
|
164
|
+
feeCt: lane.fee_ct,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
const ascii = (s) => Uint8Array.from(Buffer.from(s, "ascii"));
|
|
168
|
+
const cat = (...parts) => Uint8Array.from(parts.flatMap((x) => [...x]));
|
|
169
|
+
function le(value, size) {
|
|
170
|
+
const out = new Uint8Array(size);
|
|
171
|
+
for (let i = 0; i < size; i++)
|
|
172
|
+
out[i] = Number((value >> BigInt(8 * i)) & 0xffn);
|
|
173
|
+
return out;
|
|
174
|
+
}
|
|
175
|
+
function uleb(n) {
|
|
176
|
+
const out = [];
|
|
177
|
+
for (; n >= 0x80; n >>>= 7)
|
|
178
|
+
out.push((n & 0x7f) | 0x80);
|
|
179
|
+
return Uint8Array.from([...out, n]);
|
|
180
|
+
}
|
|
181
|
+
const bcsBytes = (b) => cat(uleb(b.length), b);
|
|
182
|
+
const addressBytes = (id) => le(BigInt(id), 32).reverse();
|
|
183
|
+
/** `loan::action_hash`: blake2b256("ess3/act/" ‖ kind ‖ "/" ‖ bcs(loan_key) ‖ bcs(nonce) ‖ args) mod R. */
|
|
184
|
+
export function actionHash(blake, loanKey, nonce, kind, args) {
|
|
185
|
+
const digest = blake(cat(ascii(`ess3/act/${kind}/`), le(loanKey, 32), le(nonce, 8), args));
|
|
186
|
+
return BigInt(`0x${Buffer.from(digest).toString("hex")}`) % R;
|
|
187
|
+
}
|
|
188
|
+
/** `settle::settle_args`: every argument in declaration order, shared objects by id, vectors and the proof as bcs. */
|
|
189
|
+
export function settleArgs(a) {
|
|
190
|
+
return cat(addressBytes(a.loan), addressBytes(a.poolL), bcsBytes(a.proof), le(a.termsCommit, 32), le(a.principalNote, 32), le(a.feeNote, 32), le(a.winNull, 32), cat(uleb(a.d.length), ...a.d.map((x) => le(x, 32))), le(a.ephX, 32), le(a.ephY, 32), le(a.feeCt, 32), bcsBytes(a.keeperCt), cat(uleb(a.encs.length), ...a.encs.map(bcsBytes)), le(a.nowS, 8));
|
|
191
|
+
}
|
|
192
|
+
/** `membership::seal_offer(room, round, seq, viewer).bind`. */
|
|
193
|
+
export const sealOfferBind = (blake, room, round, seq, viewer) => blake(cat(ascii("essey-v3/seal/offer"), addressBytes(room), le(round, 8), le(seq, 8), addressBytes(viewer)));
|
|
194
|
+
/** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request as bcs, `close_ms`. */
|
|
195
|
+
export const openRefiArgs = (a) => cat(addressBytes(a.loan), addressBytes(a.poolL), le(a.reqCommit, 32), bcsBytes(a.reqSealed), le(a.closeMs, 8));
|
|
196
|
+
/** `liquidation::topup_args`: every `topup_by_borrower` argument after the pass, in order, by `settle_args`' rule. */
|
|
197
|
+
export function topupArgs(a) {
|
|
198
|
+
const fields = (xs) => cat(uleb(xs.length), ...xs.map((x) => le(x, 32)));
|
|
199
|
+
return cat(addressBytes(a.loan), addressBytes(a.poolC), addressBytes(a.market), bcsBytes(a.lockProof), ...a.fields.map((x) => le(x, 32)), bcsBytes(a.encChange), bcsBytes(a.encFee), bcsBytes(a.keeperCt), bcsBytes(a.proof), fields(a.outs), fields(a.delivery), cat(uleb(a.encs.length), ...a.encs.map(bcsBytes)));
|
|
200
|
+
}
|
|
201
|
+
/** `refi::cancel_refi_args`: the loan and pool by id. */
|
|
202
|
+
export const cancelRefiArgs = (loan, poolL) => cat(addressBytes(loan), addressBytes(poolL));
|
|
203
|
+
/** `refi::settle_refi_args`: `settle_args`' rule with the market, the reading's bcs, the payoff pair and `p_claim`. */
|
|
204
|
+
export function settleRefiArgs(a) {
|
|
205
|
+
const fields = (xs) => cat(uleb(xs.length), ...xs.map((x) => le(x, 32)));
|
|
206
|
+
const blobs = (xs) => cat(uleb(xs.length), ...xs.map(bcsBytes));
|
|
207
|
+
return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), ...[a.termsCommit, a.borrowerNote, a.feeNote, a.winNull].map((x) => le(x, 32)), fields(a.d), ...[a.ephX, a.ephY, a.feeCt, a.payoffNote, a.pad].map((x) => le(x, 32)), fields(a.ephP), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), blobs(a.encsP), le(a.nowS, 8));
|
|
208
|
+
}
|
|
209
|
+
/** The bcs of `price::Reading`: the oracle type name, the feed id, spot, EMA, the exponent and `publish_s`. */
|
|
210
|
+
export const readingBytes = (r) => cat(bcsBytes(ascii(r.oracle)), addressBytes(r.feedId), le(r.spot, 8), le(r.ema, 8), Uint8Array.of(Number(r.expo)), le(r.publishS, 8));
|