Created
July 23, 2021 13:28
-
-
Save alexpvpmindustry/2b351a08a675f497e01c9cf0887f0eaa to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.6+commit.11564f7e.js&optimize=false&runs=200&gist=
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.8.0; | |
//SPDX-License-Identifier: SPDX-License-Identifier: <SPDX-License> | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Context.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/math/SafeMath.sol"; | |
//import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/"; | |
// https://docs.openzeppelin.com/contracts/4.x/erc20 | |
// https://docs.soliditylang.org/en/latest/common-patterns.html | |
// https://medium.com/@david.michael/sending-eth-to-a-contract-address-a73c84e2cc9c | |
contract Token is ERC20 { | |
address payable private _owner; | |
uint256 private _ExRate; | |
uint256 private _Fees; | |
uint256 private _ExRateBase; | |
address private _contract_address; | |
address payable private _payable_contract_address; | |
constructor () ERC20("DEFI ALX TOKEN", "ALX") { | |
uint256 supply = (10**6) * (10 ** uint256(decimals())); | |
_mint(msg.sender,supply);// (10**12) * (10 ** uint256(decimals()))); | |
_owner=payable (_msgSender()); | |
_ExRate = 50000000; | |
_ExRateBase=1000000000; | |
_Fees=_ExRate/1000; | |
_contract_address=address(this); | |
_payable_contract_address=payable(_contract_address); | |
_transfer( _owner,_contract_address,supply ); | |
} | |
event TxnLog(address indexed sender, string message); | |
function burrn(uint256 val) public { | |
_burn( address(_msgSender()),val); | |
} | |
function minnt(uint256 val) public { | |
require(_msgSender() == _owner); | |
_mint(_contract_address,val); | |
} | |
function showOwner() public view returns(address) { | |
return _owner; | |
} | |
function buyToken( ) public payable { | |
// Call returns a boolean value indicating success or failure. | |
// This is the current recommended method to use. | |
uint256 weii = msg.value; | |
(bool sent, bytes memory data) = _contract_address.call{value: weii}(""); | |
_transfer( _contract_address,_msgSender(),getTokVal(weii)); | |
emit TxnLog(_msgSender(),"Bought ALX"); | |
require(sent, "Failed to send Ether"); | |
} | |
// function sellToken(uint256 tok) public { | |
// _transfer( _msgSender(),_owner,getEthVal(tok)); | |
// (bool sent, bytes memory data) = (_msgSender()).call{value: msg.value}(""); | |
// emit TxnLog(_msgSender(),"Sold ALX"); | |
// require(sent, "Failed to send Ether"); | |
// } | |
function getTokVal(uint256 tok) public view returns(uint256){ // no fee | |
return SafeMath.div( SafeMath.mul(tok,_ExRateBase), _ExRate); | |
} | |
function getEthVal(uint256 tok) public view returns(uint256){ // yes fee | |
return SafeMath.mul( SafeMath.mul(tok,_ExRateBase), SafeMath.sub(_ExRate,_Fees)); | |
} | |
// see https://solidity-by-example.org/sending-ether/ | |
receive() external payable {buyToken();} | |
fallback() external payable {buyToken();} | |
function getBalEther() public view returns (uint) { | |
return _contract_address.balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment