Skip to content

Instantly share code, notes, and snippets.

@Matleo
Created October 24, 2018 14:49
Show Gist options
  • Save Matleo/516104b95e19901442184bc32427c793 to your computer and use it in GitHub Desktop.
Save Matleo/516104b95e19901442184bc32427c793 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.4.25+commit.59dbf8f1.js&optimize=false&gist=
pragma solidity ^0.4.25;
contract owned {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
contract Tekken is owned{
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
uint256 public sellPrice = 50; //in wei
uint256 public buyPrice = 100; //in wei
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* Constructor function
*
*
*/
constructor(
string tokenName,
string tokenSymbol
) public {
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value) internal {
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value > balanceOf[_to]);
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
}
/**
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value) public returns (bool success) {
_transfer(msg.sender, _to, _value);
return true;
}
/// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
/// @param newSellPrice Price the users can sell to the contract
/// @param newBuyPrice Price users can buy from the contract
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
/// @notice Buy tokens from contract by sending ether
function buy() payable public {
uint amount = (msg.value / buyPrice); // calculates the amount
totalSupply += amount;
require(balanceOf[msg.sender] + amount >= balanceOf[msg.sender]);
balanceOf[msg.sender] += amount; // create coins on the fly and
emit Transfer(0, msg.sender, amount);
//send leftover ether back
uint leftOver = msg.value % buyPrice;
msg.sender.transfer(leftOver);
}
/// @notice Sell `amount` tokens to contract
/// @param amount amount of tokens to be sold
function sell(uint256 amount) public {
address myAddress = this;
require(myAddress.balance >= amount * sellPrice); // checks if the contract has enough ether to buy
_transfer(msg.sender, 0, amount); // makes the transfers
msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks
totalSupply -= amount;
}
function etherValueOF(address _ad) public view returns (uint256 balance) {
return _ad.balance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment