@omnipair/dusk-sdk 2.27.0 → 3.0.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.
Files changed (88) hide show
  1. package/README.md +92 -104
  2. package/dist/constants.d.ts +0 -6
  3. package/dist/constants.d.ts.map +1 -1
  4. package/dist/constants.js +0 -12
  5. package/dist/constants.js.map +1 -1
  6. package/dist/get.d.ts +97 -345
  7. package/dist/get.d.ts.map +1 -1
  8. package/dist/get.js +1 -2
  9. package/dist/get.js.map +1 -1
  10. package/dist/hlp-liquidation-client.d.ts.map +1 -1
  11. package/dist/hlp-liquidation-client.js +0 -1
  12. package/dist/hlp-liquidation-client.js.map +1 -1
  13. package/dist/idl_delegate.json +82 -390
  14. package/dist/idl_v2.json +226 -1098
  15. package/dist/index.d.ts +0 -1
  16. package/dist/index.d.ts.map +1 -1
  17. package/dist/index.js +0 -1
  18. package/dist/index.js.map +1 -1
  19. package/dist/indexer.d.ts +1 -1
  20. package/dist/indexer.d.ts.map +1 -1
  21. package/dist/interest-accounting.d.ts +82 -47
  22. package/dist/interest-accounting.d.ts.map +1 -1
  23. package/dist/interest-accounting.js +103 -53
  24. package/dist/interest-accounting.js.map +1 -1
  25. package/dist/leverage-preview.d.ts +24 -86
  26. package/dist/leverage-preview.d.ts.map +1 -1
  27. package/dist/liquidation-client.js +2 -2
  28. package/dist/liquidation-client.js.map +1 -1
  29. package/dist/operator-client.d.ts.map +1 -1
  30. package/dist/operator-client.js +8 -9
  31. package/dist/operator-client.js.map +1 -1
  32. package/dist/referral.d.ts +6 -3
  33. package/dist/referral.d.ts.map +1 -1
  34. package/dist/referral.js +8 -7
  35. package/dist/referral.js.map +1 -1
  36. package/dist/tranche-client.d.ts +0 -56
  37. package/dist/tranche-client.d.ts.map +1 -1
  38. package/dist/tranche-client.js +2 -103
  39. package/dist/tranche-client.js.map +1 -1
  40. package/dist/transfer-fees.d.ts +6 -0
  41. package/dist/transfer-fees.d.ts.map +1 -1
  42. package/dist/transfer-fees.js +19 -0
  43. package/dist/transfer-fees.js.map +1 -1
  44. package/dist/type-aliases.d.ts +3 -15
  45. package/dist/type-aliases.d.ts.map +1 -1
  46. package/dist/types_delegate.d.ts +82 -390
  47. package/dist/types_delegate.d.ts.map +1 -1
  48. package/dist/types_v2.d.ts +227 -1099
  49. package/dist/types_v2.d.ts.map +1 -1
  50. package/dist/virtual-book/market.d.ts +24 -86
  51. package/dist/virtual-book/market.d.ts.map +1 -1
  52. package/dist/virtual-book/quotes.d.ts +61 -173
  53. package/dist/virtual-book/quotes.d.ts.map +1 -1
  54. package/dist/virtual-book/quotes.js +55 -6
  55. package/dist/virtual-book/quotes.js.map +1 -1
  56. package/dist/write.d.ts +1 -3
  57. package/dist/write.d.ts.map +1 -1
  58. package/dist/write.js +7 -24
  59. package/dist/write.js.map +1 -1
  60. package/dist/yield-client.d.ts +7 -20
  61. package/dist/yield-client.d.ts.map +1 -1
  62. package/dist/yield-client.js +6 -37
  63. package/dist/yield-client.js.map +1 -1
  64. package/package.json +2 -2
  65. package/src/constants.ts +0 -25
  66. package/src/get.ts +0 -3
  67. package/src/hlp-liquidation-client.ts +0 -1
  68. package/src/idl_delegate.json +82 -390
  69. package/src/idl_v2.json +226 -1098
  70. package/src/index.ts +0 -1
  71. package/src/indexer.ts +1 -1
  72. package/src/interest-accounting.ts +149 -71
  73. package/src/liquidation-client.ts +2 -2
  74. package/src/operator-client.ts +8 -9
  75. package/src/referral.ts +9 -14
  76. package/src/tranche-client.ts +3 -106
  77. package/src/transfer-fees.ts +18 -0
  78. package/src/type-aliases.ts +3 -15
  79. package/src/types_delegate.ts +82 -390
  80. package/src/types_v2.ts +226 -1098
  81. package/src/virtual-book/quotes.ts +70 -6
  82. package/src/write.ts +8 -36
  83. package/src/yield-client.ts +9 -48
  84. package/dist/income-receivable.d.ts +0 -77
  85. package/dist/income-receivable.d.ts.map +0 -1
  86. package/dist/income-receivable.js +0 -135
  87. package/dist/income-receivable.js.map +0 -1
  88. package/src/income-receivable.ts +0 -145
@@ -1,145 +0,0 @@
1
- import type { IntegerLike } from "./parameters.js";
2
- import type { Dusk } from "./dusk.js";
3
- import { address, type AddressLike } from "./address.js";
4
- import { deriveIncomeEpochAddress } from "./constants.js";
5
-
6
- const U64 = (1n << 64n) - 1n;
7
- const U128 = (1n << 128n) - 1n;
8
- const U256 = (1n << 256n) - 1n;
9
- const value = (input: IntegerLike, max = U128): bigint => {
10
- if (typeof input === "number" && !Number.isSafeInteger(input)) throw new Error("Income value lost integer precision");
11
- const text = String(input);
12
- if (!/^(0|[1-9]\d*)$/.test(text)) throw new Error("Invalid income value");
13
- const n = BigInt(text);
14
- if (n > max) throw new Error("Income value overflow");
15
- return n;
16
- };
17
- export interface IncomeGrowthInput { unitsQ64: IntegerLike; weightedPaidQ128: readonly IntegerLike[] }
18
- export interface IncomeClaimInput {
19
- epoch: IntegerLike; unitsQ64: IntegerLike; paidCheckpointQ64: IntegerLike;
20
- growthCheckpoint: IncomeGrowthInput; paidFractionQ128: IntegerLike; claimable: IntegerLike;
21
- }
22
- export interface IncomeBookInput {
23
- epoch: IntegerLike; unpaid: IntegerLike; units: IntegerLike;
24
- paidPerUnitQ64: IntegerLike; growth: readonly IncomeGrowthInput[];
25
- }
26
- export interface IncomeEndInput { ended: boolean; epoch: IntegerLike; growth: IncomeGrowthInput }
27
- export interface IncomeHolderInput {
28
- tokenKind: number; incomeInitialized: boolean; escrowBalance: IntegerLike;
29
- income: readonly IncomeClaimInput[];
30
- }
31
- export interface IncomeClaimProjection {
32
- epoch: bigint; unitsQ64: bigint; paidCheckpointQ64: bigint;
33
- growthCheckpoint: IncomeGrowthInput; paidFractionQ128: bigint; claimable: bigint;
34
- }
35
- export type IncomeArchiveMap = ReadonlyMap<string, IncomeBookInput>;
36
-
37
- function weighted(growth: IncomeGrowthInput): bigint {
38
- if (growth.weightedPaidQ128.length !== 4) throw new Error("Invalid weighted income index");
39
- return growth.weightedPaidQ128.reduce<bigint>((sum, limb, i) => sum + (value(limb, U64) << BigInt(i * 64)), 0n);
40
- }
41
-
42
- /** Pure copy of IncomeReceivableClaim::checkpoint_exposure. Fractional cash
43
- * stays at Q128 precision; newly issued units cannot earn prior payments. */
44
- export function projectIncomeClaim(input: {
45
- claim: IncomeClaimInput; book: IncomeBookInput; balance: IntegerLike; lane: number; end?: IncomeEndInput;
46
- }): IncomeClaimProjection {
47
- const { claim, book, end } = input;
48
- if (end && typeof end.ended !== "boolean") throw new Error("Invalid income exposure end");
49
- const epoch = value(book.epoch, U64);
50
- if (value(claim.epoch, U64) !== epoch) throw new Error("IncomeHistoryRequired");
51
- if (!Number.isInteger(input.lane) || input.lane < 0 || input.lane > 3 || book.growth.length !== 4) throw new Error("Invalid income lane");
52
- const ended = end?.ended === true;
53
- const endEpoch = ended ? value(end!.epoch, U64) : 0n;
54
- const growth = ended && epoch === endEpoch ? end!.growth : book.growth[input.lane]!;
55
- const balance = ended && epoch > endEpoch ? 0n : value(input.balance, U64);
56
- const delta = value(growth.unitsQ64) - value(claim.growthCheckpoint.unitsQ64);
57
- const weightedDelta = weighted(growth) - weighted(claim.growthCheckpoint);
58
- const paid = value(book.paidPerUnitQ64);
59
- const paidDelta = paid - value(claim.paidCheckpointQ64);
60
- if (delta < 0n || weightedDelta < 0n || paidDelta < 0n) throw new Error("Income index regressed");
61
- const newCash = delta * paid - weightedDelta;
62
- if (newCash < 0n) throw new Error("Income weighted index exceeds payment");
63
- const scaled = newCash * balance + value(claim.unitsQ64) * paidDelta + value(claim.paidFractionQ128);
64
- if (scaled > U256) throw new Error("Income value overflow");
65
- const unitsQ64 = value(value(claim.unitsQ64) + balance * delta);
66
- return { epoch, unitsQ64, paidCheckpointQ64: paid, growthCheckpoint: growth,
67
- paidFractionQ128: scaled & U128, claimable: value(value(claim.claimable, U64) + (scaled >> 128n), U64) };
68
- }
69
-
70
- /** Archived denominations required by the selected asset's two income books. */
71
- export function incomeArchiveRequests(holder: IncomeHolderInput, books: readonly IncomeBookInput[], asset: 0 | 1, maximumEpochs = 32) {
72
- if (asset !== 0 && asset !== 1) throw new Error("Invalid income asset");
73
- if (!Number.isInteger(maximumEpochs) || maximumEpochs < 1 || maximumEpochs > 64) throw new Error("Invalid income history bound");
74
- if (!holder.incomeInitialized || holder.tokenKind === 1) return [];
75
- if (![0, 2].includes(holder.tokenKind) || holder.income.length !== 2 || books.length !== 4) throw new Error("Invalid income holder");
76
- const requests: { bookIndex: number; epoch: bigint }[] = [];
77
- for (let source = 0; source < 2; source++) {
78
- const bookIndex = asset + source * 2;
79
- const first = value(holder.income[source]!.epoch, U64), current = value(books[bookIndex]!.epoch, U64);
80
- if (first > current || current - first > BigInt(maximumEpochs)) throw new Error("IncomeHistoryRequired");
81
- for (let epoch = first; epoch < current; epoch++) requests.push({ bookIndex, epoch });
82
- }
83
- return requests;
84
- }
85
-
86
- /** Additional funded interest Harvest materializes before ordinary growth.
87
- * Historical units remain with their owner after transfers, exits and wipes. */
88
- export function projectHolderIncome(input: {
89
- holder: IncomeHolderInput; books: readonly IncomeBookInput[]; asset: 0 | 1;
90
- walletBalance: IntegerLike; ends?: readonly IncomeEndInput[]; archives?: IncomeArchiveMap;
91
- }) {
92
- const { holder, books, asset } = input;
93
- if (input.ends && input.ends.length !== 4) throw new Error("Invalid income exposure ends");
94
- incomeArchiveRequests(holder, books, asset);
95
- if (holder.tokenKind === 1) return 0n;
96
- if (![0, 2].includes(holder.tokenKind) || holder.income.length !== 2 || books.length !== 4) throw new Error("Invalid income holder");
97
- const lane = holder.tokenKind === 2 ? 1 : 0;
98
- const balance = holder.incomeInitialized ? value(value(input.walletBalance, U64) + value(holder.escrowBalance, U64), U64) : 0n;
99
- let amount = 0n;
100
- for (let source = 0; source < 2; source++) {
101
- const bookIndex = asset + source * 2, book = books[bookIndex]!;
102
- let claim: IncomeClaimInput = { ...holder.income[source]!, epoch: holder.incomeInitialized ? holder.income[source]!.epoch : book.epoch };
103
- const end = lane === 1 ? input.ends?.[bookIndex] : undefined;
104
- while (value(claim.epoch, U64) < value(book.epoch, U64)) {
105
- const archived = input.archives?.get(`${bookIndex}:${claim.epoch}`);
106
- if (!archived) throw new Error("IncomeHistoryRequired");
107
- const projected = projectIncomeClaim({ claim, book: archived, balance, lane, end });
108
- const units = value(archived.units, U64), unpaid = value(archived.unpaid, U64);
109
- if (units === 0n && unpaid !== 0n) throw new Error("Invalid income book");
110
- claim = { ...projected, epoch: projected.epoch + 1n, unitsQ64: units === 0n ? 0n : value(projected.unitsQ64 * unpaid / units),
111
- paidCheckpointQ64: 0n, growthCheckpoint: { unitsQ64: 0n, weightedPaidQ128: [0n, 0n, 0n, 0n] } };
112
- }
113
- const projected = projectIncomeClaim({ claim, book, balance, lane, end });
114
- amount = value(amount + projected.claimable, U64);
115
- }
116
- return amount;
117
- }
118
-
119
- /** Read only immutable, PDA-authenticated archives. Callers pass the bank
120
- * floor that supplied the holder and current books; no observation is cached. */
121
- export async function readIncomeArchives(input: {
122
- dusk: Dusk; market: AddressLike; holder: IncomeHolderInput; books: readonly IncomeBookInput[];
123
- asset: 0 | 1; minContextSlot: number;
124
- }): Promise<IncomeArchiveMap> {
125
- if (!Number.isSafeInteger(input.minContextSlot) || input.minContextSlot < 0) throw new Error("Invalid income archive bank floor");
126
- const { dusk } = input, program = dusk.program.programId, market = address(input.market);
127
- const requests = incomeArchiveRequests(input.holder, input.books, input.asset);
128
- const result = new Map<string, IncomeBookInput>();
129
- for (let offset = 0; offset < requests.length; offset += 100) {
130
- const batch = requests.slice(offset, offset + 100).map(request => ({ ...request,
131
- derived: deriveIncomeEpochAddress(market, request.bookIndex, request.epoch, program) }));
132
- const read = await dusk.program.provider.connection.getMultipleAccountsInfoAndContext(batch.map(row => row.derived[0]),
133
- { commitment: "confirmed", minContextSlot: input.minContextSlot });
134
- if (read.context.slot < input.minContextSlot || read.value.length !== batch.length) throw new Error("Income archives are stale");
135
- batch.forEach((request, index) => {
136
- const account = read.value[index];
137
- if (!account || account.executable || !account.owner.equals(program)) throw new Error("IncomeHistoryRequired");
138
- const decoded = dusk.program.coder.accounts.decode("incomeEpoch", account.data);
139
- if (!decoded.market.equals(market) || decoded.bookIndex !== request.bookIndex || decoded.bump !== request.derived[1] ||
140
- String(decoded.book.epoch) !== request.epoch.toString()) throw new Error("Invalid income archive");
141
- result.set(`${request.bookIndex}:${request.epoch}`, decoded.book);
142
- });
143
- }
144
- return result;
145
- }