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 2 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
1 change: 1 addition & 0 deletions implementation/contracts/deposit/DepositFunding.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ library DepositFunding {
_d.signerFeeDivisor = _system.getSignerFeeDivisor();
_d.undercollateralizedThresholdPercent = _system.getUndercollateralizedThresholdPercent();
_d.severelyUndercollateralizedThresholdPercent = _system.getSeverelyUndercollateralizedThresholdPercent();
_d.initialCollateralizedPercent = _system.getInitialCollateralizedPercent();
_d.signingGroupRequestedAt = block.timestamp;

_d.setAwaitingSignerSetup();
Expand Down
5 changes: 2 additions & 3 deletions implementation/contracts/deposit/DepositUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ library DepositUtils {
uint256 lotSizeSatoshis;
uint8 currentState;
uint256 signerFeeDivisor;
uint128 initialCollateralizedPercent;
Copy link
Contributor

Choose a reason for hiding this comment

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

This and the two below seem like they can be smaller than uint128, perhaps? Could even be a uint8? Maybe a uint16 if we want to allow ourselves maximum flexibility, but our percentages are expressed as percentages, so uint8 gives us up to 250% as a representation 🤔

Not sure if the Solidity compiler is good enough to pack things together and save us gas in that case though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uint16 for some peace of mind. Also, the enforced initialCollateralizedPercent max is 300, which is over the 255 uint8 limit.
Solidity will automatically pack where possible, given the variables fit into a single 32-byte increment. two uint128s should be packed

uint128 undercollateralizedThresholdPercent;
uint128 severelyUndercollateralizedThresholdPercent;

Expand Down Expand Up @@ -373,9 +374,7 @@ library DepositUtils {
/// @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();
return (10000 / initialCollateralizedPercent);
return uint256(10000).div(_d.initialCollateralizedPercent);
}

/// @notice Seize the signer bond from the keep contract.
Expand Down
4 changes: 4 additions & 0 deletions implementation/contracts/test/deposit/TestDeposit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ contract TestDeposit is Deposit {
self.undercollateralizedThresholdPercent = _undercollateralizedThresholdPercent;
}

function setInitialCollateralizedPercent(uint128 _initialCollateralizedPercent) public {
self.initialCollateralizedPercent = _initialCollateralizedPercent;
}

function getUndercollateralizedThresholdPercent() public view returns (uint128) { return self.undercollateralizedThresholdPercent; }

function setSeverelyUndercollateralizedThresholdPercent(uint128 _severelyUndercollateralizedThresholdPercent) public {
Expand Down
1 change: 1 addition & 0 deletions implementation/test/DepositLiquidationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ describe("DepositLiquidation", async function() {

before(async () => {
lotSize = await testDeposit.lotSizeTbtc.call()
await testDeposit.setInitialCollateralizedPercent(new BN(150))
buyer = accounts[1]
})

Expand Down
6 changes: 3 additions & 3 deletions implementation/test/DepositUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ describe("DepositUtils", async function() {

const initialCollateralization = await tbtcSystemStub.getInitialCollateralizedPercent()

// 10000 to avoid losing value to truncating is solidity.
const expecged = new BN(10000).div(initialCollateralization)
expect(basePercentage).to.eq.BN(expecged) // 66 for 150
// 10000 to avoid losing value to truncating in solidity.
const expected = new BN(10000).div(initialCollateralization)
expect(basePercentage).to.eq.BN(expected) // 66 for 150
})
})

Expand Down