@agoric/inter-protocol 0.13.3 → 0.13.4-dev-d32a66a.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.
- package/README.md +26 -20
- package/bundles/bundle-auctioneer-js-meta.json +722 -0
- package/bundles/bundle-auctioneer.js +1 -0
- package/bundles/bundle-econCommitteeCharter-js-meta.json +514 -0
- package/bundles/bundle-econCommitteeCharter.js +1 -0
- package/bundles/bundle-feeDistributor-js-meta.json +458 -0
- package/bundles/bundle-feeDistributor.js +1 -1
- package/bundles/bundle-fluxAggregatorKit-js-meta.json +674 -0
- package/bundles/bundle-fluxAggregatorKit.js +1 -0
- package/bundles/bundle-psm-js-meta.json +666 -0
- package/bundles/bundle-psm.js +1 -1
- package/bundles/bundle-reserve-js-meta.json +662 -0
- package/bundles/bundle-reserve.js +1 -1
- package/bundles/bundle-scaledPriceAuthority-js-meta.json +542 -0
- package/bundles/bundle-scaledPriceAuthority.js +1 -1
- package/bundles/bundle-vaultFactory-js-meta.json +786 -0
- package/bundles/bundle-vaultFactory.js +1 -1
- package/exported.js +0 -2
- package/package.json +34 -31
- package/scripts/add-collateral-core.js +14 -4
- package/scripts/build-bundles.js +9 -2
- package/scripts/deploy-contracts.js +1 -4
- package/scripts/init-core.js +20 -38
- package/scripts/invite-committee-core.js +1 -1
- package/scripts/manual-price-feed.js +2 -2
- package/scripts/price-feed-core.js +7 -6
- package/scripts/start-local-chain.sh +84 -0
- package/src/auction/auctionBook.js +755 -0
- package/src/auction/auctioneer.js +720 -0
- package/src/auction/offerBook.js +221 -0
- package/src/auction/params.js +174 -0
- package/src/auction/scheduleMath.js +167 -0
- package/src/auction/scheduler.js +370 -0
- package/src/auction/sortedOffers.js +136 -0
- package/src/auction/util.js +55 -0
- package/src/clientSupport.js +324 -0
- package/src/collectFees.js +8 -14
- package/src/contractSupport.js +24 -68
- package/src/econCommitteeCharter.js +161 -77
- package/src/feeDistributor.js +60 -20
- package/src/index.js +2 -0
- package/src/interest-math.js +1 -4
- package/src/interest.js +20 -13
- package/src/price/README.md +57 -0
- package/src/price/fluxAggregatorContract.js +190 -0
- package/src/price/fluxAggregatorKit.js +375 -0
- package/src/price/priceOracleKit.js +127 -0
- package/src/price/roundsManager.js +726 -0
- package/src/proposals/addAssetToVault.js +89 -94
- package/src/proposals/committee-proposal.js +141 -40
- package/src/proposals/core-proposal.js +100 -167
- package/src/proposals/econ-behaviors.js +320 -564
- package/src/proposals/price-feed-proposal.js +175 -78
- package/src/proposals/startEconCommittee.js +25 -12
- package/src/proposals/startPSM.js +231 -211
- package/src/proposals/utils.js +31 -17
- package/src/psm/README.md +71 -0
- package/src/psm/psm.js +272 -160
- package/src/psm/types.js +2 -0
- package/src/reserve/assetReserve.js +56 -413
- package/src/reserve/assetReserveKit.js +303 -0
- package/src/reserve/doc.md +1 -3
- package/src/reserve/params.js +2 -5
- package/src/typeGuards.js +13 -0
- package/src/vaultFactory/Liquidation.md +100 -0
- package/src/vaultFactory/burn.js +2 -1
- package/src/vaultFactory/liquidation.js +270 -108
- package/src/vaultFactory/math.js +95 -0
- package/src/vaultFactory/orderedVaultStore.js +0 -1
- package/src/vaultFactory/params.js +133 -112
- package/src/vaultFactory/prioritizedVaults.js +36 -79
- package/src/vaultFactory/storeUtils.js +67 -9
- package/src/vaultFactory/type-imports.js +2 -0
- package/src/vaultFactory/types.js +22 -26
- package/src/vaultFactory/vault.js +772 -709
- package/src/vaultFactory/vaultDirector.js +480 -483
- package/src/vaultFactory/vaultFactory.js +99 -60
- package/src/vaultFactory/vaultHolder.js +121 -95
- package/src/vaultFactory/vaultKit.js +39 -31
- package/src/vaultFactory/vaultManager.js +1249 -868
- package/bundles/bundle-amm-meta.js +0 -618
- package/bundles/bundle-amm.js +0 -1
- package/bundles/bundle-feeDistributor-meta.js +0 -346
- package/bundles/bundle-interchainPool-meta.js +0 -534
- package/bundles/bundle-interchainPool.js +0 -1
- package/bundles/bundle-liquidateIncrementally-meta.js +0 -442
- package/bundles/bundle-liquidateIncrementally.js +0 -1
- package/bundles/bundle-liquidateMinimum-meta.js +0 -442
- package/bundles/bundle-liquidateMinimum.js +0 -1
- package/bundles/bundle-psm-meta.js +0 -538
- package/bundles/bundle-psmCharter-meta.js +0 -418
- package/bundles/bundle-psmCharter.js +0 -1
- package/bundles/bundle-reserve-meta.js +0 -542
- package/bundles/bundle-scaledPriceAuthority-meta.js +0 -442
- package/bundles/bundle-stakeFactory-meta.js +0 -574
- package/bundles/bundle-stakeFactory.js +0 -1
- package/bundles/bundle-vaultFactory-meta.js +0 -598
- package/scripts/add-initial-liquidity.js +0 -180
- package/src/collect.js +0 -13
- package/src/interchainPool.js +0 -135
- package/src/makeTracer.js +0 -36
- package/src/my-lien.js +0 -85
- package/src/proposals/demoIssuers.js +0 -685
- package/src/proposals/sim-behaviors.js +0 -40
- package/src/psm/psmCharter.js +0 -139
- package/src/stakeFactory/README.md +0 -62
- package/src/stakeFactory/attestation.js +0 -267
- package/src/stakeFactory/attestationTool.js +0 -66
- package/src/stakeFactory/constants.js +0 -16
- package/src/stakeFactory/params.js +0 -31
- package/src/stakeFactory/stakeFactory.js +0 -236
- package/src/stakeFactory/stakeFactoryKit.js +0 -455
- package/src/stakeFactory/stakeFactoryManager.js +0 -296
- package/src/stakeFactory/types.js +0 -42
- package/src/vaultFactory/liquidateIncrementally.js +0 -350
- package/src/vaultFactory/liquidateMinimum.js +0 -100
- package/src/vpool-xyk-amm/AttackersGuide.md +0 -89
- package/src/vpool-xyk-amm/addLiquidity.js +0 -222
- package/src/vpool-xyk-amm/addPool.js +0 -197
- package/src/vpool-xyk-amm/constantProduct/AMM-Trade.jpeg +0 -0
- package/src/vpool-xyk-amm/constantProduct/README.md +0 -204
- package/src/vpool-xyk-amm/constantProduct/calcFees.js +0 -99
- package/src/vpool-xyk-amm/constantProduct/calcSwapPrices.js +0 -85
- package/src/vpool-xyk-amm/constantProduct/core.js +0 -153
- package/src/vpool-xyk-amm/constantProduct/defaults.js +0 -5
- package/src/vpool-xyk-amm/constantProduct/getXY.js +0 -52
- package/src/vpool-xyk-amm/constantProduct/internal-types.js +0 -116
- package/src/vpool-xyk-amm/constantProduct/invariants.js +0 -43
- package/src/vpool-xyk-amm/constantProduct/swap.js +0 -272
- package/src/vpool-xyk-amm/constantProduct/types.js +0 -32
- package/src/vpool-xyk-amm/doublePool.js +0 -194
- package/src/vpool-xyk-amm/estimate.js +0 -155
- package/src/vpool-xyk-amm/multipoolMarketMaker.js +0 -520
- package/src/vpool-xyk-amm/params.js +0 -61
- package/src/vpool-xyk-amm/pool.js +0 -396
- package/src/vpool-xyk-amm/priceAuthority.js +0 -94
- package/src/vpool-xyk-amm/removeLiquidity.js +0 -31
- package/src/vpool-xyk-amm/singlePool.js +0 -73
- package/src/vpool-xyk-amm/swap.js +0 -69
- package/src/vpool-xyk-amm/types.js +0 -154
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance';
|
|
4
|
-
|
|
5
|
-
export const POOL_FEE_KEY = 'PoolFee';
|
|
6
|
-
export const PROTOCOL_FEE_KEY = 'ProtocolFee';
|
|
7
|
-
export const MIN_INITIAL_POOL_LIQUIDITY_KEY = 'MinInitialPoolLiquidity';
|
|
8
|
-
|
|
9
|
-
const DEFAULT_POOL_FEE_BP = 24n;
|
|
10
|
-
const DEFAULT_PROTOCOL_FEE_BP = 6n;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @param {Invitation} electorateInvitation - invitation for the question poser
|
|
14
|
-
* @param {bigint} protocolFeeBP
|
|
15
|
-
* @param {bigint} poolFeeBP
|
|
16
|
-
* @param {Amount<'nat'>} minInitialLiquidity
|
|
17
|
-
*/
|
|
18
|
-
const makeAmmParams = (
|
|
19
|
-
electorateInvitation,
|
|
20
|
-
protocolFeeBP,
|
|
21
|
-
poolFeeBP,
|
|
22
|
-
minInitialLiquidity,
|
|
23
|
-
) => {
|
|
24
|
-
return harden({
|
|
25
|
-
[POOL_FEE_KEY]: { type: ParamTypes.NAT, value: poolFeeBP },
|
|
26
|
-
[PROTOCOL_FEE_KEY]: { type: ParamTypes.NAT, value: protocolFeeBP },
|
|
27
|
-
[MIN_INITIAL_POOL_LIQUIDITY_KEY]: {
|
|
28
|
-
type: ParamTypes.AMOUNT,
|
|
29
|
-
value: minInitialLiquidity,
|
|
30
|
-
},
|
|
31
|
-
[CONTRACT_ELECTORATE]: {
|
|
32
|
-
type: ParamTypes.INVITATION,
|
|
33
|
-
value: electorateInvitation,
|
|
34
|
-
},
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
/** @typedef {import('@agoric/governance/src/contractGovernance/typedParamManager').ParamTypesMapFromRecord<ReturnType<typeof makeAmmParams>>} AmmParams */
|
|
39
|
-
|
|
40
|
-
const makeAmmTerms = (
|
|
41
|
-
timer,
|
|
42
|
-
poserInvitationAmount,
|
|
43
|
-
minInitialLiquidity,
|
|
44
|
-
protocolFeeBP = DEFAULT_PROTOCOL_FEE_BP,
|
|
45
|
-
poolFeeBP = DEFAULT_POOL_FEE_BP,
|
|
46
|
-
) => ({
|
|
47
|
-
timer,
|
|
48
|
-
poolFeeBP,
|
|
49
|
-
protocolFeeBP,
|
|
50
|
-
governedParams: makeAmmParams(
|
|
51
|
-
poserInvitationAmount,
|
|
52
|
-
protocolFeeBP,
|
|
53
|
-
poolFeeBP,
|
|
54
|
-
minInitialLiquidity,
|
|
55
|
-
),
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
harden(makeAmmTerms);
|
|
59
|
-
harden(makeAmmParams);
|
|
60
|
-
|
|
61
|
-
export { makeAmmTerms, makeAmmParams };
|
|
@@ -1,396 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import '@agoric/zoe/exported.js';
|
|
4
|
-
|
|
5
|
-
import { AmountMath, isNatValue } from '@agoric/ertp';
|
|
6
|
-
import { makeStoredPublishKit } from '@agoric/notifier';
|
|
7
|
-
import { vivifyKindMulti } from '@agoric/vat-data';
|
|
8
|
-
import {
|
|
9
|
-
calcLiqValueToMint,
|
|
10
|
-
calcSecondaryRequired,
|
|
11
|
-
calcValueToRemove,
|
|
12
|
-
} from '@agoric/zoe/src/contractSupport/index.js';
|
|
13
|
-
|
|
14
|
-
import { E } from '@endo/eventual-send';
|
|
15
|
-
import { makeMetricsPublisherKit } from '../contractSupport.js';
|
|
16
|
-
import { makePriceAuthority } from './priceAuthority.js';
|
|
17
|
-
import { makeSinglePool } from './singlePool.js';
|
|
18
|
-
|
|
19
|
-
// Pools represent a single pool of liquidity. Price calculations and trading
|
|
20
|
-
// happen in a wrapper class that knows whether the proposed trade involves a
|
|
21
|
-
// single pool or multiple hops.
|
|
22
|
-
|
|
23
|
-
export const publicPrices = prices => {
|
|
24
|
-
return { amountIn: prices.swapperGives, amountOut: prices.swapperGets };
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/** @typedef {{ liquidityIssuerRecord: IssuerRecord<'nat'> }} InitPublication */
|
|
28
|
-
|
|
29
|
-
/** @typedef {{ central: Amount, secondary: Amount }} NotificationState */
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @typedef {Readonly<{
|
|
33
|
-
* poolSeat: ZCFSeat,
|
|
34
|
-
* secondaryBrand: Brand<'nat'>,
|
|
35
|
-
* liquidityZcfMint: ZCFMint,
|
|
36
|
-
* }>} ImmutableState
|
|
37
|
-
*/
|
|
38
|
-
|
|
39
|
-
/** @typedef {{liqTokenSupply: bigint}} MutableState */
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @typedef {{
|
|
43
|
-
* addLiquidityInternal: AddLiquidityInternal,
|
|
44
|
-
* addLiquidityActual: AddLiquidityActual,
|
|
45
|
-
* updateMetrics: () => void,
|
|
46
|
-
* }} HelperFacet
|
|
47
|
-
*/
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @typedef {{
|
|
51
|
-
* state: ImmutableState & MutableState,
|
|
52
|
-
* facets: {
|
|
53
|
-
* helper: HelperFacet,
|
|
54
|
-
* pool: XYKPool,
|
|
55
|
-
* singlePool: VirtualPool,
|
|
56
|
-
* },
|
|
57
|
-
* }} MethodContext
|
|
58
|
-
*
|
|
59
|
-
* @typedef {object} PoolMetricsNotification
|
|
60
|
-
* @property {Amount} centralAmount
|
|
61
|
-
* @property {Amount} secondaryAmount
|
|
62
|
-
* @property {Amount} liquidityTokens - outstanding tokens
|
|
63
|
-
*/
|
|
64
|
-
|
|
65
|
-
/** @typedef {import('@agoric/vat-data').Baggage} Baggage */
|
|
66
|
-
/** @typedef {import('./multipoolMarketMaker.js').AmmPowers} AmmPowers */
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* @param {Baggage} baggage
|
|
70
|
-
* @param {AmmPowers} ammPowers
|
|
71
|
-
* @param {ERef<StorageNode>} storageNode
|
|
72
|
-
* @param {ERef<Marshaller>} marshaller
|
|
73
|
-
*/
|
|
74
|
-
export const definePoolKind = (baggage, ammPowers, storageNode, marshaller) => {
|
|
75
|
-
/** @type {StoredPublishKit<NotificationState>} */
|
|
76
|
-
const { subscriber, publisher } = makeStoredPublishKit(
|
|
77
|
-
storageNode,
|
|
78
|
-
marshaller,
|
|
79
|
-
);
|
|
80
|
-
/** @type {import('../contractSupport.js').MetricsPublisherKit<PoolMetricsNotification>} */
|
|
81
|
-
const { metricsPublication, metricsSubscription } = makeMetricsPublisherKit(
|
|
82
|
-
storageNode,
|
|
83
|
-
marshaller,
|
|
84
|
-
);
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @param {ZCFMint<'nat'>} liquidityZcfMint
|
|
88
|
-
* @param {ZCFSeat} poolSeat
|
|
89
|
-
* @param {Brand} secondaryBrand
|
|
90
|
-
* @returns {ImmutableState & MutableState}
|
|
91
|
-
*/
|
|
92
|
-
const poolInit = (liquidityZcfMint, poolSeat, secondaryBrand) => {
|
|
93
|
-
/** @type {StoredPublishKit<InitPublication>} */
|
|
94
|
-
const { publisher: initPublisher } = makeStoredPublishKit(
|
|
95
|
-
E(storageNode).makeChildNode('init'),
|
|
96
|
-
marshaller,
|
|
97
|
-
);
|
|
98
|
-
initPublisher.finish({
|
|
99
|
-
liquidityIssuerRecord: liquidityZcfMint.getIssuerRecord(),
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return {
|
|
103
|
-
liqTokenSupply: 0n,
|
|
104
|
-
poolSeat,
|
|
105
|
-
secondaryBrand,
|
|
106
|
-
liquidityZcfMint,
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
/** @param {MethodContext} context */
|
|
111
|
-
const makePriceGetters = context => {
|
|
112
|
-
const getPAInputPrice = (amountIn, brandOut) => {
|
|
113
|
-
return publicPrices(
|
|
114
|
-
context.facets.singlePool.getPriceForInput(
|
|
115
|
-
amountIn,
|
|
116
|
-
AmountMath.makeEmpty(brandOut),
|
|
117
|
-
),
|
|
118
|
-
);
|
|
119
|
-
};
|
|
120
|
-
const getPAOutputPrice = (brandIn, amountout) =>
|
|
121
|
-
publicPrices(
|
|
122
|
-
context.facets.singlePool.getPriceForOutput(
|
|
123
|
-
AmountMath.makeEmpty(brandIn),
|
|
124
|
-
amountout,
|
|
125
|
-
),
|
|
126
|
-
);
|
|
127
|
-
return { getPAInputPrice, getPAOutputPrice };
|
|
128
|
-
};
|
|
129
|
-
|
|
130
|
-
let toCentralPriceAuthority;
|
|
131
|
-
const provideToCentralPriceAuthority = context => {
|
|
132
|
-
if (!toCentralPriceAuthority) {
|
|
133
|
-
const { getPAInputPrice, getPAOutputPrice } = makePriceGetters(context);
|
|
134
|
-
const { secondaryBrand } = context.state;
|
|
135
|
-
const { quoteIssuerKit } = ammPowers;
|
|
136
|
-
toCentralPriceAuthority = makePriceAuthority(
|
|
137
|
-
getPAInputPrice,
|
|
138
|
-
getPAOutputPrice,
|
|
139
|
-
secondaryBrand,
|
|
140
|
-
ammPowers.centralBrand,
|
|
141
|
-
ammPowers.timer,
|
|
142
|
-
subscriber,
|
|
143
|
-
quoteIssuerKit,
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
return toCentralPriceAuthority;
|
|
147
|
-
};
|
|
148
|
-
|
|
149
|
-
let fromCentralPriceAuthority;
|
|
150
|
-
const provideFromCentralPriceAuthority = context => {
|
|
151
|
-
if (!fromCentralPriceAuthority) {
|
|
152
|
-
const { getPAInputPrice, getPAOutputPrice } = makePriceGetters(context);
|
|
153
|
-
const { secondaryBrand } = context.state;
|
|
154
|
-
const quoteIssuerKit = ammPowers.quoteIssuerKit;
|
|
155
|
-
fromCentralPriceAuthority = makePriceAuthority(
|
|
156
|
-
getPAInputPrice,
|
|
157
|
-
getPAOutputPrice,
|
|
158
|
-
ammPowers.centralBrand,
|
|
159
|
-
secondaryBrand,
|
|
160
|
-
ammPowers.timer,
|
|
161
|
-
subscriber,
|
|
162
|
-
quoteIssuerKit,
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
return fromCentralPriceAuthority;
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
const poolBehavior = {
|
|
169
|
-
getLiquiditySupply: ({ state: { liqTokenSupply } }) => liqTokenSupply,
|
|
170
|
-
getLiquidityIssuer: ({ state: { liquidityZcfMint } }) =>
|
|
171
|
-
liquidityZcfMint.getIssuerRecord().issuer,
|
|
172
|
-
getPoolSeat: ({ state: { poolSeat } }) => poolSeat,
|
|
173
|
-
getCentralAmount: ({ state: { poolSeat } }) =>
|
|
174
|
-
poolSeat.getAmountAllocated('Central', ammPowers.centralBrand),
|
|
175
|
-
getSecondaryAmount: ({ state }) =>
|
|
176
|
-
state.poolSeat.getAmountAllocated('Secondary', state.secondaryBrand),
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* @param {MethodContext} context
|
|
180
|
-
* @param {ZCFSeat} zcfSeat
|
|
181
|
-
* @returns {string}
|
|
182
|
-
*/
|
|
183
|
-
addLiquidity: ({ state, facets }, zcfSeat) => {
|
|
184
|
-
const { helper, pool } = facets;
|
|
185
|
-
const centralIn = zcfSeat.getStagedAllocation().Central;
|
|
186
|
-
assert(isNatValue(centralIn.value), 'User Central');
|
|
187
|
-
const secondaryIn = zcfSeat.getStagedAllocation().Secondary;
|
|
188
|
-
assert(isNatValue(secondaryIn.value), 'User Secondary');
|
|
189
|
-
|
|
190
|
-
if (state.liqTokenSupply === 0n) {
|
|
191
|
-
return helper.addLiquidityActual(pool, zcfSeat, secondaryIn, centralIn);
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
const centralPoolAmount = pool.getCentralAmount();
|
|
195
|
-
const secondaryPoolAmount = pool.getSecondaryAmount();
|
|
196
|
-
assert(isNatValue(centralPoolAmount.value), 'Pool Central');
|
|
197
|
-
assert(isNatValue(secondaryPoolAmount.value), 'Pool Secondary');
|
|
198
|
-
|
|
199
|
-
// To calculate liquidity, we'll need to calculate alpha from the primary
|
|
200
|
-
// token's value before, and the value that will be added to the pool
|
|
201
|
-
const secondaryRequired = AmountMath.make(
|
|
202
|
-
state.secondaryBrand,
|
|
203
|
-
calcSecondaryRequired(
|
|
204
|
-
centralIn.value,
|
|
205
|
-
centralPoolAmount.value,
|
|
206
|
-
secondaryPoolAmount.value,
|
|
207
|
-
secondaryIn.value,
|
|
208
|
-
),
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
// Central was specified precisely so offer must provide enough secondary.
|
|
212
|
-
assert(
|
|
213
|
-
AmountMath.isGTE(secondaryIn, secondaryRequired),
|
|
214
|
-
'insufficient Secondary deposited',
|
|
215
|
-
);
|
|
216
|
-
|
|
217
|
-
return helper.addLiquidityActual(
|
|
218
|
-
pool,
|
|
219
|
-
zcfSeat,
|
|
220
|
-
secondaryRequired,
|
|
221
|
-
centralPoolAmount,
|
|
222
|
-
);
|
|
223
|
-
},
|
|
224
|
-
removeLiquidity: ({ state, facets }, userSeat) => {
|
|
225
|
-
const { liquidityZcfMint, poolSeat, secondaryBrand } = state;
|
|
226
|
-
const liquidityBrand = liquidityZcfMint.getIssuerRecord().brand;
|
|
227
|
-
const liquidityIn = userSeat.getAmountAllocated(
|
|
228
|
-
'Liquidity',
|
|
229
|
-
liquidityBrand,
|
|
230
|
-
);
|
|
231
|
-
if (AmountMath.isEmpty(liquidityIn)) {
|
|
232
|
-
// prevent divide-by-zero. If the caller has tokens, the pool is not empty
|
|
233
|
-
userSeat.exit();
|
|
234
|
-
return 'request to remove zero liquidity';
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
const liquidityValueIn = liquidityIn.value;
|
|
238
|
-
assert(isNatValue(liquidityValueIn), 'User Liquidity');
|
|
239
|
-
const centralTokenAmountOut = AmountMath.make(
|
|
240
|
-
ammPowers.centralBrand,
|
|
241
|
-
calcValueToRemove(
|
|
242
|
-
state.liqTokenSupply,
|
|
243
|
-
facets.pool.getCentralAmount().value,
|
|
244
|
-
liquidityValueIn,
|
|
245
|
-
),
|
|
246
|
-
);
|
|
247
|
-
|
|
248
|
-
const tokenKeywordAmountOut = AmountMath.make(
|
|
249
|
-
secondaryBrand,
|
|
250
|
-
calcValueToRemove(
|
|
251
|
-
state.liqTokenSupply,
|
|
252
|
-
facets.pool.getSecondaryAmount().value,
|
|
253
|
-
liquidityValueIn,
|
|
254
|
-
),
|
|
255
|
-
);
|
|
256
|
-
|
|
257
|
-
poolSeat.incrementBy(
|
|
258
|
-
userSeat.decrementBy(harden({ Liquidity: liquidityIn })),
|
|
259
|
-
);
|
|
260
|
-
userSeat.incrementBy(
|
|
261
|
-
poolSeat.decrementBy(
|
|
262
|
-
harden({
|
|
263
|
-
Central: centralTokenAmountOut,
|
|
264
|
-
Secondary: tokenKeywordAmountOut,
|
|
265
|
-
}),
|
|
266
|
-
),
|
|
267
|
-
);
|
|
268
|
-
ammPowers.zcf.reallocate(userSeat, poolSeat);
|
|
269
|
-
state.liqTokenSupply -= liquidityValueIn;
|
|
270
|
-
|
|
271
|
-
userSeat.exit();
|
|
272
|
-
facets.pool.updateState();
|
|
273
|
-
return 'Liquidity successfully removed.';
|
|
274
|
-
},
|
|
275
|
-
getSubscriber: () => subscriber,
|
|
276
|
-
/**
|
|
277
|
-
* Update metrics and primary state
|
|
278
|
-
*
|
|
279
|
-
* @param {MethodContext} context
|
|
280
|
-
*/
|
|
281
|
-
updateState: ({ facets: { pool, helper } }) => {
|
|
282
|
-
helper.updateMetrics();
|
|
283
|
-
publisher.publish({
|
|
284
|
-
central: pool.getCentralAmount(),
|
|
285
|
-
secondary: pool.getSecondaryAmount(),
|
|
286
|
-
});
|
|
287
|
-
},
|
|
288
|
-
|
|
289
|
-
/** @deprecated priceAuthorities will not be provided by the AMM */
|
|
290
|
-
getToCentralPriceAuthority: provideToCentralPriceAuthority,
|
|
291
|
-
/** @deprecated priceAuthorities will not be provided by the AMM */
|
|
292
|
-
getFromCentralPriceAuthority: provideFromCentralPriceAuthority,
|
|
293
|
-
|
|
294
|
-
/** @param {MethodContext} context */
|
|
295
|
-
getVPool: ({ facets }) => facets.singlePool,
|
|
296
|
-
getMetrics: () => metricsSubscription,
|
|
297
|
-
};
|
|
298
|
-
|
|
299
|
-
/** @param {MethodContext} context */
|
|
300
|
-
const finish = context => {
|
|
301
|
-
context.facets.helper.updateMetrics();
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
const helperBehavior = {
|
|
305
|
-
/** @type {import('@agoric/vat-data/src/types').PlusContext<MethodContext, AddLiquidityInternal>} */
|
|
306
|
-
addLiquidityInternal: (
|
|
307
|
-
{ state },
|
|
308
|
-
zcfSeat,
|
|
309
|
-
secondaryAmount,
|
|
310
|
-
poolCentralAmount,
|
|
311
|
-
feeSeat,
|
|
312
|
-
) => {
|
|
313
|
-
const { poolSeat, liquidityZcfMint } = state;
|
|
314
|
-
// addLiquidity can't be called until the pool has been created. We verify
|
|
315
|
-
// that the asset is NAT before creating a pool.
|
|
316
|
-
|
|
317
|
-
const liquidityValueOut = calcLiqValueToMint(
|
|
318
|
-
state.liqTokenSupply,
|
|
319
|
-
// @ts-expect-error value could be not Nat
|
|
320
|
-
zcfSeat.getStagedAllocation().Central.value,
|
|
321
|
-
poolCentralAmount.value,
|
|
322
|
-
);
|
|
323
|
-
|
|
324
|
-
const liquidityAmountOut = AmountMath.make(
|
|
325
|
-
liquidityZcfMint.getIssuerRecord().brand,
|
|
326
|
-
liquidityValueOut,
|
|
327
|
-
);
|
|
328
|
-
liquidityZcfMint.mintGains(
|
|
329
|
-
harden({ Liquidity: liquidityAmountOut }),
|
|
330
|
-
poolSeat,
|
|
331
|
-
);
|
|
332
|
-
state.liqTokenSupply += liquidityValueOut;
|
|
333
|
-
|
|
334
|
-
poolSeat.incrementBy(
|
|
335
|
-
zcfSeat.decrementBy(
|
|
336
|
-
harden({
|
|
337
|
-
Central: zcfSeat.getStagedAllocation().Central,
|
|
338
|
-
Secondary: secondaryAmount,
|
|
339
|
-
}),
|
|
340
|
-
),
|
|
341
|
-
);
|
|
342
|
-
|
|
343
|
-
zcfSeat.incrementBy(
|
|
344
|
-
poolSeat.decrementBy(harden({ Liquidity: liquidityAmountOut })),
|
|
345
|
-
);
|
|
346
|
-
if (feeSeat) {
|
|
347
|
-
ammPowers.zcf.reallocate(poolSeat, zcfSeat, feeSeat);
|
|
348
|
-
} else {
|
|
349
|
-
ammPowers.zcf.reallocate(poolSeat, zcfSeat);
|
|
350
|
-
}
|
|
351
|
-
},
|
|
352
|
-
/** @type {import('@agoric/vat-data/src/types').PlusContext<MethodContext, AddLiquidityActual>} */
|
|
353
|
-
addLiquidityActual: (
|
|
354
|
-
{ facets },
|
|
355
|
-
pool,
|
|
356
|
-
zcfSeat,
|
|
357
|
-
secondaryAmount,
|
|
358
|
-
poolCentralAmount,
|
|
359
|
-
feeSeat,
|
|
360
|
-
) => {
|
|
361
|
-
const { helper } = facets;
|
|
362
|
-
|
|
363
|
-
helper.addLiquidityInternal(
|
|
364
|
-
zcfSeat,
|
|
365
|
-
secondaryAmount,
|
|
366
|
-
poolCentralAmount,
|
|
367
|
-
feeSeat,
|
|
368
|
-
);
|
|
369
|
-
zcfSeat.exit();
|
|
370
|
-
facets.pool.updateState();
|
|
371
|
-
return 'Added liquidity.';
|
|
372
|
-
},
|
|
373
|
-
/** @param {MethodContext} context */
|
|
374
|
-
updateMetrics: context => {
|
|
375
|
-
const { state, facets } = context;
|
|
376
|
-
const payload = harden({
|
|
377
|
-
centralAmount: facets.pool.getCentralAmount(),
|
|
378
|
-
secondaryAmount: facets.pool.getSecondaryAmount(),
|
|
379
|
-
liquidityTokens: AmountMath.make(
|
|
380
|
-
state.liquidityZcfMint.getIssuerRecord().brand,
|
|
381
|
-
state.liqTokenSupply,
|
|
382
|
-
),
|
|
383
|
-
});
|
|
384
|
-
|
|
385
|
-
metricsPublication.updateState(payload);
|
|
386
|
-
},
|
|
387
|
-
};
|
|
388
|
-
|
|
389
|
-
const facets = harden({
|
|
390
|
-
helper: helperBehavior,
|
|
391
|
-
pool: poolBehavior,
|
|
392
|
-
singlePool: makeSinglePool(ammPowers),
|
|
393
|
-
});
|
|
394
|
-
|
|
395
|
-
return vivifyKindMulti(baggage, 'pool', poolInit, facets, { finish });
|
|
396
|
-
};
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import { AmountMath } from '@agoric/ertp';
|
|
4
|
-
import {
|
|
5
|
-
makeNotifierFromSubscriber,
|
|
6
|
-
observeIteration,
|
|
7
|
-
subscribeLatest,
|
|
8
|
-
} from '@agoric/notifier';
|
|
9
|
-
import { E } from '@endo/eventual-send';
|
|
10
|
-
import { Far } from '@endo/marshal';
|
|
11
|
-
|
|
12
|
-
import { makeOnewayPriceAuthorityKit } from '@agoric/zoe/src/contractSupport/index.js';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
*
|
|
16
|
-
* @param {*} getOutputForGivenInput
|
|
17
|
-
* @param {*} getInputForWantedOutput
|
|
18
|
-
* @param {Brand} actualBrandIn
|
|
19
|
-
* @param {Brand} actualBrandOut
|
|
20
|
-
* @param {TimerService} timer
|
|
21
|
-
* @param {Subscriber<import('./pool').NotificationState>} subscriber
|
|
22
|
-
* @param {IssuerKit} quoteIssuerKit
|
|
23
|
-
* @returns {PriceAuthority}
|
|
24
|
-
*/
|
|
25
|
-
export const makePriceAuthority = (
|
|
26
|
-
getOutputForGivenInput,
|
|
27
|
-
getInputForWantedOutput,
|
|
28
|
-
actualBrandIn,
|
|
29
|
-
actualBrandOut,
|
|
30
|
-
timer,
|
|
31
|
-
subscriber,
|
|
32
|
-
quoteIssuerKit,
|
|
33
|
-
) => {
|
|
34
|
-
const { brand, issuer: quoteIssuer } = quoteIssuerKit;
|
|
35
|
-
|
|
36
|
-
/** @param {PriceQuoteValue} quote */
|
|
37
|
-
const authenticateQuote = quote => {
|
|
38
|
-
const quoteAmount = AmountMath.make(brand, harden(quote));
|
|
39
|
-
const quotePayment = quoteIssuerKit.mint.mintPayment(quoteAmount);
|
|
40
|
-
return harden({ quoteAmount, quotePayment });
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
// called by createQuote for each trigger to compare price to trigger level
|
|
44
|
-
const calcAmountOut = amountIn =>
|
|
45
|
-
getOutputForGivenInput(amountIn, actualBrandOut).amountOut;
|
|
46
|
-
const calcAmountIn = amountOut =>
|
|
47
|
-
getInputForWantedOutput(actualBrandIn, amountOut).amountIn;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @param {PriceQuery} priceQuery
|
|
51
|
-
* @returns {ERef<PriceQuote>=}
|
|
52
|
-
*/
|
|
53
|
-
function createQuote(priceQuery) {
|
|
54
|
-
const quote = priceQuery(calcAmountOut, calcAmountIn);
|
|
55
|
-
if (!quote) {
|
|
56
|
-
return undefined;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const { amountIn, amountOut } = quote;
|
|
60
|
-
return E(timer)
|
|
61
|
-
.getCurrentTimestamp()
|
|
62
|
-
.then(now =>
|
|
63
|
-
authenticateQuote([{ amountIn, amountOut, timer, timestamp: now }]),
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const priceAuthorityOptions = harden({
|
|
68
|
-
timer,
|
|
69
|
-
createQuote,
|
|
70
|
-
actualBrandIn,
|
|
71
|
-
actualBrandOut,
|
|
72
|
-
quoteIssuer,
|
|
73
|
-
notifier: makeNotifierFromSubscriber(subscriber),
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
const {
|
|
77
|
-
priceAuthority,
|
|
78
|
-
adminFacet: { fireTriggers },
|
|
79
|
-
} = makeOnewayPriceAuthorityKit(priceAuthorityOptions);
|
|
80
|
-
|
|
81
|
-
// An observer on a notifier that gets updated when the pool's balances
|
|
82
|
-
// change via swap or liquidity operations. It checks the comparison predicate
|
|
83
|
-
// for each active trigger. Due to `subscribeLatest` below it may drop
|
|
84
|
-
// intermediate values.
|
|
85
|
-
const priceObserver = Far('priceObserver', {
|
|
86
|
-
updateState: () => {
|
|
87
|
-
void fireTriggers(createQuote);
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
void observeIteration(subscribeLatest(subscriber), priceObserver);
|
|
92
|
-
|
|
93
|
-
return priceAuthority;
|
|
94
|
-
};
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import { assertProposalShape } from '@agoric/zoe/src/contractSupport/index.js';
|
|
4
|
-
|
|
5
|
-
import '@agoric/zoe/exported.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* @param {ZCF} zcf
|
|
9
|
-
* @param {(brand: Brand) => XYKPool} getPool
|
|
10
|
-
*/
|
|
11
|
-
export const makeMakeRemoveLiquidityInvitation = (zcf, getPool) => {
|
|
12
|
-
const removeLiquidity = seat => {
|
|
13
|
-
assertProposalShape(seat, {
|
|
14
|
-
want: {
|
|
15
|
-
Central: null,
|
|
16
|
-
Secondary: null,
|
|
17
|
-
},
|
|
18
|
-
give: {
|
|
19
|
-
Liquidity: null,
|
|
20
|
-
},
|
|
21
|
-
});
|
|
22
|
-
// Get the brand of the secondary token so we can identify the liquidity pool.
|
|
23
|
-
const secondaryBrand = seat.getProposal().want.Secondary.brand;
|
|
24
|
-
const pool = getPool(secondaryBrand);
|
|
25
|
-
return pool.removeLiquidity(seat);
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
const makeRemoveLiquidityInvitation = () =>
|
|
29
|
-
zcf.makeInvitation(removeLiquidity, 'autoswap remove liquidity');
|
|
30
|
-
return makeRemoveLiquidityInvitation;
|
|
31
|
-
};
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import { makeFeeRatio } from './constantProduct/calcFees.js';
|
|
4
|
-
import {
|
|
5
|
-
pricesForStatedInput,
|
|
6
|
-
pricesForStatedOutput,
|
|
7
|
-
} from './constantProduct/calcSwapPrices.js';
|
|
8
|
-
|
|
9
|
-
const getSecondaryBrand = pool => pool.getSecondaryAmount().brand;
|
|
10
|
-
const getCentralBrand = pool => pool.getCentralAmount().brand;
|
|
11
|
-
|
|
12
|
-
const getPools = pool => ({
|
|
13
|
-
Central: pool.getCentralAmount(),
|
|
14
|
-
Secondary: pool.getSecondaryAmount(),
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
export const makeSinglePool = ammPowers => ({
|
|
18
|
-
allocateGainsAndLosses: (context, seat, prices) => {
|
|
19
|
-
const { pool } = context.facets;
|
|
20
|
-
const { poolSeat } = context.state;
|
|
21
|
-
const { zcf, protocolSeat } = ammPowers;
|
|
22
|
-
seat.decrementBy(harden({ In: prices.swapperGives }));
|
|
23
|
-
seat.incrementBy(harden({ Out: prices.swapperGets }));
|
|
24
|
-
protocolSeat.incrementBy(harden({ Fee: prices.protocolFee }));
|
|
25
|
-
|
|
26
|
-
const inBrand = prices.swapperGives.brand;
|
|
27
|
-
if (inBrand === getSecondaryBrand(pool)) {
|
|
28
|
-
poolSeat.decrementBy(harden({ Central: prices.yDecrement }));
|
|
29
|
-
poolSeat.incrementBy(harden({ Secondary: prices.xIncrement }));
|
|
30
|
-
} else {
|
|
31
|
-
poolSeat.decrementBy(harden({ Secondary: prices.yDecrement }));
|
|
32
|
-
poolSeat.incrementBy(harden({ Central: prices.xIncrement }));
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
zcf.reallocate(poolSeat, seat, protocolSeat);
|
|
36
|
-
seat.exit();
|
|
37
|
-
pool.updateState();
|
|
38
|
-
return `Swap successfully completed.`;
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* @param {import('./pool').MethodContext} context
|
|
43
|
-
* @param {Amount} amountIn
|
|
44
|
-
* @param {Amount} amountOut
|
|
45
|
-
*/
|
|
46
|
-
getPriceForInput: ({ facets }, amountIn, amountOut) => {
|
|
47
|
-
const { params } = ammPowers;
|
|
48
|
-
return pricesForStatedInput(
|
|
49
|
-
amountIn,
|
|
50
|
-
getPools(facets.pool),
|
|
51
|
-
amountOut,
|
|
52
|
-
makeFeeRatio(params.getProtocolFee(), getCentralBrand(facets.pool)),
|
|
53
|
-
makeFeeRatio(params.getPoolFee(), amountOut.brand),
|
|
54
|
-
);
|
|
55
|
-
},
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* @param {import('./pool').MethodContext} context
|
|
59
|
-
* @param {Amount} amountIn
|
|
60
|
-
* @param {Amount} amountOut
|
|
61
|
-
*/
|
|
62
|
-
getPriceForOutput: ({ facets }, amountIn, amountOut) => {
|
|
63
|
-
const { params } = ammPowers;
|
|
64
|
-
return pricesForStatedOutput(
|
|
65
|
-
amountIn,
|
|
66
|
-
getPools(facets.pool),
|
|
67
|
-
amountOut,
|
|
68
|
-
makeFeeRatio(params.getProtocolFee(), getCentralBrand(facets.pool)),
|
|
69
|
-
makeFeeRatio(params.getPoolFee(), amountIn.brand),
|
|
70
|
-
);
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
harden(makeSinglePool);
|