feat: finish challenge
Some checks failed
Lint / ci (lts/*, ubuntu-latest) (push) Has been cancelled
Some checks failed
Lint / ci (lts/*, ubuntu-latest) (push) Has been cancelled
This commit is contained in:
@@ -10,18 +10,26 @@ contract Vendor is Ownable {
|
||||
/////////////////
|
||||
|
||||
// Errors go here...
|
||||
error InvalidEthAmount();
|
||||
error InsufficientVendorTokenBalance(uint256 available, uint256 required);
|
||||
error EthTransferFailed(address to, uint256 amount);
|
||||
error InvalidTokenAmount();
|
||||
error InsufficientVendorEthBalance(uint256 available, uint256 required);
|
||||
|
||||
//////////////////////
|
||||
/// State Variables //
|
||||
//////////////////////
|
||||
|
||||
YourToken public immutable yourToken;
|
||||
uint256 public constant tokensPerEth = 100;
|
||||
|
||||
////////////////
|
||||
/// Events /////
|
||||
////////////////
|
||||
|
||||
// Events go here...
|
||||
event BuyTokens(address indexed buyer, uint256 amountOfETH, uint256 amountOfTokens);
|
||||
event SellTokens(address indexed seller, uint256 amountOfTokens, uint256 amountOfETH);
|
||||
|
||||
///////////////////
|
||||
/// Constructor ///
|
||||
@@ -35,9 +43,29 @@ contract Vendor is Ownable {
|
||||
/// Functions /////
|
||||
///////////////////
|
||||
|
||||
function buyTokens() external payable {}
|
||||
function buyTokens() external payable {
|
||||
if (msg.value == 0) revert InvalidEthAmount();
|
||||
uint256 receivedToken = msg.value * tokensPerEth;
|
||||
uint256 availableToken = yourToken.balanceOf(address(this));
|
||||
if (availableToken < receivedToken) revert InsufficientVendorTokenBalance(availableToken, receivedToken);
|
||||
yourToken.transfer(msg.sender, receivedToken);
|
||||
emit BuyTokens(msg.sender, msg.value, receivedToken);
|
||||
}
|
||||
|
||||
function withdraw() public onlyOwner {}
|
||||
function withdraw() public onlyOwner {
|
||||
address ownerAddr = payable(owner());
|
||||
(bool sent,) = ownerAddr.call{value: address(this).balance}("");
|
||||
if (!sent) revert EthTransferFailed(ownerAddr, address(this).balance);
|
||||
}
|
||||
|
||||
function sellTokens(uint256 amount) public {}
|
||||
function sellTokens(uint256 amount) public {
|
||||
if (amount == 0) revert InvalidTokenAmount();
|
||||
yourToken.transferFrom(msg.sender, address(this), amount);
|
||||
uint256 receivedEth = amount / tokensPerEth;
|
||||
uint256 contractEth = address(this).balance;
|
||||
if (contractEth < receivedEth) revert InsufficientVendorEthBalance(contractEth, receivedEth);
|
||||
(bool sent,) = payable(msg.sender).call{value: receivedEth}("");
|
||||
if (!sent) revert EthTransferFailed(msg.sender, receivedEth);
|
||||
emit SellTokens(msg.sender, amount, receivedEth);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,5 +6,7 @@ import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
|
||||
// learn more: https://docs.openzeppelin.com/contracts/5.x/erc20
|
||||
|
||||
contract YourToken is ERC20 {
|
||||
constructor() ERC20("Gold", "GLD") {}
|
||||
constructor() ERC20("Gold", "GLD") {
|
||||
_mint(msg.sender, 1000 * (10 ** 18));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user