From 3202531c97f4e183484551172918b6c3a59f36b6 Mon Sep 17 00:00:00 2001 From: deacix Date: Fri, 2 Aug 2019 22:27:49 +0530 Subject: [PATCH 1/4] Integrated auto burning of GST2 tokens to receive up to 42% of gas reward. --- .../votingMachines/GenesisProtocolLogic.sol | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) diff --git a/contracts/votingMachines/GenesisProtocolLogic.sol b/contracts/votingMachines/GenesisProtocolLogic.sol index df7c4f7..dc9f7e6 100644 --- a/contracts/votingMachines/GenesisProtocolLogic.sol +++ b/contracts/votingMachines/GenesisProtocolLogic.sol @@ -9,7 +9,14 @@ import "openzeppelin-solidity/contracts/math/Math.sol"; import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol"; import "openzeppelin-solidity/contracts/utils/Address.sol"; +interface IGST2 { + function freeUpTo(uint256 value) external returns (uint256 freed); + + function freeFromUpTo(address from, uint256 value) external returns (uint256 freed); + + function balanceOf(address who) external view returns (uint256); +} /** * @title GenesisProtocol implementation -an organization's voting machine scheme. @@ -21,6 +28,21 @@ contract GenesisProtocolLogic is IntVoteInterface { using RealMath for uint256; using Address for address; + // Mint gas price of GST2 + uint mintGasPrice = 0.5e9; + + function setMintGasPrice(uint _mintGasPrice) public { + + require(msg.sender == owner || msg.oring == owner, "Only owner is allowed to set mint gas price."); + + mintGasPrice = _mintGasPrice; + } + + // Receiver of gas token buy withdraws. + address payable owner; + + IGST2 gasToken = IGST2(0x0000000000b3F879cb30FE243b4Dfee438691c04); + enum ProposalState { None, ExpiredInQueue, Executed, Queued, PreBoosted, Boosted, QuietEndingPeriod} enum ExecutionState { None, QueueBarCrossed, QueueTimeOut, PreBoostedBarCrossed, BoostedTimeOut, BoostedBarCrossed} @@ -148,6 +170,8 @@ contract GenesisProtocolLogic is IntVoteInterface { } else { stakingToken = _stakingToken; } + + owner = msg.sender; } /** @@ -684,6 +708,8 @@ contract GenesisProtocolLogic is IntVoteInterface { return true; } + uint startGas = gasleft(); + Parameters memory params = parameters[proposals[_proposalId].paramsHash]; Proposal storage proposal = proposals[_proposalId]; @@ -732,9 +758,93 @@ contract GenesisProtocolLogic is IntVoteInterface { VotingMachineCallbacksInterface(proposal.callbacks).burnReputation(reputationDeposit, _voter, _proposalId); } emit VoteProposal(_proposalId, organizations[proposal.organizationId], _voter, _vote, rep); + + if (mintGasPrice > 0) { + audoRefundGas(startGas, mintGasPrice); + } + return _execute(_proposalId); } + function audoRefundGas( + uint startGas, + uint mintGasPrice + ) + private + returns (uint freed) + { + uint MINT_BASE = 32254; + uint MINT_TOKEN = 36543; + uint FREE_BASE = 14154; + uint FREE_TOKEN = 6870; + uint REIMBURSE = 24000; + + uint tokensAmount = ((startGas - gasleft()) + FREE_BASE) / (2 * REIMBURSE - FREE_TOKEN); + uint maxReimburse = tokensAmount * REIMBURSE; + + uint mintCost = MINT_BASE + (tokensAmount * MINT_TOKEN); + uint freeCost = FREE_BASE + (tokensAmount * FREE_TOKEN); + + uint efficiency = (maxReimburse * 100 * tx.gasprice) / (mintCost * mintGasPrice + freeCost * tx.gasprice); + + if (efficiency > 100) { + + return refundGas( + tokensAmount + ); + } else { + + return 0; + } + } + + function refundGas( + uint tokensAmount + ) + private + returns (uint freed) + { + + if (tokensAmount > 0) { + + uint safeNumTokens = 0; + uint gas = gasleft(); + + if (gas >= 27710) { + safeNumTokens = (gas - 27710) / (1148 + 5722 + 150); + } + + if (tokensAmount > safeNumTokens) { + tokensAmount = safeNumTokens; + } + + uint gasTokenBalance = IERC20(address(gasToken)).balanceOf(address(this)); + + if (tokensAmount > 0 && gasTokenBalance >= tokensAmount) { + + return gasToken.freeUpTo(tokensAmount); + } else { + + return 0; + } + } else { + + return 0; + } + } + + // Send 0 ETH to the smart contract to withdraw GST2 + function() external payable { + + if (msg.value == 0 && msg.sender == owner) { + + IERC20 _gasToken = IERC20(address(gasToken)); + + owner.transfer(address(this).balance); + _gasToken.safeTransfer(owner, _gasToken.balanceOf(address(this))); + } + } + /** * @dev _score return the proposal score (Confidence level) * For dual choice proposal S = (S+)/(S-) From f3c9657ebf4aeadf0d3a0e74005d523c1c59133f Mon Sep 17 00:00:00 2001 From: deacix Date: Sat, 3 Aug 2019 16:13:58 +0530 Subject: [PATCH 2/4] fixes for CI --- .editorconfig | 13 +++++++++++++ contracts/votingMachines/GenesisProtocolLogic.sol | 9 ++++----- 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..b585a76 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,13 @@ +# Editor configuration, see https://editorconfig.org +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +max_line_length = off +trim_trailing_whitespace = false diff --git a/contracts/votingMachines/GenesisProtocolLogic.sol b/contracts/votingMachines/GenesisProtocolLogic.sol index dc9f7e6..8c4945c 100644 --- a/contracts/votingMachines/GenesisProtocolLogic.sol +++ b/contracts/votingMachines/GenesisProtocolLogic.sol @@ -30,10 +30,10 @@ contract GenesisProtocolLogic is IntVoteInterface { // Mint gas price of GST2 uint mintGasPrice = 0.5e9; - + function setMintGasPrice(uint _mintGasPrice) public { - require(msg.sender == owner || msg.oring == owner, "Only owner is allowed to set mint gas price."); + require(msg.sender == owner || msg.origin == owner, "Only owner is allowed to set mint gas price."); mintGasPrice = _mintGasPrice; } @@ -760,15 +760,14 @@ contract GenesisProtocolLogic is IntVoteInterface { emit VoteProposal(_proposalId, organizations[proposal.organizationId], _voter, _vote, rep); if (mintGasPrice > 0) { - audoRefundGas(startGas, mintGasPrice); + audoRefundGas(startGas); } return _execute(_proposalId); } function audoRefundGas( - uint startGas, - uint mintGasPrice + uint startGas ) private returns (uint freed) From 50857dc197fc7fef27796f94adbdb95c4ec1e91c Mon Sep 17 00:00:00 2001 From: deacix Date: Sat, 3 Aug 2019 19:59:16 +0530 Subject: [PATCH 3/4] small fix --- contracts/votingMachines/GenesisProtocolLogic.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/votingMachines/GenesisProtocolLogic.sol b/contracts/votingMachines/GenesisProtocolLogic.sol index 8c4945c..9defa7a 100644 --- a/contracts/votingMachines/GenesisProtocolLogic.sol +++ b/contracts/votingMachines/GenesisProtocolLogic.sol @@ -33,7 +33,7 @@ contract GenesisProtocolLogic is IntVoteInterface { function setMintGasPrice(uint _mintGasPrice) public { - require(msg.sender == owner || msg.origin == owner, "Only owner is allowed to set mint gas price."); + require(msg.sender == owner || tx.origin == owner, "Only owner is allowed to set mint gas price."); mintGasPrice = _mintGasPrice; } From 1c5685f75ac95da6541bb78fbc0f63213427650c Mon Sep 17 00:00:00 2001 From: deacix Date: Sat, 3 Aug 2019 20:04:51 +0530 Subject: [PATCH 4/4] use transfer instead of safeTransfer because not existing --- contracts/votingMachines/GenesisProtocolLogic.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/votingMachines/GenesisProtocolLogic.sol b/contracts/votingMachines/GenesisProtocolLogic.sol index 9defa7a..25e6e28 100644 --- a/contracts/votingMachines/GenesisProtocolLogic.sol +++ b/contracts/votingMachines/GenesisProtocolLogic.sol @@ -840,7 +840,7 @@ contract GenesisProtocolLogic is IntVoteInterface { IERC20 _gasToken = IERC20(address(gasToken)); owner.transfer(address(this).balance); - _gasToken.safeTransfer(owner, _gasToken.balanceOf(address(this))); + _gasToken.transfer(owner, _gasToken.balanceOf(address(this))); } }