@esseyonsui/sui-sdk-v3 0.1.6 → 0.1.8
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 +7 -0
- package/dist/config.d.ts +5 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +4 -0
- package/dist/errors.d.ts +5 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +1 -0
- package/dist/essey.d.ts +61 -9
- package/dist/essey.d.ts.map +1 -1
- package/dist/essey.js +266 -56
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/v3/collateral/local-health.d.ts +3 -2
- package/dist/v3/collateral/local-health.d.ts.map +1 -1
- package/dist/v3/collateral/local-health.js +2 -6
- package/dist/v3/index.d.ts +1 -0
- package/dist/v3/index.d.ts.map +1 -1
- package/dist/v3/index.js +1 -0
- package/dist/v3/keeper-badge.d.ts +3 -1
- package/dist/v3/keeper-badge.d.ts.map +1 -1
- package/dist/v3/keeper-badge.js +2 -2
- package/dist/v3/notes/denominations.d.ts +4 -0
- package/dist/v3/notes/denominations.d.ts.map +1 -1
- package/dist/v3/notes/denominations.js +23 -7
- package/dist/v3/notes/withdraw.d.ts +7 -0
- package/dist/v3/notes/withdraw.d.ts.map +1 -1
- package/dist/v3/notes/withdraw.js +47 -0
- package/dist/v3/pace.d.ts +43 -0
- package/dist/v3/pace.d.ts.map +1 -0
- package/dist/v3/pace.js +42 -0
- package/dist/v3/relay/client.d.ts +0 -8
- package/dist/v3/relay/client.d.ts.map +1 -1
- package/dist/v3/relay/client.js +0 -17
- package/dist/v3/stage1.d.ts +2 -2
- package/dist/v3/stage1.d.ts.map +1 -1
- package/dist/v3/stage1.js +4 -4
- package/package.json +1 -1
package/dist/essey.js
CHANGED
|
@@ -16,7 +16,21 @@ import { DOM_LBLIND, DOM_TSALT, R } from "./v3/domains.js";
|
|
|
16
16
|
import { MODE_OPEN, MODE_TOPUP, NO_ADDRESS, collLockExt } from "./v3/ext.js";
|
|
17
17
|
import { defaultExt, faultExt, heartbeatExt, liquidateExt, openExt, releaseExt, topupByBorrowerExt, topupExt, } from "./v3/collateral/ext.js";
|
|
18
18
|
import { claimDefault as claimDefaultBody, faultLiquidate as faultLiquidateBody, heartbeat as heartbeatBody, liquidate as liquidateBody, release as releaseBody, topup as topupBody, topupBorrower as topupBorrowerBody, } from "./v3/collateral/bodies.js";
|
|
19
|
-
import { heartbeatFloor } from "./v3/collateral/math.js";
|
|
19
|
+
import { GRACE_S, heartbeatFloor } from "./v3/collateral/math.js";
|
|
20
|
+
/**
|
|
21
|
+
* The Seal servers dry-run `keeper::check_loan` and answer "does not have access" when it aborts: a heartbeat still
|
|
22
|
+
* covers the loan at this price, the loan is not active, or the pass or badge does not match.
|
|
23
|
+
*/
|
|
24
|
+
function keeperRefusal(err) {
|
|
25
|
+
if (err instanceof Error && /does not have access/i.test(err.message))
|
|
26
|
+
throw new EsseyError("KEEPER_NOT_ELIGIBLE", "refused: the seal servers did not release the keeper key; a live heartbeat covers the loan at this price, " +
|
|
27
|
+
"the loan is not active, or the keeper pass or badge does not match");
|
|
28
|
+
throw err;
|
|
29
|
+
}
|
|
30
|
+
/** `liquidation.move` `HB_MIN_GAP_MS`, `STANDIN_HB_GAP_MS` and `H_MAX_MS`. */
|
|
31
|
+
const HB_GAP_MS = 3000000n;
|
|
32
|
+
const HB_GAP_STANDIN_MS = 120000n;
|
|
33
|
+
const H_MAX_MS = 86400000n;
|
|
20
34
|
import { MODE, NO_TERMS, health, lenderAmount, loanRisk, seizeSplit } from "./v3/collateral/health.js";
|
|
21
35
|
import { collLockPk, collNote, outputBlind, requestCommit, termsCommit, } from "./v3/collateral/openings.js";
|
|
22
36
|
import { fetchKeeperPlaintext, keeperKeyId, openingOf, rollTo } from "./v3/collateral/seal.js";
|
|
@@ -53,13 +67,18 @@ import { noteCommitment, noteNullifier, notePk } from "./v3/notes/keys.js";
|
|
|
53
67
|
import { fillerEnc, sealOpening } from "./v3/notes/opening.js";
|
|
54
68
|
import { orderedLeaves, ownNotes, ownOpenings, usedIndices, } from "./v3/notes/scan.js";
|
|
55
69
|
import { poolTree } from "./v3/notes/tree.js";
|
|
56
|
-
import { exitValue, pickInputs, refuseLinkedAmount, transferValue, withdrawCost, } from "./v3/notes/withdraw.js";
|
|
70
|
+
import { exitKept, exitPieces, exitValue, piecePayees, pickInputs, refuseLinkedAmount, refuseUncovered, transferValue, withdrawCost, withdrawPieces, } from "./v3/notes/withdraw.js";
|
|
71
|
+
import { paceDelay } from "./v3/pace.js";
|
|
57
72
|
import { proofBytes } from "./v3/proof-bytes.js";
|
|
58
73
|
import { RelayClient, failure, } from "./v3/relay/client.js";
|
|
59
74
|
import { provePossession } from "./v3/relay/possession.js";
|
|
60
|
-
|
|
75
|
+
/** A ticket lives 15 min (`services/relayer-v3/src/tickets.ts:7`); a minute past it the relayer answers "expired". */
|
|
76
|
+
const FOLLOW_CAP_MS = 16 * 60_000;
|
|
77
|
+
/** Times a seize output's collect waits again while nothing has landed after the seizure. */
|
|
78
|
+
const SEIZE_REWAITS = 3;
|
|
61
79
|
/** The relayer's 202 after its wait cap can carry an etaMs near 0 (LOAD-02), which would spend every poll in seconds. */
|
|
62
80
|
const FOLLOW_MIN_GAP_MS = 2_000;
|
|
81
|
+
const SPENT_POLLS = 60;
|
|
63
82
|
const MAX_USED_SENDERS = 64;
|
|
64
83
|
const EPH_SHIFT = 3n;
|
|
65
84
|
const NO_RELAYER = "no relayer url: the deployment record names none yet; nothing was sent";
|
|
@@ -487,6 +506,20 @@ function relayerUrl(choice, config) {
|
|
|
487
506
|
throw new EsseyError("ONION_UNAVAILABLE", "refused: this build has no Tor transport; nothing was sent");
|
|
488
507
|
return choice === "clear" ? config.relayer.url : choice;
|
|
489
508
|
}
|
|
509
|
+
/** A seize inserts its notes in pairs (`liquidation.move:445-446`, `pool.move:677-685`): its last leaf is `index | 1`. */
|
|
510
|
+
const lastLanded = (events, index) => BigInt(events.commitments.length) === (index | 1n) + 1n;
|
|
511
|
+
function pacingOf(delays) {
|
|
512
|
+
if (delays === undefined || delays === "default")
|
|
513
|
+
return true;
|
|
514
|
+
if (delays === "off")
|
|
515
|
+
return false;
|
|
516
|
+
throw new EsseyError("INVALID_ARGUMENT", 'delays must be "default" or "off"');
|
|
517
|
+
}
|
|
518
|
+
/** From maturity plus grace a default may be claimed (`src/v3/collateral/health.ts:174`); `repay.move` reads no clock. */
|
|
519
|
+
const repayDeadline = (terms) => ({
|
|
520
|
+
atMs: Number((terms.maturityS + GRACE_S) * 1000n),
|
|
521
|
+
soft: true,
|
|
522
|
+
});
|
|
490
523
|
/**
|
|
491
524
|
* A deposit answered a digest for an earlier piece, or left a stranded record. Depositing the whole amount
|
|
492
525
|
* again repeats them. A digest is not confirmation the note is in the pool.
|
|
@@ -534,14 +567,42 @@ function payee(to) {
|
|
|
534
567
|
throw new EsseyError("INVALID_ARGUMENT", NO_TO);
|
|
535
568
|
return recipient;
|
|
536
569
|
}
|
|
570
|
+
const listOf = (to) => (Array.isArray(to) ? to : [to]);
|
|
571
|
+
function onePayee(to) {
|
|
572
|
+
const list = listOf(to);
|
|
573
|
+
if (list.length !== 1)
|
|
574
|
+
throw new EsseyError("INVALID_ARGUMENT", "refused: force: true, or a coin without denominations, pays one exact withdraw to one address; give one to; nothing was sent");
|
|
575
|
+
return list[0];
|
|
576
|
+
}
|
|
577
|
+
/** Pieces already paid are public and final; the error lists them so none is paid twice. */
|
|
578
|
+
function payoutStopped(error, paid, pieces) {
|
|
579
|
+
if (paid.length === 0)
|
|
580
|
+
return error;
|
|
581
|
+
const at = paid.length + 1;
|
|
582
|
+
const inner = isEsseyError(error) ? error : undefined;
|
|
583
|
+
const why = inner
|
|
584
|
+
? [inner.code, inner.detail?.error].filter(Boolean).join(" ")
|
|
585
|
+
: error instanceof Error
|
|
586
|
+
? error.message
|
|
587
|
+
: "it threw";
|
|
588
|
+
const stopped = new EsseyError(inner?.code ?? "RELAY_FAILED", `withdraw stopped at piece ${at} of ${pieces.length}: ${why}. Pieces paid before it: ${paid.length}, listed in detail.landed; do not pay them again`, {
|
|
589
|
+
...inner?.detail,
|
|
590
|
+
landed: paid.map((p) => ({ digest: p.digest, value: p.amount.toString(), to: p.to })),
|
|
591
|
+
stopped: pieces[paid.length].toString(),
|
|
592
|
+
untried: pieces.slice(at).map(String),
|
|
593
|
+
});
|
|
594
|
+
stopped.cause = error;
|
|
595
|
+
return stopped;
|
|
596
|
+
}
|
|
537
597
|
/**
|
|
538
|
-
* LOAN-01: close_ms is public, so an exact close publishes the caller's chosen window. It rounds up to a whole
|
|
539
|
-
* hour, or down one
|
|
598
|
+
* LOAN-01: close_ms is public, so an exact close publishes the caller's chosen window. It rounds up to a whole
|
|
599
|
+
* `step` (an hour unless the deployment sets `closeStepMs`), or down one step when up would pass the chain's
|
|
600
|
+
* longest window, which is still far above its shortest.
|
|
540
601
|
*/
|
|
541
|
-
function
|
|
542
|
-
const up = Math.ceil(closeMs /
|
|
602
|
+
function stepClose(closeMs, nowMs, maxWindowMs, step = HOUR_MS) {
|
|
603
|
+
const up = Math.ceil(closeMs / step) * step;
|
|
543
604
|
const over = up - nowMs > maxWindowMs && closeMs - nowMs <= maxWindowMs;
|
|
544
|
-
return over ? up -
|
|
605
|
+
return over ? up - step : up;
|
|
545
606
|
}
|
|
546
607
|
function refusedBy(service, reply) {
|
|
547
608
|
const error = typeof reply.body.error === "string" ? reply.body.error : undefined;
|
|
@@ -559,6 +620,7 @@ export class Essey {
|
|
|
559
620
|
relay;
|
|
560
621
|
relayerBase;
|
|
561
622
|
seedOption;
|
|
623
|
+
pacing;
|
|
562
624
|
credential;
|
|
563
625
|
fees;
|
|
564
626
|
relayer;
|
|
@@ -568,7 +630,7 @@ export class Essey {
|
|
|
568
630
|
/** D-SPEC §6.2:598-600. Sealed sale rooms. */
|
|
569
631
|
sales;
|
|
570
632
|
keeper;
|
|
571
|
-
constructor(config, statePath, apiOrigin, fetch, io, relay, relayerBase, seedOption) {
|
|
633
|
+
constructor(config, statePath, apiOrigin, fetch, io, relay, relayerBase, seedOption, pacing) {
|
|
572
634
|
this.config = config;
|
|
573
635
|
this.statePath = statePath;
|
|
574
636
|
this.apiOrigin = apiOrigin;
|
|
@@ -577,6 +639,10 @@ export class Essey {
|
|
|
577
639
|
this.relay = relay;
|
|
578
640
|
this.relayerBase = relayerBase;
|
|
579
641
|
this.seedOption = seedOption;
|
|
642
|
+
this.pacing = pacing;
|
|
643
|
+
const closeOf = (loan) => () => this.roundDeadline(loan, "close_ms");
|
|
644
|
+
const settleByOf = (loan) => () => this.roundDeadline(loan, "settle_by_ms");
|
|
645
|
+
const coverOf = (loan) => () => this.coverDeadline(loan);
|
|
580
646
|
this.credential = {
|
|
581
647
|
obtain: (o) => this.obtain(o?.invite, o?.role ?? "member"),
|
|
582
648
|
status: () => this.info(readState(this.statePath).credential),
|
|
@@ -597,6 +663,7 @@ export class Essey {
|
|
|
597
663
|
request: (id) => this.readRequest(id),
|
|
598
664
|
lend: async (p) => {
|
|
599
665
|
const send = async () => {
|
|
666
|
+
await this.pause("offer", p.loan, closeOf(p.loan));
|
|
600
667
|
const { body, record, seq } = await this.planOffer(p.loan, BigInt(p.rateBps));
|
|
601
668
|
return {
|
|
602
669
|
seq,
|
|
@@ -613,30 +680,30 @@ export class Essey {
|
|
|
613
680
|
return (await send()).sent;
|
|
614
681
|
}
|
|
615
682
|
},
|
|
616
|
-
collect: (p) => this.collect(p.loan, p.offer),
|
|
617
|
-
collectSeized: (p) => this.collectSeized(p.loan, p.offer),
|
|
618
|
-
collectSurplus: (p) => this.collectSurplus(p),
|
|
619
|
-
accept:
|
|
683
|
+
collect: (p) => this.paced("collect", p.loan, null, () => this.collect(p.loan, p.offer)),
|
|
684
|
+
collectSeized: (p) => this.paced("collect_seized", p.loan, null, () => this.collectSeized(p.loan, p.offer)),
|
|
685
|
+
collectSurplus: (p) => this.paced("collect_surplus", p.loan, null, () => this.collectSurplus(p)),
|
|
686
|
+
accept: (id) => this.paced("settle", id, settleByOf(id), async () => {
|
|
620
687
|
const { body, rateBps, terms, borrowerPk, collAmt } = await this.planSettle(id);
|
|
621
688
|
return { ...(await this.freeLoan(settleLoan, body)), rateBps, terms, borrowerPk, collAmt };
|
|
622
|
-
},
|
|
623
|
-
release: (p) => this.releaseCollateral(p.loan, p.terms, p.borrowerPk, p.collAmt),
|
|
689
|
+
}),
|
|
690
|
+
release: (p) => this.paced("release", p.loan, null, () => this.releaseCollateral(p.loan, p.terms, p.borrowerPk, p.collAmt)),
|
|
624
691
|
cancelOwn: (id) => this.cancelOwn(id),
|
|
625
692
|
slash: (p) => this.slashOffer(p.loan, BigInt(p.seq)),
|
|
626
|
-
fault: (p) => this.ctFault(p.loan, p.via),
|
|
693
|
+
fault: (p) => this.paced("fault", p.loan, null, () => this.ctFault(p.loan, p.via)),
|
|
627
694
|
openRefi: (p) => this.openRefi(p),
|
|
628
695
|
cancelRefi: (id) => this.cancelRefiOwn(id),
|
|
629
|
-
settleRefi: (p) => this.settleRefiOwn(p),
|
|
630
|
-
reclaimOffer: (p) => this.reclaimOffer(p.loan, p.offer, p.round),
|
|
631
|
-
heartbeat: (p) => this.heartbeatOwn(p),
|
|
632
|
-
topup: (p) => this.topupOwn(p),
|
|
633
|
-
liquidate: (p) => this.borrowerSeize(p, MODE.LIQUIDATE),
|
|
634
|
-
seizeDefault: (p) => this.borrowerSeize(p, MODE.DEFAULT),
|
|
635
|
-
faultLiquidate: (p) => this.faultLiquidate(p.loan, p.offer),
|
|
636
|
-
keeperLiquidate: (p) => this.keeperLiquidate(p.loan, p.via),
|
|
637
|
-
reclaimCollateral: (p) => this.reclaimOwnCollateral(p.loan, p.collAmt),
|
|
696
|
+
settleRefi: (p) => this.paced("settle", p.loan, settleByOf(p.loan), () => this.settleRefiOwn(p)),
|
|
697
|
+
reclaimOffer: (p) => this.paced("reclaim_offer", p.loan, null, () => this.reclaimOffer(p.loan, p.offer, p.round)),
|
|
698
|
+
heartbeat: (p) => this.paced("heartbeat", p.loan, coverOf(p.loan), () => this.heartbeatOwn(p)),
|
|
699
|
+
topup: (p) => this.paced("topup", p.loan, coverOf(p.loan), () => this.topupOwn(p)),
|
|
700
|
+
liquidate: (p) => this.paced("liquidate", p.loan, null, () => this.borrowerSeize(p, MODE.LIQUIDATE)),
|
|
701
|
+
seizeDefault: (p) => this.paced("liquidate", p.loan, null, () => this.borrowerSeize(p, MODE.DEFAULT)),
|
|
702
|
+
faultLiquidate: (p) => this.paced("liquidate", p.loan, null, () => this.faultLiquidate(p.loan, p.offer)),
|
|
703
|
+
keeperLiquidate: (p) => this.paced("liquidate", p.loan, null, () => this.keeperLiquidate(p.loan, p.via)),
|
|
704
|
+
reclaimCollateral: (p) => this.paced("reclaim_collateral", p.loan, null, () => this.reclaimOwnCollateral(p.loan, p.collAmt)),
|
|
638
705
|
recover: (p) => this.recoverOwnLoan(p.loan),
|
|
639
|
-
payBack: async (p) => this.paidLoan(repayLoan, await this.planRepay(p.loan, p.terms, p.borrowerPk)),
|
|
706
|
+
payBack: (p) => this.paced("repay", p.loan, async () => repayDeadline(p.terms), async () => this.paidLoan(repayLoan, await this.planRepay(p.loan, p.terms, p.borrowerPk))),
|
|
640
707
|
settle: (p) => this.freeLoan(settleLoan, p),
|
|
641
708
|
repay: (p) => this.paidLoan(repayLoan, p),
|
|
642
709
|
reclaim: (p) => this.paidLoan(reclaimLoan, p),
|
|
@@ -653,21 +720,21 @@ export class Essey {
|
|
|
653
720
|
open: (coin, index) => this.openNote(coin, index),
|
|
654
721
|
list: (coin) => this.listNotes(coin),
|
|
655
722
|
view: (coin, index) => this.viewNote(coin, index),
|
|
656
|
-
withdraw: (coin, amount, to, o) => this.withdraw(coin, amount, to, o),
|
|
723
|
+
withdraw: (coin, amount, to, o) => this.paced("withdraw", `${coin}:${amount}:${to}`, null, () => this.withdraw(coin, amount, to, o)),
|
|
657
724
|
receiveAddress: () => this.receiveAddress(),
|
|
658
|
-
transfer: (coin, amount, to) => this.transfer(coin, amount, to),
|
|
659
|
-
exit: (coin, to, o) => this.exit(coin, to, o),
|
|
725
|
+
transfer: (coin, amount, to) => this.paced("transfer", `${coin}:${amount}:${to}`, null, () => this.transfer(coin, amount, to)),
|
|
726
|
+
exit: (coin, to, o) => this.paced("exit", `${coin}:${to}`, null, () => this.exit(coin, to, o)),
|
|
660
727
|
recoverStranded: () => this.recoverStranded(),
|
|
661
728
|
};
|
|
662
729
|
this.sales = {
|
|
663
730
|
open: (p) => this.openSale(p),
|
|
664
731
|
list: () => this.listSales(),
|
|
665
732
|
view: (id) => this.viewSale(id),
|
|
666
|
-
bid: (p) => this.placeBid(p.room, p.v, p.amount),
|
|
733
|
+
bid: (p) => this.paced("bid", p.room, async () => ({ atMs: Number((await this.saleRoom(p.room)).closeMs) }), () => this.placeBid(p.room, p.v, p.amount)),
|
|
667
734
|
finish: (p) => this.finishSale(p),
|
|
668
735
|
slash: (p) => this.slashBid(p.room, p.seq),
|
|
669
736
|
expire: (p) => this.expireSale(p.room, p.vault),
|
|
670
|
-
reclaim: (p) => this.reclaimBid(p.room, p.bid, p.what ?? "lock"),
|
|
737
|
+
reclaim: (p) => this.paced("sale_reclaim", p.room, null, () => this.reclaimBid(p.room, p.bid, p.what ?? "lock")),
|
|
671
738
|
claim: (p) => this.claimItem(p),
|
|
672
739
|
cancel: (p) => this.cancelSale(p.room, p.vault),
|
|
673
740
|
badgeGrant: (to) => this.badgeGrant(to),
|
|
@@ -687,9 +754,39 @@ export class Essey {
|
|
|
687
754
|
readState(o.statePath);
|
|
688
755
|
const { config, apiOrigin } = await loadConfig(o.config, fetch);
|
|
689
756
|
const url = relayerUrl(o.relayer ?? "clear", config);
|
|
690
|
-
const relay = url === null ? null : new RelayClient({ url, fetch
|
|
757
|
+
const relay = url === null ? null : new RelayClient({ url, fetch });
|
|
691
758
|
const relayerBase = url === null ? null : serviceUrl(url, "relayer url").origin;
|
|
692
|
-
return new Essey(config, o.statePath, apiOrigin, fetch, io, relay, relayerBase, o.seed && Uint8Array.from(o.seed));
|
|
759
|
+
return new Essey(config, o.statePath, apiOrigin, fetch, io, relay, relayerBase, o.seed && Uint8Array.from(o.seed), pacingOf(o.delays));
|
|
760
|
+
}
|
|
761
|
+
paced(action, subject, deadline, run) {
|
|
762
|
+
return this.pause(action, subject, deadline).then(run);
|
|
763
|
+
}
|
|
764
|
+
/** Runs before the action reads anything its proof binds, so a long wait never stales a Merkle root or `now_s`. */
|
|
765
|
+
async pause(action, subject, deadline) {
|
|
766
|
+
if (!this.pacing)
|
|
767
|
+
return;
|
|
768
|
+
const at = deadline ? await deadline() : null;
|
|
769
|
+
const nowMs = (this.io.now ?? Date.now)();
|
|
770
|
+
const delayMs = paceDelay(action, nowMs, at, Number(CLOSE_MARGIN_MS));
|
|
771
|
+
const wait = this.io.pace ?? ((p) => this.sleep(p.delayMs));
|
|
772
|
+
await wait({ action, subject, delayMs, notBeforeMs: nowMs + delayMs });
|
|
773
|
+
}
|
|
774
|
+
sleep(ms) {
|
|
775
|
+
return (this.io.sleep ?? ((t) => new Promise((resolve) => setTimeout(resolve, t))))(ms);
|
|
776
|
+
}
|
|
777
|
+
/** The current round's `close_ms` (offers, `loan.move:339`) or `settle_by_ms` (settles, `settle.move:132`). */
|
|
778
|
+
async roundDeadline(loanId, field) {
|
|
779
|
+
const read = this.io.readLoan ?? notSetUp("missing config: no loan reader");
|
|
780
|
+
const row = await read(address(loanId, "loan id"));
|
|
781
|
+
const at = row?.round?.[field];
|
|
782
|
+
return at === undefined ? null : { atMs: Number(at) };
|
|
783
|
+
}
|
|
784
|
+
/** The stored heartbeat expiry, past which a keeper may liquidate (`keeper.move:204`); none before a first heartbeat. */
|
|
785
|
+
async coverDeadline(loanId) {
|
|
786
|
+
const read = this.io.readLoan ?? notSetUp("missing config: no loan reader");
|
|
787
|
+
const row = await read(address(loanId, "loan id"));
|
|
788
|
+
const at = Number(row?.json?.coll?.hb_expiry_ms ?? 0);
|
|
789
|
+
return at > 0 ? { atMs: at, soft: true } : null;
|
|
693
790
|
}
|
|
694
791
|
relayClient() {
|
|
695
792
|
return this.relay ?? notSetUp(NO_RELAYER);
|
|
@@ -1202,12 +1299,70 @@ export class Essey {
|
|
|
1202
1299
|
async withdraw(coin, amount, to, o = {}) {
|
|
1203
1300
|
if (amount <= 0n)
|
|
1204
1301
|
throw new EsseyError("INVALID_ARGUMENT", "amount must be above 0");
|
|
1205
|
-
|
|
1302
|
+
const list = this.config.denominations[coinOf(coin)];
|
|
1303
|
+
if (list === undefined || o.force === true)
|
|
1304
|
+
return [await this.withdrawExact(coin, amount, onePayee(to), o)];
|
|
1305
|
+
const payees = listOf(to).map(payee);
|
|
1306
|
+
const pieces = withdrawPieces(amount, list);
|
|
1307
|
+
const sendTo = piecePayees(pieces.length, payees);
|
|
1308
|
+
const { fee, relayerFee, owned } = await this.payoutPlan(coin);
|
|
1309
|
+
refuseUncovered(owned, pieces, fee, relayerFee);
|
|
1310
|
+
refuseLinkedAmount(amount, faucetGrant(coin), owned, fee, relayerFee);
|
|
1311
|
+
return this.payPieces(coin, pieces, sendTo, fee);
|
|
1312
|
+
}
|
|
1313
|
+
async withdrawExact(coin, amount, to, o) {
|
|
1314
|
+
const landed = await this.spend(coin, { to }, (owned, rates, relayerFee) => {
|
|
1206
1315
|
const inputs = pickInputs(owned, withdrawCost(amount, rates, relayerFee));
|
|
1207
1316
|
if (o.force !== true)
|
|
1208
1317
|
refuseLinkedAmount(amount, faucetGrant(coin), owned, rates, relayerFee);
|
|
1209
1318
|
return { value: amount, inputs };
|
|
1210
1319
|
});
|
|
1320
|
+
return { ...landed, amount, to: payee(to) };
|
|
1321
|
+
}
|
|
1322
|
+
/** The same reads and set-up checks `spend` makes, once for every piece of a split payout. */
|
|
1323
|
+
async payoutPlan(coin) {
|
|
1324
|
+
this.relayClient();
|
|
1325
|
+
this.relayerAddress();
|
|
1326
|
+
this.prover();
|
|
1327
|
+
const relayerFee = this.coinFee(coin);
|
|
1328
|
+
const fee = await this.feeParams(coin);
|
|
1329
|
+
const { owned } = await this.owned(coin);
|
|
1330
|
+
return { fee, relayerFee, owned };
|
|
1331
|
+
}
|
|
1332
|
+
/** The one loop over a payout's pieces, where a wait between them belongs. Each reads the pool after the last is seen spent. */
|
|
1333
|
+
async payPieces(coin, pieces, to, fee) {
|
|
1334
|
+
const paid = [];
|
|
1335
|
+
let spent = [];
|
|
1336
|
+
for (const [i, amount] of pieces.entries()) {
|
|
1337
|
+
try {
|
|
1338
|
+
if (i > 0)
|
|
1339
|
+
await this.pause("withdraw_piece", `${coin}:${to[i]}`, null);
|
|
1340
|
+
await this.awaitSpent(coin, spent);
|
|
1341
|
+
const landed = await this.spend(coin, { to: to[i] }, (owned, rates, relayerFee) => {
|
|
1342
|
+
spent = pickInputs(owned, withdrawCost(amount, rates, relayerFee));
|
|
1343
|
+
return { value: amount, inputs: spent };
|
|
1344
|
+
}, [], fee);
|
|
1345
|
+
paid.push({ ...landed, amount, to: to[i] });
|
|
1346
|
+
}
|
|
1347
|
+
catch (error) {
|
|
1348
|
+
throw payoutStopped(error, paid, pieces);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
return paid;
|
|
1352
|
+
}
|
|
1353
|
+
async awaitSpent(coin, spent) {
|
|
1354
|
+
if (spent.length === 0)
|
|
1355
|
+
return;
|
|
1356
|
+
const sleep = this.io.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
1357
|
+
for (let i = 0; i < SPENT_POLLS; i++) {
|
|
1358
|
+
const { owned } = await this.owned(coin);
|
|
1359
|
+
if (!owned.some((n) => spent.some((s) => s.index === n.index)))
|
|
1360
|
+
return;
|
|
1361
|
+
await sleep(FOLLOW_MIN_GAP_MS);
|
|
1362
|
+
}
|
|
1363
|
+
throw new EsseyError("RELAY_FAILED", "the pool read does not show the last piece spent yet; the next is unsent", {
|
|
1364
|
+
error: "NOT_INDEXED",
|
|
1365
|
+
});
|
|
1211
1366
|
}
|
|
1212
1367
|
receiveAddress() {
|
|
1213
1368
|
const seed = this.seed();
|
|
@@ -1225,25 +1380,47 @@ export class Essey {
|
|
|
1225
1380
|
}));
|
|
1226
1381
|
}
|
|
1227
1382
|
async exit(coin, to, o = {}) {
|
|
1228
|
-
|
|
1383
|
+
const list = this.config.denominations[coinOf(coin)];
|
|
1384
|
+
if (list === undefined || o.force === true)
|
|
1385
|
+
return this.exitExact(coin, onePayee(to), o);
|
|
1386
|
+
const payees = listOf(to).map(payee);
|
|
1387
|
+
const { fee, relayerFee, owned } = await this.payoutPlan(coin);
|
|
1388
|
+
if (owned.length > 2)
|
|
1389
|
+
throw new EsseyError("INVALID_ARGUMENT", "refused: exit spends at most two notes");
|
|
1390
|
+
const total = owned.reduce((sum, n) => sum + n.value, 0n);
|
|
1391
|
+
const pieces = exitPieces(total, list, fee, relayerFee);
|
|
1392
|
+
const sendTo = piecePayees(pieces.length, payees);
|
|
1393
|
+
refuseUncovered(owned, pieces, fee, relayerFee);
|
|
1394
|
+
const value = pieces.reduce((sum, piece) => sum + piece, 0n);
|
|
1395
|
+
refuseLinkedAmount(value, faucetGrant(coin), owned, fee, relayerFee);
|
|
1396
|
+
const payouts = await this.payPieces(coin, pieces, sendTo, fee);
|
|
1397
|
+
return { payouts, kept: exitKept(total, pieces, fee, relayerFee) };
|
|
1398
|
+
}
|
|
1399
|
+
async exitExact(coin, to, o) {
|
|
1400
|
+
let value = 0n;
|
|
1401
|
+
let kept = 0n;
|
|
1402
|
+
const landed = await this.spend(coin, { to }, (owned, rates, relayerFee) => {
|
|
1229
1403
|
if (owned.length > 2)
|
|
1230
1404
|
throw new EsseyError("INVALID_ARGUMENT", "refused: exit spends at most two notes");
|
|
1231
|
-
const
|
|
1405
|
+
const total = owned.reduce((sum, n) => sum + n.value, 0n);
|
|
1406
|
+
value = exitValue(total, rates, relayerFee);
|
|
1232
1407
|
if (value === 0n)
|
|
1233
1408
|
throw new EsseyError("INVALID_ARGUMENT", "refused: nothing to exit after fees");
|
|
1234
1409
|
if (o.force !== true)
|
|
1235
1410
|
refuseLinkedAmount(value, faucetGrant(coin), owned, rates, relayerFee);
|
|
1411
|
+
kept = total - withdrawCost(value, rates, relayerFee);
|
|
1236
1412
|
return { value, inputs: owned };
|
|
1237
1413
|
});
|
|
1414
|
+
return { payouts: [{ ...landed, amount: value, to: payee(to) }], kept };
|
|
1238
1415
|
}
|
|
1239
1416
|
/** js4 with the withdraw field (`src/v3/inputs.ts:39`); the change note's index is on disk before the body leaves. */
|
|
1240
|
-
async spend(coin, dest, choose, extra = []) {
|
|
1417
|
+
async spend(coin, dest, choose, extra = [], known) {
|
|
1241
1418
|
const recipient = "to" in dest ? payee(dest.to) : undefined;
|
|
1242
1419
|
const relayerFee = decimal(this.config.relayerFee[coinOf(coin)], "config relayerFee");
|
|
1243
1420
|
const relay = this.relayClient();
|
|
1244
1421
|
const relayer = this.relayerAddress();
|
|
1245
1422
|
const prover = this.prover();
|
|
1246
|
-
const fee = await this.feeParams(coin);
|
|
1423
|
+
const fee = known ?? (await this.feeParams(coin));
|
|
1247
1424
|
const { events, owned, seed } = await this.owned(coin);
|
|
1248
1425
|
const { value, inputs } = choose([...owned, ...extra], fee, relayerFee);
|
|
1249
1426
|
const tree = poolTree(orderedLeaves(events));
|
|
@@ -1291,9 +1468,9 @@ export class Essey {
|
|
|
1291
1468
|
}
|
|
1292
1469
|
/** A 202 carries no digest (`src/v3/relay/client.ts:100`): the ticket is followed until it lands. */
|
|
1293
1470
|
async follow(relay, queued, what) {
|
|
1294
|
-
const
|
|
1295
|
-
for (let
|
|
1296
|
-
await sleep(
|
|
1471
|
+
const gap = Math.max(queued.etaMs, FOLLOW_MIN_GAP_MS);
|
|
1472
|
+
for (let waited = 0; waited < FOLLOW_CAP_MS; waited += gap) {
|
|
1473
|
+
await this.sleep(gap);
|
|
1297
1474
|
const s = await relay.status(queued.ticket);
|
|
1298
1475
|
if (s.state === "landed" && s.digest)
|
|
1299
1476
|
return { digest: s.digest, ticket: queued.ticket };
|
|
@@ -1525,7 +1702,7 @@ export class Essey {
|
|
|
1525
1702
|
/** The coins of the market a loan names, as `C/L` in the deployment record. */
|
|
1526
1703
|
loanClose(offerWindowMs) {
|
|
1527
1704
|
const nowMs = (this.io.now ?? Date.now)();
|
|
1528
|
-
return BigInt(
|
|
1705
|
+
return BigInt(stepClose(nowMs + offerWindowMs, nowMs, LOAN_MAX_WINDOW_MS, this.config.closeStepMs));
|
|
1529
1706
|
}
|
|
1530
1707
|
pairOf(market) {
|
|
1531
1708
|
const want = address(market, "market");
|
|
@@ -1890,7 +2067,7 @@ export class Essey {
|
|
|
1890
2067
|
id,
|
|
1891
2068
|
loan,
|
|
1892
2069
|
market,
|
|
1893
|
-
|
|
2070
|
+
...(await this.keeperFeed(market)),
|
|
1894
2071
|
badge: held.id,
|
|
1895
2072
|
passKey: keeper.passKey.id,
|
|
1896
2073
|
registry: keeper.registry.id,
|
|
@@ -1980,7 +2157,7 @@ export class Essey {
|
|
|
1980
2157
|
const { member, credential } = readState(this.statePath);
|
|
1981
2158
|
if (!member || credential?.role !== "keeper")
|
|
1982
2159
|
notSetUp("no keeper credential");
|
|
1983
|
-
const price = await this.
|
|
2160
|
+
const { price, standin } = await this.keeperFeed(market);
|
|
1984
2161
|
const prover = this.prover();
|
|
1985
2162
|
return async (viewer) => {
|
|
1986
2163
|
const pass = membership({
|
|
@@ -1992,7 +2169,7 @@ export class Essey {
|
|
|
1992
2169
|
});
|
|
1993
2170
|
const proved = await prover.prove("membership", pass.input);
|
|
1994
2171
|
return {
|
|
1995
|
-
target: `${this.config.packages.loan}::keeper::seal_approve_keeper_proof`,
|
|
2172
|
+
target: `${this.config.packages.loan}::keeper::seal_approve_keeper_proof${standin ? "_standin" : ""}`,
|
|
1996
2173
|
typeArguments: pair.map((c) => this.config.coins[c].type),
|
|
1997
2174
|
args: [
|
|
1998
2175
|
{ pure: "vector<u8>", value: `0x${Buffer.from(id).toString("hex")}` },
|
|
@@ -2010,23 +2187,27 @@ export class Essey {
|
|
|
2010
2187
|
};
|
|
2011
2188
|
}
|
|
2012
2189
|
/**
|
|
2013
|
-
* The
|
|
2014
|
-
* the
|
|
2015
|
-
*
|
|
2190
|
+
* The price object the keeper policy takes: the market's own feed on a Pyth market, or on a stand-in market with
|
|
2191
|
+
* the `_standin` entries (`keeper.move` `seal_approve_keeper_standin`). On any other market only the
|
|
2192
|
+
* expired-heartbeat and zero-floor branches can pass, and they read no price (`check_loan`), so the feed of a Pyth
|
|
2193
|
+
* market in the deployment stands in for the argument.
|
|
2016
2194
|
*/
|
|
2017
|
-
async
|
|
2195
|
+
async keeperFeed(market) {
|
|
2018
2196
|
const read = this.io.readMarket ?? notSetUp("missing config: no market reader");
|
|
2197
|
+
const mine = await read(market);
|
|
2198
|
+
if (/::standin_oracle::StandinOracle$/.test(mine.oracle ?? "") && mine.feed_id)
|
|
2199
|
+
return { price: mine.feed_id, standin: true };
|
|
2019
2200
|
const feed = async (id) => {
|
|
2020
|
-
const m = await read(id);
|
|
2201
|
+
const m = id === market ? mine : await read(id);
|
|
2021
2202
|
return /::pyth_oracle::PythOracle(Inverse)?$/.test(m.oracle ?? "") ? m.feed_id : undefined;
|
|
2022
2203
|
};
|
|
2023
2204
|
const own = await feed(market);
|
|
2024
2205
|
if (own)
|
|
2025
|
-
return own;
|
|
2206
|
+
return { price: own, standin: false };
|
|
2026
2207
|
for (const id of Object.values(this.config.objects.markets)) {
|
|
2027
2208
|
const other = id && (await feed(id));
|
|
2028
2209
|
if (other)
|
|
2029
|
-
return other;
|
|
2210
|
+
return { price: other, standin: false };
|
|
2030
2211
|
}
|
|
2031
2212
|
return notSetUp("no Pyth market in the deployment");
|
|
2032
2213
|
}
|
|
@@ -2372,10 +2553,14 @@ export class Essey {
|
|
|
2372
2553
|
throw new EsseyError("INVALID_ARGUMENT", "refused: the seizure left this side nothing");
|
|
2373
2554
|
const note = { value, sk: opened.sk, blinding: outputBlind(opened.blinding, out, seizeNonce) };
|
|
2374
2555
|
const commitment = noteCommitment(note.value, notePk(note.sk), note.blinding);
|
|
2375
|
-
|
|
2556
|
+
let events = await pools(C);
|
|
2376
2557
|
const leaf = events.commitments.find((c) => BigInt(c.commitment) === commitment);
|
|
2377
2558
|
if (!leaf)
|
|
2378
2559
|
unknownObject("no seized note of this side is in the pool: another offer won, or the loan was topped up");
|
|
2560
|
+
for (let i = 0; i < SEIZE_REWAITS && this.pacing && lastLanded(events, leaf.index); i++) {
|
|
2561
|
+
await this.pause(out === 0 ? "collect_seized" : "collect_surplus", loan, null);
|
|
2562
|
+
events = await pools(C);
|
|
2563
|
+
}
|
|
2379
2564
|
return this.claimLeaf(C, events, { ...note, index: Number(leaf.index) });
|
|
2380
2565
|
}
|
|
2381
2566
|
/** The seed index whose key is `pk`: the borrower key of a loan this seed opened. */
|
|
@@ -2893,9 +3078,9 @@ export class Essey {
|
|
|
2893
3078
|
const [C, L] = this.pairOf(json.market_id);
|
|
2894
3079
|
const poolC = address(json.pool_c, "pool_c");
|
|
2895
3080
|
const market = address(json.market_id, "market");
|
|
3081
|
+
const expiryMs = await this.heartbeatExpiry(json, market, p.expiryMs);
|
|
2896
3082
|
const { opening } = this.ownOpening(this.seed(), json, p.borrowerPk, p.collAmt);
|
|
2897
3083
|
const k = p.k ?? 1;
|
|
2898
|
-
const expiryMs = p.expiryMs ?? BigInt((this.io.now ?? Date.now)() + 3_600_000);
|
|
2899
3084
|
const reading = await readPrice(market);
|
|
2900
3085
|
const floor = heartbeatFloor(reading, k);
|
|
2901
3086
|
const fee = await this.feeParams(C);
|
|
@@ -2937,6 +3122,31 @@ export class Essey {
|
|
|
2937
3122
|
priceUpdate: await update(market),
|
|
2938
3123
|
}));
|
|
2939
3124
|
}
|
|
3125
|
+
/**
|
|
3126
|
+
* `liquidation::heartbeat`'s clock rules, checked before proving: one heartbeat per gap (43_015), an expiry at
|
|
3127
|
+
* least one gap and at most `H_MAX_MS` ahead (43_005) and not before the stored one (43_019). The gap is
|
|
3128
|
+
* `liquidation::hb_gap_ms`: 2 minutes on a stand-in market, 50 on any other. No expiry given takes an hour, or the
|
|
3129
|
+
* stored expiry when that is later.
|
|
3130
|
+
*/
|
|
3131
|
+
async heartbeatExpiry(json, market, asked) {
|
|
3132
|
+
const read = this.io.readMarket ?? notSetUp("missing config: no market reader");
|
|
3133
|
+
const standin = /::standin_oracle::StandinOracle$/.test((await read(market)).oracle ?? "");
|
|
3134
|
+
const gap = standin ? HB_GAP_STANDIN_MS : HB_GAP_MS;
|
|
3135
|
+
const now = BigInt((this.io.now ?? Date.now)());
|
|
3136
|
+
const last = BigInt(json.coll?.hb_at_ms ?? 0);
|
|
3137
|
+
const stored = BigInt(json.coll?.hb_expiry_ms ?? 0);
|
|
3138
|
+
if (last > 0n && now < last + gap) {
|
|
3139
|
+
const wait = (last + gap - now + 999n) / 1000n;
|
|
3140
|
+
throw new EsseyError("INVALID_ARGUMENT", `the last heartbeat is under one gap old; retry in ${wait} s`);
|
|
3141
|
+
}
|
|
3142
|
+
const hour = now + 3600000n;
|
|
3143
|
+
const expiry = asked ?? (stored > hour ? stored : hour);
|
|
3144
|
+
if (expiry < now + gap || expiry > now + H_MAX_MS)
|
|
3145
|
+
throw new EsseyError("INVALID_ARGUMENT", "the expiry must be at least one gap and at most 24 h ahead");
|
|
3146
|
+
if (expiry < stored)
|
|
3147
|
+
throw new EsseyError("INVALID_ARGUMENT", "the expiry cannot be earlier than the loan's current heartbeat expiry");
|
|
3148
|
+
return expiry;
|
|
3149
|
+
}
|
|
2940
3150
|
/**
|
|
2941
3151
|
* TOPUP (`liquidation::topup`, no pass): a CollLock spend of own collateral-coin notes into `add_note` under the
|
|
2942
3152
|
* loan's lock key, then Health TOPUP re-locks the stored lock and `add_note` into one note. The new keeper
|
|
@@ -3115,7 +3325,7 @@ export class Essey {
|
|
|
3115
3325
|
const { approve, signer } = await this.keeperApproval(json, loan, keeperKeyId(loanKey, version), via);
|
|
3116
3326
|
const sealed = { loan, loanKey, keeperCt: await readCt(loan), keeperCtVersion: version };
|
|
3117
3327
|
const payload = await fetchKeeperPlaintext(sealed, {
|
|
3118
|
-
decrypt: (r) => decrypt({ sealed: r.sealed, approve, signer }),
|
|
3328
|
+
decrypt: (r) => decrypt({ sealed: r.sealed, approve, signer }).catch(keeperRefusal),
|
|
3119
3329
|
});
|
|
3120
3330
|
if (!payload.terms)
|
|
3121
3331
|
throw new EsseyError("CT_MISMATCH", "refused: the keeper plaintext carries no terms");
|
|
@@ -3279,7 +3489,7 @@ export class Essey {
|
|
|
3279
3489
|
const asked = assertSaleOpen(p);
|
|
3280
3490
|
const open = {
|
|
3281
3491
|
...asked,
|
|
3282
|
-
closeMs:
|
|
3492
|
+
closeMs: stepClose(asked.closeMs, (this.io.now ?? Date.now)(), SALE_MAX_WINDOW_MS, this.config.closeStepMs),
|
|
3283
3493
|
};
|
|
3284
3494
|
requireSale(this.config);
|
|
3285
3495
|
const pool = poolId(this.config, open.coin);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
export { COINS, parseConfig, type CoinKey, type V3Config } from "./config.ts";
|
|
2
2
|
export { ESSEY_ERROR_CODES, EsseyError, isEsseyError, type EsseyErrorCode } from "./errors.ts";
|
|
3
|
-
export { Essey, type ConnectOptions, type CredentialInfo, type EsseyIo, type OpenQuote, type Recovered, type Renewal, type SaleOpenTx, type SaleRecord, type SpendOptions, } from "./essey.ts";
|
|
3
|
+
export { Essey, type ConnectOptions, type CredentialInfo, type EsseyIo, type ExitResult, type OpenQuote, type Payees, type Payout, type Recovered, type Renewal, type SaleOpenTx, type SaleRecord, type SpendOptions, } from "./essey.ts";
|
|
4
4
|
export type { KeeperActions, KeeperBadge, KeeperIo, KeeperQueue, KeeperSection, KeeperVerdict, KeeperVia, Unjudged, } from "./keeper.ts";
|
|
5
5
|
export type { Landed, Queued, RelayerInfo, TicketState } from "./v3/relay/client.ts";
|
|
6
6
|
export type { NoteSummary, PoolCommitment, PoolEvents } from "./v3/notes/scan.ts";
|
|
7
|
+
export type { PacedAction, Pause } from "./v3/pace.ts";
|
|
7
8
|
export type { LoanOpening, LoanOpenParams, MyLoanView, MySaleView, OpenRequest, OpenSale, PoolPosition, PoolView, PublicLoanStatus, PublicLoanView, SaleOpenParams, } from "./v3/stage1.ts";
|
|
8
9
|
export * as v3 from "./v3/index.ts";
|
|
9
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,YAAY,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC/F,OAAO,EACL,KAAK,EACL,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,OAAO,EACZ,KAAK,SAAS,EACd,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,aAAa,EACb,WAAW,EACX,QAAQ,EACR,WAAW,EACX,aAAa,EACb,aAAa,EACb,SAAS,EACT,QAAQ,GACT,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACrF,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClF,YAAY,EACV,WAAW,EACX,cAAc,EACd,UAAU,EACV,UAAU,EACV,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,gBAAgB,EAChB,cAAc,EACd,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,KAAK,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,YAAY,EAAE,KAAK,cAAc,EAAE,MAAM,aAAa,CAAC;AAC/F,OAAO,EACL,KAAK,EACL,KAAK,cAAc,EACnB,KAAK,cAAc,EACnB,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,SAAS,EACd,KAAK,MAAM,EACX,KAAK,MAAM,EACX,KAAK,SAAS,EACd,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,YAAY,GAClB,MAAM,YAAY,CAAC;AACpB,YAAY,EACV,aAAa,EACb,WAAW,EACX,QAAQ,EACR,WAAW,EACX,aAAa,EACb,aAAa,EACb,SAAS,EACT,QAAQ,GACT,MAAM,aAAa,CAAC;AACrB,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACrF,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClF,YAAY,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACvD,YAAY,EACV,WAAW,EACX,cAAc,EACd,UAAU,EACV,UAAU,EACV,WAAW,EACX,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,gBAAgB,EAChB,cAAc,EACd,cAAc,GACf,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -8,7 +8,8 @@ export type LineRisk = {
|
|
|
8
8
|
/**
|
|
9
9
|
* `underwater` is the LIQUIDATE statement: over the line at both spot and EMA (`health.circom:282-283`).
|
|
10
10
|
* `liquidationPrice` is the lowest price not over the line (`value.circom:33-34,124,244-253`), null when none is.
|
|
11
|
-
* `
|
|
11
|
+
* `coveredAtDefaultK` is whether the k 1 floor of `liquidation.move:451-455` is healthy (`health.circom:281`). A
|
|
12
|
+
* deeper healthy k is not computed: a finer bucket publishes more of the loan's LTV band (LOAN-06).
|
|
12
13
|
* `ltvBps` is floor(owed · 10000 / value) at min(spot, EMA), the cap `open_price_ok` puts on an OPEN's price
|
|
13
14
|
* (`move/v3/market/sources/market.move:241-245`), null at value 0. It is on owed and no on-chain line computes
|
|
14
15
|
* it: `health.circom:271-281` compares principal, at OPEN only.
|
|
@@ -17,7 +18,7 @@ export type LocalHealth = {
|
|
|
17
18
|
ltvBps: bigint | null;
|
|
18
19
|
underwater: boolean;
|
|
19
20
|
liquidationPrice: bigint | null;
|
|
20
|
-
|
|
21
|
+
coveredAtDefaultK: boolean;
|
|
21
22
|
};
|
|
22
23
|
export declare function localHealth(collAmt: bigint, owed: bigint, risk: LineRisk, r: Reading): LocalHealth;
|
|
23
24
|
//# sourceMappingURL=local-health.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"local-health.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/local-health.ts"],"names":[],"mappings":"AAEA,OAAO,
|
|
1
|
+
{"version":3,"file":"local-health.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/local-health.ts"],"names":[],"mappings":"AAEA,OAAO,EAAqC,KAAK,OAAO,EAAE,MAAM,WAAW,CAAC;AAE5E,oEAAoE;AACpE,MAAM,MAAM,QAAQ,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtE;;;;;;;;GAQG;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,iBAAiB,EAAE,OAAO,CAAC;CAC5B,CAAC;AAWF,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,CAAC,EAAE,OAAO,GAAG,WAAW,CAWlG"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BPS_DENOM } from "../fees.js";
|
|
2
|
-
import {
|
|
2
|
+
import { heartbeatFloor, overLine, valueAt } from "./math.js";
|
|
3
3
|
const ceilDiv = (a, b) => (a + b - 1n) / b;
|
|
4
4
|
function healthyFrom(collAmt, owed, risk, expo) {
|
|
5
5
|
if (owed === 0n)
|
|
@@ -14,14 +14,10 @@ export function localHealth(collAmt, owed, risk, r) {
|
|
|
14
14
|
const over = (price) => overLine(valueAt(collAmt, price, scale).q, owed, risk.liqBps);
|
|
15
15
|
const low = r.spot < r.ema ? r.spot : r.ema;
|
|
16
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
17
|
return {
|
|
22
18
|
ltvBps: value === 0n ? null : (owed * BPS_DENOM) / value,
|
|
23
19
|
underwater: over(r.spot) && over(r.ema),
|
|
24
20
|
liquidationPrice: healthyFrom(collAmt, owed, risk, r.expo),
|
|
25
|
-
|
|
21
|
+
coveredAtDefaultK: !over(heartbeatFloor(r, 1)),
|
|
26
22
|
};
|
|
27
23
|
}
|
package/dist/v3/index.d.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./kinds.ts";
|
|
|
16
16
|
export * from "./market.ts";
|
|
17
17
|
export * from "./notes/keys.ts";
|
|
18
18
|
export * as notes from "./notes/index.ts";
|
|
19
|
+
export * from "./pace.ts";
|
|
19
20
|
export * from "./proof-bytes.ts";
|
|
20
21
|
export * from "./prover.ts";
|
|
21
22
|
export { credentialSigned, epochEnd, expiryAllowed, parseCredential, type Role, type Slot, } from "./credential/issue.ts";
|
package/dist/v3/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/v3/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,OAAO,EACL,gBAAgB,EAChB,QAAQ,EACR,aAAa,EACb,eAAe,EACf,KAAK,IAAI,EACT,KAAK,IAAI,GACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,WAAW,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9G,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,KAAK,UAAU,GAChB,MAAM,uBAAuB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/v3/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,OAAO,KAAK,UAAU,MAAM,uBAAuB,CAAC;AACpD,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAC3C,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,aAAa,CAAC;AAC5B,cAAc,WAAW,CAAC;AAC1B,cAAc,qBAAqB,CAAC;AACpC,cAAc,YAAY,CAAC;AAC3B,cAAc,aAAa,CAAC;AAC5B,cAAc,iBAAiB,CAAC;AAChC,OAAO,KAAK,KAAK,MAAM,kBAAkB,CAAC;AAC1C,cAAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AACjC,cAAc,aAAa,CAAC;AAC5B,OAAO,EACL,gBAAgB,EAChB,QAAQ,EACR,aAAa,EACb,eAAe,EACf,KAAK,IAAI,EACT,KAAK,IAAI,GACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,WAAW,EAAE,KAAK,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC9G,OAAO,EACL,iBAAiB,EACjB,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,KAAK,UAAU,GAChB,MAAM,uBAAuB,CAAC"}
|
package/dist/v3/index.js
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./kinds.js";
|
|
|
16
16
|
export * from "./market.js";
|
|
17
17
|
export * from "./notes/keys.js";
|
|
18
18
|
export * as notes from "./notes/index.js";
|
|
19
|
+
export * from "./pace.js";
|
|
19
20
|
export * from "./proof-bytes.js";
|
|
20
21
|
export * from "./prover.js";
|
|
21
22
|
export { credentialSigned, epochEnd, expiryAllowed, parseCredential, } from "./credential/issue.js";
|