mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
uploading hardhat folder
This commit is contained in:
parent
71210b0630
commit
b16eb3fb2a
9 changed files with 7159 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -14,6 +14,7 @@
|
|||
*/**/*tx_database*
|
||||
*/**/*dapps*
|
||||
build/_vendor/pkg
|
||||
.docker-compose.yml.swp
|
||||
|
||||
#*
|
||||
.#*
|
||||
|
|
|
|||
12
docker-compose/docker-compose.yml
Normal file
12
docker-compose/docker-compose.yml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
version: '3.8'
|
||||
|
||||
services:
|
||||
geth:
|
||||
image: denislav-mladenov/go-ethereum:latest
|
||||
ports:
|
||||
- "8545:8545"
|
||||
- "30303:30303"
|
||||
volumes:
|
||||
- ./data:/root/.ethereum
|
||||
command: --dev --http --http.addr 0.0.0.0 --http.port 8545 --http.api eth,net,web3,personal,miner
|
||||
|
||||
17
hardhat/.gitignore
vendored
Normal file
17
hardhat/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
node_modules
|
||||
.env
|
||||
|
||||
# Hardhat files
|
||||
/cache
|
||||
/artifacts
|
||||
|
||||
# TypeChain files
|
||||
/typechain
|
||||
/typechain-types
|
||||
|
||||
# solidity-coverage files
|
||||
/coverage
|
||||
/coverage.json
|
||||
|
||||
# Hardhat Ignition default folder for deployments against a local node
|
||||
ignition/deployments/chain-31337
|
||||
66
hardhat/contracts/Token.sol
Normal file
66
hardhat/contracts/Token.sol
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
//SPDX-License-Identifier: UNLICENSED
|
||||
|
||||
// Solidity files have to start with this pragma.
|
||||
// It will be used by the Solidity compiler to validate its version.
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
|
||||
// This is the main building block for smart contracts.
|
||||
contract Token {
|
||||
// Some string type variables to identify the token.
|
||||
string public name = "My Hardhat Token";
|
||||
string public symbol = "MHT";
|
||||
|
||||
// The fixed amount of tokens, stored in an unsigned integer type variable.
|
||||
uint256 public totalSupply = 1000000;
|
||||
|
||||
// An address type variable is used to store ethereum accounts.
|
||||
address public owner;
|
||||
|
||||
// A mapping is a key/value map. Here we store each account's balance.
|
||||
mapping(address => uint256) balances;
|
||||
|
||||
// The Transfer event helps off-chain applications understand
|
||||
// what happens within your contract.
|
||||
event Transfer(address indexed _from, address indexed _to, uint256 _value);
|
||||
|
||||
/**
|
||||
* Contract initialization.
|
||||
*/
|
||||
constructor() {
|
||||
// The totalSupply is assigned to the transaction sender, which is the
|
||||
// account that is deploying the contract.
|
||||
balances[msg.sender] = totalSupply;
|
||||
owner = msg.sender;
|
||||
}
|
||||
|
||||
/**
|
||||
* A function to transfer tokens.
|
||||
*
|
||||
* The `external` modifier makes a function *only* callable from *outside*
|
||||
* the contract.
|
||||
*/
|
||||
function transfer(address to, uint256 amount) external {
|
||||
// Check if the transaction sender has enough tokens.
|
||||
// If `require`'s first argument evaluates to `false`, the
|
||||
// transaction will revert.
|
||||
require(balances[msg.sender] >= amount, "Not enough tokens");
|
||||
|
||||
// Transfer the amount.
|
||||
balances[msg.sender] -= amount;
|
||||
balances[to] += amount;
|
||||
|
||||
// Notify off-chain applications of the transfer.
|
||||
emit Transfer(msg.sender, to, amount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read only function to retrieve the token balance of a given account.
|
||||
*
|
||||
* The `view` modifier indicates that it doesn't modify the contract's
|
||||
* state, which allows us to call it without executing a transaction.
|
||||
*/
|
||||
function balanceOf(address account) external view returns (uint256) {
|
||||
return balances[account];
|
||||
}
|
||||
}
|
||||
6
hardhat/hardhat.config.js
Normal file
6
hardhat/hardhat.config.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
require("@nomicfoundation/hardhat-toolbox");
|
||||
|
||||
/** @type import('hardhat/config').HardhatUserConfig */
|
||||
module.exports = {
|
||||
solidity: "0.8.24",
|
||||
};
|
||||
9
hardhat/ignition/modules/Token.js
Normal file
9
hardhat/ignition/modules/Token.js
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");
|
||||
|
||||
const TokenModule = buildModule("TokenModule", (m) => {
|
||||
const token = m.contract("Token");
|
||||
|
||||
return { token };
|
||||
});
|
||||
|
||||
module.exports = TokenModule;
|
||||
6905
hardhat/package-lock.json
generated
Normal file
6905
hardhat/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
23
hardhat/package.json
Normal file
23
hardhat/package.json
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"name": "hardhat-pacage",
|
||||
"version": "1.0.0",
|
||||
"description": "denis hardhat project",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/denislav-mladenov/go-ethereum.git"
|
||||
},
|
||||
"author": "denis",
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/denislav-mladenov/go-ethereum/issues"
|
||||
},
|
||||
"homepage": "https://github.com/denislav-mladenov/go-ethereum#readme",
|
||||
"devDependencies": {
|
||||
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
|
||||
"hardhat": "^2.22.6"
|
||||
}
|
||||
}
|
||||
120
hardhat/test/Token.js
Normal file
120
hardhat/test/Token.js
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
// This is an example test file. Hardhat will run every *.js file in `test/`,
|
||||
// so feel free to add new ones.
|
||||
|
||||
// Hardhat tests are normally written with Mocha and Chai.
|
||||
|
||||
// We import Chai to use its asserting functions here.
|
||||
const { expect } = require("chai");
|
||||
|
||||
// We use `loadFixture` to share common setups (or fixtures) between tests.
|
||||
// Using this simplifies your tests and makes them run faster, by taking
|
||||
// advantage of Hardhat Network's snapshot functionality.
|
||||
const {
|
||||
loadFixture,
|
||||
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
|
||||
|
||||
// `describe` is a Mocha function that allows you to organize your tests.
|
||||
// Having your tests organized makes debugging them easier. All Mocha
|
||||
// functions are available in the global scope.
|
||||
//
|
||||
// `describe` receives the name of a section of your test suite, and a
|
||||
// callback. The callback must define the tests of that section. This callback
|
||||
// can't be an async function.
|
||||
describe("Token contract", function () {
|
||||
// We define a fixture to reuse the same setup in every test. We use
|
||||
// loadFixture to run this setup once, snapshot that state, and reset Hardhat
|
||||
// Network to that snapshot in every test.
|
||||
async function deployTokenFixture() {
|
||||
// Get the Signers here.
|
||||
const [owner, addr1, addr2] = await ethers.getSigners();
|
||||
|
||||
// To deploy our contract, we just have to call ethers.deployContract and await
|
||||
// its waitForDeployment() method, which happens once its transaction has been
|
||||
// mined.
|
||||
const hardhatToken = await ethers.deployContract("Token");
|
||||
|
||||
await hardhatToken.waitForDeployment();
|
||||
|
||||
// Fixtures can return anything you consider useful for your tests
|
||||
return { hardhatToken, owner, addr1, addr2 };
|
||||
}
|
||||
|
||||
// You can nest describe calls to create subsections.
|
||||
describe("Deployment", function () {
|
||||
// `it` is another Mocha function. This is the one you use to define each
|
||||
// of your tests. It receives the test name, and a callback function.
|
||||
//
|
||||
// If the callback function is async, Mocha will `await` it.
|
||||
it("Should set the right owner", async function () {
|
||||
// We use loadFixture to setup our environment, and then assert that
|
||||
// things went well
|
||||
const { hardhatToken, owner } = await loadFixture(deployTokenFixture);
|
||||
|
||||
// `expect` receives a value and wraps it in an assertion object. These
|
||||
// objects have a lot of utility methods to assert values.
|
||||
|
||||
// This test expects the owner variable stored in the contract to be
|
||||
// equal to our Signer's owner.
|
||||
expect(await hardhatToken.owner()).to.equal(owner.address);
|
||||
});
|
||||
|
||||
it("Should assign the total supply of tokens to the owner", async function () {
|
||||
const { hardhatToken, owner } = await loadFixture(deployTokenFixture);
|
||||
const ownerBalance = await hardhatToken.balanceOf(owner.address);
|
||||
expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Transactions", function () {
|
||||
it("Should transfer tokens between accounts", async function () {
|
||||
const { hardhatToken, owner, addr1, addr2 } = await loadFixture(
|
||||
deployTokenFixture
|
||||
);
|
||||
// Transfer 50 tokens from owner to addr1
|
||||
await expect(
|
||||
hardhatToken.transfer(addr1.address, 50)
|
||||
).to.changeTokenBalances(hardhatToken, [owner, addr1], [-50, 50]);
|
||||
|
||||
// Transfer 50 tokens from addr1 to addr2
|
||||
// We use .connect(signer) to send a transaction from another account
|
||||
await expect(
|
||||
hardhatToken.connect(addr1).transfer(addr2.address, 50)
|
||||
).to.changeTokenBalances(hardhatToken, [addr1, addr2], [-50, 50]);
|
||||
});
|
||||
|
||||
it("Should emit Transfer events", async function () {
|
||||
const { hardhatToken, owner, addr1, addr2 } = await loadFixture(
|
||||
deployTokenFixture
|
||||
);
|
||||
|
||||
// Transfer 50 tokens from owner to addr1
|
||||
await expect(hardhatToken.transfer(addr1.address, 50))
|
||||
.to.emit(hardhatToken, "Transfer")
|
||||
.withArgs(owner.address, addr1.address, 50);
|
||||
|
||||
// Transfer 50 tokens from addr1 to addr2
|
||||
// We use .connect(signer) to send a transaction from another account
|
||||
await expect(hardhatToken.connect(addr1).transfer(addr2.address, 50))
|
||||
.to.emit(hardhatToken, "Transfer")
|
||||
.withArgs(addr1.address, addr2.address, 50);
|
||||
});
|
||||
|
||||
it("Should fail if sender doesn't have enough tokens", async function () {
|
||||
const { hardhatToken, owner, addr1 } = await loadFixture(
|
||||
deployTokenFixture
|
||||
);
|
||||
const initialOwnerBalance = await hardhatToken.balanceOf(owner.address);
|
||||
|
||||
// Try to send 1 token from addr1 (0 tokens) to owner.
|
||||
// `require` will evaluate false and revert the transaction.
|
||||
await expect(
|
||||
hardhatToken.connect(addr1).transfer(owner.address, 1)
|
||||
).to.be.revertedWith("Not enough tokens");
|
||||
|
||||
// Owner balance shouldn't have changed.
|
||||
expect(await hardhatToken.balanceOf(owner.address)).to.equal(
|
||||
initialOwnerBalance
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in a new issue