This project includes two main smart contracts: CoffeeToken
and CoffeeTokenSale
. The CoffeeToken
is an ERC20 token with burnable functionality and role-based access control. The CoffeeTokenSale
contract handles the sale of CoffeeToken
tokens, allowing users to purchase tokens with Ether.
To work with this project, you need to have the following installed:
-
Clone the repository:
git clone https://github.com/your-username/coffee-token-project.git cd coffee-token-project
-
Install dependencies:
npm install
-
Compile the contracts:
truffle compile
-
Deploy the contracts to a local blockchain (Ganache):
truffle migrate
CoffeeToken
is an ERC20 token with additional features:
- Minting: Only addresses with the
MINTER_ROLE
can mint new tokens. - Burning: Any token holder can burn their tokens.
- Role-based Access Control: Uses OpenZeppelin's
AccessControl
for role management.
defaultAdmin
- The address that will have the default admin role.minter
- The address that will have the minter role.
mint(address to, uint256 amount)
: Mint new tokens to the specified address. Requires the caller to have theMINTER_ROLE
.buyOneCoffee()
: Burns 1 token from the caller's balance. Emits theCoffeePurchased
event.buyOneCoffeeFrom(address account)
: Burns 1 token from the specified account's balance. Requires the caller to have an allowance for the specified account. Emits theCoffeePurchased
event.
CoffeeTokenSale
handles the sale of CoffeeToken
tokens. It allows users to purchase tokens by sending Ether.
_owner
- The owner of the contract who has theADMIN_ROLE
._token
- The address of the deployedCoffeeToken
contract._priceInWei
- The price of one token in Wei.
setPrice(uint256 _priceInWei)
: Sets the price of tokens in Wei. Requires the caller to have thePRICE_CHANGER_ROLE
.grantPriceChangerRole(address account)
: Grants thePRICE_CHANGER_ROLE
to the specified address. Requires the caller to have theADMIN_ROLE
.revokePriceChangerRole(address account)
: Revokes thePRICE_CHANGER_ROLE
from the specified address. Requires the caller to have theADMIN_ROLE
.purchase()
: Allows users to purchase tokens by sending Ether. If the sent Ether is more than the price, the remainder is refunded.withdraw()
: Allows the owner to withdraw all Ether from the contract. Requires the caller to have theADMIN_ROLE
.
To run the tests, use the following command:
truffle test
-
Run a local blockchain:
Start Ganache and configure it to match the settings in your
truffle-config.js
. -
Deploy the contracts:
truffle migrate
-
Interact with the contracts:
Use Truffle Console or a front-end application to interact with the deployed contracts. For example:
truffle console
In the console:
let coffeeToken = await CoffeeToken.deployed(); let coffeeTokenSale = await CoffeeTokenSale.deployed(); // Mint tokens await coffeeToken.mint("0xYourAddress", 100); // Purchase tokens await coffeeTokenSale.purchase({ from: "0xYourAddress", value: web3.utils.toWei("1", "ether") });