-
Notifications
You must be signed in to change notification settings - Fork 237
/
Copy pathsinglePool.js
77 lines (69 loc) · 2.3 KB
/
singlePool.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
// @ts-check
import { makeFeeRatio } from './constantProduct/calcFees.js';
import {
pricesForStatedInput,
pricesForStatedOutput,
} from './constantProduct/calcSwapPrices.js';
const getSecondaryBrand = pool => pool.getSecondaryAmount().brand;
const getCentralBrand = pool => pool.getCentralAmount().brand;
const getPools = pool => ({
Central: pool.getCentralAmount(),
Secondary: pool.getSecondaryAmount(),
});
export const singlePool = {
allocateGainsAndLosses: (context, seat, prices) => {
const { pool } = context.facets;
const { poolSeat, zcf, protocolSeat } = context.state;
seat.decrementBy(harden({ In: prices.swapperGives }));
seat.incrementBy(harden({ Out: prices.swapperGets }));
context.state.protocolSeat.incrementBy(harden({ RUN: prices.protocolFee }));
const inBrand = prices.swapperGives.brand;
if (inBrand === getSecondaryBrand(pool)) {
poolSeat.decrementBy(harden({ Central: prices.yDecrement }));
poolSeat.incrementBy(harden({ Secondary: prices.xIncrement }));
} else {
poolSeat.decrementBy(harden({ Secondary: prices.yDecrement }));
poolSeat.incrementBy(harden({ Central: prices.xIncrement }));
}
zcf.reallocate(poolSeat, seat, protocolSeat);
seat.exit();
pool.updateState();
return `Swap successfully completed.`;
},
/**
* @param {import('./pool').MethodContext} context
* @param {Amount} amountIn
* @param {Amount} amountOut
*/
getPriceForInput: ({ state, facets }, amountIn, amountOut) => {
const { paramAccessor } = state;
return pricesForStatedInput(
amountIn,
getPools(facets.pool),
amountOut,
makeFeeRatio(
paramAccessor.getProtocolFee(),
getCentralBrand(facets.pool),
),
makeFeeRatio(paramAccessor.getPoolFee(), amountOut.brand),
);
},
/**
* @param {import('./pool').MethodContext} context
* @param {Amount} amountIn
* @param {Amount} amountOut
*/
getPriceForOutput: ({ state, facets }, amountIn, amountOut) => {
const { paramAccessor } = state;
return pricesForStatedOutput(
amountIn,
getPools(facets.pool),
amountOut,
makeFeeRatio(
paramAccessor.getProtocolFee(),
getCentralBrand(facets.pool),
),
makeFeeRatio(paramAccessor.getPoolFee(), amountIn.brand),
);
},
};