Skip to content

Instantly share code, notes, and snippets.

@k06a
Last active August 6, 2019 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save k06a/e39ce915d802a671f9b1da6fa987626c to your computer and use it in GitHub Desktop.
Save k06a/e39ce915d802a671f9b1da6fa987626c to your computer and use it in GitHub Desktop.
StakingPool
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