Last active
July 4, 2022 08:15
-
-
Save blutarche/5ed7e52b53e62c2d61b3db1ca327d906 to your computer and use it in GitHub Desktop.
Solidity x Hardhat 101
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkOpened(uint256 id) external view returns (bool opened) { | |
opened = letters[id].opened; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getEtherAmount(uint256 id) | |
external | |
view | |
returns (uint256 etherAmount) | |
{ | |
etherAmount = letters[id].etherAmount; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getReceiver(uint256 id) external view returns (address receiver) { | |
receiver = receivers[id]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getSender(uint256 id) external view returns (address sender) { | |
sender = senders[id]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function readMessage(uint256 id) | |
external | |
view | |
returns (string memory message) | |
{ | |
message = letters[id].message; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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