Skip to content

Instantly share code, notes, and snippets.

@blutarche
Last active July 4, 2022 08:15
Show Gist options
  • Save blutarche/5ed7e52b53e62c2d61b3db1ca327d906 to your computer and use it in GitHub Desktop.
Save blutarche/5ed7e52b53e62c2d61b3db1ca327d906 to your computer and use it in GitHub Desktop.
Solidity x Hardhat 101
import * as dotenv from "dotenv";
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomiclabs/hardhat-etherscan";
import "@nomiclabs/hardhat-waffle";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";
dotenv.config();
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
const config: HardhatUserConfig = {
solidity: "0.8.4",
networks: {
ropsten: {
url: process.env.ROPSTEN_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: "USD",
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
};
export default config;
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract LoveLetter {
uint256 public totalLetters;
mapping(uint256 => address) public senders;
mapping(uint256 => address) public receivers;
struct Letter {
string message;
uint256 etherAmount;
bool opened;
}
mapping(uint256 => Letter) public letters;
constructor() {
totalLetters = 0;
}
}
function checkOpened(uint256 id) external view returns (bool opened) {
opened = letters[id].opened;
}
function getEtherAmount(uint256 id)
external
view
returns (uint256 etherAmount)
{
etherAmount = letters[id].etherAmount;
}
function getReceiver(uint256 id) external view returns (address receiver) {
receiver = receivers[id];
}
function getSender(uint256 id) external view returns (address sender) {
sender = senders[id];
}
function open(uint256 id) external returns (string memory message) {
require(receivers[id] == msg.sender, "Not receiver");
message = letters[id].message;
letters[id].opened = true;
uint256 amount = letters[id].etherAmount;
console.log("open amount", amount);
if (amount > 0) {
payable(msg.sender).transfer(amount);
}
}
function readMessage(uint256 id)
external
view
returns (string memory message)
{
message = letters[id].message;
}
function send(address to, string memory message) external payable {
senders[totalLetters] = msg.sender;
receivers[totalLetters] = to;
letters[totalLetters] = Letter({
message: message,
etherAmount: msg.value,
opened: false
});
console.log("totalLetters", totalLetters);
totalLetters++;
}
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
contract LoveLetter {
uint256 public totalLetters;
mapping(uint256 => address) public senders;
mapping(uint256 => address) public receivers;
struct Letter {
string message;
uint256 etherAmount;
bool opened;
}
mapping(uint256 => Letter) public letters;
constructor() {
totalLetters = 0;
}
function send(address to, string memory message) external payable {
senders[totalLetters] = msg.sender;
receivers[totalLetters] = to;
letters[totalLetters] = Letter({
message: message,
etherAmount: msg.value,
opened: false
});
console.log("totalLetters", totalLetters);
totalLetters++;
}
function open(uint256 id) external returns (string memory message) {
require(receivers[id] == msg.sender, "Not receiver");
message = letters[id].message;
letters[id].opened = true;
uint256 amount = letters[id].etherAmount;
console.log("open amount", amount);
if (amount > 0) {
payable(msg.sender).transfer(amount);
}
}
function readMessage(uint256 id)
external
view
returns (string memory message)
{
message = letters[id].message;
}
function checkOpened(uint256 id) external view returns (bool opened) {
opened = letters[id].opened;
}
function getEtherAmount(uint256 id)
external
view
returns (uint256 etherAmount)
{
etherAmount = letters[id].etherAmount;
}
function getSender(uint256 id) external view returns (address sender) {
sender = senders[id];
}
function getReceiver(uint256 id) external view returns (address receiver) {
receiver = receivers[id];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment