This project implements a decentralized vending machine for a custom ERC20 token called YourToken. It consists of two main smart contracts: YourToken and Vendor.
YourToken is an ERC20 token implemented using the OpenZeppelin library.
Key features:
- Standard ERC20 functionality (transfer, approve, transferFrom, etc.)
- Initial supply of 1000 tokens minted to the contract deployer
The Vendor contract acts as a decentralized vending machine for YourToken.
Key features:
- Buy tokens with ETH
- Sell tokens back to the contract for ETH
- Withdraw accumulated ETH (owner only)
- Set token price (owner only)
-
Install dependencies:
yarn install
-
Compile contracts:
yarn hardhat compile
-
Deploy contracts:
yarn hardhat deploy --network <your-network>
Replace
<your-network>
with your desired network (e.g., localhost, sepolia, mainnet)
Users can buy tokens by calling the buyTokens()
function on the Vendor contract and sending ETH.
Example:
await vendor.buyTokens({ value: ethers.utils.parseEther("1") });
To sell tokens back to the contract:
-
Approve the Vendor contract to spend your tokens:
await yourToken.approve(vendorAddress, amountToSell);
-
Call the
sellTokens()
function:await vendor.sellTokens(amountToSell);
The contract owner can withdraw accumulated ETH:
await vendor.withdraw();
The owner can set a new token price:
await vendor.setTokensPerEth(newPrice);
Run the test suite:
yarn hardhat test