Created
November 20, 2024 05:02
-
-
Save nautical/1ef41d3a51936ad10853501f48c8a3ce to your computer and use it in GitHub Desktop.
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.5.0; | |
contract Token { | |
mapping (address => uint) tokenBalance; | |
mapping (address => uint) etherBalance; | |
uint currentRate; | |
constructor() public { | |
currentRate = 2; | |
} | |
function getTokenCountFor(address x) public view returns(uint) { | |
return tokenBalance[x]; | |
} | |
function getEtherCountFor(address x) public view returns(uint) { | |
return etherBalance[x]; | |
} | |
function getTokenCount() public view returns(uint) { | |
return tokenBalance[msg.sender]; | |
} | |
function depositEther() public payable { | |
if (msg.value > 0) { etherBalance[msg.sender] += msg.value; } | |
} | |
function exchangeTokens(uint amount) public { | |
if (tokenBalance[msg.sender] >= amount) { | |
uint etherAmount = amount * currentRate; | |
etherBalance[msg.sender] += etherAmount; | |
tokenBalance[msg.sender] -= amount; | |
} | |
} | |
function exchangeEther(uint amount) public payable { | |
etherBalance[msg.sender] += msg.value; | |
if (etherBalance[msg.sender] >= amount) { | |
uint tokenAmount = amount / currentRate; | |
etherBalance[msg.sender] -= amount; | |
tokenBalance[msg.sender] += tokenAmount; | |
} | |
} | |
function transferToken(address to, uint amount) public { | |
if (tokenBalance[msg.sender] >= amount) { | |
tokenBalance[to] += amount; | |
tokenBalance[msg.sender] -= amount; | |
} | |
} | |
function exchangeAndWithdrawToken(uint amount) public { | |
if (tokenBalance[msg.sender] >= amount) { | |
uint etherAmount = tokenBalance[msg.sender] * currentRate; | |
tokenBalance[msg.sender] -= amount; | |
msg.sender.transfer(etherAmount); | |
} | |
} | |
function withdrawAll() public { | |
uint etherAmount = etherBalance[msg.sender]; | |
uint tokenAmount = tokenBalance[msg.sender]; | |
if (etherAmount > 0 && tokenAmount > 0) { | |
uint e = etherAmount + (tokenAmount * currentRate); | |
etherBalance[msg.sender] = 0; | |
msg.sender.call.value(e)(""); | |
tokenBalance[msg.sender] = 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment