truffle 學習筆記(一)基本命令和配置

部署命令

初次部署: truffle migrate

重新部署:truffle migrate --reset

部署文件

​ 先修改./migrations/2_initial_migration.js

​ 注意,文件名的前綴是數字,後綴是描述。爲了記錄遷移是否成功運行,需要有編號的前綴。後綴是純粹爲了人類的可讀性和理解力。

1.基本配置

artifacts.require()

​ 在遷移開始時,我們通過artifacts.require()方法告訴Truffle我們想與哪些合同進行交互。 這個方法類似於Node的要求,但在我們的例子中,它特別返回了我們可以在其餘部署腳本中使用的抽象合約。 指定的名稱應與該源文件中的合同定義的名稱匹配。 不傳遞源文件的名稱,因爲文件可以包含多個合同。舉例:

// fileName: ./contracts/Contracts.sol
contract ContractOne { // ... }

contract ContractTwo { // ... }

如果只用合約中的ContractTwo你的artifacts.require()這樣寫:

var ContractTwo = artifacts.require("ContractTwo");

如果兩者都要用:

var ContractOne = artifacts.require("ContractOne");
var ContractTwo = artifacts.require("ContractTwo");

module.exports

​ 所有遷移都必須通過module.exports語法導出函數。 每次遷移導出的函數都應接受部署者對象作爲其第一個參數。 此對象通過爲部署智能合約提供清晰的語法以及執行某些部署更普通的職責(例如保存已部署的工件以供以後使用)來幫助部署。 部署者對象是用於暫存部署任務的主要界面,其API在本頁底部描述。

Initial migration

​ Truffle 框架中必須保留 contracts/Migrations.solmigrations/1_initial_migration.js這兩個文件,

部署的時候,將先部署這個合約。

2.部署器(Deployer)

​ 您的部署文件將使用部署器來完成部署任務。 因此,您可以同步編寫部署任務,它們將以正確的順序執行:

// 先部署A再部署B,兩者直接無直接聯繫
deployer.deploy(A);
deployer.deploy(B);
// 部署A後把A的地址作爲參數再部署B
deployer.deploy(A).then(function() {
  return deployer.deploy(B, A.address);
});

後面有詳細的API文檔說明

3.網絡配置

可以根據部署到的網絡有條件地運行部署步驟。 這是一項高級功能,因此請在繼續之前先查看“Networks”部分。

要有條件地暫存部署步驟,請編寫遷移,以便它們接受第二個參數,稱爲Network。 例:

module.exports = function(deployer, network) {
  if (network == "live") {
    // Do something specific to the network named "live".
  } else {
    // Perform a different step otherwise.
  }
}

4.選擇賬戶

​ 遷移還傳遞了Ethereum客戶端和web3提供商提供給您的帳戶列表,供您在部署期間使用。這是從web3.eth.getAccounts()返回的相同的帳戶列表。

module.exports = function(deployer, network, accounts) {
  // Use the accounts within your migrations.
}

5.Deployer.API

部署程序包含許多可用於簡化遷移的功能。

deployer.deploy

deployer.deploy(contract, args..., options)

使用可選的構造函數參數部署由合同對象指定的特定合同。這對單例合約很有用,因此dapp只存在此合約的一個實例。這將在部署後設置合同的地址(即,Contract.address將等於新部署的地址),並且它將覆蓋存儲的任何先前地址。

您可以選擇傳遞一組合同或一組數組,以加快多個合同的部署。此外,最後一個參數是一個可選對象,可以包含名爲overwrite的鍵以及其他事務參數,例如gas和from。如果overwrite設置爲false,則部署者將不會部署此合同(如果已經部署了該合同)。這對於由外部依賴項提供合同地址的某些情況很有用。

請注意,在調用deploy之前,您需要首先部署和鏈接合同所依賴的任何庫。有關詳細信息,請參閱下面的鏈接功能。

有關更多信息,請參閱松露合同文檔

// 在沒有構造函數參數的情況下部署單個合約
deployer.deploy(A);

// 使用構造函數參數部署單個合同
deployer.deploy(A, arg1, arg2, ...);

// 如果已經部署了此合同,請不要部署它
deployer.deploy(A, {overwrite: false});

//爲部署設置最大Gas 和 “from” 地址
deployer.deploy(A, {gas: 4612388, from: "0x...."});

// Deploy multiple contracts, some with arguments and some without.
// This is quicker than writing three `deployer.deploy()` statements as the deployer
// can perform the deployment as a single batched request.
deployer.deploy([
  [A, arg1, arg2, ...],
  B,
  [C, arg1]
]);

// External dependency example:
//
// For this example, our dependency provides an address when we're deploying to the
// live network, but not for any other networks like testing and development.
// When we're deploying to the live network we want it to use that address, but in
// testing and development we need to deploy a version of our own. Instead of writing
// a bunch of conditionals, we can simply use the `overwrite` key.
deployer.deploy(SomeDependency, {overwrite: false});

deployer.link

deployer.link(library, destinations)

​ 將已部署的庫鏈接到合同或多個合同。 目的地可以是單個合同或多個合同的數組。 如果目的地內的任何合同不依賴於鏈接的庫,則合同將被忽略。

// Deploy library LibA, then link LibA to contract B, then deploy B.
deployer.deploy(LibA);
deployer.link(LibA, B);
deployer.deploy(B);

// Link LibA to many contracts
deployer.link(LibA, [B, C, D]);

deployer.then

deployer.then(function() {...})

​ 就像promise一樣,運行任意部署步驟。 使用此選項可在遷移期間調用特定的合同函數,以添加,編輯和重新組織合同數據。

var a, b;
deployer.then(function() {
  // Create a new version of A
  return A.new();
}).then(function(instance) {
  a = instance;
  // Get the deployed instance of B
  return B.deployed();
}).then(function(instance) {
  b = instance;
  // Set the new instance of A's address on B via B's setA() function.
  return b.setA(a.address);
});

部署其他配置

solc

編譯器配置:

compilers: {
	solc: {
		version: "0.5.1",
		settings: {
       		optimizer: {
         		enabled: true,
         		runs: 200,
       		}}}}

wallet

在項目根目錄的項目配置文件truffle.js中,可以使用種子,在主網或測試網部署合約。下面提供一種部署到測試網rinkeby的配置

const HDWalletProvider = require('truffle-hdwallet-provider');
const fs = require('fs');
// 讀取種子,12個單詞組成的種子
const mnemonic = fs.readFileSync("./path/to/mnemonic.secret").toString().trim();

module.exports ={
    networks:{
        rinkebyTest:{
        	provider: () => new HDWalletProvider(
        		mnemonic, 
        		`https://rinkeby.infura.io/v3/aa86f***60803c`,// your infura API key
        		0, // 地址的起始索引
        		10 // 生成的地址數量
      		),
      		network_id: 4,
      		// gas: 6500000,
      		confirmations: 2,
      		gasPrice: 5000000000, // 5 Gwei
      		skipDryRun: true // 跳過預執行,直接部署
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章