ox 1.0.3 → 1.0.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # ox
2
2
 
3
+ ## 1.0.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#312](https://github.com/wevm/ox/pull/312) [`d7721a1`](https://github.com/wevm/ox/commit/d7721a16276e584080a3a0bde23ce2b28aff5a42) Thanks [@jxom](https://github.com/jxom)! - Added `EarnShares` utilities for Tempo vault share conversions, fee share calculation, and slippage bounds.
8
+
3
9
  ## 1.0.3
4
10
 
5
11
  ### Patch Changes
@@ -0,0 +1,179 @@
1
+ import * as Errors from '../core/Errors.js';
2
+ /** Basis-point denominator used by slippage bounds. */
3
+ export declare const basisPointScale = 10000;
4
+ /**
5
+ * Tempo Earn `VaultAdapter` conversion anchor.
6
+ *
7
+ * The adapter prices vault shares against venue shares through this pair:
8
+ * `engineShares` venue shares are worth `shareSupply` vault shares. It is initialised
9
+ * 1:1 and restated on `contribute` and `migrateEngine`.
10
+ *
11
+ * These conversions are raw and fee-blind; they ignore pending fee dilution
12
+ * and are unsuitable for user-facing value (use the adapter's `previewRedeem`).
13
+ */
14
+ export type Anchor = {
15
+ /** Venue shares held by the engine at the anchor point. */
16
+ engineShares: bigint;
17
+ /** Vault share supply at the anchor point. */
18
+ shareSupply: bigint;
19
+ };
20
+ /**
21
+ * Converts venue shares to a vault share amount at the anchor rate, rounding down.
22
+ *
23
+ * Mirrors `VaultAdapter.sharesToTokens`.
24
+ *
25
+ * @example
26
+ * ```ts twoslash
27
+ * import { EarnShares } from 'ox/tempo'
28
+ *
29
+ * const shareAmount = EarnShares.toAmount(
30
+ * { engineShares: 3n, shareSupply: 2n },
31
+ * 7n
32
+ * )
33
+ * // @log: 4n
34
+ * ```
35
+ *
36
+ * @param anchor - The conversion anchor.
37
+ * @param venueShareAmount - Venue share amount, base units.
38
+ * @returns Vault share amount, rounded down.
39
+ */
40
+ export declare function toAmount(anchor: Anchor, venueShareAmount: bigint): bigint;
41
+ export declare namespace toAmount {
42
+ type ErrorType = Errors.GlobalErrorType;
43
+ }
44
+ /**
45
+ * Converts venue shares to a vault share amount at the anchor rate, rounding up.
46
+ *
47
+ * Mirrors the adapter's ceiling conversion used by exact-asset exits.
48
+ *
49
+ * @example
50
+ * ```ts twoslash
51
+ * import { EarnShares } from 'ox/tempo'
52
+ *
53
+ * const shareAmount = EarnShares.toAmountUp(
54
+ * { engineShares: 3n, shareSupply: 2n },
55
+ * 7n
56
+ * )
57
+ * // @log: 5n
58
+ * ```
59
+ *
60
+ * @param anchor - The conversion anchor.
61
+ * @param venueShareAmount - Venue share amount, base units.
62
+ * @returns Vault share amount, rounded up.
63
+ */
64
+ export declare function toAmountUp(anchor: Anchor, venueShareAmount: bigint): bigint;
65
+ export declare namespace toAmountUp {
66
+ type ErrorType = Errors.GlobalErrorType;
67
+ }
68
+ /**
69
+ * Converts a vault share amount to venue shares at the anchor rate, rounding down.
70
+ *
71
+ * Mirrors `VaultAdapter.tokensToShares`.
72
+ *
73
+ * @example
74
+ * ```ts twoslash
75
+ * import { EarnShares } from 'ox/tempo'
76
+ *
77
+ * const venueShareAmount = EarnShares.toVenueAmount(
78
+ * { engineShares: 3n, shareSupply: 2n },
79
+ * 7n
80
+ * )
81
+ * // @log: 10n
82
+ * ```
83
+ *
84
+ * @param anchor - The conversion anchor.
85
+ * @param shareAmount - Vault share amount, base units.
86
+ * @returns Venue share amount, rounded down.
87
+ */
88
+ export declare function toVenueAmount(anchor: Anchor, shareAmount: bigint): bigint;
89
+ export declare namespace toVenueAmount {
90
+ type ErrorType = Errors.GlobalErrorType;
91
+ }
92
+ /**
93
+ * Computes the dilution-correct vault shares minted for an asset-denominated fee.
94
+ *
95
+ * Mirrors `FeeMath`:
96
+ * `feeShares = floor(fee * shareSupply / (activeAssets - fee))`, zero when the
97
+ * fee is zero or not smaller than the active assets. Minting this amount to the
98
+ * fee ledger prices the fee at post-mint value per share.
99
+ *
100
+ * @example
101
+ * ```ts twoslash
102
+ * import { EarnShares } from 'ox/tempo'
103
+ *
104
+ * const shares = EarnShares.feeShares({
105
+ * activeAssets: 1_100n,
106
+ * shareSupply: 1_000n,
107
+ * totalFeeAssets: 100n
108
+ * })
109
+ * // @log: 100n
110
+ * ```
111
+ *
112
+ * @param options - Fee accrual inputs.
113
+ * @returns Vault shares to mint for the fee, rounded down.
114
+ */
115
+ export declare function feeShares(options: feeShares.Options): bigint;
116
+ export declare namespace feeShares {
117
+ type Options = {
118
+ /** Assets backing the active (non-queued) supply, base units. */
119
+ activeAssets: bigint;
120
+ /** Active vault share supply, base units. */
121
+ shareSupply: bigint;
122
+ /** Total fee liability in asset units. */
123
+ totalFeeAssets: bigint;
124
+ };
125
+ type ErrorType = Errors.GlobalErrorType;
126
+ }
127
+ /**
128
+ * Lowers an expected output by a basis-point slippage tolerance, flooring to `1n`.
129
+ *
130
+ * Suitable for lower bounds such as a deposit's minimum shares or a redeem's
131
+ * minimum assets; not for upper bounds such as an exact withdrawal's maximum
132
+ * shares.
133
+ *
134
+ * @example
135
+ * ```ts twoslash
136
+ * import { EarnShares } from 'ox/tempo'
137
+ *
138
+ * const minimumShares = EarnShares.minimumOutput(
139
+ * 1_000_000n,
140
+ * 50
141
+ * )
142
+ * // @log: 995_000n
143
+ * ```
144
+ *
145
+ * @param expectedAmount - Expected output in base units.
146
+ * @param slippageBps - Allowed slippage in basis points from `0` through `9_999`.
147
+ * @returns The minimum accepted output, floored to `1n`.
148
+ * @throws `InvalidExpectedOutputError` when `expectedAmount` is not positive.
149
+ * @throws `InvalidSlippageError` when `slippageBps` is outside its valid range.
150
+ */
151
+ export declare function minimumOutput(expectedAmount: bigint, slippageBps: number): bigint;
152
+ export declare namespace minimumOutput {
153
+ type ErrorType = InvalidExpectedOutputError | InvalidSlippageError | Errors.GlobalErrorType;
154
+ }
155
+ /**
156
+ * Error thrown when an expected output is not positive.
157
+ */
158
+ export declare class InvalidExpectedOutputError extends Errors.BaseError {
159
+ readonly name = "EarnShares.InvalidExpectedOutputError";
160
+ constructor(options: InvalidExpectedOutputError.Options);
161
+ }
162
+ export declare namespace InvalidExpectedOutputError {
163
+ type Options = {
164
+ expectedAmount: bigint;
165
+ };
166
+ }
167
+ /**
168
+ * Error thrown when a slippage tolerance is not an integer from `0` through `9_999`.
169
+ */
170
+ export declare class InvalidSlippageError extends Errors.BaseError {
171
+ readonly name = "EarnShares.InvalidSlippageError";
172
+ constructor(options: InvalidSlippageError.Options);
173
+ }
174
+ export declare namespace InvalidSlippageError {
175
+ type Options = {
176
+ slippageBps: number;
177
+ };
178
+ }
179
+ //# sourceMappingURL=EarnShares.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EarnShares.d.ts","sourceRoot":"","sources":["../../src/tempo/EarnShares.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAA;AAE3C,uDAAuD;AACvD,eAAO,MAAM,eAAe,QAAS,CAAA;AAErC;;;;;;;;;GASG;AACH,MAAM,MAAM,MAAM,GAAG;IACnB,2DAA2D;IAC3D,YAAY,EAAE,MAAM,CAAA;IACpB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAA;CACpB,CAAA;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,GAAG,MAAM,CAEzE;AAED,MAAM,CAAC,OAAO,WAAW,QAAQ,CAAC;IAChC,KAAK,SAAS,GAAG,MAAM,CAAC,eAAe,CAAA;CACxC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,gBAAgB,EAAE,MAAM,GAAG,MAAM,CAG3E;AAED,MAAM,CAAC,OAAO,WAAW,UAAU,CAAC;IAClC,KAAK,SAAS,GAAG,MAAM,CAAC,eAAe,CAAA;CACxC;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MAAM,CAAC,OAAO,WAAW,oBAAoB,CAAC;IAC5C,KAAY,OAAO,GAAG;QACpB,WAAW,EAAE,MAAM,CAAA;KACpB,CAAA;CACF"}
@@ -0,0 +1,160 @@
1
+ import * as Errors from '../core/Errors.js';
2
+ /** Basis-point denominator used by slippage bounds. */
3
+ export const basisPointScale = 10_000;
4
+ /**
5
+ * Converts venue shares to a vault share amount at the anchor rate, rounding down.
6
+ *
7
+ * Mirrors `VaultAdapter.sharesToTokens`.
8
+ *
9
+ * @example
10
+ * ```ts twoslash
11
+ * import { EarnShares } from 'ox/tempo'
12
+ *
13
+ * const shareAmount = EarnShares.toAmount(
14
+ * { engineShares: 3n, shareSupply: 2n },
15
+ * 7n
16
+ * )
17
+ * // @log: 4n
18
+ * ```
19
+ *
20
+ * @param anchor - The conversion anchor.
21
+ * @param venueShareAmount - Venue share amount, base units.
22
+ * @returns Vault share amount, rounded down.
23
+ */
24
+ export function toAmount(anchor, venueShareAmount) {
25
+ return (venueShareAmount * anchor.shareSupply) / anchor.engineShares;
26
+ }
27
+ /**
28
+ * Converts venue shares to a vault share amount at the anchor rate, rounding up.
29
+ *
30
+ * Mirrors the adapter's ceiling conversion used by exact-asset exits.
31
+ *
32
+ * @example
33
+ * ```ts twoslash
34
+ * import { EarnShares } from 'ox/tempo'
35
+ *
36
+ * const shareAmount = EarnShares.toAmountUp(
37
+ * { engineShares: 3n, shareSupply: 2n },
38
+ * 7n
39
+ * )
40
+ * // @log: 5n
41
+ * ```
42
+ *
43
+ * @param anchor - The conversion anchor.
44
+ * @param venueShareAmount - Venue share amount, base units.
45
+ * @returns Vault share amount, rounded up.
46
+ */
47
+ export function toAmountUp(anchor, venueShareAmount) {
48
+ const { engineShares, shareSupply } = anchor;
49
+ return (venueShareAmount * shareSupply + engineShares - 1n) / engineShares;
50
+ }
51
+ /**
52
+ * Converts a vault share amount to venue shares at the anchor rate, rounding down.
53
+ *
54
+ * Mirrors `VaultAdapter.tokensToShares`.
55
+ *
56
+ * @example
57
+ * ```ts twoslash
58
+ * import { EarnShares } from 'ox/tempo'
59
+ *
60
+ * const venueShareAmount = EarnShares.toVenueAmount(
61
+ * { engineShares: 3n, shareSupply: 2n },
62
+ * 7n
63
+ * )
64
+ * // @log: 10n
65
+ * ```
66
+ *
67
+ * @param anchor - The conversion anchor.
68
+ * @param shareAmount - Vault share amount, base units.
69
+ * @returns Venue share amount, rounded down.
70
+ */
71
+ export function toVenueAmount(anchor, shareAmount) {
72
+ return (shareAmount * anchor.engineShares) / anchor.shareSupply;
73
+ }
74
+ /**
75
+ * Computes the dilution-correct vault shares minted for an asset-denominated fee.
76
+ *
77
+ * Mirrors `FeeMath`:
78
+ * `feeShares = floor(fee * shareSupply / (activeAssets - fee))`, zero when the
79
+ * fee is zero or not smaller than the active assets. Minting this amount to the
80
+ * fee ledger prices the fee at post-mint value per share.
81
+ *
82
+ * @example
83
+ * ```ts twoslash
84
+ * import { EarnShares } from 'ox/tempo'
85
+ *
86
+ * const shares = EarnShares.feeShares({
87
+ * activeAssets: 1_100n,
88
+ * shareSupply: 1_000n,
89
+ * totalFeeAssets: 100n
90
+ * })
91
+ * // @log: 100n
92
+ * ```
93
+ *
94
+ * @param options - Fee accrual inputs.
95
+ * @returns Vault shares to mint for the fee, rounded down.
96
+ */
97
+ export function feeShares(options) {
98
+ const { activeAssets, shareSupply, totalFeeAssets } = options;
99
+ if (totalFeeAssets === 0n || totalFeeAssets >= activeAssets)
100
+ return 0n;
101
+ return (totalFeeAssets * shareSupply) / (activeAssets - totalFeeAssets);
102
+ }
103
+ /**
104
+ * Lowers an expected output by a basis-point slippage tolerance, flooring to `1n`.
105
+ *
106
+ * Suitable for lower bounds such as a deposit's minimum shares or a redeem's
107
+ * minimum assets; not for upper bounds such as an exact withdrawal's maximum
108
+ * shares.
109
+ *
110
+ * @example
111
+ * ```ts twoslash
112
+ * import { EarnShares } from 'ox/tempo'
113
+ *
114
+ * const minimumShares = EarnShares.minimumOutput(
115
+ * 1_000_000n,
116
+ * 50
117
+ * )
118
+ * // @log: 995_000n
119
+ * ```
120
+ *
121
+ * @param expectedAmount - Expected output in base units.
122
+ * @param slippageBps - Allowed slippage in basis points from `0` through `9_999`.
123
+ * @returns The minimum accepted output, floored to `1n`.
124
+ * @throws `InvalidExpectedOutputError` when `expectedAmount` is not positive.
125
+ * @throws `InvalidSlippageError` when `slippageBps` is outside its valid range.
126
+ */
127
+ export function minimumOutput(expectedAmount, slippageBps) {
128
+ if (expectedAmount <= 0n)
129
+ throw new InvalidExpectedOutputError({ expectedAmount });
130
+ if (!Number.isInteger(slippageBps) ||
131
+ slippageBps < 0 ||
132
+ slippageBps >= basisPointScale)
133
+ throw new InvalidSlippageError({ slippageBps });
134
+ const scale = BigInt(basisPointScale);
135
+ const bounded = (expectedAmount * (scale - BigInt(slippageBps))) / scale;
136
+ return bounded === 0n ? 1n : bounded;
137
+ }
138
+ /**
139
+ * Error thrown when an expected output is not positive.
140
+ */
141
+ export class InvalidExpectedOutputError extends Errors.BaseError {
142
+ name = 'EarnShares.InvalidExpectedOutputError';
143
+ constructor(options) {
144
+ super(`Expected output \`${options.expectedAmount}\` must be greater than zero.`);
145
+ }
146
+ }
147
+ /**
148
+ * Error thrown when a slippage tolerance is not an integer from `0` through `9_999`.
149
+ */
150
+ export class InvalidSlippageError extends Errors.BaseError {
151
+ name = 'EarnShares.InvalidSlippageError';
152
+ constructor(options) {
153
+ super(`Slippage tolerance \`${options.slippageBps}\` is invalid.`, {
154
+ metaMessages: [
155
+ `Slippage must be a whole number from 0 through ${basisPointScale - 1} basis points.`,
156
+ ],
157
+ });
158
+ }
159
+ }
160
+ //# sourceMappingURL=EarnShares.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EarnShares.js","sourceRoot":"","sources":["../../src/tempo/EarnShares.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,mBAAmB,CAAA;AAE3C,uDAAuD;AACvD,MAAM,CAAC,MAAM,eAAe,GAAG,MAAM,CAAA;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AM,IAAI,oBAAoB,CAAC,EAAE,WAAW,EAAE,CAAC,CAAA;IACjD,MAAM,KAAK,GAAG,MAAM,CAAC,eAAe,CAAC,CAAA;IACrC,MAAM,OAAO,GAAG,CAAC,cAAc,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,KAAK,CAAA;IACxE,OAAO,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAA;AACtC,CAAC;AASD;;GAEG;AACH,MAAM,OAAO,0BAA2B,SAAQ,MAAM,CAAC,SAAS;IAC5C,IAAI,GAAG,uCAAuC,CAAA;IAEhE,YAAY,OAA2C;QACrD,KAAK,CACH,qBAAqB,OAAO,CAAC,cAAc,+BAA+B,CAC3E,CAAA;IACH,CAAC;CACF;AAQD;;GAEG;AACH,MAAM,OAAO,oBAAqB,SAAQ,MAAM,CAAC,SAAS;IACtC,IAAI,GAAG,iCAAiC,CAAA;IAE1D,YAAY,OAAqC;QAC/C,KAAK,CAAC,wBAAwB,OAAO,CAAC,WAAW,gBAAgB,EAAE;YACjE,YAAY,EAAE;gBACZ,kDAAkD,eAAe,GAAG,CAAC,gBAAgB;aACtF;SACF,CAAC,CAAA;IACJ,CAAC;CACF"}
@@ -66,6 +66,28 @@ export * as AuthorizationTempo from './AuthorizationTempo.js';
66
66
  * @category Reference
67
67
  */
68
68
  export * as Channel from './Channel.js';
69
+ /**
70
+ * Tempo Earn `VaultAdapter` share math: raw vault-share and venue-share conversions
71
+ * at the anchor rate, the dilution-correct fee-share formula, and the
72
+ * `minimumOutput` slippage floor.
73
+ *
74
+ * Conversions are fee-blind mirrors of the adapter's anchor arithmetic; use the
75
+ * adapter's `previewRedeem` for user-facing value.
76
+ *
77
+ * @example
78
+ * ```ts twoslash
79
+ * import { EarnShares } from 'ox/tempo'
80
+ *
81
+ * const shareAmount = EarnShares.toAmount(
82
+ * { engineShares: 3n, shareSupply: 2n },
83
+ * 7n
84
+ * )
85
+ * // @log: 4n
86
+ * ```
87
+ *
88
+ * @category Reference
89
+ */
90
+ export * as EarnShares from './EarnShares.js';
69
91
  /**
70
92
  * Tempo key authorization utilities for provisioning and signing access keys.
71
93
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/tempo/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAGhC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,OAAO,KAAK,kBAAkB,MAAM,yBAAyB,CAAA;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AACvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,OAAO,KAAK,gBAAgB,MAAM,uBAAuB,CAAA;AACzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAA;AACrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AAErC;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,oBAAoB,MAAM,2BAA2B,CAAA;AACjE;;;;;;;;;;;;;;;;;GAiBG;AACH,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAA;AAErD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,OAAO,KAAK,iBAAiB,MAAM,wBAAwB,CAAA;AAC3D;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,IAAI,MAAM,WAAW,CAAA;AACjC;;;;;;;;;;;;;;;;GAgBG;AACH,OAAO,KAAK,SAAS,MAAM,gBAAgB,CAAA;AAC3C;;;;;;;;;;;;;;;;;;;;;;;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AAA;AACrC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,KAAK,qBAAqB,MAAM,4BAA4B,CAAA"}
@@ -67,6 +67,28 @@ export * as AuthorizationTempo from './AuthorizationTempo.js';
67
67
  * @category Reference
68
68
  */
69
69
  export * as Channel from './Channel.js';
70
+ /**
71
+ * Tempo Earn `VaultAdapter` share math: raw vault-share and venue-share conversions
72
+ * at the anchor rate, the dilution-correct fee-share formula, and the
73
+ * `minimumOutput` slippage floor.
74
+ *
75
+ * Conversions are fee-blind mirrors of the adapter's anchor arithmetic; use the
76
+ * adapter's `previewRedeem` for user-facing value.
77
+ *
78
+ * @example
79
+ * ```ts twoslash
80
+ * import { EarnShares } from 'ox/tempo'
81
+ *
82
+ * const shareAmount = EarnShares.toAmount(
83
+ * { engineShares: 3n, shareSupply: 2n },
84
+ * 7n
85
+ * )
86
+ * // @log: 4n
87
+ * ```
88
+ *
89
+ * @category Reference
90
+ */
91
+ export * as EarnShares from './EarnShares.js';
70
92
  /**
71
93
  * Tempo key authorization utilities for provisioning and signing access keys.
72
94
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/tempo/index.ts"],"names":[],"mappings":"AAAA,gCAAgC;AAChC,2DAA2D;AAE3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,OAAO,KAAK,kBAAkB,MAAM,yBAAyB,CAAA;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,KAAK,OAAO,MAAM,cAAc,CAAA;AACvC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiDG;AACH,OAAO,KAAK,gBAAgB,MAAM,uBAAuB,CAAA;AACzD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAA;AACrD;;;;;;;;;;;;;;;;;;;;;;;;;;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AgDG;AACH,OAAO,KAAK,WAAW,MAAM,kBAAkB,CAAA;AAC/C;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,kBAAkB,MAAM,yBAAyB,CAAA;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,KAAK,kBAAkB,MAAM,yBAAyB,CAAA;AAC7D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,OAAO,KAAK,eAAe,MAAM,sBAAsB,CAAA;AACvD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,KAAK,cAAc,MAAM,qBAAqB,CAAA;AACrD;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;AACH,OAAO,KAAK,aAAa,MAAM,oBAAoB,CAAA;AAEnD;;;;;;;;;;;;;;;;;;GAkBG;AACH,OAAO,KAAK,MAAM,MAAM,aAAa,CAAA;AACrC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,KAAK,qBAAqB,MAAM,4BAA4B,CAAA"}
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "ox",
3
3
  "description": "Ethereum Standard Library",
4
4
  "type": "module",
5
- "version": "1.0.3",
5
+ "version": "1.0.4",
6
6
  "main": "./dist/index.js",
7
7
  "sideEffects": false,
8
8
  "license": "MIT",
@@ -520,6 +520,11 @@
520
520
  "types": "./dist/tempo/Channel.d.ts",
521
521
  "default": "./dist/tempo/Channel.js"
522
522
  },
523
+ "./tempo/EarnShares": {
524
+ "src": "./src/tempo/EarnShares.ts",
525
+ "types": "./dist/tempo/EarnShares.d.ts",
526
+ "default": "./dist/tempo/EarnShares.js"
527
+ },
523
528
  "./tempo/KeyAuthorization": {
524
529
  "src": "./src/tempo/KeyAuthorization.ts",
525
530
  "types": "./dist/tempo/KeyAuthorization.d.ts",
@@ -0,0 +1,120 @@
1
+ import { EarnShares } from 'ox/tempo'
2
+ import { describe, expect, test } from 'vite-plus/test'
3
+
4
+ const anchor = { engineShares: 3n, shareSupply: 2n } as const
5
+
6
+ describe('toAmount', () => {
7
+ test('default', () => {
8
+ expect(EarnShares.toAmount(anchor, 7n)).toBe(4n)
9
+ })
10
+
11
+ test('behavior: rounds down', () => {
12
+ expect(EarnShares.toAmount({ engineShares: 3n, shareSupply: 1n }, 2n)).toBe(
13
+ 0n,
14
+ )
15
+ })
16
+ })
17
+
18
+ describe('toAmountUp', () => {
19
+ test('default', () => {
20
+ expect(EarnShares.toAmountUp(anchor, 7n)).toBe(5n)
21
+ })
22
+
23
+ test('behavior: exact conversions do not round up', () => {
24
+ expect(EarnShares.toAmountUp(anchor, 3n)).toBe(2n)
25
+ })
26
+ })
27
+
28
+ describe('toVenueAmount', () => {
29
+ test('default', () => {
30
+ expect(EarnShares.toVenueAmount(anchor, 7n)).toBe(10n)
31
+ })
32
+
33
+ test('behavior: identity at the initial 1:1 anchor', () => {
34
+ expect(
35
+ EarnShares.toVenueAmount({ engineShares: 1n, shareSupply: 1n }, 12_345n),
36
+ ).toBe(12_345n)
37
+ })
38
+ })
39
+
40
+ describe('feeShares', () => {
41
+ test('default', () => {
42
+ expect(
43
+ EarnShares.feeShares({
44
+ activeAssets: 1_100n,
45
+ shareSupply: 1_000n,
46
+ totalFeeAssets: 100n,
47
+ }),
48
+ ).toBe(100n)
49
+ })
50
+
51
+ test('behavior: zero fee mints nothing', () => {
52
+ expect(
53
+ EarnShares.feeShares({
54
+ activeAssets: 1_100n,
55
+ shareSupply: 1_000n,
56
+ totalFeeAssets: 0n,
57
+ }),
58
+ ).toBe(0n)
59
+ })
60
+
61
+ test('behavior: fee at or above active assets mints nothing', () => {
62
+ expect(
63
+ EarnShares.feeShares({
64
+ activeAssets: 100n,
65
+ shareSupply: 1_000n,
66
+ totalFeeAssets: 100n,
67
+ }),
68
+ ).toBe(0n)
69
+ })
70
+
71
+ test('behavior: rounds down', () => {
72
+ expect(
73
+ EarnShares.feeShares({
74
+ activeAssets: 1_000n,
75
+ shareSupply: 999n,
76
+ totalFeeAssets: 100n,
77
+ }),
78
+ ).toBe(111n)
79
+ })
80
+ })
81
+
82
+ describe('minimumOutput', () => {
83
+ test('default', () => {
84
+ expect(EarnShares.minimumOutput(1_000_000n, 50)).toBe(995_000n)
85
+ })
86
+
87
+ test('behavior: zero slippage returns the expected output', () => {
88
+ expect(EarnShares.minimumOutput(1_000_000n, 0)).toBe(1_000_000n)
89
+ })
90
+
91
+ test('behavior: floors to 1n', () => {
92
+ expect(EarnShares.minimumOutput(1n, 9_999)).toBe(1n)
93
+ })
94
+
95
+ test('error: non-positive expected output', () => {
96
+ expect(() =>
97
+ EarnShares.minimumOutput(0n, 50),
98
+ ).toThrowErrorMatchingInlineSnapshot(
99
+ `[EarnShares.InvalidExpectedOutputError: Expected output \`0\` must be greater than zero.]`,
100
+ )
101
+ })
102
+
103
+ test('error: out-of-range slippage', () => {
104
+ expect(() => EarnShares.minimumOutput(1_000_000n, 10_000))
105
+ .toThrowErrorMatchingInlineSnapshot(`
106
+ [EarnShares.InvalidSlippageError: Slippage tolerance \`10000\` is invalid.
107
+
108
+ Slippage must be a whole number from 0 through 9999 basis points.]
109
+ `)
110
+ })
111
+
112
+ test('error: non-integer slippage', () => {
113
+ expect(() => EarnShares.minimumOutput(1_000_000n, 0.5))
114
+ .toThrowErrorMatchingInlineSnapshot(`
115
+ [EarnShares.InvalidSlippageError: Slippage tolerance \`0.5\` is invalid.
116
+
117
+ Slippage must be a whole number from 0 through 9999 basis points.]
118
+ `)
119
+ })
120
+ })
@@ -0,0 +1,235 @@
1
+ import * as Errors from '../core/Errors.js'
2
+
3
+ /** Basis-point denominator used by slippage bounds. */
4
+ export const basisPointScale = 10_000
5
+
6
+ /**
7
+ * Tempo Earn `VaultAdapter` conversion anchor.
8
+ *
9
+ * The adapter prices vault shares against venue shares through this pair:
10
+ * `engineShares` venue shares are worth `shareSupply` vault shares. It is initialised
11
+ * 1:1 and restated on `contribute` and `migrateEngine`.
12
+ *
13
+ * These conversions are raw and fee-blind; they ignore pending fee dilution
14
+ * and are unsuitable for user-facing value (use the adapter's `previewRedeem`).
15
+ */
16
+ export type Anchor = {
17
+ /** Venue shares held by the engine at the anchor point. */
18
+ engineShares: bigint
19
+ /** Vault share supply at the anchor point. */
20
+ shareSupply: bigint
21
+ }
22
+
23
+ /**
24
+ * Converts venue shares to a vault share amount at the anchor rate, rounding down.
25
+ *
26
+ * Mirrors `VaultAdapter.sharesToTokens`.
27
+ *
28
+ * @example
29
+ * ```ts twoslash
30
+ * import { EarnShares } from 'ox/tempo'
31
+ *
32
+ * const shareAmount = EarnShares.toAmount(
33
+ * { engineShares: 3n, shareSupply: 2n },
34
+ * 7n
35
+ * )
36
+ * // @log: 4n
37
+ * ```
38
+ *
39
+ * @param anchor - The conversion anchor.
40
+ * @param venueShareAmount - Venue share amount, base units.
41
+ * @returns Vault share amount, rounded down.
42
+ */
43
+ export function toAmount(anchor: Anchor, venueShareAmount: bigint): bigint {
44
+ return (venueShareAmount * anchor.shareSupply) / anchor.engineShares
45
+ }
46
+
47
+ export declare namespace toAmount {
48
+ type ErrorType = Errors.GlobalErrorType
49
+ }
50
+
51
+ /**
52
+ * Converts venue shares to a vault share amount at the anchor rate, rounding up.
53
+ *
54
+ * Mirrors the adapter's ceiling conversion used by exact-asset exits.
55
+ *
56
+ * @example
57
+ * ```ts twoslash
58
+ * import { EarnShares } from 'ox/tempo'
59
+ *
60
+ * const shareAmount = EarnShares.toAmountUp(
61
+ * { engineShares: 3n, shareSupply: 2n },
62
+ * 7n
63
+ * )
64
+ * // @log: 5n
65
+ * ```
66
+ *
67
+ * @param anchor - The conversion anchor.
68
+ * @param venueShareAmount - Venue share amount, base units.
69
+ * @returns Vault share amount, rounded up.
70
+ */
71
+ export function toAmountUp(anchor: Anchor, venueShareAmount: bigint): bigint {
72
+ const { engineShares, shareSupply } = anchor
73
+ return (venueShareAmount * shareSupply + engineShares - 1n) / engineShares
74
+ }
75
+
76
+ export declare namespace toAmountUp {
77
+ type ErrorType = Errors.GlobalErrorType
78
+ }
79
+
80
+ /**
81
+ * Converts a vault share amount to venue shares at the anchor rate, rounding down.
82
+ *
83
+ * Mirrors `VaultAdapter.tokensToShares`.
84
+ *
85
+ * @example
86
+ * ```ts twoslash
87
+ * import { EarnShares } from 'ox/tempo'
88
+ *
89
+ * const venueShareAmount = EarnShares.toVenueAmount(
90
+ * { engineShares: 3n, shareSupply: 2n },
91
+ * 7n
92
+ * )
93
+ * // @log: 10n
94
+ * ```
95
+ *
96
+ * @param anchor - The conversion anchor.
97
+ * @param shareAmount - Vault share amount, base units.
98
+ * @returns Venue share amount, rounded down.
99
+ */
100
+ export function toVenueAmount(anchor: Anchor, shareAmount: bigint): bigint {
101
+ return (shareAmount * anchor.engineShares) / anchor.shareSupply
102
+ }
103
+
104
+ export declare namespace toVenueAmount {
105
+ type ErrorType = Errors.GlobalErrorType
106
+ }
107
+
108
+ /**
109
+ * Computes the dilution-correct vault shares minted for an asset-denominated fee.
110
+ *
111
+ * Mirrors `FeeMath`:
112
+ * `feeShares = floor(fee * shareSupply / (activeAssets - fee))`, zero when the
113
+ * fee is zero or not smaller than the active assets. Minting this amount to the
114
+ * fee ledger prices the fee at post-mint value per share.
115
+ *
116
+ * @example
117
+ * ```ts twoslash
118
+ * import { EarnShares } from 'ox/tempo'
119
+ *
120
+ * const shares = EarnShares.feeShares({
121
+ * activeAssets: 1_100n,
122
+ * shareSupply: 1_000n,
123
+ * totalFeeAssets: 100n
124
+ * })
125
+ * // @log: 100n
126
+ * ```
127
+ *
128
+ * @param options - Fee accrual inputs.
129
+ * @returns Vault shares to mint for the fee, rounded down.
130
+ */
131
+ export function feeShares(options: feeShares.Options): bigint {
132
+ const { activeAssets, shareSupply, totalFeeAssets } = options
133
+ if (totalFeeAssets === 0n || totalFeeAssets >= activeAssets) return 0n
134
+ return (totalFeeAssets * shareSupply) / (activeAssets - totalFeeAssets)
135
+ }
136
+
137
+ export declare namespace feeShares {
138
+ export type Options = {
139
+ /** Assets backing the active (non-queued) supply, base units. */
140
+ activeAssets: bigint
141
+ /** Active vault share supply, base units. */
142
+ shareSupply: bigint
143
+ /** Total fee liability in asset units. */
144
+ totalFeeAssets: bigint
145
+ }
146
+ export type ErrorType = Errors.GlobalErrorType
147
+ }
148
+
149
+ /**
150
+ * Lowers an expected output by a basis-point slippage tolerance, flooring to `1n`.
151
+ *
152
+ * Suitable for lower bounds such as a deposit's minimum shares or a redeem's
153
+ * minimum assets; not for upper bounds such as an exact withdrawal's maximum
154
+ * shares.
155
+ *
156
+ * @example
157
+ * ```ts twoslash
158
+ * import { EarnShares } from 'ox/tempo'
159
+ *
160
+ * const minimumShares = EarnShares.minimumOutput(
161
+ * 1_000_000n,
162
+ * 50
163
+ * )
164
+ * // @log: 995_000n
165
+ * ```
166
+ *
167
+ * @param expectedAmount - Expected output in base units.
168
+ * @param slippageBps - Allowed slippage in basis points from `0` through `9_999`.
169
+ * @returns The minimum accepted output, floored to `1n`.
170
+ * @throws `InvalidExpectedOutputError` when `expectedAmount` is not positive.
171
+ * @throws `InvalidSlippageError` when `slippageBps` is outside its valid range.
172
+ */
173
+ export function minimumOutput(
174
+ expectedAmount: bigint,
175
+ slippageBps: number,
176
+ ): bigint {
177
+ if (expectedAmount <= 0n)
178
+ throw new InvalidExpectedOutputError({ expectedAmount })
179
+ if (
180
+ !Number.isInteger(slippageBps) ||
181
+ slippageBps < 0 ||
182
+ slippageBps >= basisPointScale
183
+ )
184
+ throw new InvalidSlippageError({ slippageBps })
185
+ const scale = BigInt(basisPointScale)
186
+ const bounded = (expectedAmount * (scale - BigInt(slippageBps))) / scale
187
+ return bounded === 0n ? 1n : bounded
188
+ }
189
+
190
+ export declare namespace minimumOutput {
191
+ type ErrorType =
192
+ | InvalidExpectedOutputError
193
+ | InvalidSlippageError
194
+ | Errors.GlobalErrorType
195
+ }
196
+
197
+ /**
198
+ * Error thrown when an expected output is not positive.
199
+ */
200
+ export class InvalidExpectedOutputError extends Errors.BaseError {
201
+ override readonly name = 'EarnShares.InvalidExpectedOutputError'
202
+
203
+ constructor(options: InvalidExpectedOutputError.Options) {
204
+ super(
205
+ `Expected output \`${options.expectedAmount}\` must be greater than zero.`,
206
+ )
207
+ }
208
+ }
209
+
210
+ export declare namespace InvalidExpectedOutputError {
211
+ export type Options = {
212
+ expectedAmount: bigint
213
+ }
214
+ }
215
+
216
+ /**
217
+ * Error thrown when a slippage tolerance is not an integer from `0` through `9_999`.
218
+ */
219
+ export class InvalidSlippageError extends Errors.BaseError {
220
+ override readonly name = 'EarnShares.InvalidSlippageError'
221
+
222
+ constructor(options: InvalidSlippageError.Options) {
223
+ super(`Slippage tolerance \`${options.slippageBps}\` is invalid.`, {
224
+ metaMessages: [
225
+ `Slippage must be a whole number from 0 through ${basisPointScale - 1} basis points.`,
226
+ ],
227
+ })
228
+ }
229
+ }
230
+
231
+ export declare namespace InvalidSlippageError {
232
+ export type Options = {
233
+ slippageBps: number
234
+ }
235
+ }
@@ -68,6 +68,28 @@ export * as AuthorizationTempo from './AuthorizationTempo.js'
68
68
  * @category Reference
69
69
  */
70
70
  export * as Channel from './Channel.js'
71
+ /**
72
+ * Tempo Earn `VaultAdapter` share math: raw vault-share and venue-share conversions
73
+ * at the anchor rate, the dilution-correct fee-share formula, and the
74
+ * `minimumOutput` slippage floor.
75
+ *
76
+ * Conversions are fee-blind mirrors of the adapter's anchor arithmetic; use the
77
+ * adapter's `previewRedeem` for user-facing value.
78
+ *
79
+ * @example
80
+ * ```ts twoslash
81
+ * import { EarnShares } from 'ox/tempo'
82
+ *
83
+ * const shareAmount = EarnShares.toAmount(
84
+ * { engineShares: 3n, shareSupply: 2n },
85
+ * 7n
86
+ * )
87
+ * // @log: 4n
88
+ * ```
89
+ *
90
+ * @category Reference
91
+ */
92
+ export * as EarnShares from './EarnShares.js'
71
93
  /**
72
94
  * Tempo key authorization utilities for provisioning and signing access keys.
73
95
  *
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  /** @internal */
2
- export const version = '1.0.3'
2
+ export const version = '1.0.4'