Last active
August 6, 2019 06:18
-
-
Save k06a/e39ce915d802a671f9b1da6fa987626c to your computer and use it in GitHub Desktop.
StakingPool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol"; | |
contract StakingPool1 is ERC20 { | |
function () public payable { | |
} | |
function deposit() public payable { | |
uint256 amount = msg.value; | |
if (totalSupply() > 0) { | |
amount = msg.value | |
.mul(totalSupply()) | |
.div(address(this).balance.sub(msg.value)); | |
} | |
_mint(msg.sender, amount); | |
} | |
function withdrawal(uint256 share) public { | |
msg.sender.transfer( | |
share | |
.mul(address(this).balance) | |
.div(totalSupply()) | |
); | |
_burn(msg.sender, share); | |
} | |
} | |
contract StakingPool2 { | |
using SafeMath for uint256; | |
mapping(address => uint256) public shares; | |
uint256 public totalShares; | |
function () public payable { | |
} | |
function deposit() public payable { | |
uint256 amount = msg.value; | |
if (totalShares > 0) { | |
amount = msg.value | |
.mul(totalShares) | |
.div(address(this).balance.sub(msg.value)); | |
} | |
shares[msg.sender] = shares[msg.sender].add(amount); | |
totalShares = totalShares.add(amount); | |
} | |
function withdrawal(uint256 share) public { | |
msg.sender.transfer( | |
share | |
.mul(address(this).balance) | |
.div(totalShares) | |
); | |
totalShares = totalShares.sub(share); | |
shares[msg.sender] = shares[msg.sender].sub(share); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment