Slash collateral
This commit is contained in:
parent
2f59927b30
commit
ab38473688
|
@ -44,17 +44,27 @@ contract Collateral {
|
||||||
assert(token.transfer(msg.sender, amount));
|
assert(token.transfer(msg.sender, amount));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function _slash(address account, uint256 percentage) internal invariant {
|
||||||
|
uint256 amount = (balanceOf(account) * percentage) / 100;
|
||||||
|
totals.slashed += amount;
|
||||||
|
subtract(account, amount);
|
||||||
|
}
|
||||||
|
|
||||||
modifier invariant() {
|
modifier invariant() {
|
||||||
Totals memory oldTotals = totals;
|
Totals memory oldTotals = totals;
|
||||||
_;
|
_;
|
||||||
assert(totals.deposited >= oldTotals.deposited);
|
assert(totals.deposited >= oldTotals.deposited);
|
||||||
assert(totals.withdrawn >= oldTotals.withdrawn);
|
assert(totals.withdrawn >= oldTotals.withdrawn);
|
||||||
assert(totals.deposited == totals.balance + totals.withdrawn);
|
assert(totals.slashed >= oldTotals.slashed);
|
||||||
|
assert(
|
||||||
|
totals.deposited == totals.balance + totals.withdrawn + totals.slashed
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Totals {
|
struct Totals {
|
||||||
uint256 balance;
|
uint256 balance;
|
||||||
uint256 deposited;
|
uint256 deposited;
|
||||||
uint256 withdrawn;
|
uint256 withdrawn;
|
||||||
|
uint256 slashed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
pragma solidity ^0.8.0;
|
||||||
|
|
||||||
|
import "./Collateral.sol";
|
||||||
|
|
||||||
|
// exposes internal functions for testing
|
||||||
|
contract TestCollateral is Collateral {
|
||||||
|
// solhint-disable-next-line no-empty-blocks
|
||||||
|
constructor(IERC20 token) Collateral(token) {}
|
||||||
|
|
||||||
|
function slash(address account, uint256 percentage) public {
|
||||||
|
_slash(account, percentage);
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,7 +5,7 @@ describe("Collateral", function () {
|
||||||
let account0, account1
|
let account0, account1
|
||||||
|
|
||||||
beforeEach(async function () {
|
beforeEach(async function () {
|
||||||
let Collateral = await ethers.getContractFactory("Collateral")
|
let Collateral = await ethers.getContractFactory("TestCollateral")
|
||||||
let TestToken = await ethers.getContractFactory("TestToken")
|
let TestToken = await ethers.getContractFactory("TestToken")
|
||||||
token = await TestToken.deploy()
|
token = await TestToken.deploy()
|
||||||
collateral = await Collateral.deploy(token.address)
|
collateral = await Collateral.deploy(token.address)
|
||||||
|
@ -73,4 +73,20 @@ describe("Collateral", function () {
|
||||||
expect(after - before).to.equal(balance)
|
expect(after - before).to.equal(balance)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("slashing", function () {
|
||||||
|
beforeEach(async function () {
|
||||||
|
await token.connect(account0).approve(collateral.address, 1000)
|
||||||
|
await token.connect(account1).approve(collateral.address, 1000)
|
||||||
|
await collateral.connect(account0).deposit(1000)
|
||||||
|
await collateral.connect(account1).deposit(1000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("reduces the amount of collateral by a percentage", async function () {
|
||||||
|
await collateral.slash(account0.address, 10)
|
||||||
|
await collateral.slash(account1.address, 5)
|
||||||
|
expect(await collateral.balanceOf(account0.address)).to.equal(900)
|
||||||
|
expect(await collateral.balanceOf(account1.address)).to.equal(950)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue