/
multipoolMarketMaker.js
519 lines (475 loc) · 16.8 KB
/
multipoolMarketMaker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// @ts-check
import '@agoric/zoe/exported.js';
import { AssetKind, makeIssuerKit } from '@agoric/ertp';
import { handleParamGovernance, ParamTypes } from '@agoric/governance';
import {
makeAtomicProvider,
provide,
} from '@agoric/store/src/stores/store-utils.js';
import {
assertIssuerKeywords,
offerTo,
} from '@agoric/zoe/src/contractSupport/index.js';
import { E } from '@endo/far';
import { Far } from '@endo/marshal';
import {
provideDurableMapStore,
provideDurableWeakMapStore,
vivifyKindMulti,
} from '@agoric/vat-data';
import { makeMakeCollectFeesInvitation } from '../collectFees.js';
import { makeMetricsPublisherKit } from '../contractSupport.js';
import { makeTracer } from '../makeTracer.js';
import {
makeMakeAddLiquidityAtRateInvitation,
makeMakeAddLiquidityInvitation,
} from './addLiquidity.js';
import { makeAddIssuer, makeAddPoolInvitation } from './addPool.js';
import { makeDoublePool } from './doublePool.js';
import {
MIN_INITIAL_POOL_LIQUIDITY_KEY,
POOL_FEE_KEY,
PROTOCOL_FEE_KEY,
} from './params.js';
import { publicPrices } from './pool.js';
import { makeMakeRemoveLiquidityInvitation } from './removeLiquidity.js';
import { makeMakeSwapInvitation } from './swap.js';
import '@agoric/governance/src/exported.js';
const { quote: q, details: X } = assert;
const trace = makeTracer('XykAmm', false);
/**
* @typedef {object} MetricsNotification
* @property {Brand[]} XYK brands of pools that use an X*Y=K pricing policy
*/
/** @typedef {GovernanceTerms< {MinInitialPoolLiquidity: 'amount'} & {ProtocolFee: 'amount'} & {PoolFee: 'amount'}>} AmmGovernanceParams */
/** @typedef {{brands:BrandKeywordRecord,timer:TimerService}} AmmTerms */
/**
* @typedef {Readonly<{
* zcf: ZCF<AmmTerms>,
* secondaryBrandToPool: WeakMapStore<Brand,PoolFacets>,
* secondaryBrandToLiquidityMint: WeakMapStore<Brand,ZCFMint>,
* centralBrand: Brand,
* timer: TimerService,
* quoteIssuerKit: IssuerKit,
* params: import('@agoric/governance/src/contractGovernance/typedParamManager').Getters<import('./params.js').AmmParams>,
* protocolSeat: ZCFSeat,
* }>} AmmPowers
*/
// All the state at the AMM level is shared lexically. Some functions here have
// to ignore their first parameter since they have other parameters and are
// included in virtual/durable facets.
/** @typedef {{}} MethodContext */
/** @typedef {import('@agoric/vat-data').Baggage} Baggage */
/**
* Multipool AMM is a rewrite of Uniswap that supports multiple liquidity pools,
* and direct exchanges across pools. Please see the documentation for more:
* https://agoric.com/documentation/zoe/guide/contracts/multipoolAMM.html It
* also uses a unique approach to charging fees. Each pool grows on every trade,
* and a protocolFee is extracted.
*
* We expect that this contract will have tens to hundreds of issuers. Each
* liquidity pool is between the central token and a secondary token. Secondary
* tokens can be exchanged with each other, but only through the central
* token. For example, if X and Y are two token types and C is the central
* token, a swap giving X and wanting Y would first use the pool (X, C) then the
* pool (Y, C). There are no liquidity pools directly between two secondary
* tokens.
*
* There should only need to be one instance of this contract, so liquidity can
* be shared as much as possible.
*
* When the contract is instantiated, the central token is specified in the
* terms. Separate invitations are available by calling methods on the
* publicFacet for adding and removing liquidity and for making trades. Other
* publicFacet operations support querying prices and the sizes of pools. New
* Pools can be created by first calling addIssuer() and then exercising an
* invitation from addPoolInvitation().
*
* When making trades or requesting prices, the caller must specify either a
* maximum input amount (swapIn, getInputPrice) or a minimum output amount
* (swapOut, getOutPutPrice) or both. For swaps, the required keywords are `In`
* for the trader's `give` amount, and `Out` for the trader's `want` amount.
* getInputPrice and getOutputPrice each take two Amounts. The price functions
* return both amountIn (which may be lower than the original amount) and
* amountOut (which may be higher). When both prices are specified, no swap will
* be made (and no price provided) if both restrictions can't be honored.
*
* When adding and removing liquidity, the keywords are Central, Secondary, and
* Liquidity. adding liquidity has Central and Secondary in the `give` section,
* while removing liquidity has `want` and `give` swapped.
*
* Transactions that don't require an invitation include addPool and the
* queries: getInputPrice, getOutputPrice, getPoolAllocation,
* getLiquidityIssuer, and getLiquiditySupply.
*
* This contract has two parameters (poolFee and protocolFee) that are
* managed by governance. The contract calls `handleParamGovernance()` at
* startup, which allows a contractManager to call for votes that would change
* the parameter values in a transparent way. When correctly set up, customers
* with access to this contract's publicFacet can verify the connectivity, and
* see which Electorate has the ability to vote on changes, and which votes are
* ongoing or have taken place. If not correctly set up, the validation checks
* will fail visibly.
*
* The initial values of the parameters are provided as poolFeeBP and
* protocolFeeBP in terms. The poolFee is charged in {Central} and each collateral, so
* it is provided as a bigint. The protocolFee is always charged in {Fee}, but the
* initial value is specified as a bigint for consistency.
*
* The contract gets the initial values for those parameters from its terms, and
* thereafter can be seen to only use the values provided by the
* getter method returned by the paramManager.
*
* `handleParamGovernance()` adds several methods to the publicFacet of the
* contract, and bundles the privateFacet to ensure that governance
* functionality is only accessible to the contractGovernor.
*
* The creatorFacet has one method (makeCollectFeesInvitation, which returns
* collected fees to the creator). `handleParamGovernance()` adds internal
* methods used by the contractGovernor. The contractGovernor then has access to
* those internal methods, and reveals the original AMM creatorFacet to its own
* creator.
*
* @param {ZCF<AMMTerms>} zcf
* @param {{
* initialPoserInvitation: Invitation,
* storageNode: ERef<StorageNode>,
* marshaller: ERef<Marshaller>,
* }} privateArgs
* @param {Baggage} baggage
*/
const start = async (zcf, privateArgs, baggage) => {
/**
* This contract must have a "Central" keyword and issuer in the
* IssuerKeywordRecord.
*/
const {
brands: { Central: centralBrand },
timer,
} = zcf.getTerms();
assertIssuerKeywords(zcf, ['Central']);
assert(centralBrand !== undefined, 'centralBrand must be present');
const [
{ augmentVirtualPublicFacet, makeVirtualGovernorFacet, params },
centralDisplayInfo,
] = await Promise.all([
handleParamGovernance(
zcf,
privateArgs.initialPoserInvitation,
{
[POOL_FEE_KEY]: ParamTypes.NAT,
[PROTOCOL_FEE_KEY]: ParamTypes.NAT,
[MIN_INITIAL_POOL_LIQUIDITY_KEY]: ParamTypes.AMOUNT,
},
privateArgs.storageNode,
privateArgs.marshaller,
),
E(centralBrand).getDisplayInfo(),
privateArgs.storageNode,
privateArgs.marshaller,
]);
assert.equal(
centralDisplayInfo.assetKind,
AssetKind.NAT,
X`Central must be of kind ${q(AssetKind.NAT)}, not ${q(
centralDisplayInfo.assetKind,
)}`,
);
/** @type {Store<Brand,PoolFacets>} */
const secondaryBrandToPool = provideDurableMapStore(
baggage,
'secondaryBrandToPool',
);
const getPool = brand => {
// TODO: https://github.com/Agoric/agoric-sdk/issues/5776
assert(
secondaryBrandToPool.has(brand),
`"secondaryBrandToPool" not found: ${q(brand)}`,
);
return secondaryBrandToPool.get(brand).pool;
};
const getPoolHelper = brand => secondaryBrandToPool.get(brand).helper;
// XXX a brand can be in secondaryBrandToLiquidityMint and return false for this check
// if it's called between addIssuer and addPool
const isSecondary = secondaryBrandToPool.has;
// The liquidityBrand has to exist to allow the addPool Offer to specify want
/** @type {WeakMapStore<Brand,ZCFMint<'nat'>>} */
const secondaryBrandToLiquidityMint = provideDurableWeakMapStore(
baggage,
'secondaryBrandToLiquidityMint',
);
const secondaryBrandToLiquidityMintProvider = makeAtomicProvider(
secondaryBrandToLiquidityMint,
);
const quoteIssuerKit = provide(baggage, 'quoteIssuerKit', () =>
makeIssuerKit('Quote', AssetKind.SET),
);
const { metricsPublication, metricsSubscription } = makeMetricsPublisherKit(
privateArgs.storageNode,
privateArgs.marshaller,
);
const updateMetrics = () => {
metricsPublication.updateState(
harden({ XYK: Array.from(secondaryBrandToPool.keys()) }),
);
};
updateMetrics();
// For now, this seat collects protocol fees. It needs to be connected to
// something that will extract the fees.
const protocolSeat = provide(
baggage,
'protocolSeat',
() => zcf.makeEmptySeatKit().zcfSeat,
);
/** @type {AssetReservePublicFacet=} */
let reserveFacet = baggage.has('reserveFacet')
? baggage.get('reserveFacet')
: undefined;
/**
* @param {Brand} secondaryBrand
* @param {ZCFSeat} reserveLiquidityTokenSeat
* @param {Keyword} liquidityKeyword
* @returns {Promise<void>} up to caller whether to await or handle rejections
*/
const handlePoolAdded = async (
secondaryBrand,
reserveLiquidityTokenSeat,
liquidityKeyword,
) => {
trace('handlePoolAdded', { secondaryBrand, liquidityKeyword });
updateMetrics();
assert(reserveFacet, 'Missing reserveFacet');
assert(reserveLiquidityTokenSeat, 'Missing reserveLiquidityTokenSeat');
const secondaryIssuer = await zcf.getIssuerForBrand(secondaryBrand);
trace('ensuring reserve has the liquidity issuer', {
secondaryBrand,
secondaryIssuer,
});
await E(reserveFacet).addLiquidityIssuer(secondaryIssuer);
trace(
`move ${liquidityKeyword} to the reserve`,
reserveLiquidityTokenSeat.getCurrentAllocation(),
);
const addCollateral = await E(reserveFacet).makeAddCollateralInvitation();
const proposal = harden({
give: {
Collateral:
reserveLiquidityTokenSeat.getCurrentAllocation()[liquidityKeyword],
},
});
const { deposited, userSeatPromise } = await offerTo(
zcf,
addCollateral,
harden({ [liquidityKeyword]: 'Collateral' }),
proposal,
reserveLiquidityTokenSeat,
);
const deposits = await deposited;
trace('handlePoolAdded deposited', deposits);
await E(userSeatPromise).getOfferResult();
reserveLiquidityTokenSeat.exit();
trace('handlePoolAdded done');
};
/**
* @param {MethodContext} _context
* @param {Brand} brand
*/
const getLiquidityIssuer = (_context, brand) =>
secondaryBrandToLiquidityMint.get(brand).getIssuerRecord().issuer;
/**
* @param {MethodContext} _context
* @param {Brand} brand
*/
const getLiquiditySupply = (_context, brand) =>
getPool(brand).getLiquiditySupply();
/**
* @param {MethodContext} _context
* @param {Brand} brand
*/
const getSecondaryIssuer = (_context, brand) => {
assert(
secondaryBrandToLiquidityMint.has(brand),
'Brand not a secondary of the AMM',
);
return zcf.getIssuerForBrand(brand);
};
const poolStorageNode = await (privateArgs.storageNode &&
E(privateArgs.storageNode).getChildNode(
// NB: the set of pools grows monotonically
`pool${secondaryBrandToPool.getSize()}`,
));
// shared AMM state that will be lexically accessible to all pools
/** @type {AmmPowers} */
const ammPowers = harden({
zcf,
secondaryBrandToPool,
secondaryBrandToLiquidityMint,
centralBrand,
timer,
quoteIssuerKit,
params,
protocolSeat,
});
const addPoolInvitation = makeAddPoolInvitation(
baggage,
ammPowers,
handlePoolAdded,
poolStorageNode,
privateArgs.marshaller,
);
const addIssuer = makeAddIssuer(
zcf,
isSecondary,
secondaryBrandToLiquidityMintProvider,
() => {
assert(reserveFacet, 'Must first resolveReserveFacet');
return E(reserveFacet).addIssuerFromAmm;
},
);
/**
* @param {unknown} _context
* @param {Brand} brand
*/
const getPoolAllocation = (_context, brand) =>
getPool(brand).getPoolSeat().getCurrentAllocation();
const getPriceAuthorities = (_context, brand) => {
const pool = getPool(brand);
return {
toCentral: pool.getToCentralPriceAuthority(),
fromCentral: pool.getFromCentralPriceAuthority(),
};
};
/**
* @param {MethodContext} _context
* @param {Brand} brand
*/
const getPoolMetrics = (_context, brand) => getPool(brand).getMetrics();
/**
* @param {Brand} brandIn
* @param {Brand} [brandOut]
* @returns {VirtualPool}
*/
const provideVPool = (brandIn, brandOut) => {
if (!brandOut) {
return getPool(brandIn).getVPool();
}
if (isSecondary(brandIn) && isSecondary(brandOut)) {
return makeDoublePool(
zcf,
getPool(brandIn),
getPool(brandOut),
params.getProtocolFee,
params.getPoolFee,
protocolSeat,
);
}
const pool = isSecondary(brandOut) ? getPool(brandOut) : getPool(brandIn);
return pool.getVPool();
};
const getInputPrice = (_context, amountIn, amountOut) => {
const pool = provideVPool(amountIn.brand, amountOut.brand);
return publicPrices(pool.getPriceForInput(amountIn, amountOut));
};
const getOutputPrice = (_context, amountIn, amountOut) => {
const pool = provideVPool(amountIn.brand, amountOut.brand);
return publicPrices(pool.getPriceForOutput(amountIn, amountOut));
};
const { makeSwapInInvitation, makeSwapOutInvitation } =
makeMakeSwapInvitation(zcf, provideVPool);
const makeAddLiquidityInvitation = makeMakeAddLiquidityInvitation(
zcf,
getPool,
);
const makeAddLiquidityAtRateInvitation = makeMakeAddLiquidityAtRateInvitation(
zcf,
getPool,
provideVPool,
protocolSeat,
getPoolHelper,
);
const makeRemoveLiquidityInvitation = makeMakeRemoveLiquidityInvitation(
zcf,
getPool,
);
const { makeCollectFeesInvitation } = makeMakeCollectFeesInvitation(
zcf,
protocolSeat,
centralBrand,
'Fee',
);
const publicFacet = augmentVirtualPublicFacet(
Far('AMM public facet', {
addPoolInvitation,
addIssuer,
getPoolAllocation,
getLiquidityIssuer,
getLiquiditySupply,
getInputPrice,
getOutputPrice,
getSecondaryIssuer,
makeSwapInvitation: makeSwapInInvitation,
makeSwapInInvitation,
makeSwapOutInvitation,
makeAddLiquidityInvitation,
makeAddLiquidityAtRateInvitation,
makeRemoveLiquidityInvitation,
getQuoteIssuer: () => quoteIssuerKit.issuer,
getPriceAuthorities,
getAllPoolBrands: () =>
Object.values(zcf.getTerms().brands).filter(isSecondary),
getProtocolPoolBalance: () => protocolSeat.getCurrentAllocation(),
getMetrics: () => metricsSubscription,
getPoolMetrics,
...params,
}),
);
const { governorFacet, limitedCreatorFacet } = makeVirtualGovernorFacet(
// @ts-expect-error TS is confused.
Far('AMM Fee Collector facet', {
makeCollectFeesInvitation,
/**
* Must be called before adding pools. Not provided at contract start time
* due to cyclic dependency.
*
* @param {MethodContext} _context
* @param {AssetReservePublicFacet} facet
*/
resolveReserveFacet: (_context, facet) => {
assert(!reserveFacet, 'reserveFacet already resolved');
reserveFacet = facet;
baggage.init('reserveFacet', facet);
},
}),
);
const makeAMM = vivifyKindMulti(baggage, 'AMM', () => ({}), {
publicFacet,
creatorFacet: governorFacet,
limitedCreatorFacet,
});
const { creatorFacet, publicFacet: pFacet } = makeAMM();
return { creatorFacet, publicFacet: pFacet };
};
harden(start);
export { start };
/**
* @typedef {object} AMMParamGetters
* @property {() => NatValue} getPoolFee
* @property {() => NatValue} getProtocolFee
* @property {() => Amount<'nat'>} getMinInitialPoolLiquidity
* @property {() => Amount} getElectorate
*/
/**
* @typedef { bigint } BasisPoints -- hundredths of a percent
*
* @typedef {GovernanceTerms<{
* PoolFee: 'nat',
* ProtocolFee: 'nat',
* MinInitialPoolLiquidity: 'amount',
* }> & {
* brands: { Central: Brand },
* issuers: {},
* timer: TimerService,
* poolFeeBP: BasisPoints, // portion of the fees that go into the pool
* protocolFeeBP: BasisPoints, // portion of the fees that are shared with validators
* }} AMMTerms
*/