Skip to content

Instantly share code, notes, and snippets.

@0mkara
Last active August 21, 2020 11:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0mkara/2f322c46eb783d710aca26664cb2e97f to your computer and use it in GitHub Desktop.
Save 0mkara/2f322c46eb783d710aca26664cb2e97f to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.0;
import "./token.sol";
contract ERC20Manager {
address payable seller;
uint public price = 1 ether;
ET tokenContract;
constructor() public
{
seller = msg.sender;
}
modifier onlyOwner
{
require(
msg.sender == seller,
"Only owner can call this function."
);
_;
}
modifier isConfigured
{
require(
tokenContract != ET(0),
"Token is not configured yet"
);
_;
}
modifier gtPrice
{
require(msg.value >= price, "Value should be greater than price!");
_;
}
function config(address _tokenContract) public onlyOwner
{
tokenContract = ET(_tokenContract);
}
// get configured token name
function getTokenName() public view isConfigured returns (string memory)
{
return tokenContract.name();
}
// proxy balanceOf
function balanceOf(address addr) public view isConfigured returns (uint256)
{
return tokenContract.balanceOf(addr);
}
// swap token
function swap(uint256 amount) external isConfigured
{
require(tokenContract.balanceOf(msg.sender) > amount);
tokenContract.transferFrom(msg.sender, seller, amount);
msg.sender.transfer(amount * price);
}
// transferTo someone else and wrap ether
function transferTo(uint256 amount, address toAddr) external payable isConfigured gtPrice
{
require(msg.sender == seller);
require(tokenContract.balanceOf(msg.sender) > amount);
require(msg.value > price * amount);
tokenContract.transferFrom(msg.sender, toAddr, amount);
}
// fallback
fallback() external payable
{
revert("Not enough Ether provided.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment