Skip to content

Commit

Permalink
Update staking and unstaking for bonds to activate and no need for co…
Browse files Browse the repository at this point in the history
…oldowns. Add unit tests
  • Loading branch information
klivin committed Apr 26, 2019
1 parent 8d68cee commit 7fb4a13
Show file tree
Hide file tree
Showing 12 changed files with 413 additions and 346 deletions.
38 changes: 19 additions & 19 deletions contracts/staking/XyBond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,6 @@ contract XyBond is GovernorRole, Initializable {
emit BondWithdraw(bondId, to, withdrawAmount);
}

function unstake (bytes32 bondId, bytes32 stakingId)
public
{
bytes32 checkBondId = XyStakingConsensus(consensus).bondedStake(stakingId);
(uint amount,,,,,,) = XyStakingConsensus(consensus).stakeData(stakingId);
require(checkBondId == bondId, "Stake needs to be bonded");

Bond storage bs = bond[bondId];
require (msg.sender == bs.owner || governable(bs), "owner or governable can unstake");

require(bs.allocated >= amount, "Cannot unstake over bond allocation");
bs.allocated = bs.allocated.sub(amount);

// will fail if already withdrew
XyStakingConsensus(consensus).unstakeBonded(bondId, stakingId, amount);

emit BondUnstake(bondId, msg.sender, stakingId, amount);
}

function stake (bytes32 bondId, address payable beneficiary, address[] memory stakees, uint[] memory amounts)
public
{
Expand Down Expand Up @@ -169,6 +150,25 @@ contract XyBond is GovernorRole, Initializable {
stake(bondId, beneficiary, stakees, amounts);
}

function unstake (bytes32 bondId, bytes32 stakingId)
public
{
bytes32 checkBondId = XyStakingConsensus(consensus).bondedStake(stakingId);
(uint amount,,,,,,) = XyStakingConsensus(consensus).stakeData(stakingId);
require(checkBondId == bondId, "Stake needs to be bonded");

Bond storage bs = bond[bondId];
require (msg.sender == bs.owner || governable(bs), "owner or governable can unstake");

require(bs.allocated >= amount, "Cannot unstake over bond allocation");
bs.allocated = bs.allocated.sub(amount);

// will fail if already withdrew
XyStakingConsensus(consensus).unstakeBonded(bondId, stakingId);

emit BondUnstake(bondId, msg.sender, stakingId, amount);
}

function isExpired(bytes32 bondId)
public
view
Expand Down
206 changes: 108 additions & 98 deletions contracts/staking/XyStakingModel.sol

Large diffs are not rendered by default.

43 changes: 31 additions & 12 deletions contracts/tests/XyConsensusMock.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
pragma solidity >=0.5.0 <0.6.0;
import "../staking/XyStakingConsensus.sol";
import "../XyPayOnDelivery.sol";

contract XyConsensusMock is XyStakingConsensus {

function fake_data(uint amount, address stakee) internal view returns (Stake memory) {
Stake memory data = Stake(
amount, // amount
block.number, // stakeBlock
0, // unstakeBlock
stakee, // stakee
msg.sender, // staker
true, // isActivated
false // is coolded down
);
return data;
}
constructor(
address[] memory stakees,
address _token,
address _stakableToken,
address _governance)
address[] memory stakees)
public
XyStakingConsensus()
XyStakingConsensus()
{
initialize(_token, _stakableToken, _governance);
uint activeAmount = 1000;
for (uint i = 0; i < stakees.length; i++) {
updateCacheOnStake(activeAmount, stakees[i]);
updateCacheOnActivate(activeAmount, stakees[i]);
Stake memory data = fake_data(activeAmount, stakees[i]);
updateCacheOnStake(data);
updateCacheOnActivate(data);
}

}
function mock_handleResponses(bytes32[] memory _requests, bytes memory responses)
public
Expand All @@ -40,9 +48,20 @@ contract XyConsensusMock is XyStakingConsensus {
checkSigsAndStakes(messageHash, signers, sigR, sigS, sigV);
}
function fake_updateCacheOnStake(uint amount, address stakee) public {
updateCacheOnStake(amount, stakee);
updateCacheOnStake(fake_data(amount, stakee));
}
function fake_updateCacheOnActivate(uint amount, address stakee) public {
updateCacheOnActivate(amount, stakee);
updateCacheOnActivate(fake_data(amount, stakee));
}
function submitResponse(
bytes32 requestId,
uint8 requestType,
bytes memory responseData,
address pOnDAddress
)
public
payable
{
XyPayOnDelivery(pOnDAddress).submitResponse(requestId,requestType,responseData);
}
}
29 changes: 0 additions & 29 deletions contracts/tests/XyConsensusMock2.sol

This file was deleted.

29 changes: 0 additions & 29 deletions contracts/tests/XyConsensusMock3.sol

This file was deleted.

19 changes: 16 additions & 3 deletions contracts/tests/XyStakingMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,25 @@ contract XyStakingMock is XyStakingModel {
{
init(_token, _stakableToken, _governance);
}

function fake_data(uint amount, address stakee) internal view returns (Stake memory) {
Stake memory data = Stake(
amount, // amount
block.number, // stakeBlock
0, // unstakeBlock
stakee, // stakee
msg.sender, // staker
true, // isActivated
false // is coolded down
);
return data;
}
/** Increase and decrease cached stake amounts */
function fake_updateCacheOnStake(uint amount, address stakee) public {
updateCacheOnStake(amount, stakee);
updateCacheOnStake(fake_data(amount, stakee));
}
function fake_updateCacheOnActivate(uint amount, address stakee) public {
updateCacheOnActivate(amount, stakee);
updateCacheOnActivate(fake_data(amount, stakee));
}
function stub_updateCacheOnUnstake(uint amount, address stakee) public {
Stake memory data = Stake(
Expand All @@ -33,6 +46,6 @@ contract XyStakingMock is XyStakingModel {
updateCacheOnUnstake(tempStake);
}
function fake_updateCacheOnWithdraw(uint amount, address stakee) public {
updateCacheOnWithdraw(amount, stakee);
updateCacheOnWithdraw(fake_data(amount, stakee));
}
}
1 change: 1 addition & 0 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = async function (deployer, network, [contractsOwner, bp2]) {
await consensus.initialize(erc20.address, blockProducerInstance.address, gov.address)
await bonder.initialize(erc20.address, consensus.address, bondPeriodSeconds)

await gov.ownerSet(`XyBondContract`, bonder.address)
console.log(`INNITIALIZED WITH PARAMS`, parameters)

printAddress([SCSC, Governance, XYOERC20, PayOnD, BlockProducer, Bond])
Expand Down
Loading

0 comments on commit 7fb4a13

Please sign in to comment.