truffle环境搭建和应用

前言:总是到了不得不学习的时候才学truffle,虽然我知道他很简单。作为新生事物,还是不要小瞧。

1,安装

sudo npm i truffle -g 

+ truffle@5.3.10
added 1747 packages from 1101 contributors in 317.505s

批注:没加sudo还报错了。

2,样例

https://www.trufflesuite.com/boxes

https://www.trufflesuite.com/boxes/metacoin

https://github.com/truffle-box/metacoin-box

(1)创建

mkdir MetaCoin
cd MetaCoin

sudo truffle unbox metacoin

(2)编译 compile

$ truffle compile

Compiling your contracts...
===========================
✔ Fetching solc version list from solc-bin. Attempt #1
✔ Downloading compiler. Attempt #1.
> Compiling ./contracts/ConvertLib.sol
> Compiling ./contracts/MetaCoin.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/xx/code/local/MetaCoin/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang

生成build/contracts/文件夹,xx.json文件。

(3)部署 migration,要到一个网络上

先修改根目录下的truffle-config.js的读写权限。

test: {
host: "https://exchaintestrpc.okex.org",
// port: "80",
network_id: "*"
}

$ truffle migrate

Migrations 直译”迁移“,当作为一个名词时,有时指的是用来部署的脚本文件,称之为迁移文件,作为动词会翻译成部署,请读者了解。

配置参考:

https://learnblockchain.cn/docs/truffle/reference/configuration.html

https://github.com/Soteria-core/soteria/blob/main/truffle-config.js

truffle-config.js 本地配置:

module.exports = {
  // Uncommenting the defaults below 
  // provides for an easier quick-start with Ganache.
  // You can also follow this format for other networks;
  // see <http://truffleframework.com/docs/advanced/configuration>
  // for more details on how to specify configuration options!
  //
  networks: {
   development: {
     host: "127.0.0.1",
     port: 8545,
     network_id: "*"
   },
    test: {
      host: "https://exchaintestrpc.okex.org",
  //    port: "80",
      network_id: "*"
    }
  },
  compilers: {
    solc: {
      version: "0.5.17",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: true,
          runs: 200
        },
        //  evmVersion: "byzantium"
      }
    },
  }
  
};

对应,在本地起一条私链。truffle-config.js

truffle-config.js 远程配置:

const HDWalletProvider = require("truffle-hdwallet-provider");

//直接填写私钥,不要带0x前缀
const privateKeys = '...';

module.exports = {
  // Uncommenting the defaults below 
  // provides for an easier quick-start with Ganache.
  // You can also follow this format for other networks;
  // see <http://truffleframework.com/docs/advanced/configuration>
  // for more details on how to specify configuration options!
  //
  networks: {
   development: {
     host: "127.0.0.1",
     port: 8545,
     network_id: "*"
   },
    // bnb: {
    //   provider: () => new HDWalletProvider(privateKeys, `https://bsc-dataseed.binance.org/`),
    //   network_id: "*"
    // },
    // bnbtestnet: {
    //   provider: () => new HDWalletProvider(privateKeys, `https://data-seed-prebsc-1-s3.binance.org:8545/`),
    //   network_id: "*"
    // },
    dev: {
      provider: () => new HDWalletProvider(privateKeys, `https://exchaintestrpc.okex.org`),
      network_id: "65"
    },
  },
  compilers: {
    solc: {
      version: "0.5.17",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      settings: {          // See the solidity docs for advice about optimization and evmVersion
        optimizer: {
          enabled: true,
          runs: 200
        },
        //  evmVersion: "byzantium"
      }
    },
  }
  
};

对应package.json

{
  "name": "MetaCoin",
  "version": "1.0.0",
  "description": "MetaCoin smart contracts",
  "scripts": {
    "test": "scripts/test.sh"
  },
  "keywords": [
    "solidity",
    "ethereum",
    "smart",
    "contracts",
    "insurance"
  ],
  "author": "MetaCoin.fund",
  "license": "GPL-3.0",
  "homepage": "https://soteria.fund",
  "dependencies": {
    "chai": "^4.1.2",
    "chai-bignumber": "^2.0.2",
    "ethereumjs-abi": "^0.6.5",
    "ganache-cli": "^6.9.1",
    "mocha": "^8.0.1",
    "solc": "^0.5.7",
    "solhint": "^1.5.1",
    "truffle": "^5.1.34",
    "truffle-hdwallet-provider": "^1.0.17",
    "truffle-plugin-verify": "^0.3.11",
    "web3": "^1.2.9"
  },
  "devDependencies": { }
}

运行:

 truffle test --network dev

(4)与合约进行交互

(5)truffle和metamask配合

用EthPM进行包管理

用NPM进行包管理

(6)调试合约

3,编写测试用例

(1)测试合约,要到一个网络上

$ truffle test

(2)用js写测试用例

(3)用solidity写测试用例

4,高级用法&参考引用

选择网络及部署

truffle配置

原文地址:https://www.cnblogs.com/zccst/p/14900056.html