Truffle Openzeppelin instance (version 2022)

1. Build the directory structure

Open cmd, create a new folder, ooxxb, enter the folder

2. Initialize the project with truffle

enter

truffle init

3. Introducing Openzeppelin

github address

npm install @openzeppelin/contracts

You can see that there is an additional folder node_modules under the project folder

There is also a dependency package in the package.json file, indicating that the introduction was successful!
insert image description here

 

4. The solidity code file of the new token

Create a new file OoxxCoin.sol under the contracts folder with the following contents:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

import "../node_modules/@OpenZeppelin/contracts/token/ERC20/ERC20.sol";

contract OoxxCoin is ERC20 {
    constructor() ERC20("OoxxCoin", "ooxxb") {
        _mint(msg.sender, 666666);// 初始化666666个ooxxb
    }
}

5. Create a new deployment file

Add 2_deploy_ooxxcoin.js under the migrations folder , the content is:

const OoxxCoin = artifacts.require("OoxxCoin");

module.exports = function (deployer) {
  deployer.deploy(OoxxCoin);
};

For details, please refer to this article

6. Modify the project configuration file

Open truffle-config.js under the project root directory file

Search for solc , open the version comment, and change it to 0.8.1 (because Openzeppelin indicates that the compilation requires version 0.8.0 or above)
insert image description here

7. Compile

Command line input development environment

truffle develop

After opening the develop mode, enter

compile

For details, please refer to this article

8. Deployment

enter

migrate

For details, please refer to this article

9. Validation results

  • Type in the command line first: let contract; Enter
  • Re-enter: OoxxCoin.deployed().then(instance => contract = instance) Enter
  • Enter the picture again  contract.name(); , you can see that the result is correct!
    insert image description here

10. Token usage

For the 10 virtual accounts that appear after the truffle  develop command, the default deployment contract uses the first address to store the total issuance, and then uses the first 3 virtual accounts to simulate operations

  • 0xc5965f591942d77e06e8a992de650d0044034efd
  • 0xd3648210093776256360315f16f443a67b33e5ff
  • 0x221b2b110afc7261347ebddb738a37338724a673
    insert image description here

10.1 Query the total amount of tokens issued

enter

contract.totalSupply();

insert image description here

10.2 Query an account balance

  • Query the balance of the first account 0xc5965f591942d77e06e8a992de650d0044034efd
contract.balanceOf("0xc5965f591942d77e06e8a992de650d0044034efd");

PS: The default deployment contract uses the first address to store the total amount of issuance, so it can be found that the balance is equal to the total amount of issuance.
insert image description here

  • Query the balance of the second account 0xd3648210093776256360315f16f443a67b33e5ff
contract.balanceOf("0xd3648210093776256360315f16f443a67b33e5ff");

This time, the balance is 0.
insert image description here

  • Query the balance of the third account 0x221b2b110afc7261347ebddb738a37338724a673
 contract.balanceOf("0x221b2b110afc7261347ebddb738a37338724a673");

insert image description here
The same balance is 0

10.3 Send 6000 tokens to the second account

enter

contract.transfer("0xd3648210093776256360315f16f443a67b33e5ff",6000);

Query the balance of the second account, there is 6000, the result is correct

contract.balanceOf("0xd3648210093776256360315f16f443a67b33e5ff");

insert image description here
Query the balance of circulation, 666666-6000=66066, the result is correct

contract.balanceOf("0xc5965f591942d77e06e8a992de650d0044034efd");

insert image description here
over,enjoy!

Guess you like

Origin blog.csdn.net/u013288190/article/details/123870856