Created
May 11, 2022 23:53
-
-
Save anoochit/b3d3a081ca256e41eaff9e3ee5bc88af to your computer and use it in GitHub Desktop.
social network wallet - swap
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.4; | |
import "@openzeppelin/contracts@4.6.0/token/ERC20/utils/SafeERC20.sol"; | |
import "@openzeppelin/contracts@4.6.0/token/ERC20/ERC20.sol"; | |
contract SWAPToken { | |
using SafeERC20 for ERC20; | |
address owner; | |
address public GIFT = 0xE557638C01f783b9124A5F354bC256229f1ea431; | |
event SwapToken( address from, uint256 value, uint256 amount); | |
event RedeemCoin( address from, uint256 value, uint256 amount); | |
constructor() { | |
owner = msg.sender; | |
} | |
function buyToken() public payable { | |
uint256 amount = msg.value; | |
uint256 dexBalance = ERC20(GIFT).balanceOf(address(this)); | |
require(amount > 0, "You need to send some ether"); | |
require(amount <= dexBalance, "Not enough tokens in the reserve"); | |
ERC20(GIFT).transfer(msg.sender, amount); | |
emit SwapToken(msg.sender, msg.value, amount); | |
} | |
function sellToken(uint256 amount) public payable { | |
require(amount > 0, "You need to sell at least some tokens"); | |
uint256 allowance = ERC20(GIFT).allowance(msg.sender, address(this)); | |
require(allowance >= amount, "Check the token allowance"); | |
ERC20(GIFT).transferFrom(msg.sender, address(this),amount); | |
payable(msg.sender).transfer(amount); | |
} | |
function tokenSupply() public view returns (uint256) { | |
return ERC20(GIFT).balanceOf(address(this)); | |
} | |
function coinSupply() public view returns (uint256) { | |
return address(this).balance; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment