Skip to content

Commit

Permalink
tokenhub payable and rename event
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Jun 16, 2020
1 parent c0c58b5 commit f1a6d23
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 40 deletions.
16 changes: 7 additions & 9 deletions contracts/SystemReward.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ contract SystemReward is System, ISystemReward{
require(operators[msg.sender],"only operator is available to call the method");
_;
}

event NewOperator(address indexed operator);
event DeleteOperator(address indexed operator);
event RewardTo(address indexed to, uint256 amount);
event RewardEmpty();
event ReceiveDeposit(address indexed from, uint256 amount);

event rewardTo(address indexed to, uint256 amount);
event rewardEmpty();
event receiveDeposit(address indexed from, uint256 amount);


receive() external payable{
if (msg.value>0){
emit ReceiveDeposit(msg.sender, msg.value);
emit receiveDeposit(msg.sender, msg.value);
}
}

Expand All @@ -46,9 +44,9 @@ contract SystemReward is System, ISystemReward{
}
if(actualAmount>0){
to.transfer(actualAmount);
emit RewardTo(to, actualAmount);
emit rewardTo(to, actualAmount);
}else{
emit RewardEmpty();
emit rewardEmpty();
}
return actualAmount;
}
Expand Down
23 changes: 12 additions & 11 deletions contracts/SystemReward.template
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,18 @@ contract SystemReward is System, ISystemReward{
require(operators[msg.sender],"only operator is available to call the method");
_;
}

event NewOperator(address indexed operator);
event DeleteOperator(address indexed operator);
event RewardTo(address indexed to, uint256 amount);
event RewardEmpty();
event ReceiveDeposit(address indexed from, uint256 amount);
{% if mock %}
event newOperator(address indexed operator);
event deleteOperator(address indexed operator);
{% endif %}
event rewardTo(address indexed to, uint256 amount);
event rewardEmpty();
event receiveDeposit(address indexed from, uint256 amount);


receive() external payable{
if (msg.value>0){
emit ReceiveDeposit(msg.sender, msg.value);
emit receiveDeposit(msg.sender, msg.value);
}
}

Expand All @@ -52,13 +53,13 @@ contract SystemReward is System, ISystemReward{
function addOperator(address operator) external doInit onlyOperatorNotExist(operator){
operators[operator] = true;
numOperator ++;
emit NewOperator(operator);
emit newOperator(operator);
}

function removeOperator(address operator) external doInit onlyOperatorExist(operator){
delete operators[operator];
numOperator --;
emit DeleteOperator(operator);
emit deleteOperator(operator);
}
{% endif %}
function claimRewards(address payable to, uint256 amount) external override(ISystemReward) doInit onlyOperator returns(uint256) {
Expand All @@ -68,9 +69,9 @@ contract SystemReward is System, ISystemReward{
}
if(actualAmount>0){
to.transfer(actualAmount);
emit RewardTo(to, actualAmount);
emit rewardTo(to, actualAmount);
}else{
emit RewardEmpty();
emit rewardEmpty();
}
return actualAmount;
}
Expand Down
11 changes: 8 additions & 3 deletions contracts/TokenHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,22 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
event refundSuccess(address bep2eAddr, address refundAddr, uint256 amount, uint32 status);
event refundFailure(address bep2eAddr, address refundAddr, uint256 amount, uint32 status);
event rewardTo(address to, uint256 amount);
event receiveDeposit(address from, uint256 amount);
event unexpectedPackage(uint8 channelId, bytes msgBytes);

event paramChange(string key, bytes value);

constructor() public {}

function init() onlyNotInit external {
relayFee = INIT_MINIMUM_RELAY_FEE;
alreadyInit=true;
}


receive() external payable{
if (msg.value>0){
emit receiveDeposit(msg.sender, msg.value);
}
}

function claimRewards(address payable to, uint256 amount) onlyInit onlyRelayerIncentivize external override returns(uint256) {
uint256 actualAmount = amount < address(this).balance ? amount : address(this).balance;
Expand Down
16 changes: 8 additions & 8 deletions contracts/TokenHub.template
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,22 @@ contract TokenHub is ITokenHub, System, IParamSubscriber, IApplication, ISystemR
event refundSuccess(address bep2eAddr, address refundAddr, uint256 amount, uint32 status);
event refundFailure(address bep2eAddr, address refundAddr, uint256 amount, uint32 status);
event rewardTo(address to, uint256 amount);
event receiveDeposit(address from, uint256 amount);
event unexpectedPackage(uint8 channelId, bytes msgBytes);

event paramChange(string key, bytes value);

constructor() public {}
{% if mock %}
function init() onlyNotInit public payable {
relayFee = INIT_MINIMUM_RELAY_FEE;
alreadyInit=true;
}
{% else %}

function init() onlyNotInit external {
relayFee = INIT_MINIMUM_RELAY_FEE;
alreadyInit=true;
}
{% endif %}

receive() external payable{
if (msg.value>0){
emit receiveDeposit(msg.sender, msg.value);
}
}

function claimRewards(address payable to, uint256 amount) onlyInit onlyRelayerIncentivize external override returns(uint256) {
uint256 actualAmount = amount < address(this).balance ? amount : address(this).balance;
Expand Down
4 changes: 2 additions & 2 deletions genesis.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,8 @@ module.exports = function(deployer, network, accounts) {
deployer.link(RLPDecode, TokenHub);
deployer.link(SafeMath, TokenHub);
tokenHubInstance=_tokenHubInstance;
tokenHubInstance.init({
from: accounts[0],
value: 50e18})
tokenHubInstance.init();
tokenHubInstance.sendTransaction({from:accounts[0],value:50e18});
});
}).then(function() {
// deploy mock
Expand Down
6 changes: 3 additions & 3 deletions test/SystemReward.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract('SystemReward', (accounts) => {
let tx = await systemRewardInstance.send(1e8, {from: accounts[3]});
let balance_wei = await web3.eth.getBalance(systemRewardInstance.address);
assert.equal(balance_wei, 1e8, "balance not equal");
truffleAssert.eventEmitted(tx, "ReceiveDeposit",(ev) => {
truffleAssert.eventEmitted(tx, "receiveDeposit",(ev) => {
return ev.amount.toNumber() === 1e8 && ev.from === accounts[3];
});
});
Expand All @@ -36,7 +36,7 @@ contract('SystemReward', (accounts) => {
await systemRewardInstance.send(1e8, {from: accounts[3]});
let tx = await systemRewardInstance.claimRewards(newAccount.address, 1e7, {from: accounts[0]})

truffleAssert.eventEmitted(tx, "RewardTo",(ev) => {
truffleAssert.eventEmitted(tx, "rewardTo",(ev) => {
return ev.amount.toNumber() === 1e7 && ev.to === newAccount.address;
});

Expand Down Expand Up @@ -64,7 +64,7 @@ contract('SystemReward', (accounts) => {
const systemRewardInstance = await SystemReward.deployed();
let newAccount = web3.eth.accounts.create();
let tx = await systemRewardInstance.claimRewards(newAccount.address, 1e7, {from: accounts[0]})
truffleAssert.eventEmitted(tx, "RewardEmpty");
truffleAssert.eventEmitted(tx, "rewardEmpty");
let balance_wei = await web3.eth.getBalance(newAccount.address);
assert.equal(balance_wei, 0, "balance not equal");
});
Expand Down
2 changes: 1 addition & 1 deletion truffle-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ module.exports = {
settings: { // See the solidity docs for advice about optimization and evmVersion
optimizer: {
enabled: true,
runs: 200
runs: 10
}
}
}
Expand Down

0 comments on commit f1a6d23

Please sign in to comment.