Contract Name:
AutoStaking
Contract Source Code:
File 1 of 1 : AutoStaking
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.6;
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint);
function symbol() external view returns (string memory);
function decimals() external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function mint(address account, uint amount) external;
function burn(address account, uint amount) external;
function transferFrom(address sender, address recipient, uint amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
contract ERC20 {
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
mapping (address => uint) public balanceOf;
mapping (address => mapping (address => uint)) public allowance;
string public name;
string public symbol;
uint8 public immutable decimals;
uint public totalSupply;
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
require(_decimals > 0, "decimals");
}
function transfer(address _recipient, uint _amount) external returns (bool) {
_transfer(msg.sender, _recipient, _amount);
return true;
}
function approve(address _spender, uint _amount) external returns (bool) {
_approve(msg.sender, _spender, _amount);
return true;
}
function transferFrom(address _sender, address _recipient, uint _amount) external returns (bool) {
require(allowance[_sender][msg.sender] >= _amount, "ERC20: insufficient approval");
_transfer(_sender, _recipient, _amount);
_approve(_sender, msg.sender, allowance[_sender][msg.sender] - _amount);
return true;
}
function _transfer(address _sender, address _recipient, uint _amount) internal {
require(_sender != address(0), "ERC20: transfer from the zero address");
require(_recipient != address(0), "ERC20: transfer to the zero address");
require(balanceOf[_sender] >= _amount, "ERC20: insufficient funds");
balanceOf[_sender] -= _amount;
balanceOf[_recipient] += _amount;
emit Transfer(_sender, _recipient, _amount);
}
function _mint(address _account, uint _amount) internal {
require(_account != address(0), "ERC20: mint to the zero address");
totalSupply += _amount;
balanceOf[_account] += _amount;
emit Transfer(address(0), _account, _amount);
}
function _burn(address _account, uint _amount) internal {
require(_account != address(0), "ERC20: burn from the zero address");
balanceOf[_account] -= _amount;
totalSupply -= _amount;
emit Transfer(_account, address(0), _amount);
}
function _approve(address _owner, address _spender, uint _amount) internal {
require(_owner != address(0), "ERC20: approve from the zero address");
require(_spender != address(0), "ERC20: approve to the zero address");
allowance[_owner][_spender] = _amount;
emit Approval(_owner, _spender, _amount);
}
}
contract AutoStaking is ERC20("xWILD", "xWILD", 18) {
IERC20 public wild;
event Deposit(uint wildAmount, uint shareAmount);
event Withdraw(uint wildAmount, uint shareAmount);
constructor(IERC20 _wild) {
wild = _wild;
}
function deposit(uint _wildAmount) external {
uint poolWILD = wild.balanceOf(address(this));
uint shareAmount;
if (totalSupply == 0 || poolWILD == 0) {
_mint(msg.sender, _wildAmount);
} else {
shareAmount = _wildAmount * totalSupply / poolWILD;
_mint(msg.sender, shareAmount);
}
wild.transferFrom(msg.sender, address(this), _wildAmount);
emit Deposit(_wildAmount, shareAmount);
}
function withdraw(uint _share) external {
uint wildAmount = _share * wild.balanceOf(address(this)) / totalSupply;
_burn(msg.sender, _share);
wild.transfer(msg.sender, wildAmount);
emit Withdraw(wildAmount, _share);
}
}