@agoric/inter-protocol 0.13.3 → 0.13.4-dev-800c2bb.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,685 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
|
|
3
|
-
import { AmountMath, AssetKind } from '@agoric/ertp';
|
|
4
|
-
import { objectMap } from '@agoric/internal';
|
|
5
|
-
import {
|
|
6
|
-
makeRatio,
|
|
7
|
-
natSafeMath,
|
|
8
|
-
} from '@agoric/zoe/src/contractSupport/index.js';
|
|
9
|
-
import { E, Far } from '@endo/far';
|
|
10
|
-
import { Stake, Stable } from '@agoric/vats/src/tokens.js';
|
|
11
|
-
import { Nat } from '@endo/nat';
|
|
12
|
-
import * as Collect from '../collect.js';
|
|
13
|
-
|
|
14
|
-
const { details: X, quote: q } = assert;
|
|
15
|
-
const { multiply, floorDivide } = natSafeMath;
|
|
16
|
-
const { entries, fromEntries, keys, values } = Object;
|
|
17
|
-
|
|
18
|
-
const QUOTE_INTERVAL = 5n * 60n;
|
|
19
|
-
|
|
20
|
-
/** @type {Record<string, number>} */
|
|
21
|
-
export const DecimalPlaces = {
|
|
22
|
-
[Stake.symbol]: Stake.displayInfo.decimalPlaces,
|
|
23
|
-
[Stable.symbol]: Stable.displayInfo.decimalPlaces,
|
|
24
|
-
ATOM: 6,
|
|
25
|
-
WETH: 18,
|
|
26
|
-
LINK: 18,
|
|
27
|
-
USDC: 18,
|
|
28
|
-
moola: 2,
|
|
29
|
-
simolean: 0,
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/** @type {Record<string, { proposedName: string, balance: bigint}>} */
|
|
33
|
-
const FaucetPurseDetail = {
|
|
34
|
-
[Stake.symbol]: {
|
|
35
|
-
proposedName: Stake.proposedName,
|
|
36
|
-
balance: 5000n,
|
|
37
|
-
},
|
|
38
|
-
[Stable.symbol]: {
|
|
39
|
-
proposedName: Stable.proposedName,
|
|
40
|
-
balance: 53n,
|
|
41
|
-
},
|
|
42
|
-
LINK: { proposedName: 'Oracle fee', balance: 51n },
|
|
43
|
-
USDC: { proposedName: 'USD Coin', balance: 1_323n },
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const PCT = 100n;
|
|
47
|
-
const BASIS = 10_000n;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @param {bigint} frac
|
|
51
|
-
* @param {number} exp
|
|
52
|
-
*/
|
|
53
|
-
const pad0 = (frac, exp) =>
|
|
54
|
-
`${`${'0'.repeat(exp)}${frac}`.slice(-exp)}`.replace(/0+$/, '');
|
|
55
|
-
|
|
56
|
-
/** @param {bigint} whole */
|
|
57
|
-
const separators = whole => {
|
|
58
|
-
const sep = '_';
|
|
59
|
-
// ack: https://stackoverflow.com/a/45950572/7963, https://regex101.com/
|
|
60
|
-
const revStr = s => s.split('').reverse().join('');
|
|
61
|
-
const lohi = revStr(`${whole}`);
|
|
62
|
-
const s = lohi.replace(/(?=\d{4})(\d{3})/g, (m, p1) => `${p1}${sep}`);
|
|
63
|
-
return revStr(s);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* @param {bigint} n
|
|
68
|
-
* @param {number} exp
|
|
69
|
-
*/
|
|
70
|
-
export const decimal = (n, exp) => {
|
|
71
|
-
const unit = 10n ** BigInt(exp);
|
|
72
|
-
const [whole, frac] = [n / unit, n % unit];
|
|
73
|
-
return frac !== 0n
|
|
74
|
-
? `${separators(whole)}.${pad0(frac, exp)}`
|
|
75
|
-
: `${separators(whole)}`;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export const showBrand = b =>
|
|
79
|
-
`${b}`.replace(/.object Alleged: (.*) brand./, '$1');
|
|
80
|
-
export const showAmount = ({ brand, value }) => {
|
|
81
|
-
const b = `${showBrand(brand)}`;
|
|
82
|
-
assert(b in DecimalPlaces, X`unknown brand name: ${q(b)}`);
|
|
83
|
-
return `${decimal(value, DecimalPlaces[b])} ${b}`;
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const defaultConfig = /** @type {const} */ ({
|
|
87
|
-
collateralValue: 1_000_000n,
|
|
88
|
-
initialMargin: [150n, PCT],
|
|
89
|
-
liquidationMargin: [125n, PCT],
|
|
90
|
-
liquidationPenalty: [10n, PCT],
|
|
91
|
-
interestRate: [250n, BASIS],
|
|
92
|
-
loanFee: [1n, BASIS],
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* @typedef {readonly [bigint, bigint]} Rational
|
|
97
|
-
*
|
|
98
|
-
* @type { Record<string, {
|
|
99
|
-
* config?: {
|
|
100
|
-
* collateralValue: bigint,
|
|
101
|
-
* initialMargin: Rational,
|
|
102
|
-
* liquidationMargin: Rational,
|
|
103
|
-
* liquidationPenalty: Rational,
|
|
104
|
-
* interestRate: Rational,
|
|
105
|
-
* loanFee: Rational,
|
|
106
|
-
* },
|
|
107
|
-
* trades: Array<{ central: number, collateral: bigint}>
|
|
108
|
-
* }>}
|
|
109
|
-
*/
|
|
110
|
-
export const AMMDemoState = {
|
|
111
|
-
// TODO: getRUN makes BLD obsolete here
|
|
112
|
-
BLD: {
|
|
113
|
-
config: {
|
|
114
|
-
...defaultConfig,
|
|
115
|
-
collateralValue: 20_000_000n,
|
|
116
|
-
},
|
|
117
|
-
trades: [
|
|
118
|
-
{ central: 1.23, collateral: 1n },
|
|
119
|
-
{ central: 1.21, collateral: 1n },
|
|
120
|
-
{ central: 1.22, collateral: 1n },
|
|
121
|
-
],
|
|
122
|
-
},
|
|
123
|
-
|
|
124
|
-
/* We actually can IBC-transfer Atoms via Pegasus right now. */
|
|
125
|
-
ATOM: {
|
|
126
|
-
config: defaultConfig,
|
|
127
|
-
trades: [
|
|
128
|
-
{ central: 33.28, collateral: 1n },
|
|
129
|
-
{ central: 34.61, collateral: 1n },
|
|
130
|
-
{ central: 37.83, collateral: 1n },
|
|
131
|
-
],
|
|
132
|
-
},
|
|
133
|
-
|
|
134
|
-
WETH: {
|
|
135
|
-
config: defaultConfig,
|
|
136
|
-
trades: [
|
|
137
|
-
{ central: 3286.01, collateral: 1n },
|
|
138
|
-
{ central: 3435.86, collateral: 1n },
|
|
139
|
-
{ central: 3443.21, collateral: 1n },
|
|
140
|
-
],
|
|
141
|
-
},
|
|
142
|
-
|
|
143
|
-
LINK: {
|
|
144
|
-
config: defaultConfig,
|
|
145
|
-
trades: [
|
|
146
|
-
{ central: 26.9, collateral: 1n },
|
|
147
|
-
{ central: 30.59, collateral: 1n },
|
|
148
|
-
{ central: 30.81, collateral: 1n },
|
|
149
|
-
],
|
|
150
|
-
},
|
|
151
|
-
|
|
152
|
-
USDC: {
|
|
153
|
-
config: {
|
|
154
|
-
...defaultConfig,
|
|
155
|
-
collateralValue: 10_000_000n,
|
|
156
|
-
},
|
|
157
|
-
trades: [{ central: 1, collateral: 1n }],
|
|
158
|
-
},
|
|
159
|
-
|
|
160
|
-
moola: {
|
|
161
|
-
trades: [
|
|
162
|
-
{ central: 1, collateral: 1n },
|
|
163
|
-
{ central: 1.3, collateral: 1n },
|
|
164
|
-
{ central: 1.2, collateral: 1n },
|
|
165
|
-
{ central: 1.8, collateral: 1n },
|
|
166
|
-
{ central: 1.5, collateral: 1n },
|
|
167
|
-
],
|
|
168
|
-
},
|
|
169
|
-
|
|
170
|
-
simolean: {
|
|
171
|
-
trades: [
|
|
172
|
-
{ central: 21.35, collateral: 1n },
|
|
173
|
-
{ central: 21.72, collateral: 1n },
|
|
174
|
-
{ central: 21.24, collateral: 1n },
|
|
175
|
-
],
|
|
176
|
-
},
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
/** @param {number} f */
|
|
180
|
-
const run2places = f =>
|
|
181
|
-
BigInt(Math.round(f * 100)) * 10n ** BigInt(DecimalPlaces[Stable.symbol] - 2);
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* @param {bigint} value
|
|
185
|
-
* @param {{
|
|
186
|
-
* centralSupplyInstall: ERef<Installation>,
|
|
187
|
-
* feeMintAccess: ERef<FeeMintAccess>,
|
|
188
|
-
* zoe: ERef<ZoeService>,
|
|
189
|
-
* }} powers
|
|
190
|
-
* @returns {Promise<Payment>}
|
|
191
|
-
*/
|
|
192
|
-
const mintRunPayment = async (
|
|
193
|
-
value,
|
|
194
|
-
{ centralSupplyInstall, feeMintAccess: feeMintAccessP, zoe },
|
|
195
|
-
) => {
|
|
196
|
-
const feeMintAccess = await feeMintAccessP;
|
|
197
|
-
|
|
198
|
-
const { creatorFacet: ammSupplier } = await E(zoe).startInstance(
|
|
199
|
-
centralSupplyInstall,
|
|
200
|
-
{},
|
|
201
|
-
{ bootstrapPaymentValue: value },
|
|
202
|
-
{ feeMintAccess },
|
|
203
|
-
);
|
|
204
|
-
// TODO: stop the contract vat?
|
|
205
|
-
return E(ammSupplier).getBootstrapPayment();
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
/**
|
|
209
|
-
* @param {string} name
|
|
210
|
-
* @param {MintsVat} mints
|
|
211
|
-
*/
|
|
212
|
-
const provideCoin = async (name, mints) => {
|
|
213
|
-
return E(mints).provideIssuerKit(name, AssetKind.NAT, {
|
|
214
|
-
decimalPlaces: DecimalPlaces[name],
|
|
215
|
-
});
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
/**
|
|
219
|
-
* @param { BootstrapSpace &
|
|
220
|
-
* { consume: {loadVat: VatLoader<MintsVat>} }
|
|
221
|
-
* } powers
|
|
222
|
-
*
|
|
223
|
-
* TODO: sync this type with end-user docs?
|
|
224
|
-
* @typedef {{
|
|
225
|
-
* issuer: ERef<Issuer>,
|
|
226
|
-
* issuerPetname: string,
|
|
227
|
-
* payment: Payment,
|
|
228
|
-
* brand: Brand,
|
|
229
|
-
* pursePetname: string,
|
|
230
|
-
* }} UserPaymentRecord
|
|
231
|
-
*/
|
|
232
|
-
export const connectFaucet = async ({
|
|
233
|
-
consume: {
|
|
234
|
-
bankManager,
|
|
235
|
-
bldIssuerKit: bldP,
|
|
236
|
-
client,
|
|
237
|
-
feeMintAccess,
|
|
238
|
-
loadVat,
|
|
239
|
-
zoe,
|
|
240
|
-
},
|
|
241
|
-
installation: {
|
|
242
|
-
consume: { centralSupply: centralSupplyInstall },
|
|
243
|
-
},
|
|
244
|
-
produce: { mints },
|
|
245
|
-
}) => {
|
|
246
|
-
const vats = {
|
|
247
|
-
mints: E(loadVat)('mints'),
|
|
248
|
-
};
|
|
249
|
-
mints.resolve(vats.mints);
|
|
250
|
-
|
|
251
|
-
const bldIssuerKit = await bldP;
|
|
252
|
-
const LOTS = 1_000_000_000_000n; // TODO: design this.
|
|
253
|
-
let faucetRunSupply = await mintRunPayment(LOTS, {
|
|
254
|
-
centralSupplyInstall,
|
|
255
|
-
feeMintAccess,
|
|
256
|
-
zoe,
|
|
257
|
-
});
|
|
258
|
-
const runIssuer = await E(zoe).getFeeIssuer();
|
|
259
|
-
const runBrand = await E(runIssuer).getBrand();
|
|
260
|
-
const runIssuerKit = {
|
|
261
|
-
issuer: runIssuer,
|
|
262
|
-
brand: runBrand,
|
|
263
|
-
mint: {
|
|
264
|
-
mintPayment: async amount => {
|
|
265
|
-
// TODO: what happens if faucetRunSupply doesn't have enough
|
|
266
|
-
// remaining?
|
|
267
|
-
let fragment;
|
|
268
|
-
[fragment, faucetRunSupply] = await E(runIssuer).split(
|
|
269
|
-
faucetRunSupply,
|
|
270
|
-
amount,
|
|
271
|
-
);
|
|
272
|
-
return fragment;
|
|
273
|
-
},
|
|
274
|
-
},
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
const makeFaucet = async address => {
|
|
278
|
-
const bank = await E(bankManager).getBankForAddress(address);
|
|
279
|
-
|
|
280
|
-
/** @type {UserPaymentRecord[][]} */
|
|
281
|
-
const userPaymentRecords = await Promise.all(
|
|
282
|
-
entries(FaucetPurseDetail).map(async ([issuerName, record]) => {
|
|
283
|
-
/** @param {string} name */
|
|
284
|
-
const provideIssuerKit = async name => {
|
|
285
|
-
switch (name) {
|
|
286
|
-
case Stable.symbol:
|
|
287
|
-
return runIssuerKit;
|
|
288
|
-
case Stake.symbol:
|
|
289
|
-
return bldIssuerKit;
|
|
290
|
-
default: {
|
|
291
|
-
const { mint, issuer, brand } = await provideCoin(
|
|
292
|
-
name,
|
|
293
|
-
vats.mints,
|
|
294
|
-
);
|
|
295
|
-
return { issuer, brand, mint };
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
const { issuer, brand, mint } = await provideIssuerKit(issuerName);
|
|
300
|
-
const unit = 10n ** BigInt(DecimalPlaces[issuerName]);
|
|
301
|
-
const amount = AmountMath.make(brand, Nat(record.balance) * unit);
|
|
302
|
-
const payment = await E(mint).mintPayment(amount);
|
|
303
|
-
|
|
304
|
-
/** @type {UserPaymentRecord[]} */
|
|
305
|
-
let toFaucet = [];
|
|
306
|
-
// Use the bank layer for BLD, IST.
|
|
307
|
-
if (issuerName === Stake.symbol || issuerName === Stable.symbol) {
|
|
308
|
-
const purse = E(bank).getPurse(brand);
|
|
309
|
-
await E(purse).deposit(payment);
|
|
310
|
-
} else {
|
|
311
|
-
toFaucet = [
|
|
312
|
-
{
|
|
313
|
-
issuer,
|
|
314
|
-
brand,
|
|
315
|
-
issuerPetname: issuerName,
|
|
316
|
-
payment,
|
|
317
|
-
pursePetname: record.proposedName,
|
|
318
|
-
},
|
|
319
|
-
];
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
return toFaucet;
|
|
323
|
-
}),
|
|
324
|
-
);
|
|
325
|
-
|
|
326
|
-
const faucetPaymentInfo = userPaymentRecords.flat();
|
|
327
|
-
|
|
328
|
-
const userFeePurse = await E(zoe).makeFeePurse();
|
|
329
|
-
|
|
330
|
-
const faucet = Far('faucet', {
|
|
331
|
-
/**
|
|
332
|
-
* reap the spoils of our on-chain provisioning.
|
|
333
|
-
*
|
|
334
|
-
* @returns {Promise<Array<UserPaymentRecord>>}
|
|
335
|
-
*/
|
|
336
|
-
tapFaucet: async () => faucetPaymentInfo,
|
|
337
|
-
getFeePurse() {
|
|
338
|
-
return userFeePurse;
|
|
339
|
-
},
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
return faucet;
|
|
343
|
-
};
|
|
344
|
-
|
|
345
|
-
return E(client).assignBundle([address => ({ faucet: makeFaucet(address) })]);
|
|
346
|
-
};
|
|
347
|
-
harden(connectFaucet);
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Calculate how much RUN we need to fund the AMM pools
|
|
351
|
-
*
|
|
352
|
-
* @param {typeof AMMDemoState} issuers
|
|
353
|
-
*/
|
|
354
|
-
export const ammPoolRunDeposits = issuers => {
|
|
355
|
-
let ammTotal = 0n;
|
|
356
|
-
const balanceEntries = entries(issuers)
|
|
357
|
-
.filter(([_i, { config }]) => config) // skip RUN and fake issuers
|
|
358
|
-
.map(([issuerName, record]) => {
|
|
359
|
-
assert(record.config);
|
|
360
|
-
assert(record.trades);
|
|
361
|
-
|
|
362
|
-
/** @param {bigint} n */
|
|
363
|
-
const inCollateral = n => n * 10n ** BigInt(DecimalPlaces[issuerName]);
|
|
364
|
-
|
|
365
|
-
// The initial trade represents the fair value of RUN for collateral.
|
|
366
|
-
const initialTrade = record.trades[0];
|
|
367
|
-
// The collateralValue to be deposited is given, and we want to deposit
|
|
368
|
-
// the same value of RUN in the pool. For instance, We're going to
|
|
369
|
-
// deposit 2 * 10^13 BLD, and 10^6 build will trade for 28.9 * 10^6 RUN
|
|
370
|
-
const poolBalance = floorDivide(
|
|
371
|
-
multiply(
|
|
372
|
-
inCollateral(record.config.collateralValue),
|
|
373
|
-
run2places(initialTrade.central),
|
|
374
|
-
),
|
|
375
|
-
inCollateral(initialTrade.collateral),
|
|
376
|
-
);
|
|
377
|
-
ammTotal += poolBalance;
|
|
378
|
-
return /** @type {[string, bigint]} */ ([issuerName, poolBalance]);
|
|
379
|
-
});
|
|
380
|
-
return {
|
|
381
|
-
ammTotal,
|
|
382
|
-
balances: fromEntries(balanceEntries),
|
|
383
|
-
};
|
|
384
|
-
};
|
|
385
|
-
|
|
386
|
-
/**
|
|
387
|
-
* @param {Payment} bootstrapPayment
|
|
388
|
-
* @param {Record<string, bigint>} balances
|
|
389
|
-
* @param {{ issuer: ERef<Issuer>, brand: Brand }} central
|
|
390
|
-
*/
|
|
391
|
-
export const splitAllCentralPayments = async (
|
|
392
|
-
bootstrapPayment,
|
|
393
|
-
balances,
|
|
394
|
-
central,
|
|
395
|
-
) => {
|
|
396
|
-
const ammPoolAmounts = values(balances).map(b =>
|
|
397
|
-
AmountMath.make(central.brand, b),
|
|
398
|
-
);
|
|
399
|
-
|
|
400
|
-
const allPayments = await E(central.issuer).splitMany(
|
|
401
|
-
bootstrapPayment,
|
|
402
|
-
ammPoolAmounts,
|
|
403
|
-
);
|
|
404
|
-
|
|
405
|
-
const issuerMap = fromEntries(
|
|
406
|
-
keys(balances).map((name, i) => [
|
|
407
|
-
name,
|
|
408
|
-
{
|
|
409
|
-
payment: allPayments[i],
|
|
410
|
-
amount: ammPoolAmounts[i],
|
|
411
|
-
},
|
|
412
|
-
]),
|
|
413
|
-
);
|
|
414
|
-
|
|
415
|
-
return issuerMap;
|
|
416
|
-
};
|
|
417
|
-
|
|
418
|
-
/**
|
|
419
|
-
* @param {string} issuerName
|
|
420
|
-
* @param {typeof AMMDemoState['ATOM']} record
|
|
421
|
-
* @param {Record<string, { issuer: ERef<Issuer>, brand: Brand }>} kits
|
|
422
|
-
* @param {{ issuer: ERef<Issuer>, brand: Brand }} central
|
|
423
|
-
*/
|
|
424
|
-
export const poolRates = (issuerName, record, kits, central) => {
|
|
425
|
-
/** @param {bigint} n */
|
|
426
|
-
const inCollateral = n => n * 10n ** BigInt(DecimalPlaces[issuerName]);
|
|
427
|
-
const config = record.config;
|
|
428
|
-
assert(config);
|
|
429
|
-
assert(record.trades);
|
|
430
|
-
const initialPrice = record.trades[0];
|
|
431
|
-
assert(initialPrice);
|
|
432
|
-
const initialPriceNumerator = run2places(record.trades[0].central);
|
|
433
|
-
|
|
434
|
-
/**
|
|
435
|
-
* @param {Rational} r
|
|
436
|
-
* @param {Brand} b
|
|
437
|
-
*/
|
|
438
|
-
const toRatio = ([n, d], b) => makeRatio(n, b, d);
|
|
439
|
-
const rates = {
|
|
440
|
-
debtLimit: AmountMath.make(central.brand, 1_000_000_000n),
|
|
441
|
-
initialPrice: makeRatio(
|
|
442
|
-
initialPriceNumerator,
|
|
443
|
-
central.brand,
|
|
444
|
-
inCollateral(initialPrice.collateral),
|
|
445
|
-
kits[issuerName].brand,
|
|
446
|
-
),
|
|
447
|
-
initialMargin: toRatio(config.initialMargin, central.brand),
|
|
448
|
-
liquidationMargin: toRatio(config.liquidationMargin, central.brand),
|
|
449
|
-
liquidationPenalty: toRatio(config.liquidationPenalty, central.brand),
|
|
450
|
-
interestRate: toRatio(config.interestRate, central.brand),
|
|
451
|
-
loanFee: toRatio(config.loanFee, central.brand),
|
|
452
|
-
};
|
|
453
|
-
return { rates, initialValue: inCollateral(config.collateralValue) };
|
|
454
|
-
};
|
|
455
|
-
|
|
456
|
-
/**
|
|
457
|
-
* @param { import('./econ-behaviors.js').EconomyBootstrapPowers & {
|
|
458
|
-
* consume: { mints }
|
|
459
|
-
* }} powers
|
|
460
|
-
*/
|
|
461
|
-
export const fundAMM = async ({
|
|
462
|
-
consume: {
|
|
463
|
-
bldIssuerKit,
|
|
464
|
-
chainTimerService,
|
|
465
|
-
feeMintAccess,
|
|
466
|
-
mints,
|
|
467
|
-
priceAuthorityVat,
|
|
468
|
-
priceAuthorityAdmin,
|
|
469
|
-
vaultFactoryCreator,
|
|
470
|
-
zoe,
|
|
471
|
-
},
|
|
472
|
-
installation: {
|
|
473
|
-
consume: { centralSupply: centralSupplyInstall },
|
|
474
|
-
},
|
|
475
|
-
issuer: {
|
|
476
|
-
consume: { IST: centralIssuer },
|
|
477
|
-
},
|
|
478
|
-
brand: {
|
|
479
|
-
consume: { IST: centralBrand },
|
|
480
|
-
},
|
|
481
|
-
instance: {
|
|
482
|
-
consume: { amm: ammInstance },
|
|
483
|
-
},
|
|
484
|
-
}) => {
|
|
485
|
-
const { ammTotal: ammDepositValue, balances } =
|
|
486
|
-
ammPoolRunDeposits(AMMDemoState);
|
|
487
|
-
|
|
488
|
-
const vats = { mints, priceAuthority: priceAuthorityVat };
|
|
489
|
-
|
|
490
|
-
const kits = await Collect.allValues(
|
|
491
|
-
objectMap(
|
|
492
|
-
fromEntries([Stable.symbol, ...keys(AMMDemoState)].map(n => [n, n])),
|
|
493
|
-
async issuerName => {
|
|
494
|
-
switch (issuerName) {
|
|
495
|
-
case Stable.symbol: {
|
|
496
|
-
const [issuer, brand] = await Promise.all([
|
|
497
|
-
centralIssuer,
|
|
498
|
-
centralBrand,
|
|
499
|
-
]);
|
|
500
|
-
return { mint: undefined, issuer, brand };
|
|
501
|
-
}
|
|
502
|
-
case 'BLD':
|
|
503
|
-
return bldIssuerKit;
|
|
504
|
-
default:
|
|
505
|
-
return provideCoin(issuerName, vats.mints);
|
|
506
|
-
}
|
|
507
|
-
},
|
|
508
|
-
),
|
|
509
|
-
);
|
|
510
|
-
const central = kits[Stable.symbol];
|
|
511
|
-
|
|
512
|
-
/** @type {[ XYKAMMPublicFacet, TimerService]} */
|
|
513
|
-
const [ammPublicFacet, timer] = await Promise.all([
|
|
514
|
-
E(zoe).getPublicFacet(ammInstance),
|
|
515
|
-
chainTimerService,
|
|
516
|
-
]);
|
|
517
|
-
|
|
518
|
-
const ammBootstrapPayment = await mintRunPayment(ammDepositValue, {
|
|
519
|
-
centralSupplyInstall,
|
|
520
|
-
feeMintAccess,
|
|
521
|
-
zoe,
|
|
522
|
-
});
|
|
523
|
-
|
|
524
|
-
async function addAllCollateral() {
|
|
525
|
-
const issuerMap = await splitAllCentralPayments(
|
|
526
|
-
ammBootstrapPayment,
|
|
527
|
-
balances,
|
|
528
|
-
central,
|
|
529
|
-
);
|
|
530
|
-
|
|
531
|
-
return Promise.all(
|
|
532
|
-
entries(AMMDemoState).map(async ([issuerName, record]) => {
|
|
533
|
-
if (!record.config) {
|
|
534
|
-
return undefined;
|
|
535
|
-
}
|
|
536
|
-
const { rates, initialValue } = poolRates(
|
|
537
|
-
issuerName,
|
|
538
|
-
record,
|
|
539
|
-
kits,
|
|
540
|
-
central,
|
|
541
|
-
);
|
|
542
|
-
|
|
543
|
-
const kit = kits[issuerName];
|
|
544
|
-
assert(kit.mint, X`no mint for ${q(issuerName)}`);
|
|
545
|
-
// @@ doesn't work for BLD?
|
|
546
|
-
const secondaryPayment = await E(kit.mint).mintPayment(
|
|
547
|
-
AmountMath.make(kit.brand, initialValue),
|
|
548
|
-
);
|
|
549
|
-
assert(secondaryPayment, X`no payment for ${q(issuerName)}`);
|
|
550
|
-
|
|
551
|
-
assert(kit.issuer, X`No issuer for ${q(issuerName)}`);
|
|
552
|
-
const liquidityIssuer = E(ammPublicFacet).addIssuer(
|
|
553
|
-
kit.issuer,
|
|
554
|
-
issuerName,
|
|
555
|
-
);
|
|
556
|
-
const [secondaryAmount, liquidityBrand] = await Promise.all([
|
|
557
|
-
// Error: (an undefined) was not a live payment for brand
|
|
558
|
-
// at (.../vats/src/demoIssuers.js:591)
|
|
559
|
-
E(kit.issuer).getAmountOf(secondaryPayment),
|
|
560
|
-
E(liquidityIssuer).getBrand(),
|
|
561
|
-
]);
|
|
562
|
-
const centralAmount = issuerMap[issuerName].amount;
|
|
563
|
-
const proposal = harden({
|
|
564
|
-
want: { Liquidity: AmountMath.makeEmpty(liquidityBrand) },
|
|
565
|
-
give: { Secondary: secondaryAmount, Central: centralAmount },
|
|
566
|
-
});
|
|
567
|
-
|
|
568
|
-
await E(zoe).offer(
|
|
569
|
-
E(ammPublicFacet).addPoolInvitation(),
|
|
570
|
-
proposal,
|
|
571
|
-
harden({
|
|
572
|
-
Secondary: secondaryPayment,
|
|
573
|
-
Central: issuerMap[issuerName].payment,
|
|
574
|
-
}),
|
|
575
|
-
);
|
|
576
|
-
|
|
577
|
-
const issuerPresence = await kit.issuer;
|
|
578
|
-
return E(vaultFactoryCreator).addVaultType(
|
|
579
|
-
issuerPresence,
|
|
580
|
-
issuerName,
|
|
581
|
-
rates,
|
|
582
|
-
);
|
|
583
|
-
}),
|
|
584
|
-
);
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
/**
|
|
588
|
-
* @param {ERef<Brand>} brandIn
|
|
589
|
-
* @param {ERef<Brand>} brandOut
|
|
590
|
-
* @param {Array<[bigint | number, bigint | number]>} tradeList
|
|
591
|
-
*/
|
|
592
|
-
const makeFakePriceAuthority = (brandIn, brandOut, tradeList) =>
|
|
593
|
-
E(vats.priceAuthority).makeFakePriceAuthority({
|
|
594
|
-
actualBrandIn: brandIn,
|
|
595
|
-
actualBrandOut: brandOut,
|
|
596
|
-
tradeList,
|
|
597
|
-
timer,
|
|
598
|
-
quoteInterval: QUOTE_INTERVAL,
|
|
599
|
-
});
|
|
600
|
-
|
|
601
|
-
await addAllCollateral();
|
|
602
|
-
|
|
603
|
-
const brandsWithPriceAuthorities = await E(ammPublicFacet).getAllPoolBrands();
|
|
604
|
-
|
|
605
|
-
await Promise.all(
|
|
606
|
-
// TODO: exactly what is the list of things to iterate here?
|
|
607
|
-
entries(AMMDemoState).map(async ([issuerName, record]) => {
|
|
608
|
-
// Create priceAuthority pairs for centralIssuer based on the
|
|
609
|
-
// AMM or FakePriceAuthority.
|
|
610
|
-
console.debug(`Creating ${issuerName}-${Stable.symbol}`);
|
|
611
|
-
const issuer = kits[issuerName].issuer;
|
|
612
|
-
const { trades } = record;
|
|
613
|
-
/** @param {bigint} n */
|
|
614
|
-
const inCollateral = n => n * 10n ** BigInt(DecimalPlaces[issuerName]);
|
|
615
|
-
const tradesGivenCentral = trades.map(
|
|
616
|
-
({ central: num, collateral: unit }) =>
|
|
617
|
-
/** @type {[bigint, bigint]} */ ([
|
|
618
|
-
run2places(num),
|
|
619
|
-
inCollateral(unit),
|
|
620
|
-
]),
|
|
621
|
-
);
|
|
622
|
-
assert(issuer);
|
|
623
|
-
const brand = await E(issuer).getBrand();
|
|
624
|
-
let toCentral;
|
|
625
|
-
let fromCentral;
|
|
626
|
-
|
|
627
|
-
if (brandsWithPriceAuthorities.includes(brand)) {
|
|
628
|
-
({ toCentral, fromCentral } = await E(ammPublicFacet)
|
|
629
|
-
.getPriceAuthorities(brand)
|
|
630
|
-
.catch(_e => {
|
|
631
|
-
// console.warn('could not get AMM priceAuthorities', _e);
|
|
632
|
-
return { toCentral: undefined, fromCentral: undefined };
|
|
633
|
-
}));
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
if (!fromCentral && tradesGivenCentral) {
|
|
637
|
-
// We have no amm from-central price authority, make one from trades.
|
|
638
|
-
if (issuerName !== Stable.symbol) {
|
|
639
|
-
console.log(
|
|
640
|
-
`Making fake price authority for ${Stable.symbol}-${issuerName}`,
|
|
641
|
-
);
|
|
642
|
-
}
|
|
643
|
-
fromCentral = makeFakePriceAuthority(
|
|
644
|
-
central.brand,
|
|
645
|
-
brand,
|
|
646
|
-
tradesGivenCentral,
|
|
647
|
-
);
|
|
648
|
-
}
|
|
649
|
-
|
|
650
|
-
if (!toCentral && central.issuer !== issuer && tradesGivenCentral) {
|
|
651
|
-
// We have no amm to-central price authority, make one from trades.
|
|
652
|
-
console.log(
|
|
653
|
-
`Making fake price authority for ${issuerName}-${Stable.symbol}`,
|
|
654
|
-
);
|
|
655
|
-
/** @type {Array<[bigint | number, bigint | number]>} */
|
|
656
|
-
const tradesGivenOther = tradesGivenCentral.map(
|
|
657
|
-
([valueCentral, valueOther]) => [valueOther, valueCentral],
|
|
658
|
-
);
|
|
659
|
-
toCentral = makeFakePriceAuthority(
|
|
660
|
-
brand,
|
|
661
|
-
central.brand,
|
|
662
|
-
tradesGivenOther,
|
|
663
|
-
);
|
|
664
|
-
}
|
|
665
|
-
|
|
666
|
-
// Register the price pairs.
|
|
667
|
-
await Promise.all(
|
|
668
|
-
[
|
|
669
|
-
{ pa: fromCentral, fromBrand: central.brand, toBrand: brand },
|
|
670
|
-
{ pa: toCentral, fromBrand: brand, toBrand: central.brand },
|
|
671
|
-
].map(async ({ pa, fromBrand, toBrand }) => {
|
|
672
|
-
const paPresence = await pa;
|
|
673
|
-
if (!paPresence) {
|
|
674
|
-
return;
|
|
675
|
-
}
|
|
676
|
-
await E(priceAuthorityAdmin).registerPriceAuthority(
|
|
677
|
-
paPresence,
|
|
678
|
-
fromBrand,
|
|
679
|
-
toBrand,
|
|
680
|
-
);
|
|
681
|
-
}),
|
|
682
|
-
);
|
|
683
|
-
}),
|
|
684
|
-
);
|
|
685
|
-
};
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// @ts-check
|
|
2
|
-
import { E, Far } from '@endo/far';
|
|
3
|
-
import { addRemote } from '@agoric/vats/src/core/utils.js';
|
|
4
|
-
|
|
5
|
-
export { connectFaucet, fundAMM } from './demoIssuers.js';
|
|
6
|
-
|
|
7
|
-
/** @param {BootstrapPowers} powers */
|
|
8
|
-
export const installSimEgress = async ({
|
|
9
|
-
vatParameters: { argv },
|
|
10
|
-
vats: { vattp, comms },
|
|
11
|
-
consume: { clientCreator },
|
|
12
|
-
}) => {
|
|
13
|
-
const PROVISIONER_INDEX = 1;
|
|
14
|
-
|
|
15
|
-
return Promise.all(
|
|
16
|
-
argv.hardcodedClientAddresses.map(async (addr, i) => {
|
|
17
|
-
const clientFacet = await E(clientCreator).createClientFacet(
|
|
18
|
-
`solo${i}`,
|
|
19
|
-
addr,
|
|
20
|
-
['agoric.ALL_THE_POWERS'],
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
await addRemote(addr, { vats: { comms, vattp } });
|
|
24
|
-
await E(comms).addEgress(addr, PROVISIONER_INDEX, clientFacet);
|
|
25
|
-
}),
|
|
26
|
-
);
|
|
27
|
-
};
|
|
28
|
-
harden(installSimEgress);
|
|
29
|
-
|
|
30
|
-
/** @param {BootstrapPowers} powers */
|
|
31
|
-
export const grantRunBehaviors = async ({
|
|
32
|
-
runBehaviors,
|
|
33
|
-
consume: { client },
|
|
34
|
-
}) => {
|
|
35
|
-
const bundle = {
|
|
36
|
-
behaviors: Far('behaviors', { run: manifest => runBehaviors(manifest) }),
|
|
37
|
-
};
|
|
38
|
-
return E(client).assignBundle([_addr => bundle]);
|
|
39
|
-
};
|
|
40
|
-
harden(grantRunBehaviors);
|