-
Notifications
You must be signed in to change notification settings - Fork 2
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
Feat/eng 382 unbond function #15
Changes from all commits
b4e0d12
953d08c
8d8c501
fcfc80a
f7e3ee2
811d020
4d8f72b
3fa0617
70f9f03
9ce5a70
322045f
68a1890
38a5ee8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,18 @@ import "./ProviderPool.sol"; | |
|
||
contract TransmuteDPOS is TransmuteToken, RoundManager, ProviderPool { | ||
|
||
event DelegatorBonded( | ||
address indexed _delegator, | ||
address indexed _provider, | ||
uint _amount | ||
); | ||
|
||
event DelegatorUnbonded( | ||
address indexed _delegator, | ||
address indexed _provider, | ||
uint _amount | ||
); | ||
|
||
event ProviderAdded ( | ||
address indexed _provider, | ||
uint _pricePerStorageMineral, | ||
|
@@ -26,8 +38,11 @@ contract TransmuteDPOS is TransmuteToken, RoundManager, ProviderPool { | |
address indexed _provider | ||
); | ||
|
||
enum DelegatorStatus { Unbonded, UnbondedWithTokensToWithdraw, Bonded } | ||
|
||
struct Delegator { | ||
address delegateAddress; | ||
// TODO: rename variable | ||
uint amountBonded; | ||
} | ||
|
||
|
@@ -48,6 +63,16 @@ contract TransmuteDPOS is TransmuteToken, RoundManager, ProviderPool { | |
uint public numberOfProviders; | ||
mapping(address => Provider) public providers; | ||
|
||
mapping (address => uint) public withdrawBlocks; | ||
|
||
// FIXME: Those are temporary values | ||
constructor() public { | ||
// Set constants from RoundManager | ||
electionPeriodLength = 20; | ||
rateLockDeadline = 5; | ||
unbondingPeriod = 10; | ||
} | ||
|
||
function provider(uint _pricePerStorageMineral, uint _pricePerComputeMineral, uint _blockRewardCut, uint _feeShare) | ||
external onlyBeforeActiveRoundIsLocked | ||
{ | ||
|
@@ -92,5 +117,42 @@ contract TransmuteDPOS is TransmuteToken, RoundManager, ProviderPool { | |
if (p.status == ProviderStatus.Registered) { | ||
updateProvider(_provider, p.totalAmountBonded); | ||
} | ||
emit DelegatorBonded(msg.sender, _provider, _amount); | ||
} | ||
|
||
function unbond() external { | ||
// Only Bonded Delegators can call the function | ||
require(delegatorStatus(msg.sender) == DelegatorStatus.Bonded); | ||
// TODO: What if a Provider calls unbond() on himself ? | ||
// Should he resign ? | ||
// What about the tokens of the Delegators that bonded to him ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refunded - but who pays the gas cost for that? Him, I suppose. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, but that could potentially be a huge cost in gas. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does Livepeer handle this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They resign the Transcoder without refunding the delegators. |
||
// For now we prevent providers from calling this function | ||
require(providers[msg.sender].status == ProviderStatus.Unregistered); | ||
|
||
Delegator storage d = delegators[msg.sender]; | ||
Provider storage p = providers[d.delegateAddress]; | ||
// Sets the block number from which the Delegator will be able to withdraw() his tokens | ||
withdrawBlocks[msg.sender] = block.number.add(unbondingPeriod); | ||
// Decrease the totalAmountBonded parameter of the provider | ||
p.totalAmountBonded = p.totalAmountBonded.sub(d.amountBonded); | ||
updateProvider(d.delegateAddress, p.totalAmountBonded); | ||
emit DelegatorUnbonded(msg.sender, d.delegateAddress, d.amountBonded); | ||
// Remove delegator from the list. He is no longer in the the Bonded State | ||
delete delegators[msg.sender]; | ||
} | ||
|
||
// TODO: Create the same function for Providers | ||
// This will remove the need for ProviderStatus inside the Provider Struct | ||
function delegatorStatus(address _delegator) public view returns (DelegatorStatus) { | ||
if (delegators[_delegator].amountBonded != 0) { | ||
// If _delegator is in the mapping, he is Bonded | ||
return DelegatorStatus.Bonded; | ||
} else if (withdrawBlocks[_delegator] != 0) { | ||
// Else if _delegator has a withdrawBlock, he just called unbond() and didn't withdraw() yet | ||
return DelegatorStatus.UnbondedWithTokensToWithdraw; | ||
} else { | ||
// Else he is Unbonded: either he didn't call bond() or he called bond() unbond() and withdraw() | ||
return DelegatorStatus.Unbonded; | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems reasonable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do that next PR