Skip to content

Commit

Permalink
scenario/, spec/, tests/: add tests for supply cap
Browse files Browse the repository at this point in the history
  • Loading branch information
bun919tw committed Jan 19, 2021
1 parent 2b66163 commit 4ede85b
Show file tree
Hide file tree
Showing 26 changed files with 301 additions and 28 deletions.
1 change: 1 addition & 0 deletions scenario/src/Contract/CToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface CTokenMethods {
pendingAdmin(): Callable<string>;
_setPendingAdmin(address: string): Sendable<number>;
_acceptAdmin(): Sendable<number>;
gulp(): Sendable<void>;
}

export interface CTokenScenarioMethods extends CTokenMethods {
Expand Down
4 changes: 4 additions & 0 deletions scenario/src/Contract/Comptroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ interface ComptrollerMethods {
_setBorrowCapGuardian(string): Sendable<void>
borrowCapGuardian(): Callable<string>
borrowCaps(string): Callable<string>
_setMarketSupplyCaps(cTokens:string[], supplyCaps:encodedNumber[]): Sendable<void>
_setSupplyCapGuardian(string): Sendable<void>
supplyCapGuardian(): Callable<string>
supplyCaps(string): Callable<string>
}

export interface Comptroller extends Contract {
Expand Down
24 changes: 24 additions & 0 deletions scenario/src/Event/CTokenEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ async function evilSeize(world: World, from: string, cToken: CToken, treasure: C
return world;
}

async function gulp(world: World, from: string, cToken: CToken): Promise<World> {
let invokation = await invoke(world, cToken.methods.gulp(), from, CTokenErrorReporter);

world = addAction(
world,
`CToken ${cToken.name}: Gulp`,
invokation
);

return world;
}

async function setPendingAdmin(world: World, from: string, cToken: CToken, newPendingAdmin: string): Promise<World> {
let invokation = await invoke(world, cToken.methods._setPendingAdmin(newPendingAdmin), from, CTokenErrorReporter);

Expand Down Expand Up @@ -653,6 +665,18 @@ export function cTokenCommands() {
(world, from, { cToken, treasure, liquidator, borrower, seizeTokens }) => evilSeize(world, from, cToken, treasure, liquidator.val, borrower.val, seizeTokens),
{ namePos: 1 }
),
new Command<{ cToken: CToken}>(`
#### Gulp
* "CToken <cToken> Gulp" - Gulps for the cToken
* E.g. "CToken cZRX Gulp"
`,
"Gulp",
[
new Arg("cToken", getCTokenV)
],
(world, from, { cToken }) => gulp(world, from, cToken),
{ namePos: 1 }
),
new Command<{ cToken: CToken, amount: NumberV }>(`
#### ReduceReserves
Expand Down
49 changes: 49 additions & 0 deletions scenario/src/Event/ComptrollerEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,30 @@ async function setBorrowCapGuardian(world: World, from: string, comptroller: Com
return world;
}

async function setMarketSupplyCaps(world: World, from: string, comptroller: Comptroller, cTokens: CToken[], supplyCaps: NumberV[]): Promise<World> {
let invokation = await invoke(world, comptroller.methods._setMarketSupplyCaps(cTokens.map(c => c._address), supplyCaps.map(c => c.encode())), from, ComptrollerErrorReporter);

world = addAction(
world,
`Supply caps on ${cTokens} set to ${supplyCaps}`,
invokation
);

return world;
}

async function setSupplyCapGuardian(world: World, from: string, comptroller: Comptroller, newSupplyCapGuardian: string): Promise<World> {
let invokation = await invoke(world, comptroller.methods._setSupplyCapGuardian(newSupplyCapGuardian), from, ComptrollerErrorReporter);

world = addAction(
world,
`Comptroller: ${describeUser(world, from)} sets supply cap guardian to ${newSupplyCapGuardian}`,
invokation
);

return world;
}

export function comptrollerCommands() {
return [
new Command<{comptrollerParams: EventV}>(`
Expand Down Expand Up @@ -840,6 +864,31 @@ export function comptrollerCommands() {
new Arg("newBorrowCapGuardian", getAddressV)
],
(world, from, {comptroller, newBorrowCapGuardian}) => setBorrowCapGuardian(world, from, comptroller, newBorrowCapGuardian.val)
),
new Command<{comptroller: Comptroller, cTokens: CToken[], supplyCaps: NumberV[]}>(`
#### SetMarketSupplyCaps
* "Comptroller SetMarketSupplyCaps (<CToken> ...) (<supplyCap> ...)" - Sets Market Supply Caps
* E.g. "Comptroller SetMarketSupplyCaps (cZRX cUSDC) (10000.0e18, 1000.0e6)
`,
"SetMarketSupplyCaps",
[
new Arg("comptroller", getComptroller, {implicit: true}),
new Arg("cTokens", getCTokenV, {mapped: true}),
new Arg("supplyCaps", getNumberV, {mapped: true})
],
(world, from, {comptroller, cTokens, supplyCaps}) => setMarketSupplyCaps(world, from, comptroller, cTokens, supplyCaps)
),
new Command<{comptroller: Comptroller, newSupplyCapGuardian: AddressV}>(`
#### SetSupplyCapGuardian
* "Comptroller SetSupplyCapGuardian newSupplyCapGuardian:<Address>" - Sets the Supply Cap Guardian for the Comptroller
* E.g. "Comptroller SetSupplyCapGuardian Geoff"
`,
"SetSupplyCapGuardian",
[
new Arg("comptroller", getComptroller, {implicit: true}),
new Arg("newSupplyCapGuardian", getAddressV)
],
(world, from, {comptroller, newSupplyCapGuardian}) => setSupplyCapGuardian(world, from, comptroller, newSupplyCapGuardian.val)
)
];
}
Expand Down
26 changes: 25 additions & 1 deletion scenario/src/Value/ComptrollerValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,31 @@ export function comptrollerFetchers() {
async (world, {comptroller, CToken}) => {
return new NumberV(await comptroller.methods.borrowCaps(CToken._address).call());
}
)
),
new Fetcher<{comptroller: Comptroller}, AddressV>(`
#### SupplyCapGuardian
* "SupplyCapGuardian" - Returns the Comptrollers's SupplyCapGuardian
* E.g. "Comptroller SupplyCapGuardian"
`,
"SupplyCapGuardian",
[
new Arg("comptroller", getComptroller, {implicit: true})
],
async (world, {comptroller}) => new AddressV(await comptroller.methods.supplyCapGuardian().call())
),
new Fetcher<{comptroller: Comptroller, CToken: CToken}, NumberV>(`
#### SupplyCaps
* "Comptroller SupplyCaps cZRX
`,
"SupplyCaps",
[
new Arg("comptroller", getComptroller, {implicit: true}),
new Arg("CToken", getCTokenV),
],
async (world, {comptroller, CToken}) => {
return new NumberV(await comptroller.methods.supplyCaps(CToken._address).call());
}
),
];
}

Expand Down
8 changes: 4 additions & 4 deletions spec/scenario/Borrow.scen
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Test "Borrow some BAT and enters BAT if BAT not entered"
NewComptroller price:1.0
NewCToken ZRX cZRX
NewCToken BAT cBAT
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down Expand Up @@ -53,7 +53,7 @@ Test "Borrow fails if market not listed"
NewComptroller price:1.0
NewCToken ZRX cZRX
NewCToken BAT cBAT
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Mint Geoff 100e18 cZRX
Expand All @@ -67,7 +67,7 @@ Test "Borrow some BAT from Excess Cash"
NewComptroller price:1.0
NewCToken ZRX cZRX
NewCToken BAT cBAT
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand All @@ -83,7 +83,7 @@ Test "Borrow some BAT reverts if borrow is paused"
Comptroller SetPauseGuardian Coburn
NewCToken ZRX cZRX
NewCToken BAT cBAT
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down
2 changes: 1 addition & 1 deletion spec/scenario/BorrowBalance.scen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Macro NewBorrow borrowAmount borrowRate user=Geoff
NewComptroller price:1.0 -- TODO: This should really be a price for a specific asset
NewCToken ZRX cZRX
NewCToken BAT cBAT borrowRate -- note: cannot use macros with named args right now
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
SimpleBorrow user borrowAmount
Expand Down
10 changes: 5 additions & 5 deletions spec/scenario/BorrowCap.scen
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Test "Attempt to borrow over set cap ERC20"
NewCToken BAT cBAT
Comptroller SetMarketBorrowCaps (cBAT) (0.5e18)
Assert Equal (Comptroller BorrowCaps cBAT) (Exactly 0.5e18)
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand All @@ -23,7 +23,7 @@ Test "Attempt to borrow at set cap ERC20"
NewCToken ZRX cZRX
NewCToken BAT cBAT
Comptroller SetMarketBorrowCaps (cBAT) (1000000000000000001)
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand All @@ -42,7 +42,7 @@ Test "Attempt to borrow below set cap ERC20"
NewCToken ZRX cZRX
NewCToken BAT cBAT
Comptroller SetMarketBorrowCaps (cBAT) (10e18)
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down Expand Up @@ -148,8 +148,8 @@ Test "SetBorrowCaps works correctly too"
Comptroller SetMarketBorrowCaps (cBAT cUSDC) (0.5e18 1000001)
Assert Equal (Comptroller BorrowCaps cBAT) (Exactly 0.5e18)
Assert Equal (Comptroller BorrowCaps cUSDC) (Exactly 1000001)
Give cBAT 10e18 BAT -- Faucet some bat to borrow
Give cUSDC 20e6 USDC
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cUSDC 20e6 USDC
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Support cUSDC collateralFactor:0.5
Expand Down
6 changes: 3 additions & 3 deletions spec/scenario/BorrowWBTC.scen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Test "Borrow some WBTC enters WBTC and succeeds when not entered"
NewComptroller price:1.0
NewCToken ZRX cZRX
NewCToken WBTC cWBTC tokenType:WBTC
Give cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
GiveCToken cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
Support cZRX collateralFactor:0.5
Support cWBTC collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down Expand Up @@ -33,7 +33,7 @@ Test "Borrow some WBTC fails when WBTC paused"
NewComptroller price:1.0
NewCToken ZRX cZRX
NewCToken WBTC cWBTC tokenType:WBTC
Give cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
GiveCToken cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
Support cZRX collateralFactor:0.5
Support cWBTC collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand All @@ -50,7 +50,7 @@ Test "Borrow some WBTC from Excess Cash"
NewComptroller price:1.0
NewCToken ZRX cZRX
NewCToken WBTC cWBTC tokenType:WBTC
Give cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
GiveCToken cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
Support cZRX collateralFactor:0.5
Support cWBTC collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down
2 changes: 1 addition & 1 deletion spec/scenario/BreakLiquidate.scen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Macro NewBorrow borrowAmount mintAmount borrowRate=0.000005 user=Geoff collatera
Comptroller LiquidationIncentive liquidationIncentive
NewCToken ZRX cZRX
NewCToken BAT cBAT borrowRate
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
PriceOracle SetPrice cZRX collateralPrice
Support cZRX collateralFactor:0.7
PriceOracle SetPrice cBAT borrowPrice
Expand Down
17 changes: 17 additions & 0 deletions spec/scenario/ChangeDelegate.scen
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,20 @@ Test "Change the delegate"
CTokenDelegate Deploy CErc20Delegate cErc20Delegate2
CToken cDEL SetImplementation (CTokenDelegate cErc20Delegate2 Address) True "0x0"
Redeem Jared 50e9 cDEL

Test "Update the delegate for internal cash"
NewComptroller
NewCToken ZRX cZRX
Support cZRX collateralFactor:0.5
Prep Jared Some ZRX cZRX
Mint Jared 100e18 cZRX
Give cZRX 100e18 ZRX -- Transfer 100e18 ZRX to cZRX
-- Cash: 100e18, Balance: 200e18
Assert Equal (CToken cZRX Cash) (Exactly 100e18)
Assert Equal (Erc20 ZRX TokenBalance cZRX) (Exactly 200e18)
-- New Deleagte
CTokenDelegate Deploy CErc20Delegate cErc20Delegate2
CToken cZRX SetImplementation (CTokenDelegate cErc20Delegate2 Address) True "0x0"
-- Cash: 200e18, Balance: 200e18
Assert Equal (CToken cZRX Cash) (Exactly 200e18)
Assert Equal (Erc20 ZRX TokenBalance cZRX) (Exactly 200e18)
4 changes: 4 additions & 0 deletions spec/scenario/CoreMacros
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ Macro SetPriceCF cToken price collateralFactor
Macro Give user amount erc20
Erc20 erc20 Faucet user amount

Macro GiveCToken cToken amount erc20
Erc20 erc20 Faucet cToken amount
CToken cToken Gulp

Macro Donate token amount
(Trx Value amount (CToken token Donate))

Expand Down
2 changes: 1 addition & 1 deletion spec/scenario/EnterExitMarkets.scen
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Test "Realistic Market Scenario"
EnterMarkets Geoff cZRX
Assert Equal (Comptroller Liquidity Geoff) 1.0e18
-- Fail to borrow BAT due to liquidity
Give cBAT 1000e18 BAT
GiveCToken cBAT 1000e18 BAT
HoldInvariants
Borrow Geoff 1000e18 cBAT -- 1000e18 * 0.0015 = 1.5e18 required liquidity
-- But since we're only in ZRX, we only have 1.0e18 liquidity
Expand Down
2 changes: 2 additions & 0 deletions spec/scenario/ExchangeRate.scen
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Test "ZRX: Exch. Rate:2e9, Cash(51e18) + Borrows(2.0e18) - Reserves(0.5e18) / To
-- Set cash
Erc20 ZRX Faucet cZRX 1.0e18
Assert Equal (Erc20 ZRX TokenBalance cZRX) (Exactly 51.0e18)
CToken cZRX Gulp
-- Mock total borrows
CToken cZRX Mock totalBorrows 2.0e18
Assert Equal (CToken cZRX TotalBorrows) (Exactly 2.0e18)
Expand All @@ -62,6 +63,7 @@ Test "USDC: Exch. Rate:2e-3, Cash(51e18) + Borrows(2.0e18) - Reserves(0.5e18) /
-- Set cash
Erc20 USDC Faucet cUSDC 2.0e6
Assert Equal (Erc20 USDC TokenBalance cUSDC) (Exactly 52.0e6)
CToken cUSDC Gulp
-- Mock total borrows
CToken cUSDC Mock totalBorrows 5.0e6
Assert Equal (CToken cUSDC TotalBorrows) (Exactly 5.0e6)
Expand Down
4 changes: 2 additions & 2 deletions spec/scenario/Fee.scen
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Test "Repay borrow should work and not change exchange rate"
PriceOracle SetPrice cUSDT 1.0
Support cZRX 0.5
Support cUSDT 0.5
Give cUSDT 10e18 USDT -- Faucet some Tether to borrow
GiveCToken cUSDT 10e18 USDT -- Faucet some Tether to borrow
Invariant Static (CToken cUSDT ExchangeRate)
Prep Torrey 100e18 ZRX cZRX
Mint Torrey 100e18 cZRX
Expand All @@ -52,7 +52,7 @@ Test "Should be able to liquidate fee token borrow"
PriceOracle SetPrice cUSDT 1.0
Support cZRX 0.5
Support cUSDT 0.5
Give cUSDT 10e18 USDT -- Faucet some Tether to borrow
GiveCToken cUSDT 10e18 USDT -- Faucet some Tether to borrow
Invariant Static (CToken cUSDT ExchangeRate)
Invariant Static (CToken cZRX ExchangeRate)
Prep Torrey 2e18 ZRX cZRX
Expand Down
2 changes: 1 addition & 1 deletion spec/scenario/InKindLiquidation.scen
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Macro InKindBorrow borrowAmount borrowRate user=Geoff borrowPrice=1.0 mintAmount
PricedComptroller
Comptroller LiquidationIncentive 1.1
NewCToken BAT cBAT borrowRate 2e9 8 borrowTokenType -- note: cannot use macros with named args right now
Give cBAT giveAmount BAT -- Faucet some bat
GiveCToken cBAT giveAmount BAT -- Faucet some bat
PriceOracle SetPrice cBAT borrowPrice
Support cBAT collateralFactor:0.5
Prep user mintAmount BAT cBAT
Expand Down
2 changes: 1 addition & 1 deletion spec/scenario/RepayBorrow.scen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Macro NewBorrow borrowAmount borrowRate
NewComptroller price:1.0 -- TODO: This should really be a price for a specific asset
NewCToken ZRX cZRX
NewCToken BAT cBAT borrowRate -- note: cannot use macros with named args right now
Give cBAT 10e18 BAT -- Faucet some bat to borrow
GiveCToken cBAT 10e18 BAT -- Faucet some bat to borrow
Support cZRX collateralFactor:0.5
Support cBAT collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down
2 changes: 1 addition & 1 deletion spec/scenario/RepayBorrowWBTC.scen
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Macro NewBorrow borrowAmount borrowRate
NewComptroller price:1.0 -- TODO: This should really be a price for a specific asset
NewCToken ZRX cZRX
NewCToken WBTC cWBTC borrowRate 0.1 8 WBTC -- note: cannot use macros with named args right now
Give cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
GiveCToken cWBTC 10e8 WBTC -- Faucet some WBTC to borrow
Support cZRX collateralFactor:0.5
Support cWBTC collateralFactor:0.5
Prep Geoff Some ZRX cZRX
Expand Down
Loading

0 comments on commit 4ede85b

Please sign in to comment.