-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
556094d
commit 3c5af2e
Showing
5 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters