Skip to content

Commit

Permalink
add transferOut sequence to refund package
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed May 25, 2020
1 parent ad7c557 commit 7e961ec
Show file tree
Hide file tree
Showing 12 changed files with 515 additions and 425 deletions.
76 changes: 38 additions & 38 deletions contracts/RelayerIncentivize.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,76 +13,76 @@ contract RelayerIncentivize is IRelayerIncentivize, System {

//TODO add governance later
uint256 public constant moleculeHeaderRelayer = 1;
uint256 public constant denominaroeHeaderRelayer = 5;
uint256 public constant denominatorHeaderRelayer = 5;
uint256 public constant moleculeCallerCompensation = 1;
uint256 public constant denominaroeCallerCompensation = 80;
uint256 public constant denominatorCallerCompensation = 80;

mapping(address => uint256) public _headerRelayersSubmitCount;
address payable[] public _headerRelayerAddressRecord;
mapping(address => uint256) public headerRelayersSubmitCount;
address payable[] public headerRelayerAddressRecord;

mapping(address => uint256) public _transferRelayersSubmitCount;
address payable[] public _transferRelayerAddressRecord;
mapping(address => uint256) public transferRelayersSubmitCount;
address payable[] public transferRelayerAddressRecord;

uint256 public _collectedRewardForHeaderRelayer=0;
uint256 public _collectedRewardForTransferRelayer=0;
uint256 public collectedRewardForHeaderRelayer=0;
uint256 public collectedRewardForTransferRelayer=0;

uint256 public _roundSequence=0;
uint256 public _countInRound=0;
uint256 public roundSequence=0;
uint256 public countInRound=0;

event LogDistributeCollectedReward(uint256 sequence, uint256 roundRewardForHeaderRelayer, uint256 roundRewardForTransferRelayer);


function addReward(address payable headerRelayerAddr, address payable caller) external onlyTokenHub override payable returns (bool) {

_countInRound++;
countInRound++;

uint256 reward = calculateRewardForHeaderRelayer(msg.value);
_collectedRewardForHeaderRelayer = _collectedRewardForHeaderRelayer.add(reward);
_collectedRewardForTransferRelayer = _collectedRewardForTransferRelayer.add(msg.value).sub(reward);
collectedRewardForHeaderRelayer = collectedRewardForHeaderRelayer.add(reward);
collectedRewardForTransferRelayer = collectedRewardForTransferRelayer.add(msg.value).sub(reward);

if (_headerRelayersSubmitCount[headerRelayerAddr]==0){
_headerRelayerAddressRecord.push(headerRelayerAddr);
if (headerRelayersSubmitCount[headerRelayerAddr]==0){
headerRelayerAddressRecord.push(headerRelayerAddr);
}
_headerRelayersSubmitCount[headerRelayerAddr]++;
headerRelayersSubmitCount[headerRelayerAddr]++;

if (_transferRelayersSubmitCount[caller]==0){
_transferRelayerAddressRecord.push(caller);
if (transferRelayersSubmitCount[caller]==0){
transferRelayerAddressRecord.push(caller);
}
_transferRelayersSubmitCount[caller]++;
transferRelayersSubmitCount[caller]++;

if (_countInRound==ROUND_SIZE){
emit LogDistributeCollectedReward(_roundSequence, _collectedRewardForHeaderRelayer, _collectedRewardForTransferRelayer);
if (countInRound==ROUND_SIZE){
emit LogDistributeCollectedReward(roundSequence, collectedRewardForHeaderRelayer, collectedRewardForTransferRelayer);

distributeHeaderRelayerReward(caller);
distributeTransferRelayerReward(caller);

address payable systemPayable = address(uint160(SYSTEM_REWARD_ADDR));
systemPayable.transfer(address(this).balance);

_roundSequence++;
_countInRound = 0;
roundSequence++;
countInRound = 0;
}
return true;
}

function calculateRewardForHeaderRelayer(uint256 reward) internal view returns (uint256) {
return reward.mul(moleculeHeaderRelayer).div(denominaroeHeaderRelayer);
return reward.mul(moleculeHeaderRelayer).div(denominatorHeaderRelayer);
}

function distributeHeaderRelayerReward(address payable caller) internal returns (bool) {
uint256 totalReward = _collectedRewardForHeaderRelayer;
uint256 totalReward = collectedRewardForHeaderRelayer;

uint256 totalWeight=0;
address payable[] memory relayers = _headerRelayerAddressRecord;
address payable[] memory relayers = headerRelayerAddressRecord;
uint256[] memory relayerWeight = new uint256[](relayers.length);
for(uint256 index = 0; index < relayers.length; index++) {
address relayer = relayers[index];
uint256 weight = calculateHeaderRelayerWeight(_headerRelayersSubmitCount[relayer]);
uint256 weight = calculateHeaderRelayerWeight(headerRelayersSubmitCount[relayer]);
relayerWeight[index] = weight;
totalWeight = totalWeight.add(weight);
}

uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominaroeCallerCompensation);
uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominatorCallerCompensation);
totalReward = totalReward.sub(callerReward);
uint256 remainReward = totalReward;
for(uint256 index = 1; index < relayers.length; index++) {
Expand All @@ -93,27 +93,27 @@ contract RelayerIncentivize is IRelayerIncentivize, System {
relayers[0].send(remainReward);
caller.send(callerReward);

_collectedRewardForHeaderRelayer = 0;
collectedRewardForHeaderRelayer = 0;
for (uint256 index = 0; index < relayers.length; index++){
delete _headerRelayersSubmitCount[relayers[index]];
delete headerRelayersSubmitCount[relayers[index]];
}
delete _headerRelayerAddressRecord;
delete headerRelayerAddressRecord;
}

function distributeTransferRelayerReward(address payable caller) internal returns (bool) {
uint256 totalReward = _collectedRewardForTransferRelayer;
uint256 totalReward = collectedRewardForTransferRelayer;

uint256 totalWeight=0;
address payable[] memory relayers = _transferRelayerAddressRecord;
address payable[] memory relayers = transferRelayerAddressRecord;
uint256[] memory relayerWeight = new uint256[](relayers.length);
for(uint256 index = 0; index < relayers.length; index++) {
address relayer = relayers[index];
uint256 weight = calculateTransferRelayerWeight(_transferRelayersSubmitCount[relayer]);
uint256 weight = calculateTransferRelayerWeight(transferRelayersSubmitCount[relayer]);
relayerWeight[index] = weight;
totalWeight = totalWeight + weight;
}

uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominaroeCallerCompensation);
uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominatorCallerCompensation);
totalReward = totalReward.sub(callerReward);
uint256 remainReward = totalReward;
for(uint256 index = 1; index < relayers.length; index++) {
Expand All @@ -124,11 +124,11 @@ contract RelayerIncentivize is IRelayerIncentivize, System {
relayers[0].send(remainReward);
caller.send(callerReward);

_collectedRewardForTransferRelayer = 0;
collectedRewardForTransferRelayer = 0;
for (uint256 index = 0; index < relayers.length; index++){
delete _transferRelayersSubmitCount[relayers[index]];
delete transferRelayersSubmitCount[relayers[index]];
}
delete _transferRelayerAddressRecord;
delete transferRelayerAddressRecord;
}

function calculateTransferRelayerWeight(uint256 count) public pure returns(uint256) {
Expand Down
76 changes: 38 additions & 38 deletions contracts/RelayerIncentivize.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ contract RelayerIncentivize is IRelayerIncentivize, System {

//TODO add governance later
uint256 public constant moleculeHeaderRelayer = {{moleculeHeaderRelayer}};
uint256 public constant denominaroeHeaderRelayer = {{denominaroeHeaderRelayer}};
uint256 public constant denominatorHeaderRelayer = {{denominatorHeaderRelayer}};
uint256 public constant moleculeCallerCompensation = {{moleculeCallerCompensation}};
uint256 public constant denominaroeCallerCompensation = {{denominaroeCallerCompensation}};
uint256 public constant denominatorCallerCompensation = {{denominatorCallerCompensation}};

mapping(address => uint256) public _headerRelayersSubmitCount;
address payable[] public _headerRelayerAddressRecord;
mapping(address => uint256) public headerRelayersSubmitCount;
address payable[] public headerRelayerAddressRecord;

mapping(address => uint256) public _transferRelayersSubmitCount;
address payable[] public _transferRelayerAddressRecord;
mapping(address => uint256) public transferRelayersSubmitCount;
address payable[] public transferRelayerAddressRecord;

uint256 public _collectedRewardForHeaderRelayer=0;
uint256 public _collectedRewardForTransferRelayer=0;
uint256 public collectedRewardForHeaderRelayer=0;
uint256 public collectedRewardForTransferRelayer=0;

uint256 public _roundSequence=0;
uint256 public _countInRound=0;
uint256 public roundSequence=0;
uint256 public countInRound=0;

event LogDistributeCollectedReward(uint256 sequence, uint256 roundRewardForHeaderRelayer, uint256 roundRewardForTransferRelayer);

Expand All @@ -36,55 +36,55 @@ contract RelayerIncentivize is IRelayerIncentivize, System {
{% else %}
function addReward(address payable headerRelayerAddr, address payable caller) external onlyTokenHub override payable returns (bool) {
{% endif %}
_countInRound++;
countInRound++;

uint256 reward = calculateRewardForHeaderRelayer(msg.value);
_collectedRewardForHeaderRelayer = _collectedRewardForHeaderRelayer.add(reward);
_collectedRewardForTransferRelayer = _collectedRewardForTransferRelayer.add(msg.value).sub(reward);
collectedRewardForHeaderRelayer = collectedRewardForHeaderRelayer.add(reward);
collectedRewardForTransferRelayer = collectedRewardForTransferRelayer.add(msg.value).sub(reward);

if (_headerRelayersSubmitCount[headerRelayerAddr]==0){
_headerRelayerAddressRecord.push(headerRelayerAddr);
if (headerRelayersSubmitCount[headerRelayerAddr]==0){
headerRelayerAddressRecord.push(headerRelayerAddr);
}
_headerRelayersSubmitCount[headerRelayerAddr]++;
headerRelayersSubmitCount[headerRelayerAddr]++;

if (_transferRelayersSubmitCount[caller]==0){
_transferRelayerAddressRecord.push(caller);
if (transferRelayersSubmitCount[caller]==0){
transferRelayerAddressRecord.push(caller);
}
_transferRelayersSubmitCount[caller]++;
transferRelayersSubmitCount[caller]++;

if (_countInRound==ROUND_SIZE){
emit LogDistributeCollectedReward(_roundSequence, _collectedRewardForHeaderRelayer, _collectedRewardForTransferRelayer);
if (countInRound==ROUND_SIZE){
emit LogDistributeCollectedReward(roundSequence, collectedRewardForHeaderRelayer, collectedRewardForTransferRelayer);

distributeHeaderRelayerReward(caller);
distributeTransferRelayerReward(caller);

address payable systemPayable = address(uint160(SYSTEM_REWARD_ADDR));
systemPayable.transfer(address(this).balance);

_roundSequence++;
_countInRound = 0;
roundSequence++;
countInRound = 0;
}
return true;
}

function calculateRewardForHeaderRelayer(uint256 reward) internal view returns (uint256) {
return reward.mul(moleculeHeaderRelayer).div(denominaroeHeaderRelayer);
return reward.mul(moleculeHeaderRelayer).div(denominatorHeaderRelayer);
}

function distributeHeaderRelayerReward(address payable caller) internal returns (bool) {
uint256 totalReward = _collectedRewardForHeaderRelayer;
uint256 totalReward = collectedRewardForHeaderRelayer;

uint256 totalWeight=0;
address payable[] memory relayers = _headerRelayerAddressRecord;
address payable[] memory relayers = headerRelayerAddressRecord;
uint256[] memory relayerWeight = new uint256[](relayers.length);
for(uint256 index = 0; index < relayers.length; index++) {
address relayer = relayers[index];
uint256 weight = calculateHeaderRelayerWeight(_headerRelayersSubmitCount[relayer]);
uint256 weight = calculateHeaderRelayerWeight(headerRelayersSubmitCount[relayer]);
relayerWeight[index] = weight;
totalWeight = totalWeight.add(weight);
}

uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominaroeCallerCompensation);
uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominatorCallerCompensation);
totalReward = totalReward.sub(callerReward);
uint256 remainReward = totalReward;
for(uint256 index = 1; index < relayers.length; index++) {
Expand All @@ -95,27 +95,27 @@ contract RelayerIncentivize is IRelayerIncentivize, System {
relayers[0].send(remainReward);
caller.send(callerReward);

_collectedRewardForHeaderRelayer = 0;
collectedRewardForHeaderRelayer = 0;
for (uint256 index = 0; index < relayers.length; index++){
delete _headerRelayersSubmitCount[relayers[index]];
delete headerRelayersSubmitCount[relayers[index]];
}
delete _headerRelayerAddressRecord;
delete headerRelayerAddressRecord;
}

function distributeTransferRelayerReward(address payable caller) internal returns (bool) {
uint256 totalReward = _collectedRewardForTransferRelayer;
uint256 totalReward = collectedRewardForTransferRelayer;

uint256 totalWeight=0;
address payable[] memory relayers = _transferRelayerAddressRecord;
address payable[] memory relayers = transferRelayerAddressRecord;
uint256[] memory relayerWeight = new uint256[](relayers.length);
for(uint256 index = 0; index < relayers.length; index++) {
address relayer = relayers[index];
uint256 weight = calculateTransferRelayerWeight(_transferRelayersSubmitCount[relayer]);
uint256 weight = calculateTransferRelayerWeight(transferRelayersSubmitCount[relayer]);
relayerWeight[index] = weight;
totalWeight = totalWeight + weight;
}

uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominaroeCallerCompensation);
uint256 callerReward = totalReward.mul(moleculeCallerCompensation).div(denominatorCallerCompensation);
totalReward = totalReward.sub(callerReward);
uint256 remainReward = totalReward;
for(uint256 index = 1; index < relayers.length; index++) {
Expand All @@ -126,11 +126,11 @@ contract RelayerIncentivize is IRelayerIncentivize, System {
relayers[0].send(remainReward);
caller.send(callerReward);

_collectedRewardForTransferRelayer = 0;
collectedRewardForTransferRelayer = 0;
for (uint256 index = 0; index < relayers.length; index++){
delete _transferRelayersSubmitCount[relayers[index]];
delete transferRelayersSubmitCount[relayers[index]];
}
delete _transferRelayerAddressRecord;
delete transferRelayerAddressRecord;
}

function calculateTransferRelayerWeight(uint256 count) public pure returns(uint256) {
Expand Down
Loading

0 comments on commit 7e961ec

Please sign in to comment.