This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Exec upgrade (defer liquidity for multiple accounts, max amount support on pToken wrap/unwrap) #163
Open
kasperpawlowski
wants to merge
4
commits into
master
Choose a base branch
from
exec-upgrade
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c1684f8
defer liquidity check on more than one account, support max wrap/unwr…
kasperpawlowski e643f8f
add test for max wrap/unwrap of pTokens
kasperpawlowski b4edbe4
add tests for defer liquidity checks
kasperpawlowski 52d68ac
updated as per review remarks
kasperpawlowski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../modules/Exec.sol"; | ||
import "../modules/Markets.sol"; | ||
import "../modules/DToken.sol"; | ||
|
||
|
||
contract DeferredLiquidityCheckTest is IDeferredLiquidityCheck { | ||
uint constant AMOUNT = 1 ether; | ||
address euler; | ||
address markets; | ||
address exec; | ||
|
||
event onDeferredLiquidityCheckEvent(); | ||
|
||
constructor(address eulerAddr, address marketsAddr, address execAddr) { | ||
euler = eulerAddr; | ||
markets = marketsAddr; | ||
exec = execAddr; | ||
} | ||
|
||
function getSubAccount(uint subAccountId) internal view returns (address) { | ||
require(subAccountId < 256, "sub-account-id-too-big"); | ||
return address(uint160(address(this)) ^ uint160(subAccountId)); | ||
} | ||
|
||
function onDeferredLiquidityCheck(bytes memory data) external override { | ||
(address underlying, address[] memory accounts, uint scenario) = abi.decode(data, (address, address[], uint)); | ||
|
||
address dToken = Markets(markets).underlyingToDToken(underlying); | ||
IERC20(underlying).approve(euler, type(uint).max); | ||
emit onDeferredLiquidityCheckEvent(); | ||
|
||
if (scenario == 1) { | ||
DToken(dToken).borrow(0, AMOUNT); | ||
DToken(dToken).repay(0, AMOUNT); | ||
} else if (scenario == 2) { | ||
DToken(dToken).borrow(0, AMOUNT); | ||
DToken(dToken).borrow(1, AMOUNT); | ||
DToken(dToken).repay(0, AMOUNT); | ||
DToken(dToken).repay(1, AMOUNT); | ||
} else if (scenario == 3) { | ||
Exec(exec).deferLiquidityCheckMulti(accounts, abi.encode(underlying, accounts, 1)); | ||
} else if (scenario == 4) { | ||
Exec(exec).deferLiquidityCheck(accounts[accounts.length - 1], abi.encode(underlying, accounts, 1)); | ||
} else if (scenario == 5) { | ||
address account = getSubAccount(1); | ||
accounts[0] = account; | ||
Exec(exec).deferLiquidityCheck(account, abi.encode(underlying, accounts, 1)); | ||
Exec(exec).deferLiquidityCheck(account, abi.encode(underlying, accounts, 2)); | ||
Exec(exec).deferLiquidityCheckMulti(accounts, abi.encode(underlying, accounts, 1)); | ||
Exec(exec).deferLiquidityCheckMulti(accounts, abi.encode(underlying, accounts, 2)); | ||
} else if (scenario == 6) { | ||
Exec.EulerBatchItem[] memory items = new Exec.EulerBatchItem[](2); | ||
items[0] = Exec.EulerBatchItem(false, dToken, abi.encodeWithSelector(DToken.borrow.selector, 0, AMOUNT)); | ||
items[1] = Exec.EulerBatchItem(false, dToken, abi.encodeWithSelector(DToken.repay.selector, 0, AMOUNT)); | ||
accounts[0] = getSubAccount(0); | ||
Exec(exec).batchDispatch(items, accounts); | ||
} else if (scenario == 7) { | ||
Exec.EulerBatchItem[] memory items = new Exec.EulerBatchItem[](2); | ||
items[0] = Exec.EulerBatchItem(false, dToken, abi.encodeWithSelector(DToken.borrow.selector, 0, AMOUNT)); | ||
items[1] = Exec.EulerBatchItem(false, dToken, abi.encodeWithSelector(DToken.repay.selector, 0, AMOUNT)); | ||
accounts[0] = getSubAccount(0); | ||
accounts[1] = address(0); | ||
Exec(exec).batchDispatch(items, accounts); | ||
} else { | ||
revert("onDeferredLiquidityCheck: wrong scenario"); | ||
} | ||
} | ||
|
||
function test(address underlying, address[] memory accounts, uint scenario) external { | ||
if (scenario == 1) { | ||
Exec(exec).deferLiquidityCheck(accounts[0], abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 2) { | ||
Exec(exec).deferLiquidityCheckMulti(accounts, abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 3) { | ||
Exec(exec).deferLiquidityCheck(accounts[0], abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 4) { | ||
Exec(exec).deferLiquidityCheckMulti(accounts, abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 5) { | ||
Exec(exec).deferLiquidityCheck(accounts[0], abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 6) { | ||
Exec(exec).deferLiquidityCheck(accounts[0], abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 7) { | ||
Exec(exec).deferLiquidityCheckMulti(accounts, abi.encode(underlying, accounts, scenario)); | ||
} else if (scenario == 8) { | ||
Exec.EulerBatchItem[] memory items = new Exec.EulerBatchItem[](1); | ||
items[0] = Exec.EulerBatchItem( | ||
false, | ||
exec, | ||
abi.encodeWithSelector( | ||
Exec.deferLiquidityCheck.selector, | ||
accounts[0], | ||
abi.encode(underlying, accounts, 1) | ||
) | ||
); | ||
Exec(exec).batchDispatch(items, accounts); | ||
} else if (scenario == 9) { | ||
Exec.EulerBatchItem[] memory items = new Exec.EulerBatchItem[](1); | ||
items[0] = Exec.EulerBatchItem( | ||
false, | ||
exec, | ||
abi.encodeWithSelector( | ||
Exec.deferLiquidityCheckMulti.selector, | ||
accounts, | ||
abi.encode(underlying, accounts, 1) | ||
) | ||
); | ||
Exec(exec).batchDispatch(items, accounts); | ||
} else if (scenario == 10) { | ||
Exec.EulerBatchItem[] memory items = new Exec.EulerBatchItem[](1); | ||
items[0] = Exec.EulerBatchItem( | ||
false, | ||
exec, | ||
abi.encodeWithSelector( | ||
Exec.deferLiquidityCheck.selector, | ||
getSubAccount(1), | ||
abi.encode(underlying, accounts, 1) | ||
) | ||
); | ||
Exec(exec).batchDispatch(items, accounts); | ||
} else { | ||
revert("test: wrong scenario"); | ||
} | ||
} | ||
} |
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,153 @@ | ||
const et = require('./lib/eTestLib'); | ||
|
||
et.testSet({ | ||
desc: "defer liquidity check", | ||
|
||
preActions: ctx => [ | ||
{ send: 'tokens.TST.mint', args: [ctx.wallet.address, et.eth(100)], }, | ||
{ send: 'tokens.TST.approve', args: [ctx.contracts.euler.address, et.MaxUint256,], }, | ||
{ send: 'eTokens.eTST.deposit', args: [0, et.MaxUint256], }, | ||
{ action: 'updateUniswapPrice', pair: 'TST/WETH', price: '.01', }, | ||
{ action: 'cb', cb: async () => { | ||
ctx.contracts.deferredLiquidityCheckTest = await (await ctx.factories.DeferredLiquidityCheckTest.deploy( | ||
ctx.contracts.euler.address, | ||
ctx.contracts.markets.address, | ||
ctx.contracts.exec.address, | ||
)).deployed(); | ||
}} | ||
], | ||
}) | ||
|
||
.test({ | ||
desc: "simple defer liquidity check", | ||
actions: ctx => [ | ||
// should revert as liquidity deferred for wrong address | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.AddressZero ], 1], | ||
expectError: 'e/collateral-violation' | ||
}, | ||
|
||
// should pass as liquidity deferred for correct address | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ ctx.contracts.deferredLiquidityCheckTest.address ], 1], | ||
onLogs: logs => { | ||
et.expect(logs.findIndex(log => log.name === "onDeferredLiquidityCheckEvent")).to.gt(-1); | ||
}}, | ||
], | ||
}) | ||
|
||
.test({ | ||
desc: "extended defer liquidity check", | ||
actions: ctx => [ | ||
// should revert as liquidity deferred only for one address | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 0) ], 2], | ||
expectError: 'e/collateral-violation' | ||
}, | ||
|
||
// should pass as liquidity deferred for both addresses | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 0), et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1) ], 2], | ||
onLogs: logs => { | ||
et.expect(logs.findIndex(log => log.name === "onDeferredLiquidityCheckEvent")).to.gt(-1); | ||
}}, | ||
], | ||
}) | ||
|
||
.test({ | ||
desc: "defer liquidity check - reentrancies", | ||
actions: ctx => [ | ||
// should revert due to reentrancy enforced by scenario 3 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ ctx.contracts.deferredLiquidityCheckTest.address ], 3], | ||
expectError: 'e/defer/reentrancy' | ||
}, | ||
|
||
// should revert due to reentrancy enforced by scenario 4 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, | ||
[ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 0), | ||
et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1), | ||
et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 2) | ||
], 4], | ||
expectError: 'e/defer/reentrancy' | ||
}, | ||
|
||
// should pass as scenario 5 re-enters, but defers liquidity for an address not deferred before | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ ctx.contracts.deferredLiquidityCheckTest.address ], 5], | ||
onLogs: logs => { | ||
for(const i = 0; i < 4; i++) { | ||
if (i > 0) { | ||
logs.splice(index, 1) | ||
} | ||
const index = logs.findIndex(log => log.name === "onDeferredLiquidityCheckEvent") | ||
et.expect(index).to.gt(-1); | ||
} | ||
}}, | ||
], | ||
}) | ||
|
||
.test({ | ||
desc: "batch dispatch from defer liquidity check", | ||
actions: ctx => [ | ||
// should revert due to reentrancy enforced from defer liquidity check in scenario 6 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ ctx.contracts.deferredLiquidityCheckTest.address ], 6], | ||
expectError: 'e/batch/reentrancy' | ||
}, | ||
|
||
// should pass as defer liquidity check defers liquidity for different account than batch dispatch called from defer liquidity check | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1) ], 6], | ||
onLogs: logs => { | ||
et.expect(logs.findIndex(log => log.name === "onDeferredLiquidityCheckEvent")).to.gt(-1); | ||
}}, | ||
|
||
// should revert due to reentrancy enforced from defer liquidity check in scenario 7 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 0), et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1) ], 7], | ||
expectError: 'e/batch/reentrancy' | ||
}, | ||
|
||
// should pass as defer liquidity check defers liquidity for different account than batch dispatch called from defer liquidity check | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1), et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 2) ], 7], | ||
onLogs: logs => { | ||
et.expect(logs.findIndex(log => log.name === "onDeferredLiquidityCheckEvent")).to.gt(-1); | ||
}}, | ||
], | ||
}) | ||
|
||
.test({ | ||
desc: "defer liquidity check from batch dispatch", | ||
actions: ctx => [ | ||
// should revert due to reentrancy enforced from batch dispatch in scenario 8 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ ctx.contracts.deferredLiquidityCheckTest.address ], 8], | ||
expectError: 'e/defer/reentrancy' | ||
}, | ||
|
||
// should revert due to reentrancy enforced from batch dispatch in scenario 9 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 0), et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1) ], 9], | ||
expectError: 'e/defer/reentrancy' | ||
}, | ||
|
||
// should revert due to reentrancy enforced from batch dispatch in scenario 10 | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 0), et.getSubAccount(ctx.contracts.deferredLiquidityCheckTest.address, 1) ], 10], | ||
expectError: 'e/defer/reentrancy' | ||
}, | ||
|
||
// should pass as batch dispatch defers liquidity for different account than defer liquidity check called from batch dispatch | ||
{ call: 'deferredLiquidityCheckTest.test', | ||
args: [ctx.contracts.tokens.TST.address, [ ctx.contracts.deferredLiquidityCheckTest.address ], 10], | ||
onLogs: logs => { | ||
et.expect(logs.findIndex(log => log.name === "onDeferredLiquidityCheckEvent")).to.gt(-1); | ||
}}, | ||
], | ||
}) | ||
|
||
|
||
.run(); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at it here, I think it would make sense to re-use the code between the single and multi-versions by having the single call the multi. Technically it would cost a bit more gas, but I think it's worth it to keep the code clean.