Skip to content

Commit

Permalink
#9 The protocol is now able to work with agoric version 0.15, lending…
Browse files Browse the repository at this point in the history
…Pool contract is governed.
  • Loading branch information
anilhelvaci committed May 20, 2022
1 parent 6118232 commit 24d8051
Show file tree
Hide file tree
Showing 11 changed files with 831 additions and 1,374 deletions.
297 changes: 169 additions & 128 deletions contract/deploy/deploy.js

Large diffs are not rendered by default.

28 changes: 14 additions & 14 deletions contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
"type": "module",
"private": true,
"dependencies": {
"@agoric/assert": "0.3.16",
"@agoric/ertp": "0.13.3",
"@agoric/assert": "0.4.0",
"@agoric/ertp": "0.14.1",
"@agoric/eventual-send": "0.14.1",
"@agoric/governance": "0.4.3",
"@agoric/nat": "^4.1.0",
"@agoric/notifier": "0.3.35",
"@agoric/store": "0.6.10",
"@agoric/swingset-vat": "0.25.1",
"@agoric/vats": "0.7.0",
"@agoric/zoe": "0.21.3",
"@endo/bundle-source": "^2.1.1",
"@endo/captp": "2.0.3",
"@endo/far": "0.1.9",
"@endo/marshal": "0.6.3",
"@endo/promise-kit": "0.2.37"
"@agoric/governance": "0.6.0",
"@agoric/nat": "4.1.0",
"@agoric/notifier": "0.4.0",
"@agoric/store": "0.7.1",
"@agoric/swingset-vat": "0.27.0",
"@agoric/vats": "0.9.0",
"@agoric/zoe": "0.23.0",
"@endo/bundle-source": "2.2.0",
"@endo/captp": "2.0.7",
"@endo/far": "0.2.3",
"@endo/marshal": "0.6.7",
"@endo/promise-kit": "0.2.41"
},
"devDependencies": {
"@endo/init": "0.5.37",
Expand Down
29 changes: 9 additions & 20 deletions contract/src/lendingPool/lendingPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,8 @@
import '@agoric/zoe/exported.js';
import '@agoric/zoe/src/contracts/exported.js';

// The vaultFactory owns a number of VaultManagers and a mint for RUN.
//
// addVaultType is a closely held method that adds a brand new collateral type.
// It specifies the initial exchange rate for that type. It depends on a
// separately specified AMM to provide the ability to liquidate loans that are
// in arrears. We could check that the AMM has sufficient liquidity, but for the
// moment leave that to those participating in the governance process for adding
// new collateral type to ascertain.

// This contract wants to be managed by a contractGovernor, but it isn't
// compatible with contractGovernor, since it has a separate paramManager for
// each Vault. This requires it to manually replicate the API of contractHelper
// to satisfy contractGovernor. It needs to return a creatorFacet with
// { getParamMgrRetriever, getInvitation, getLimitedCreatorFacet }.

import { E } from '@agoric/eventual-send';
import '@agoric/governance/src/exported';
import '@agoric/governance/src/exported.js';
import { AmountMath, AssetKind } from '@agoric/ertp';

import { makeScalarMap, keyEQ } from '@agoric/store';
Expand Down Expand Up @@ -53,11 +38,13 @@ export const start = async (zcf, privateArgs) => {
timerService,
liquidationInstall,
electionManager,
main: { [CONTRACT_ELECTORATE]: electorateParam },
governedParams: { [CONTRACT_ELECTORATE]: electorateParam },
loanTimingParams,
bootstrappedAssets: bootstrappedAssetIssuers,
compareCurrencyBrand
} = zcf.getTerms();
const { initialPoserInvitation } = privateArgs;
const electorateParamManager = await makeElectorateParamManager(E(zcf).getZoeService(), initialPoserInvitation);

console.log('[PRICE_MANAGER]', priceManager);
console.log('[COMPARE_CURRENCY_BRAND]', compareCurrencyBrand);
Expand Down Expand Up @@ -87,7 +74,7 @@ export const start = async (zcf, privateArgs) => {
const poolParamManager = makePoolParamManager(ratesUpdated);
poolParamManagers.init(underlyingBrand, poolParamManager);

//TODO Create liquadition for dynamic underdlying assets
// TODO Create liquadition for dynamic underdlying assets
// const { creatorFacet: liquidationFacet } = await E(zoe).startInstance(
// liquidationInstall,
// harden({ RUN: runIssuer, Collateral: underlyingIssuer }),
Expand Down Expand Up @@ -190,10 +177,10 @@ export const start = async (zcf, privateArgs) => {
const getParamMgrRetriever = () =>
Far('paramManagerRetriever', {
get: paramDesc => {
if (paramDesc.key === 'main') {
if (paramDesc.key === 'governedParams') {
return electorateParamManager;
} else {
return vaultParamManagers.get(paramDesc.collateralBrand);
return poolParamManagers.get(paramDesc.collateralBrand);
}
},
});
Expand All @@ -207,6 +194,8 @@ export const start = async (zcf, privateArgs) => {
const lendingPoolWrapper = Far('powerful lendingPool wrapper', {
getParamMgrRetriever,
getLimitedCreatorFacet: () => lendingPool,
getGovernedApis: () => harden({}),
getGovernedApiNames: () => harden({}),
});

return harden({
Expand Down
70 changes: 0 additions & 70 deletions contract/src/lendingPool/orderedVaultStore.js

This file was deleted.

85 changes: 36 additions & 49 deletions contract/src/lendingPool/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import './types.js';

import {
makeGovernedNat,
makeGovernedInvitation,
makeGovernedRatio,
makeParamManagerBuilder,
makeParamManagerSync,
makeParamManager,
CONTRACT_ELECTORATE,
ParamTypes
} from '@agoric/governance';

export const CHARGING_PERIOD_KEY = 'ChargingPeriod';
Expand All @@ -26,7 +25,7 @@ export const MULTIPILIER_RATE_KEY = 'MultipilierRate';
*/
const makeElectorateParams = electorateInvitationAmount => {
return harden({
[CONTRACT_ELECTORATE]: makeGovernedInvitation(electorateInvitationAmount),
[CONTRACT_ELECTORATE]: [ParamTypes.INVITATION, electorateInvitationAmount],
});
};

Expand All @@ -35,60 +34,51 @@ const makeElectorateParams = electorateInvitationAmount => {
* @param {Rates} rates
*/
const makeLoanParams = (loanTiming, rates) => {
return harden({
[CHARGING_PERIOD_KEY]: makeGovernedNat(loanTiming.chargingPeriod),
[RECORDING_PERIOD_KEY]: makeGovernedNat(loanTiming.recordingPeriod),
[LIQUIDATION_MARGIN_KEY]: makeGovernedRatio(rates.liquidationMargin),
[INTEREST_RATE_KEY]: makeGovernedRatio(rates.interestRate),
[LOAN_FEE_KEY]: makeGovernedRatio(rates.loanFee),
[BASE_RATE_KEY]: makeGovernedRatio(rates.baseRate),
[MULTIPILIER_RATE_KEY]: makeGovernedRatio(rates.multipilierRate),
});
return {
[CHARGING_PERIOD_KEY]: [ParamTypes.NAT, loanTiming.chargingPeriod],
[RECORDING_PERIOD_KEY]: [ParamTypes.NAT, loanTiming.recordingPeriod],
[LIQUIDATION_MARGIN_KEY]: [ParamTypes.RATIO, rates.liquidationMargin],
[INTEREST_RATE_KEY]: [ParamTypes.RATIO, rates.interestRate],
[LOAN_FEE_KEY]: [ParamTypes.RATIO, rates.loanFee],
[BASE_RATE_KEY]: [ParamTypes.RATIO, rates.baseRate],
[MULTIPILIER_RATE_KEY]: [ParamTypes.RATIO, rates.multipilierRate],
};
};

/**
* @param {LoanTiming} initialValues
* @returns {ParamManagerFull & {
* updateChargingPeriod: (period: bigint) => void,
* updateRecordingPeriod: (period: bigint) => void,
* }}
*/
const makeLoanTimingManager = initialValues => {
// @ts-expect-error until makeParamManagerBuilder can be generic */
return makeParamManagerBuilder()
.addNat(CHARGING_PERIOD_KEY, initialValues.chargingPeriod)
.addNat(RECORDING_PERIOD_KEY, initialValues.recordingPeriod)
.addNat(PRICE_CHECK_PERIOD_KEY, initialValues.priceCheckPeriod)
.build();
return makeParamManagerSync({
[CHARGING_PERIOD_KEY]: [ParamTypes.NAT, initialValues.chargingPeriod],
[RECORDING_PERIOD_KEY]: [ParamTypes.NAT, initialValues.recordingPeriod],
[PRICE_CHECK_PERIOD_KEY]: [ParamTypes.NAT, initialValues.priceCheckPeriod]
})
};

/**
* @param {Rates} rates
* @returns {VaultParamManager}
*/
const makeVaultParamManager = rates => {
// @ts-expect-error until makeParamManagerBuilder can be generic */
return makeParamManagerBuilder()
.addBrandedRatio(LIQUIDATION_MARGIN_KEY, rates.liquidationMargin)
.addBrandedRatio(INTEREST_RATE_KEY, rates.interestRate)
.addBrandedRatio(LOAN_FEE_KEY, rates.loanFee)
.build();
return makeParamManagerSync({
[LIQUIDATION_MARGIN_KEY]: [ParamTypes.RATIO, rates.liquidationMargin],
[INTEREST_RATE_KEY]: [ParamTypes.RATIO, rates.interestRate],
[LOAN_FEE_KEY]: [ParamTypes.RATIO, rates.loanFee],
})
};

/**
* @param {Rates} rates
* @returns {VaultParamManager}
*/
const makePoolParamManager = rates => {
// @ts-expect-error until makeParamManagerBuilder can be generic */
return makeParamManagerBuilder()
.addBrandedRatio(LIQUIDATION_MARGIN_KEY, rates.liquidationMargin)
.addBrandedRatio(INTEREST_RATE_KEY, rates.interestRate)
.addBrandedRatio(LOAN_FEE_KEY, rates.loanFee)
.addBrandedRatio(INITIAL_EXCHANGE_RATE_KEY, rates.initialExchangeRate)
.addBrandedRatio(BASE_RATE_KEY, rates.baseRate)
.addBrandedRatio(MULTIPILIER_RATE_KEY, rates.multipilierRate)
.build();
return makeParamManagerSync({
[LIQUIDATION_MARGIN_KEY]: [ParamTypes.RATIO, rates.liquidationMargin],
[INTEREST_RATE_KEY]: [ParamTypes.RATIO, rates.interestRate],
[LOAN_FEE_KEY]: [ParamTypes.RATIO, rates.loanFee],
[INITIAL_EXCHANGE_RATE_KEY]: [ParamTypes.RATIO, rates.initialExchangeRate],
[BASE_RATE_KEY]: [ParamTypes.RATIO, rates.baseRate],
[MULTIPILIER_RATE_KEY]: [ParamTypes.RATIO, rates.multipilierRate],
})
};

/**
Expand All @@ -102,10 +92,10 @@ const makePoolParamManager = rates => {
* }>}
*/
const makeElectorateParamManager = async (zoe, electorateInvitation) => {
// @ts-expect-error casting to ElectorateParamManager
return makeParamManagerBuilder(zoe)
.addInvitation(CONTRACT_ELECTORATE, electorateInvitation)
.then(builder => builder.build());
return makeParamManager({
[CONTRACT_ELECTORATE]: [ParamTypes.INVITATION, electorateInvitation],
},
zoe);
};

/**
Expand All @@ -116,7 +106,6 @@ const makeElectorateParamManager = async (zoe, electorateInvitation) => {
* @param {Amount} invitationAmount
* @param {Rates} rates
* @param {XYKAMMPublicFacet} ammPublicFacet
* @param {[]} bootstrappedAssets
* @param {bigint=} bootstrapPaymentValue
* @param {Brand} compareCurrencyBrand
*/
Expand All @@ -128,7 +117,6 @@ const makeGovernedTerms = (
invitationAmount,
rates,
ammPublicFacet,
bootstrappedAssets,
bootstrapPaymentValue = 0n,
compareCurrencyBrand
) => {
Expand All @@ -143,9 +131,8 @@ const makeGovernedTerms = (
loanTimingParams: timingParamMgr.getParams(),
timerService,
liquidationInstall,
main: makeElectorateParams(invitationAmount),
governedParams: makeElectorateParams(invitationAmount),
bootstrapPaymentValue,
bootstrappedAssets,
compareCurrencyBrand
});
};
Expand Down
12 changes: 0 additions & 12 deletions contract/src/lendingPool/poolManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ import { makeNotifierKit, observeNotifier } from '@agoric/notifier';
import { AmountMath } from '@agoric/ertp';
import { Far } from '@endo/marshal';

// import { makeScalarBigMapStore } from '@agoric/swingset-vat/src/storeModule';
import { makeScalarMap } from '@agoric/store';
import { makeInnerVault } from './vault.js';
// import { makePrioritizedVaults } from './prioritizedVaults.js';
import { liquidate } from './liquidation.js';
import { makeTracer } from '../makeTracer.js';
import {
Expand Down Expand Up @@ -137,16 +135,6 @@ export const makePoolManager = (

let vaultCounter = 0;

/**
* A store for vaultKits prioritized by their collaterization ratio.
*
* It should be set only once but it's a `let` because it can't be set until after the
* definition of reschedulePriceCheck, which refers to sortedVaultKits
*
* @type {ReturnType<typeof makePrioritizedVaults>=}
*/
// XXX misleading mutability and confusing flow control; could be refactored with a listener
let prioritizedVaults;
/**
* @type {MapStore<Brand, DebtsPerCollateral>}
*/
Expand Down
Loading

0 comments on commit 24d8051

Please sign in to comment.