Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PVE007 rewardAmount in AgreementLiquidated and other minor fixes #218

Merged
merged 4 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -643,12 +643,18 @@ contract ConstantFlowAgreementV1 is
uint256 liquidationPeriod = gov.getConfigAsUint256(
ISuperfluid(msg.sender), token, _LIQUIDATION_PERIOD_CONFIG_KEY);

//oldDeposit = _calculateDeposit(oldFlowData.flowRate, liquidationPeriod, false).toInt256();
depositDelta = _calculateDeposit(flowParams.flowRate, liquidationPeriod).toInt256();

// for app allowance, rounding down the number instead,
// in order not to give the downstream app chance to create larger flow rate
// rounding up the number for app allowance too
// CAFEAT:
// - Now app could create a flow rate that is slightly higher than the incoming flow rate.
// - The app may be jailed due to negative balance if it does this without its own balance.
// Rule of thumbs:
// - App can use app allowance to create a flow that has the same incoming flow rate
// - But due to deposit clipping, there is no guarantee that the sum of the out going flow
// deposit can be covered by the allowance always.
// - It is advisable for the app to check the allowance usages carefully, and if possible
// Always have some its own balances to cover the deposits.
appAllowance = _calculateDeposit(flowParams.flowRate, liquidationPeriod);
depositDelta = appAllowance.toInt256();

// STEP 2: calculate deposit delta
depositDelta = depositDelta
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ abstract contract SuperfluidToken is ISuperfluidToken
msg.sender, id,
penaltyAccount,
liquidator /* rewardAccount */,
bailoutAmount
rewardAmount
);
emit Bailout(
rewardAccount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const _ = require("lodash");
const { BN } = require("@openzeppelin/test-helpers");
const { expectEvent } = require("@openzeppelin/test-helpers");
const { web3tx, toBN } = require("@decentral.ee/web3-helpers");

function clipDepositNumber(deposit, roundingDown = false) {
Expand Down Expand Up @@ -794,6 +795,16 @@ async function _shouldChangeFlow({
expectedRewardAmount
)
);
await expectEvent.inTransaction(
tx.tx,
testenv.sf.contracts.ISuperToken,
"AgreementLiquidated",
{
penaltyAccount: roles.sender,
rewardAccount: roles.reward,
rewardAmount: expectedRewardAmount.toString()
}
);
} else {
const expectedRewardAmount = toBN(flows.main.flowInfo1.deposit);
const expectedBailoutAmount = toBN(
Expand Down Expand Up @@ -828,6 +839,25 @@ async function _shouldChangeFlow({
expectedBailoutAmount
)
);
await expectEvent.inTransaction(
tx.tx,
testenv.sf.contracts.ISuperToken,
"AgreementLiquidated",
{
penaltyAccount: roles.sender,
rewardAccount: roles.agent,
rewardAmount: expectedRewardAmount.toString()
}
);
await expectEvent.inTransaction(
tx.tx,
testenv.sf.contracts.ISuperToken,
"Bailout",
{
bailoutAccount: roles.reward,
bailoutAmount: expectedBailoutAmount.toString()
}
);
}
console.log("--------");
}
Expand Down Expand Up @@ -900,6 +930,37 @@ async function _shouldChangeFlow({
);
console.log("--------");

// validate FlowUpdated event
await expectEvent.inTransaction(
tx.tx,
testenv.sf.agreements.cfa.contract,
"FlowUpdated",
{
token: superToken.address,
sender: roles.sender,
receiver: roles.receiver,
flowRate: flowRate.toString(),
// we don't test total flow rates when using mfa
// since mfa mangles with flows in callbacks
...(!mfa
? {
totalSenderFlowRate: getAccountFlowInfo({
testenv,
superToken: superToken.address,
account: roles.sender
}).flowRate.toString(),
totalReceiverFlowRate: getAccountFlowInfo({
testenv,
superToken: superToken.address,
account: mfa ? roles.mfa : roles.receiver
}).flowRate.toString()
}
: {}),
userData: userData ? userData : null
}
);
console.log("--------");

//console.log("!!! 2", JSON.stringify(testenv.data, null, 4));
console.log(`======== ${fn} ends ========`);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,13 @@ contract("Using ConstantFlowAgreement v1", accounts => {
assert.equal(events[0].args.reason.toString(), reasonCode.toString());
}

async function shouldTestLiquidations({
function shouldTestLiquidationByAgent({
titlePrefix,
sender,
receiver,
by,
allowCriticalAccount
by
}) {
const liquidationType =
by === sender ? "liquidate by agent" : "self liquidate";

it(`${titlePrefix}.1 should ${liquidationType} when critical but solvent`, async () => {
it(`${titlePrefix}.a should be liquidated by agent when critical but solvent`, async () => {
assert.isFalse(
await superToken.isAccountCriticalNow(t.aliases[sender])
);
Expand Down Expand Up @@ -143,8 +139,16 @@ contract("Using ConstantFlowAgreement v1", accounts => {

await verifyAll();
});
}

it(`${titlePrefix}.2 should ${liquidationType} when insolvent`, async () => {
function shouldTestSelfLiquidation({
titlePrefix,
sender,
receiver,
by,
allowCriticalAccount
}) {
it(`${titlePrefix}.b can self liquidate when insolvent`, async () => {
assert.isFalse(
await superToken.isAccountCriticalNow(t.aliases[sender])
);
Expand Down Expand Up @@ -517,7 +521,13 @@ contract("Using ConstantFlowAgreement v1", accounts => {
"set reward address to admin"
)(admin);
});
shouldTestLiquidations({
shouldTestLiquidationByAgent({
titlePrefix: "#1.3.6",
sender,
receiver,
by: sender
});
shouldTestSelfLiquidation({
titlePrefix: "#1.3.6",
sender,
receiver,
Expand All @@ -532,7 +542,13 @@ contract("Using ConstantFlowAgreement v1", accounts => {
"set reward address to zero"
)(ZERO_ADDRESS);
});
shouldTestLiquidations({
shouldTestLiquidationByAgent({
titlePrefix: "#1.3.7",
sender,
receiver,
by: sender
});
shouldTestSelfLiquidation({
titlePrefix: "#1.3.7",
sender,
receiver,
Expand Down Expand Up @@ -599,7 +615,13 @@ contract("Using ConstantFlowAgreement v1", accounts => {
"set reward address to admin"
)(admin);
});
shouldTestLiquidations({
shouldTestLiquidationByAgent({
titlePrefix: "#1.4.4",
sender,
receiver,
by: agent
});
shouldTestSelfLiquidation({
titlePrefix: "#1.4.4",
sender,
receiver,
Expand All @@ -614,7 +636,13 @@ contract("Using ConstantFlowAgreement v1", accounts => {
"set reward address to zero"
)(ZERO_ADDRESS);
});
shouldTestLiquidations({
shouldTestLiquidationByAgent({
titlePrefix: "#1.4.5",
sender,
receiver,
by: agent
});
shouldTestSelfLiquidation({
titlePrefix: "#1.4.5",
sender,
receiver,
Expand Down