Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Dynamic base liquidation percentage #539

Merged
merged 8 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
13 changes: 12 additions & 1 deletion implementation/contracts/deposit/DepositUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ library DepositUtils {
}

// This should make a smooth flow from base% to 100%
uint256 _basePercentage = TBTCConstants.getAuctionBasePercentage();
uint256 _basePercentage = getAuctionBasePercentage(_d);
uint256 _elapsedPercentage = uint256(100).sub(_basePercentage).mul(_elapsed).div(TBTCConstants.getAuctionDuration());
uint256 _percentage = _basePercentage.add(_elapsedPercentage);

Expand Down Expand Up @@ -367,6 +367,17 @@ library DepositUtils {
_d.lastRequestedDigest = bytes32(0);
}


/// @notice Get the starting percentage of the bond at auction.
/// @dev This will return the same value regardless of collateral price.
/// @return The percentage of the InitialCollateralizationPercent that will result
/// in a 100% bond value base auction given perfect collateralization.
function getAuctionBasePercentage(Deposit storage _d) internal view returns (uint256) {
ITBTCSystem _sys = ITBTCSystem(_d.TBTCSystem);
uint256 initialCollateralizedPercent = _sys.getInitialCollateralizedPercent();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to store this at deposit creation, otherwise the existing deposit's behavior changes based on governance actions :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

derp.

return (10000 / initialCollateralizedPercent);
}

/// @notice Seize the signer bond from the keep contract.
/// @dev we check our balance before and after.
/// @return The amount seized in wei.
Expand Down
2 changes: 0 additions & 2 deletions implementation/contracts/deposit/TBTCConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ library TBTCConstants {
// Liquidation Flow
uint256 public constant COURTESY_CALL_DURATION = 6 * 60 * 60; // seconds
uint256 public constant AUCTION_DURATION = 24 * 60 * 60; // seconds
uint256 public constant AUCTION_BASE_PERCENTAGE = 90; // percents
uint256 public constant PERMITTED_FEE_BUMPS = 5; // number of times the fee can be increased

// Getters for easy access
Expand All @@ -47,6 +46,5 @@ library TBTCConstants {

function getCourtesyCallTimeout() public pure returns (uint256) { return COURTESY_CALL_DURATION; }
function getAuctionDuration() public pure returns (uint256) { return AUCTION_DURATION; }
function getAuctionBasePercentage() public pure returns (uint256) { return AUCTION_BASE_PERCENTAGE; }
function getPermittedFeeBumps() public pure returns (uint256) {return PERMITTED_FEE_BUMPS; }
}
2 changes: 2 additions & 0 deletions implementation/contracts/interfaces/ITBTCSystem.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface ITBTCSystem {
// passthrough requests for the oracle
function fetchRelayCurrentDifficulty() external view returns (uint256);
function fetchRelayPreviousDifficulty() external view returns (uint256);

function getInitialCollateralizedPercent() external view returns (uint128);
}
4 changes: 4 additions & 0 deletions implementation/contracts/test/deposit/TestDeposit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,8 @@ contract TestDeposit is Deposit {
function pushFundsToKeepGroup(uint256 _ethValue) public returns (bool) {
return self.pushFundsToKeepGroup(_ethValue);
}

function getAuctionBasePercentage() public view returns (uint256) {
return self.getAuctionBasePercentage();
}
}
2 changes: 0 additions & 2 deletions implementation/contracts/test/deposit/TestTBTCConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ library TestTBTCConstants {
// Liquidation Flow
uint256 public constant COURTESY_CALL_DURATION = 6 * 60 * 60; // seconds
uint256 public constant AUCTION_DURATION = 24 * 60 * 60; // seconds
uint256 public constant AUCTION_BASE_PERCENTAGE = 90; // percents
uint256 public constant PERMITTED_FEE_BUMPS = 5; // number of times the fee can be increased

function getBeneficiaryRewardDivisor() public pure returns (uint256) { return BENEFICIARY_FEE_DIVISOR; }
Expand All @@ -46,6 +45,5 @@ library TestTBTCConstants {

function getCourtesyCallTimeout() public pure returns (uint256) { return COURTESY_CALL_DURATION; }
function getAuctionDuration() public pure returns (uint256) { return AUCTION_DURATION; }
function getAuctionBasePercentage() public pure returns (uint256) { return AUCTION_BASE_PERCENTAGE; }
function getPermittedFeeBumps() public pure returns (uint256) {return PERMITTED_FEE_BUMPS; }
}
4 changes: 2 additions & 2 deletions implementation/test/DepositLiquidationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ describe("DepositLiquidation", async function() {
const block = await web3.eth.getBlock("latest")
const notifiedTime = block.timestamp
const value = 1000000000000
const basePercentage = await tbtcConstants.getAuctionBasePercentage.call()
const basePercentage = await testDeposit.getAuctionBasePercentage.call()

await ecdsaKeepStub.pushFundsFromKeep(testDeposit.address, {value: value})

Expand Down Expand Up @@ -227,7 +227,7 @@ describe("DepositLiquidation", async function() {
const notifiedTime = block.timestamp
const liquidationInitiator = accounts[2]
const value = 1000000000000
const basePercentage = await tbtcConstants.getAuctionBasePercentage.call()
const basePercentage = await testDeposit.getAuctionBasePercentage.call()

await ecdsaKeepStub.pushFundsFromKeep(testDeposit.address, {value: value})

Expand Down
14 changes: 13 additions & 1 deletion implementation/test/DepositUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,24 @@ describe("DepositUtils", async function() {
})
})

describe("getAuctionBasePercentage()", async () => {
it("returns correct base percentage", async () => {
const basePercentage = await testDeposit.getAuctionBasePercentage.call()

const initialCollateralization = await tbtcSystemStub.getInitialCollateralizedPercent()

// 10000 to avoid losing value to truncating is solidity.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in?

const expecged = new BN(10000).div(initialCollateralization)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sp

expect(basePercentage).to.eq.BN(expecged) // 66 for 150
})
})

describe("auctionValue()", async () => {
let duration
let basePercentage
before(async () => {
duration = await tbtcConstants.getAuctionDuration.call()
basePercentage = await tbtcConstants.getAuctionBasePercentage.call()
basePercentage = await testDeposit.getAuctionBasePercentage.call()
auctionValue = new BN(100000000)
})
beforeEach(async () => {
Expand Down