This is a decentralized application (DApp) built with Hardhat and Solidity. It is deployed on the Sepolia testnet and uses Alchemy as its node, as well as the Alchemy API for interacting with the Ethereum network.
To use this project, you will need to install:
To install this project:
- Clone the repository:
git clone https://github.com/your-username/hardhat-solidity-project.git
- Go to the project directory:
cd hardhat-solidity-project
- Install the project dependencies:
npm install
To use this project, you need an Alchemy API key. You can get a key by signing up for a free account at https://alchemyapi.io/.
Once you have your API key, create a file called .env
in the root of the project and add the following lines, replacing YOUR_API_KEY
with your actual API key and YOUR_GOERLI_URL
with the Goerli URL provided by Alchemy and YOUR_PRIVATE_KEY
with the private key for your Metamask account:
SEPOLIA_URL=YOUR_SEPOLIA_URL
SEPOLIA_API_KEY=SEPOLIA_API_KEY
PRIVATE_KEY=PRIVATE_KEY
With the API key in place, you can use these Hardhat commands to compile, deploy, and interact with the DApp:
- Compile the Solidity contracts:
npx hardhat compile
- Deploy the contracts to the Goerli testnet:
npx hardhat run scripts/deploy.js --network goerli
- Run the Hardhat console, which lets you interact with the deployed contracts:
npx hardhat console --network goerli
- Verify the contract:
npx hardhat verify --network goerli <YOUR-CONTRACT-ADDRESS>
This smart contract allows users to send ether (ETH) to the contract, along with a name
and message
. The contract is named BuyMeACoffee
and is written in the Solidity programming language for the Ethereum blockchain.
- Emits an event
NewMemo
when a user buys a coffee, which includes information about the transaction (from address, timestamp, name, and message)
event NewMemo(
address indexed from,
uint256 timestamp,
string name,
string message
);
function buyCoffee(
string memory _name,
string memory _message
) public payable {
require(msg.value > 0, "Can't buy coffee with 0 eth");
memos.push(Memo(msg.sender, block.timestamp, _name, _message));
// Emit a log event when a new memo is created
emit NewMemo(msg.sender, block.timestamp, _name, _message);
}
- Stores an array of
Memos
that are received from users
Memo[] memos;
- Has a variable
owner
that stores the address of the contract deployer
// Address of contract deployer
address payable owner;
// Deploy logic
constructor() {
owner = payable(msg.sender);
}
- Allows the
contract owner
to withdraw all the ether that is stored in the contract
function withdrawTips() public {
require(owner.send(address(this).balance));
}
- Allows anyone to retrieve all the memos stored in the contract.
function getMemos() public view returns (Memo[] memory) {
return memos;
}
- Memo:
from
: address of the user who sent the transactiontimestamp
: timestamp of the transactionname
: name of the user who sent the transactionmessage
: message sent by the user
This function allows a user to send ether to the contract and stores their name and message as a Memo
in the contract.
Input
- _name: name of the user sending the transaction
- _message: message sent by the user
This function allows the contract owner to withdraw all the ether that is stored in the contract.
This function allows anyone to retrieve all the memos stored in the contract.
Output
- Memo[] : array of memos stored in the contract
The address of the contract deployer is assigned to the owner
variable upon deployment.
This smart contract is shared under UNLICENSED license
. It is intended for educational and informational purposes only, and is not a recommendation or endorsement of any particular smart contract. Use caution when interacting with any smart contract, as they can be complex and potentially unsafe.