项目作者: MASDXI

项目描述 :
Initial Coin Offering (ICO) smart contract written in Solidity. This repository budling with hardhat and openzeppelin with best practice code pattern.
高级语言: JavaScript
项目地址: git://github.com/MASDXI/Sample-ICO-contract.git
创建时间: 2021-02-10T09:54:49Z
项目社区:https://github.com/MASDXI/Sample-ICO-contract

开源协议:MIT License

下载


Initial Coin Offering (ICO) contract

Tutorial using Hardhat(Buidler) to complie, deploy and automated unit tests Solidity smart contract.
you use this repository as Initial Coin Offering (ICO) contract template or ERC20 template.
To run these tutorials, you must have the following installed:

  1. $ npm install

to compile your smart contract to get an ABI and artifact of a smart contract.

  1. $ npm run compile

for a unit testing smart contract using the command line.

  1. $ npm run test

expecting sample-test.js result.

  1. Initial Coin Offering (ICO) contract
  2. Assigns initial balance (97ms)
  3. Do not have permission to minting token (95ms)
  4. Do not have permission to burning token (74ms)
  5. Buy token with ether (212ms)
  6. Do not have permission to withdraw ether from contract (69ms)
  7. Transfer adds amount to destination account (99ms)
  8. Transfer emits event (132ms)
  9. Can not transfer above the amount (63ms)
  10. Can not transfer from empty account (77ms)
  11. Minting token (100ms)
  12. Burning token (73ms)
  13. Withdraw ether from contract (61ms)
  14. Do not have enough ether to buy token (30ms)
  15. 13 passing (2s)

after testing if you want to deploy the contract using the command line.

  1. $ npm run test-rpc
  2. # Open another Terminal
  3. $ npm run deploy-local
  4. # result in npx hardhat node Terminal
  5. web3_clientVersion
  6. eth_chainId
  7. eth_accounts
  8. eth_chainId
  9. eth_estimateGas
  10. eth_gasPrice
  11. eth_sendTransaction
  12. Contract deployment: <UnrecognizedContract>
  13. Contract address: 0x5fb...aa3
  14. Transaction: 0x4d8...945
  15. From: 0xf39...266
  16. Value: 0 ETH
  17. Gas used: 323170 of 323170
  18. Block #1: 0xee6...85d
  19. eth_chainId
  20. eth_getTransactionByHash
  21. eth_blockNumber
  22. eth_chainId (2)
  23. eth_getTransactionReceipt
  24. # result in npx hardhat run Terminal
  25. Initial Coin Offering (ICO) contract deployed to: 0x5Fb...aa3

your can edit deploy network endpoint at hardhat.config.js.

  1. module.exports = {
  2. networks: {
  3. {
  4. localhost: {
  5. url: "http://127.0.0.1:8545"
  6. },
  7. hardhat: {
  8. // See its defaults
  9. }
  10. }
  11. };

Example customized function
If you want to pay token fee to the miner or validator in the network.

  1. // pragma solidity 0.8.0
  2. // Solidity 0.8.X has an integrated SafeMath Library
  3. // override function transfer to distribute fee to miner or validator in the network
  4. /* Diagram transfer with token fees
  5. ##### Before #####
  6. A balance: 101
  7. B balance: 0
  8. C balance: 0
  9. A.transfer(B.address,101);
  10. A
  11. |
  12. | transfer Tx
  13. |_____
  14. | |
  15. v v
  16. B C
  17. ##### After #####
  18. A balance: 0
  19. B balance: 100
  20. C balance: 1
  21. */
  22. function transfer(address account,uint256 amount) public override returns(bool){
  23. require(amount % 10 != 0, "ERC20: insufficient funds");
  24. uint256 fee = amount % 10;
  25. _transfer(msg.sender,account,amount-fee);
  26. _transfer(msg.sender,block.coinbase,fee);
  27. return true;
  28. }