From 208577e398e4b4472f84777688884fd225529ab6 Mon Sep 17 00:00:00 2001 From: anilhelvaci Date: Tue, 25 Oct 2022 10:43:36 +0300 Subject: [PATCH] #40 Made paramManagers work again, removed copy-paste code and used imported ones instead, should update timer.tick() and test numbers --- contract/src/lendingPool/lendingPool.js | 8 +-- contract/src/lendingPool/loanStoreUtils.js | 4 +- contract/src/lendingPool/params.js | 10 ++-- contract/src/lendingPool/poolManager.js | 3 +- contract/test/lendingPool/helpers.js | 6 +-- .../lendingPool/lendingPoolScenrioHelpers.js | 8 +-- contract/test/lendingPool/setup.js | 13 +++-- contract/test/lendingPool/test-lendingPool.js | 52 ++++++++----------- 8 files changed, 51 insertions(+), 53 deletions(-) diff --git a/contract/src/lendingPool/lendingPool.js b/contract/src/lendingPool/lendingPool.js index d46094f..a9b78b5 100644 --- a/contract/src/lendingPool/lendingPool.js +++ b/contract/src/lendingPool/lendingPool.js @@ -74,7 +74,7 @@ const trace = makeTracer('LendingPool'); * since we might add a DAO later. * * @param {ZCF} zcf - * @param {{initialPoserInvitation: Invitation}} privateArgs + * @param {{initialPoserInvitation: Invitation, storageNode: ERef, marshaller: ERef}} privateArgs */ export const start = async (zcf, privateArgs) => { /** @type {LendingPoolTerms}*/ @@ -88,9 +88,11 @@ export const start = async (zcf, privateArgs) => { compareCurrencyBrand, } = terms; - const { initialPoserInvitation } = privateArgs; + const { initialPoserInvitation, storageNode, marshaller } = privateArgs; const electorateParamManager = await makeElectorateParamManager( E(zcf).getZoeService(), + storageNode, + marshaller, initialPoserInvitation, ); @@ -169,7 +171,7 @@ export const start = async (zcf, privateArgs) => { initialExchangeRate, }); - const poolParamManager = makePoolParamManager(ratesUpdated); + const poolParamManager = makePoolParamManager(storageNode, marshaller, ratesUpdated); poolParamManagers.init(underlyingBrand, poolParamManager); const [startTimeStamp, priceAuthNotifier] = await Promise.all([ diff --git a/contract/src/lendingPool/loanStoreUtils.js b/contract/src/lendingPool/loanStoreUtils.js index fe7d461..4fb48cd 100644 --- a/contract/src/lendingPool/loanStoreUtils.js +++ b/contract/src/lendingPool/loanStoreUtils.js @@ -1,4 +1,4 @@ -import { keyEQ, keyLT } from '@agoric/store'; +import { keyEQ, keyLT, makeScalarMap } from '@agoric/store'; import { AmountMath } from '@agoric/ertp'; import { toVaultKey } from '@agoric/inter-protocol/src/vaultFactory/storeUtils.js'; import { makeOrderedVaultStore } from '@agoric/inter-protocol/src/vaultFactory/orderedVaultStore.js'; @@ -26,7 +26,7 @@ import { LoanPhase } from './loan.js' * @return {LoanStore} */ export const makeLoanStoreUtils = () => { - const store = makeOrderedVaultStore('store'); + const store = makeOrderedVaultStore(makeScalarMap('store')); let firstKey; let reschedule; diff --git a/contract/src/lendingPool/params.js b/contract/src/lendingPool/params.js index e5384df..9bd764c 100644 --- a/contract/src/lendingPool/params.js +++ b/contract/src/lendingPool/params.js @@ -8,7 +8,7 @@ import { CONTRACT_ELECTORATE, ParamTypes } from '@agoric/governance'; -import { makeStoredPublishKit } from '@agoric/notifier'; +import { makeStoredPublisherKit } from '@agoric/notifier'; export const CHARGING_PERIOD_KEY = 'ChargingPeriod'; export const RECORDING_PERIOD_KEY = 'RecordingPeriod'; @@ -49,7 +49,7 @@ const makeLoanParams = (loanTiming, rates) => { * @param {LendingPoolTiming} initialValues */ const makeLoanTimingManager = (storageNode, marshaller, initialValues) => { - return makeParamManagerSync(makeStoredPublishKit(storageNode, marshaller),{ + return makeParamManagerSync(makeStoredPublisherKit(storageNode, marshaller),{ [CHARGING_PERIOD_KEY]: [ParamTypes.NAT, initialValues.chargingPeriod], [RECORDING_PERIOD_KEY]: [ParamTypes.NAT, initialValues.recordingPeriod], [PRICE_CHECK_PERIOD_KEY]: [ParamTypes.NAT, initialValues.priceCheckPeriod] // TODO this now deprecated and not being used anywhere, should remove it @@ -62,7 +62,7 @@ const makeLoanTimingManager = (storageNode, marshaller, initialValues) => { * @param {Rates} rates */ const makeLoanParamManager = (storageNode, marshaller, rates) => { - return makeParamManagerSync(makeStoredPublishKit(storageNode, marshaller), { + return makeParamManagerSync(makeStoredPublisherKit(storageNode, marshaller), { [LIQUIDATION_MARGIN_KEY]: [ParamTypes.RATIO, rates.liquidationMargin], }) }; @@ -73,7 +73,7 @@ const makeLoanParamManager = (storageNode, marshaller, rates) => { * @param {Rates} rates */ const makePoolParamManager = (storageNode, marshaller, rates) => { - return makeParamManagerSync(makeStoredPublishKit(storageNode, marshaller), { + return makeParamManagerSync(makeStoredPublisherKit(storageNode, marshaller), { [LIQUIDATION_MARGIN_KEY]: [ParamTypes.RATIO, rates.liquidationMargin], [INITIAL_EXCHANGE_RATE_KEY]: [ParamTypes.RATIO, rates.initialExchangeRate], [BASE_RATE_KEY]: [ParamTypes.RATIO, rates.baseRate], @@ -95,7 +95,7 @@ const makePoolParamManager = (storageNode, marshaller, rates) => { * }>} */ const makeElectorateParamManager = async (zoe, storageNode, marshaller, electorateInvitation) => { - return makeParamManager(makeStoredPublishKit(storageNode, marshaller), { + return makeParamManager(makeStoredPublisherKit(storageNode, marshaller), { [CONTRACT_ELECTORATE]: [ParamTypes.INVITATION, electorateInvitation], }, zoe); diff --git a/contract/src/lendingPool/poolManager.js b/contract/src/lendingPool/poolManager.js index 28b7ef8..6828663 100644 --- a/contract/src/lendingPool/poolManager.js +++ b/contract/src/lendingPool/poolManager.js @@ -401,7 +401,8 @@ export const makePoolManager = ( }, }; - observeNotifier(periodNotifier, timeObserver); + observeNotifier(periodNotifier, timeObserver) + .catch(error => console.log('Error building observer', error)); /** @type {ManagerFacet} */ const managerFacet = harden({ diff --git a/contract/test/lendingPool/helpers.js b/contract/test/lendingPool/helpers.js index cddf471..53df10a 100644 --- a/contract/test/lendingPool/helpers.js +++ b/contract/test/lendingPool/helpers.js @@ -7,7 +7,7 @@ import { makeTracer } from '@agoric/run-protocol/src/makeTracer.js'; import * as Collect from '@agoric/run-protocol/src/collect.js'; import { floorDivideBy } from '@agoric/zoe/src/contractSupport/ratio.js'; import { makeManualPriceAuthority } from '@agoric/zoe/tools/manualPriceAuthority.js'; -import { waitForPromisesToSettle } from './test-lendingPool.js'; +import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js'; const trace = makeTracer('Helper'); const BASIS_POINTS = 10000n; @@ -135,7 +135,7 @@ export const adjust = async (zoe, loan, collateralConfig = undefined, debtConfig { collateralUnderlyingBrand }, ); - await waitForPromisesToSettle(); + await eventLoopIteration(); return seat; }; @@ -179,7 +179,7 @@ export const closeLoan = async ( proposal, payment, ); - await waitForPromisesToSettle(); + await eventLoopIteration(); return seat; }; diff --git a/contract/test/lendingPool/lendingPoolScenrioHelpers.js b/contract/test/lendingPool/lendingPoolScenrioHelpers.js index d303f9d..f768e60 100644 --- a/contract/test/lendingPool/lendingPoolScenrioHelpers.js +++ b/contract/test/lendingPool/lendingPoolScenrioHelpers.js @@ -9,7 +9,7 @@ import { } from './helpers.js'; import { AmountMath } from '@agoric/ertp'; import { Nat } from '@agoric/nat'; -import { waitForPromisesToSettle } from './test-lendingPool.js'; +import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js'; import { makeScalarMap } from '@agoric/store'; import { floorDivideBy, floorMultiplyBy, makeRatio, oneMinus } from '@agoric/zoe/src/contractSupport/index.js'; @@ -209,7 +209,7 @@ export const makeLendingPoolScenarioHelpers = ( { collateralUnderlyingBrand }, ); - await waitForPromisesToSettle(); + await eventLoopIteration(); return seat; }; @@ -243,7 +243,7 @@ export const makeLendingPoolScenarioHelpers = ( proposal, payment, ); - await waitForPromisesToSettle(); + await eventLoopIteration(); return seat; }; @@ -282,7 +282,7 @@ export const makeLendingPoolScenarioHelpers = ( redeemPaymentRecord ); - await waitForPromisesToSettle(); + await eventLoopIteration(); return redeemUserSeat }; diff --git a/contract/test/lendingPool/setup.js b/contract/test/lendingPool/setup.js index 816bff1..b7c0282 100644 --- a/contract/test/lendingPool/setup.js +++ b/contract/test/lendingPool/setup.js @@ -68,7 +68,7 @@ export const setupServices = async ( ammPoolsConfig = undefined, ) => { const { - zoe, + farZoeKit: { zoe }, compareCurrencyKit: { brand: compCurrencyBrand }, vanKit: { mint: vanMint }, panKit: { mint: panMint }, @@ -228,7 +228,9 @@ export const startLendingPool = async ( instance.consume.economicCommittee, contractGovernor, ]); + const ammPublicFacet = await E(zoe).getPublicFacet(ammInstance); + console.log('ammPublicFacet', ammPublicFacet) const priceManager = await priceManagerP; const timer = await chainTimerService; const storageNode = await makeStorageNodeChild(chainStorage, STORAGE_PATH); @@ -246,7 +248,7 @@ export const startLendingPool = async ( compareBrand, undefined ); - // console.log("loanFactoryTerms", loanFactoryTerms) + const governorTerms = harden({ timer, electorateInstance, @@ -254,7 +256,6 @@ export const startLendingPool = async ( governed: { terms: loanFactoryTerms, issuerKeywordRecord: {}, - privateArgs: harden({ initialPoserInvitation }), }, }); const { creatorFacet: governorCreatorFacet, instance: governorInstance } = @@ -262,7 +263,11 @@ export const startLendingPool = async ( contractGovernorInstall, undefined, governorTerms, - harden({ electorateCreatorFacet }), + harden({ + electorateCreatorFacet, governed: { + initialPoserInvitation, storageNode, marshaller, + }, + }), ); const [lendingPoolInstance, lendingPoolCreator] = await Promise.all([ diff --git a/contract/test/lendingPool/test-lendingPool.js b/contract/test/lendingPool/test-lendingPool.js index 31ff4ea..fc2790d 100644 --- a/contract/test/lendingPool/test-lendingPool.js +++ b/contract/test/lendingPool/test-lendingPool.js @@ -25,7 +25,7 @@ import { calculateProtocolFromUnderlying, splitCollateral, } from './helpers.js'; - +import { eventLoopIteration } from '@agoric/zoe/tools/eventLoopIteration.js'; import { setupServices, CONTRACT_ROOTS, @@ -49,15 +49,6 @@ const trace = makeTracer('TestST'); const BASIS_POINTS = 10000n; const secondsPerDay = SECONDS_PER_YEAR / 365n; -// Some notifier updates aren't propagating sufficiently quickly for the tests. -// This invocation (thanks to Warner) waits for all promises that can fire to -// have all their callbacks run -export async function waitForPromisesToSettle() { - const pk = makePromiseKit(); - setImmediate(pk.resolve); - return pk.promise; -} - /** * Runs before every test separetly and injects necessary data to its `context` * property. @@ -251,7 +242,7 @@ test('borrow', async t => { priceCheckPeriod: secondsPerDay * 5n * 2n, }; - t.plan(27); + t.plan(29); // Start services const { lendingPool: { lendingPoolPublicFacet }, assertions, scenarioHelpers } = await setupServices( @@ -320,12 +311,11 @@ test('borrow-rate-fluctuate', async t => { * zoe: ZoeService }} TestContext */ const { - vanKit: { mint: vanMint, issuer: vanIssuer, brand: vanBrand }, + vanKit: { brand: vanBrand }, compareCurrencyKit: { brand: usdBrand }, - panKit: { mint: panMint, issuer: panIssuer, brand: panBrand }, + panKit: { brand: panBrand }, vanRates, panRates, - zoe, } = t.context; // Set loan timing @@ -404,14 +394,14 @@ test('borrow-rate-fluctuate', async t => { checkPanPoolStateInSync(), ]); // Accrue some interest - await timer.tick(); - await waitForPromisesToSettle(); + await timer.advanceTo(secondsPerDay * 7n); + await eventLoopIteration(); const expectedValuesAfterInterest = { principalDebt: 6n * 10n ** 6n, accruedInterest: 3183n, exchangeRateNumerator: 2000016n, - borrowingRate: 281n, + borrowingRate: 280n, }; await Promise.all([ @@ -715,7 +705,7 @@ test('adjust-balances-interest-accrued', async t => { // Accrue interst by one chargingPeriod await timer.tick(); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterInterestCharged = { principalDebt: 4n * 10n ** 6n, @@ -756,7 +746,7 @@ test('adjust-balances-interest-accrued', async t => { // Accrue one more chargingPeriod of interest await timer.tick(); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterSecondInterestCharged = { principalDebt: 4n * 10n ** 6n + 7n * 10n ** 6n + 280n, @@ -1054,7 +1044,7 @@ test('redeem-underlying', async t => { // interest time await timer.tick(); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterInterest = { principalDebt: 4n * 10n ** 6n, @@ -1175,7 +1165,7 @@ test('collateral-price-drop-liquidate', async t => { // Collateral price goes down, new max amount of debt is 66 USD worth PAN // This means that we're now underwater, so liquidation should be triggerred vanUsdPriceAuthority.setPrice(makeRatio(100n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterLiquidation = { debtAmount: AmountMath.make(panBrand, 35n * 10n ** 6n + 3021n), @@ -1310,7 +1300,7 @@ test('close-the-first-loan-liquidate-second', async t => { // Collateral price goes down, new max amount of debt is 66 USD worth PAN // This means that we're now underwater, so liquidation should be triggerred vanUsdPriceAuthority.setPrice(makeRatio(100n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterLiquidation = { initialLiquidityBeforeLoan: AmountMath.make(panBrand, 10n * 10n ** 8n), @@ -1409,7 +1399,7 @@ test('debt-price-up-liquidate', async t => { // Value of the debt is now 77 USD, so liquidate panUsdPriceAuthority.setPrice(makeRatio(220n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, panBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterLiquidation = { initialLiquidityBeforeLoan: AmountMath.make(panBrand, 10n * 10n ** 8n), @@ -1454,7 +1444,7 @@ test('debt-price-up-col-price-down-liquidate', async t => { panInitialLiquidityValue: 10n ** 8n * 100n, }, ); - await waitForPromisesToSettle(); + await eventLoopIteration(); const { assertEnoughLiquidityInPool, @@ -1517,7 +1507,7 @@ test('debt-price-up-col-price-down-liquidate', async t => { panUsdPriceAuthority.setPrice(makeRatio(190n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, panBrand)); vanUsdPriceAuthority.setPrice(makeRatio(102n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); // Check market state after price change await Promise.all([ @@ -1530,7 +1520,7 @@ test('debt-price-up-col-price-down-liquidate', async t => { panUsdPriceAuthority.setPrice(makeRatio(193n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, panBrand)); vanUsdPriceAuthority.setPrice(makeRatio(100n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterLiquidation = { initialLiquidityBeforeLoan: AmountMath.make(panBrand, 10n * 10n ** 8n), @@ -1572,7 +1562,7 @@ test('prices-fluctuate-multiple-loans-liquidate', async t => { buildManualTimer(console.log, 0n, secondsPerDay), ); - await waitForPromisesToSettle(); + await eventLoopIteration(); const { assertEnoughLiquidityInPool, @@ -1692,7 +1682,7 @@ test('prices-fluctuate-multiple-loans-liquidate', async t => { panUsdPriceAuthority.setPrice(makeRatio(190n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, panBrand)); vanUsdPriceAuthority.setPrice(makeRatio(106n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); await Promise.all([ assertActiveLoan(aliceLoan), @@ -1709,7 +1699,7 @@ test('prices-fluctuate-multiple-loans-liquidate', async t => { panUsdPriceAuthority.setPrice(makeRatio(193n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, panBrand)); vanUsdPriceAuthority.setPrice(makeRatio(100n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterAliceLiquidation = { initialLiquidityBeforeLoan: AmountMath.make(panBrand, 10n * 10n ** 8n - 35n * 10n ** 6n - 4n * 10n ** 6n), @@ -1826,7 +1816,7 @@ test('prices-hold-still-liquidates-with-interest-accrual', async t => { // On one tick 10 periods of interest will accrue in a compounded manner await timer.tick() - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterInterest = { principalDebt: 4019n * 10n ** 4n, @@ -1844,7 +1834,7 @@ test('prices-hold-still-liquidates-with-interest-accrual', async t => { // A price update is necessary to initiate liquidation vanUsdPriceAuthority.setPrice(makeRatio(108n * 10n ** 6n, usdBrand, 1n * 10n ** 8n, vanBrand)); - await waitForPromisesToSettle(); + await eventLoopIteration(); const expectedValuesAfterLiquidation = { initialLiquidityBeforeLoan: AmountMath.make(panBrand, 10n * 10n ** 8n),