-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVendor.sol
81 lines (60 loc) · 2.79 KB
/
Vendor.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "./CreateToken.sol";
// Learn more about the ERC20 implementation
// on OpenZeppelin docs: https://docs.openzeppelin.com/contracts/4.x/api/access#Ownable
import "@openzeppelin/contracts/access/Ownable.sol";
contract Vendor is Ownable {
// Our Token Contract
CreateToken createToken;
// token price for ETH
uint256 public tokensPerEth = 100;
// Event that log buy operation
event BuyTokens(address buyer, uint256 amountOfETH, uint256 amountOfTokens);
constructor(address tokenAddress) {
createToken = CreateToken(tokenAddress);
}
/**
* @notice Allow users to buy token for ETH
*/
function buyTokens() public payable returns (uint256 tokenAmount) {
require(msg.value > 0, "Send ETH to buy some tokens");
uint256 amountToBuy = msg.value * tokensPerEth;
// check if the Vendor Contract has enough amount of tokens for the transaction
uint256 vendorBalance = createToken.balanceOf(address(this));
require(vendorBalance >= amountToBuy, "Vendor contract has not enough tokens in its balance");
// Transfer token to the msg.sender
(bool sent) = createToken.transfer(msg.sender, amountToBuy);
require(sent, "Failed to transfer token to user");
// emit the event
emit BuyTokens(msg.sender, msg.value, amountToBuy);
return amountToBuy;
}
/**
* @notice Allow users to sell tokens for ETH
*/
function sellTokens(uint256 tokenAmountToSell) public {
// Check that the requested amount of tokens to sell is more than 0
require(tokenAmountToSell > 0, "Specify an amount of token greater than zero");
// Check that the user's token balance is enough to do the swap
uint256 userBalance = createToken.balanceOf(msg.sender);
require(userBalance >= tokenAmountToSell, "Your balance is lower than the amount of tokens you want to sell");
// Check that the Vendor's balance is enough to do the swap
uint256 amountOfETHToTransfer = tokenAmountToSell / tokensPerEth;
uint256 ownerETHBalance = address(this).balance;
require(ownerETHBalance >= amountOfETHToTransfer, "Vendor has not enough funds to accept the sell request");
(bool sent) = createToken.transferFrom(msg.sender, address(this), tokenAmountToSell);
require(sent, "Failed to transfer tokens from user to vendor");
(sent,) = msg.sender.call{value: amountOfETHToTransfer}("");
require(sent, "Failed to send ETH to the user");
}
/**
* @notice Allow the owner of the contract to withdraw ETH
*/
function withdraw() public onlyOwner {
uint256 ownerBalance = address(this).balance;
require(ownerBalance > 0, "Owner has not balance to withdraw");
(bool sent,) = msg.sender.call{value: address(this).balance}("");
require(sent, "Failed to send user balance back to the owner");
}
}