@agoric/inter-protocol 0.13.2-dev-70ba9b0.0 → 0.13.2-dev-ed5ee58.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/bundles/bundle-econCommitteeCharter-meta.js +78 -78
  2. package/bundles/bundle-econCommitteeCharter.js +1 -1
  3. package/bundles/bundle-feeDistributor-meta.js +61 -61
  4. package/bundles/bundle-feeDistributor.js +1 -1
  5. package/bundles/bundle-fluxAggregator-meta.js +188 -188
  6. package/bundles/bundle-fluxAggregator.js +1 -1
  7. package/bundles/bundle-psm-meta.js +112 -112
  8. package/bundles/bundle-psm.js +1 -1
  9. package/bundles/bundle-reserve-meta.js +110 -114
  10. package/bundles/bundle-reserve.js +1 -1
  11. package/bundles/bundle-scaledPriceAuthority-meta.js +78 -78
  12. package/bundles/bundle-stakeFactory-meta.js +118 -118
  13. package/bundles/bundle-stakeFactory.js +1 -1
  14. package/bundles/bundle-vaultFactory-meta.js +130 -130
  15. package/bundles/bundle-vaultFactory.js +1 -1
  16. package/exported.js +0 -1
  17. package/package.json +16 -16
  18. package/scripts/deploy-contracts.js +0 -2
  19. package/scripts/init-core.js +0 -16
  20. package/src/feeDistributor.js +1 -1
  21. package/src/proposals/addAssetToVault.js +4 -57
  22. package/src/proposals/committee-proposal.js +3 -6
  23. package/src/proposals/core-proposal.js +1 -73
  24. package/src/proposals/demoIssuers.js +1 -232
  25. package/src/proposals/econ-behaviors.js +10 -193
  26. package/src/proposals/sim-behaviors.js +1 -1
  27. package/src/reserve/assetReserve.js +9 -170
  28. package/src/reserve/params.js +1 -4
  29. package/src/vaultFactory/liquidateIncrementally.js +16 -141
  30. package/src/vaultFactory/liquidateMinimum.js +19 -21
  31. package/src/vaultFactory/params.js +0 -29
  32. package/src/vaultFactory/types.js +0 -6
  33. package/src/vaultFactory/vault.js +1 -3
  34. package/src/vaultFactory/vaultDirector.js +1 -33
  35. package/src/vaultFactory/vaultFactory.js +2 -12
  36. package/src/vaultFactory/vaultManager.js +10 -217
  37. package/bundles/bundle-amm-meta.js +0 -686
  38. package/bundles/bundle-amm.js +0 -1
  39. package/bundles/bundle-interchainPool-meta.js +0 -606
  40. package/bundles/bundle-interchainPool.js +0 -1
  41. package/bundles/bundle-liquidateIncrementally-meta.js +0 -470
  42. package/bundles/bundle-liquidateIncrementally.js +0 -1
  43. package/bundles/bundle-liquidateMinimum-meta.js +0 -470
  44. package/bundles/bundle-liquidateMinimum.js +0 -1
  45. package/scripts/add-initial-liquidity.js +0 -179
  46. package/src/interchainPool.js +0 -134
  47. package/src/vpool-xyk-amm/AttackersGuide.md +0 -89
  48. package/src/vpool-xyk-amm/addLiquidity.js +0 -221
  49. package/src/vpool-xyk-amm/addPool.js +0 -200
  50. package/src/vpool-xyk-amm/constantProduct/AMM-Trade.jpeg +0 -0
  51. package/src/vpool-xyk-amm/constantProduct/README.md +0 -204
  52. package/src/vpool-xyk-amm/constantProduct/calcFees.js +0 -93
  53. package/src/vpool-xyk-amm/constantProduct/calcSwapPrices.js +0 -83
  54. package/src/vpool-xyk-amm/constantProduct/core.js +0 -145
  55. package/src/vpool-xyk-amm/constantProduct/defaults.js +0 -3
  56. package/src/vpool-xyk-amm/constantProduct/getXY.js +0 -48
  57. package/src/vpool-xyk-amm/constantProduct/internal-types.js +0 -114
  58. package/src/vpool-xyk-amm/constantProduct/invariants.js +0 -39
  59. package/src/vpool-xyk-amm/constantProduct/swap.js +0 -270
  60. package/src/vpool-xyk-amm/constantProduct/types.js +0 -30
  61. package/src/vpool-xyk-amm/doublePool.js +0 -200
  62. package/src/vpool-xyk-amm/estimate.js +0 -175
  63. package/src/vpool-xyk-amm/multipoolMarketMaker.js +0 -530
  64. package/src/vpool-xyk-amm/params.js +0 -59
  65. package/src/vpool-xyk-amm/pool.js +0 -398
  66. package/src/vpool-xyk-amm/priceAuthority.js +0 -92
  67. package/src/vpool-xyk-amm/removeLiquidity.js +0 -29
  68. package/src/vpool-xyk-amm/singlePool.js +0 -88
  69. package/src/vpool-xyk-amm/swap.js +0 -68
  70. package/src/vpool-xyk-amm/types.js +0 -152
@@ -1,200 +0,0 @@
1
- import { AmountMath } from '@agoric/ertp';
2
- import {
3
- atomicRearrange,
4
- fromOnly,
5
- toOnly,
6
- } from '@agoric/zoe/src/contractSupport/index.js';
7
- import { Far } from '@endo/marshal';
8
- import { makeFeeRatio } from './constantProduct/calcFees.js';
9
- import {
10
- pricesForStatedInput,
11
- pricesForStatedOutput,
12
- } from './constantProduct/calcSwapPrices.js';
13
-
14
- const { Fail } = assert;
15
-
16
- // Price calculations and swap using a pair of pools. Both pools map between RUN
17
- // and some collateral. We arrange the trades so collateralInPool will have
18
- // collateral added and collateralOutPool subtracted. When traders specify an
19
- // input price, that brand will be the inPool; when they specify the output
20
- // price that brand is the outPool.
21
-
22
- /**
23
- * doublePool is the virtualPool implementation for calculating prices when
24
- * transiting through two pools. virtual pools wrap something that can do a swap
25
- * and calculate prices. Current wrapped pools are single pools and pairs of
26
- * pools, but we've also contemplated using a similar virtual wrapper for other
27
- * curves.
28
- *
29
- * @param {ZCF} zcf
30
- * @param {XYKPool} collateralInPool
31
- * @param {XYKPool} collateralOutPool
32
- * @param {() => bigint} getProtocolFeeBP - retrieve governed protocol fee value
33
- * @param {() => bigint} getPoolFeeBP - retrieve governed pool fee value
34
- * @param {ZCFSeat} feeSeat
35
- * @returns {VirtualPool<'double'>}
36
- */
37
- export const makeDoublePool = (
38
- zcf,
39
- collateralInPool,
40
- collateralOutPool,
41
- getProtocolFeeBP,
42
- getPoolFeeBP,
43
- feeSeat,
44
- ) => {
45
- const inCentral = collateralInPool.getCentralAmount();
46
- const inSecondary = collateralInPool.getSecondaryAmount();
47
-
48
- const outCentral = collateralOutPool.getCentralAmount();
49
- const outSecondary = collateralOutPool.getSecondaryAmount();
50
-
51
- const inAllocation = { Central: inCentral, Secondary: inSecondary };
52
- const outAllocation = { Central: outCentral, Secondary: outSecondary };
53
-
54
- const centralBrand = inCentral.brand;
55
- const emptyCentralAmount = AmountMath.makeEmpty(centralBrand);
56
- centralBrand === outCentral.brand ||
57
- Fail`The central brands on the two pools must match: ${centralBrand}, ${outCentral.brand}`;
58
-
59
- const allocateGainsAndLosses = (seat, prices) => {
60
- const inPoolSeat = collateralInPool.getPoolSeat();
61
- const outPoolSeat = collateralOutPool.getPoolSeat();
62
-
63
- atomicRearrange(
64
- zcf,
65
- harden([
66
- fromOnly(seat, { In: prices.swapperGives }),
67
- fromOnly(inPoolSeat, { Central: prices.inPoolDecrement }),
68
- fromOnly(outPoolSeat, { Secondary: prices.outPoolDecrement }),
69
-
70
- toOnly(seat, { Out: prices.swapperGets }),
71
- toOnly(inPoolSeat, { Secondary: prices.inPoolIncrement }),
72
- toOnly(outPoolSeat, { Central: prices.outPoolIncrement }),
73
- toOnly(feeSeat, { Fee: prices.protocolFee }),
74
- ]),
75
- );
76
-
77
- seat.exit();
78
- collateralInPool.updateState();
79
- collateralOutPool.updateState();
80
- return `Swap successfully completed.`;
81
- };
82
-
83
- const getPriceForInput = (amountIn, amountOut) => {
84
- const protocolFeeRatio = makeFeeRatio(getProtocolFeeBP(), centralBrand);
85
- const poolFeeRatioCentral = makeFeeRatio(getPoolFeeBP(), centralBrand);
86
- const poolFeeRatioAmountOut = makeFeeRatio(getPoolFeeBP(), amountOut.brand);
87
-
88
- // We must do two consecutive swapInPrice() calls,
89
- // followed by a call to swapOutPrice().
90
- // 1) from amountIn to the central token, which tells us how much central
91
- // would be provided for amountIn,
92
- // 2) from that amount of central to brandOut, which tells us how much of
93
- // brandOut will be provided as well as the minimum price in central
94
- // tokens, then finally
95
- // 3) call swapOutPrice() to see if the same proceeds can be purchased for
96
- // less.
97
- // Notice that in the second call, the original amountOut is used, and in
98
- // the third call, the original amountIn is used.
99
- const interimInpoolPrices = pricesForStatedInput(
100
- amountIn,
101
- inAllocation,
102
- emptyCentralAmount,
103
- protocolFeeRatio,
104
- poolFeeRatioCentral,
105
- );
106
- const outPoolPrices = pricesForStatedInput(
107
- interimInpoolPrices.swapperGets,
108
- outAllocation,
109
- amountOut,
110
- protocolFeeRatio,
111
- poolFeeRatioAmountOut,
112
- );
113
- const finalInPoolPrices = pricesForStatedOutput(
114
- amountIn,
115
- inAllocation,
116
- outPoolPrices.swapperGives,
117
- protocolFeeRatio,
118
- poolFeeRatioCentral,
119
- );
120
-
121
- // When finalInPoolPrices has a price improvement that makes the difference
122
- // between finalInPoolPrices.yDecrement and outPoolPrices.xIncrement more
123
- // than the calculated protocol fees add the excess to the protocol fee.
124
- const protocolFee = AmountMath.max(
125
- AmountMath.add(finalInPoolPrices.protocolFee, outPoolPrices.protocolFee),
126
- AmountMath.subtract(
127
- finalInPoolPrices.yDecrement,
128
- outPoolPrices.xIncrement,
129
- ),
130
- );
131
-
132
- return harden({
133
- swapperGives: finalInPoolPrices.swapperGives,
134
- swapperGets: outPoolPrices.swapperGets,
135
- inPoolIncrement: finalInPoolPrices.xIncrement,
136
- inPoolDecrement: finalInPoolPrices.yDecrement,
137
- outPoolIncrement: outPoolPrices.xIncrement,
138
- outPoolDecrement: outPoolPrices.yDecrement,
139
- protocolFee,
140
- });
141
- };
142
-
143
- const getPriceForOutput = (amountIn, amountOut) => {
144
- const protocolFeeRatio = makeFeeRatio(getProtocolFeeBP(), centralBrand);
145
- const poolFeeRatioCentral = makeFeeRatio(getPoolFeeBP(), centralBrand);
146
- const poolFeeRatioAmountIn = makeFeeRatio(getPoolFeeBP(), amountIn.brand);
147
-
148
- // We must do two consecutive swapOutPrice() calls, followed by a call to
149
- // swapInPrice().
150
- // 1) from amountOut to the central token, which tells us how much central
151
- // is required to obtain amountOut,
152
- // 2) from that amount of central to brandIn, which tells us how much of
153
- // brandIn is required as well as the max proceeds in central tokens, then
154
- // finally
155
- // 3) call swapInPrice() to see if those central proceeds could purchase
156
- // larger amount
157
- // Notice that the amountIn parameter to the first call to swapOutPrice
158
- // specifies an empty amount. This is interpreted as "no limit", which is
159
- // necessary since we can't guess a reasonable maximum of the central token.
160
- const interimOutpoolPrices = pricesForStatedOutput(
161
- emptyCentralAmount,
162
- outAllocation,
163
- amountOut,
164
- protocolFeeRatio,
165
- poolFeeRatioCentral,
166
- );
167
- const inpoolPrices = pricesForStatedOutput(
168
- amountIn,
169
- inAllocation,
170
- interimOutpoolPrices.swapperGives,
171
- protocolFeeRatio,
172
- poolFeeRatioAmountIn,
173
- );
174
- const finalOutpoolPrices = pricesForStatedInput(
175
- inpoolPrices.swapperGets,
176
- outAllocation,
177
- amountOut,
178
- protocolFeeRatio,
179
- poolFeeRatioCentral,
180
- );
181
- return harden({
182
- swapperGives: inpoolPrices.swapperGives,
183
- swapperGets: finalOutpoolPrices.swapperGets,
184
- inPoolIncrement: inpoolPrices.xIncrement,
185
- inPoolDecrement: inpoolPrices.yDecrement,
186
- outPoolIncrement: finalOutpoolPrices.xIncrement,
187
- outPoolDecrement: finalOutpoolPrices.yDecrement,
188
- protocolFee: AmountMath.add(
189
- finalOutpoolPrices.protocolFee,
190
- inpoolPrices.protocolFee,
191
- ),
192
- });
193
- };
194
-
195
- return Far('double pool', {
196
- getPriceForInput,
197
- getPriceForOutput,
198
- allocateGainsAndLosses,
199
- });
200
- };
@@ -1,175 +0,0 @@
1
- import { natSafeMath } from '@agoric/zoe/src/contractSupport/index.js';
2
- import { AmountMath } from '@agoric/ertp';
3
- import { BASIS_POINTS } from './constantProduct/defaults.js';
4
-
5
- /**
6
- * @file support the UI in calculating how much to offer, taking slippage
7
- * into account, and not requiring a round trip to the chain.
8
- */
9
-
10
- /** @typedef {{ centralAmount: Amount<'nat'>, secondaryAmount: Amount<'nat'> }} Pool */
11
-
12
- const { ceilDivide, floorDivide } = natSafeMath;
13
-
14
- /**
15
- * @param {bigint} chargeBP
16
- * @param {Amount<'nat'>} amount
17
- */
18
- export const charge = (chargeBP, amount) => {
19
- return AmountMath.make(
20
- amount.brand,
21
- ceilDivide(amount.value * chargeBP, BASIS_POINTS),
22
- );
23
- };
24
-
25
- /**
26
- * @param {bigint} chargeBP
27
- * @param {Amount<'nat'>} amount
28
- */
29
- export const chargeFloor = (chargeBP, amount) => {
30
- return AmountMath.make(
31
- amount.brand,
32
- floorDivide(amount.value * chargeBP, BASIS_POINTS),
33
- );
34
- };
35
-
36
- const makeEstimator = (centralBrand, rates) => {
37
- const { poolFeeBP, protocolFeeBP, slippageBP } = rates;
38
-
39
- /**
40
- * Proceeds less slippage when specifying the amount to add to a single pool
41
- *
42
- * @param {Amount<'nat'>} amountIn
43
- * @param {Brand<'nat'>} brandOut
44
- * @param {MapStore<Brand, Pool>} pools
45
- */
46
- function estimateSinglePoolProceeds(amountIn, brandOut, pools) {
47
- const isCentralIn = amountIn.brand === centralBrand;
48
- const pool = isCentralIn ? pools.get(brandOut) : pools.get(amountIn.brand);
49
- const kPre = pool.secondaryAmount.value * pool.centralAmount.value;
50
-
51
- function calcYFromX(X) {
52
- const denom = isCentralIn
53
- ? AmountMath.add(pool.centralAmount, X)
54
- : AmountMath.add(pool.secondaryAmount, X);
55
- const finalOutBalance = AmountMath.make(
56
- brandOut,
57
- ceilDivide(kPre, denom.value),
58
- );
59
-
60
- return isCentralIn
61
- ? AmountMath.subtract(pool.secondaryAmount, finalOutBalance)
62
- : AmountMath.subtract(pool.centralAmount, finalOutBalance);
63
- }
64
-
65
- const amountAdded = isCentralIn
66
- ? AmountMath.subtract(amountIn, charge(protocolFeeBP, amountIn))
67
- : amountIn;
68
- const amountOutForFees = calcYFromX(amountIn);
69
- const amountOut = calcYFromX(amountAdded);
70
- const amountRemoved = AmountMath.subtract(
71
- amountOut,
72
- charge(poolFeeBP, amountOutForFees),
73
- );
74
- const amountReturned = isCentralIn
75
- ? amountRemoved
76
- : AmountMath.subtract(
77
- amountRemoved,
78
- charge(protocolFeeBP, amountOutForFees),
79
- );
80
-
81
- return amountReturned;
82
- }
83
-
84
- /**
85
- * Payment w/slippage when specifying the amount to receive from a single pool
86
- *
87
- * @param {Brand<'nat'>} brandIn
88
- * @param {Amount<'nat'>} amountOut
89
- * @param {MapStore<Brand, Pool>} pools
90
- */
91
- function estimateSinglePoolRequired(brandIn, amountOut, pools) {
92
- const isCentralIn = brandIn === centralBrand;
93
- const pool = isCentralIn ? pools.get(amountOut.brand) : pools.get(brandIn);
94
- const kPre = pool.secondaryAmount.value * pool.centralAmount.value;
95
- const amountWithdrawn = isCentralIn
96
- ? AmountMath.add(amountOut, charge(protocolFeeBP, amountOut))
97
- : amountOut;
98
-
99
- const calcXFromY = y => {
100
- const denom = isCentralIn
101
- ? AmountMath.subtract(pool.secondaryAmount, y)
102
- : AmountMath.subtract(pool.centralAmount, y);
103
- const finalInBalance = AmountMath.make(
104
- brandIn,
105
- ceilDivide(kPre, denom.value),
106
- );
107
- return isCentralIn
108
- ? AmountMath.subtract(finalInBalance, pool.centralAmount)
109
- : AmountMath.subtract(finalInBalance, pool.secondaryAmount);
110
- };
111
-
112
- const amountInForFees = calcXFromY(amountOut);
113
- const amountIn = calcXFromY(amountWithdrawn);
114
- const amountAddToPool = AmountMath.add(
115
- amountIn,
116
- charge(poolFeeBP, amountInForFees),
117
- );
118
- const amountToBePaid = isCentralIn
119
- ? amountAddToPool
120
- : AmountMath.add(
121
- amountAddToPool,
122
- chargeFloor(protocolFeeBP, amountInForFees),
123
- );
124
-
125
- return amountToBePaid;
126
- }
127
-
128
- // ignores price improvement
129
- function estimateDoublePoolProceeds(amountIn, brandOut, pools) {
130
- const inProceeds = estimateSinglePoolProceeds(
131
- amountIn,
132
- centralBrand,
133
- pools,
134
- );
135
- const outProceeds = estimateSinglePoolProceeds(inProceeds, brandOut, pools);
136
- return outProceeds;
137
- }
138
-
139
- // ignores price improvement
140
- function estimateDoublePoolRequired(brandIn, amountOut, pools) {
141
- const outRequired = estimateSinglePoolRequired(
142
- centralBrand,
143
- amountOut,
144
- pools,
145
- );
146
- const inRequired = estimateSinglePoolRequired(brandIn, outRequired, pools);
147
- return inRequired;
148
- }
149
-
150
- const estimateProceeds = (amountIn, brandOut, pools) => {
151
- let proceeds;
152
- if (amountIn.brand === centralBrand || brandOut === centralBrand) {
153
- proceeds = estimateSinglePoolProceeds(amountIn, brandOut, pools);
154
- } else {
155
- proceeds = estimateDoublePoolProceeds(amountIn, brandOut, pools);
156
- }
157
- return AmountMath.subtract(proceeds, charge(slippageBP, proceeds));
158
- };
159
-
160
- const estimateRequired = (brandIn, amountOut, pools) => {
161
- let required;
162
- if (brandIn === centralBrand || amountOut.brand === centralBrand) {
163
- required = estimateSinglePoolRequired(brandIn, amountOut, pools);
164
- } else {
165
- required = estimateDoublePoolRequired(brandIn, amountOut, pools);
166
- }
167
-
168
- return AmountMath.add(required, charge(slippageBP, required));
169
- };
170
-
171
- return { estimateProceeds, estimateRequired };
172
- };
173
- harden(makeEstimator);
174
-
175
- export { makeEstimator };