Created
July 26, 2022 05:17
-
-
Save iHusk/3f0e190ccf1597eb68ee68dfbe344c6a 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.11+commit.d7f03943.js&optimize=false&runs=200&gist=
This file contains hidden or 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: GPL-3.0 | |
| pragma solidity ^0.8.0; | |
| import "./2.sol"; | |
| // Purpose: Smart Contract for a Vendor | |
| contract VendorAXC is Ownership { | |
| MyERC20 _myERC20; | |
| address private VOwner; | |
| uint TokensPerEther = 100; | |
| event TokenBuy(address indexed buyer, uint _value, uint _tokenQuantity); | |
| event TokenSell(address indexed seller, uint _tokenQuantity, uint _value); | |
| event BalanceWithdrawl(address indexed vendor, uint _balance); | |
| constructor(address MyERC20_) { | |
| VOwner = msg.sender; | |
| _myERC20 = MyERC20(MyERC20_); | |
| } | |
| function BuyToken() payable public returns(bool success) { | |
| require(msg.value >= 1 ether, "Minimum purchase is 1 ether!"); | |
| uint tokenQuantity = (msg.value / 1 ether) * TokensPerEther; | |
| uint vendorTokenBalance = _myERC20.balanceOf(address(this)); | |
| require(vendorTokenBalance >= tokenQuantity, "Insufficient Funds"); | |
| // Calling transfer from 2.sol | |
| (bool sent) = _myERC20.transfer(msg.sender, tokenQuantity); | |
| require(sent, "Transfer Failed!"); | |
| emit TokenBuy(msg.sender, msg.value, tokenQuantity); | |
| return true; | |
| } | |
| function SellToken(uint _amount) public payable returns(bool success) { | |
| uint _token = _amount % TokensPerEther; | |
| require(_token == 0, "Must sell in multiples of 100."); | |
| // Do we have permission? | |
| uint limitQuantity = _myERC20.allowance(msg.sender, address(this)); | |
| require(limitQuantity >= _amount, "Exceeded Maximum Allowance."); | |
| uint etherQuantity = _amount / TokensPerEther; | |
| // address(this).balance returns a value in wei, must convert | |
| uint vendorBalance = ((address(this).balance) / 1 ether); | |
| require(etherQuantity <= vendorBalance, "Insuficcent Vendor Balance"); | |
| (bool succ) = _myERC20.transferFrom(msg.sender, address(this), _amount); | |
| require(succ, "Error when Transferring from sender to contract."); | |
| // Must pay in wei, so multiply by 1e18 | |
| (bool sent,) = msg.sender.call{value: etherQuantity * 1e18}(""); | |
| require(sent, "Error when Transferring from contract to sender"); | |
| emit TokenSell(msg.sender, _amount, etherQuantity); | |
| return true; | |
| } | |
| function Withdrawl() public payable returns(bool success) { | |
| require(msg.sender == VOwner, "Only Vendor can Withdrawl."); | |
| uint contractBalance = address(this).balance; | |
| require(contractBalance >= 0, "No balance to withdrawl."); | |
| uint balance = address(this).balance; | |
| (bool succ,) = msg.sender.call{value: balance}(""); | |
| require(succ, "Error when withdrawling."); | |
| emit BalanceWithdrawl(msg.sender, balance); | |
| return true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment