Skip to content

Commit

Permalink
ReserveConstraint
Browse files Browse the repository at this point in the history
  • Loading branch information
scott-silver committed Jun 27, 2023
1 parent 556094d commit 3c5af2e
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions scenario/WithdrawReservesScenario.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { expect } from 'chai';
scenario(
'Comet#withdrawReserves > governor withdraw reserves',
{
reserves: '>= 150000',
tokenBalances: {
betty: { $base: '== 100000' },
albert: { $base: '== 0' },
Expand Down
1 change: 1 addition & 0 deletions scenario/constraints/Requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export interface Requirements {
tokenBalances?: object; // Token balance constraint
utilization?: number; // Utilization constraint
prices?: object; // Price constraint
reserves?: number | string // Reserves constraint
}
66 changes: 66 additions & 0 deletions scenario/constraints/ReservesConstraint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Constraint, Solution } from '../../plugins/scenario';
import { CometContext } from '../context/CometContext';
import CometActor from '../context/CometActor';
import { expect } from 'chai';
import { Requirements } from './Requirements';
import { baseBalanceOf, exp, factorScale } from '../../test/helpers';
import { ComparativeAmount, ComparisonOp, getAssetFromName, parseAmount, getExpectedBaseBalance, getToTransferAmount } from '../utils';
import { BigNumber } from 'ethers';

export class ReservesConstraint<T extends CometContext, R extends Requirements> implements Constraint<T, R> {
async solve(requirements: R, initialContext: T) {
const reservesRequirement = requirements.reserves;
if (reservesRequirement !== undefined) {

const comet = await initialContext.getComet();

const solutions: Solution<T>[] = [];
solutions.push(async function barelyMeet(context: T) {
const baseToken = await comet.baseToken();
const currentReserves = (await comet.getReserves()).toBigInt();
const amount = parseAmount(reservesRequirement);
const decimals = await comet.decimals();

expect(amount.op).to.equal(ComparisonOp.GTE, `Operation ${amount.op} not supported (yet) by reserve cap constraint`);

const amountToSource = getToTransferAmount(amount, currentReserves, decimals);
// add buffer to adjust for interest accrual
await context.sourceTokens(amountToSource * 105n / 100n, baseToken, comet.address);

return context;
});
return solutions;
} else {
return null;
}
}

async check(requirements: R, context: T) {
const reservesRequirement = requirements.reserves;
if (reservesRequirement !== undefined) {
const comet = await context.getComet();
const amount = parseAmount(reservesRequirement);
const decimals = await comet.decimals();
const currentReserves = (await comet.getReserves()).toBigInt();
const expectedReserves = exp(amount.val, decimals);

switch (amount.op) {
case ComparisonOp.EQ:
expect(currentReserves).to.equal(expectedReserves);
break;
case ComparisonOp.GTE:
expect(currentReserves).to.be.at.least(expectedReserves);
break;
case ComparisonOp.LTE:
expect(currentReserves).to.be.at.most(expectedReserves);
break;
case ComparisonOp.GT:
expect(currentReserves).to.be.above(expectedReserves);
break;
case ComparisonOp.LT:
expect(currentReserves).to.be.below(expectedReserves);
break;
}
}
}
}
1 change: 1 addition & 0 deletions scenario/constraints/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { MigrationConstraint } from './MigrationConstraint';
export { ProposalConstraint } from './ProposalConstraint';
export { FilterConstraint } from './FilterConstraint';
export { PriceConstraint } from './PriceConstraint';
export { ReservesConstraint } from './ReservesConstraint';
6 changes: 4 additions & 2 deletions scenario/context/CometContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
MigrationConstraint,
ProposalConstraint,
FilterConstraint,
PriceConstraint
PriceConstraint,
ReservesConstraint
} from '../constraints';
import CometActor from './CometActor';
import CometAsset from './CometAsset';
Expand Down Expand Up @@ -414,7 +415,8 @@ export const dynamicConstraints: DynamicConstraint<CometContext, Requirements>[]
new CometBalanceConstraint(),
new TokenBalanceConstraint(),
new UtilizationConstraint(),
new PriceConstraint()
new PriceConstraint(),
new ReservesConstraint()
];

export const scenarioLoader = Loader.get<CometContext, CometProperties, Requirements>().configure(
Expand Down

0 comments on commit 3c5af2e

Please sign in to comment.