在以太坊模擬鏈及私鏈上編譯、部署智能合約

如需轉載請註明出處https://my.oschina.net/feistel/blog/3007913web

 

1.part6爲在Remix上編譯測試Solidity編寫的智能合約
2.part7爲在用geth搭建的私鏈上部署合約
3.其餘部分介紹在ganache-cli模擬鏈上用truffle編譯、部署、調用shell

----------------------------------------------------------------------------------------------------------------api

1跨域

圖形界面版 本地測試鏈ganache-cli
https://truffleframework.com/ganache
用於模擬以太坊區塊鏈生態,包括有曠工在挖礦,保證成功部署到測試鏈上,能夠在該鏈上測試發佈智能合約瀏覽器

(注意:ganache關閉後,節點服務的數據也會被清空)服務器

----------------------------------------------------------------------------------------------------------------網絡

2cors

pragma solidity >=0.4.21 <0.6.0;dom

contract Migrations {
  address public owner;
  uint public last_completed_migration;ide

  constructor() public {
    owner = msg.sender;
  }

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
  
  function getOwner() view public returns (address) {
  return owner;
  }
}

----------------------------------------------------------------------------------------------------------------

3

//構建新項目
truffle init
建立part2的Solidity合約代碼

//編譯,自動編譯contracts文件夾下sol文件
truffle compile (--all 強制從新編譯全部文件) (--network name,指定使用的網絡)

//配置truffle-config.js文件
修改:
development: {
    host: "127.0.0.1",     // Localhost (default: none)
    port: 7545,            // Standard Ethereum port (default: none)
    network_id: "*",       // Any network (default: none)
    }

//部署到區塊鏈上,保證模擬的區塊鏈客戶端ganache在運行
//truffle會鏈接到上述區塊鏈網絡中,進行編譯
truffle migrate (--reset 表示強制從新部署)

----------------------------------------------------------------------------------------------------------------

4

PS E:\@2019\truffle\test> truffle migrate
⚠️  Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.


Starting migrations...
======================
> Network name:    'development'
> Network id:      5777
> Block gas limit: 6721975


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0xecf11deb00bf225021fda820ba1f73ba111d372cdcd8cb485fb1033bbc914e54
   > Blocks: 0            Seconds: 0
   > contract address:    0x77D4F14109d81Dc1d700F9C8A6E9475Fb783D4e9
   > account:             0xf0f6e21B63D0c9eF5A5C6f3E3c3272ABdBC5815E
   > balance:             99.99430184
   > gas used:            284908
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00569816 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00569816 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.00569816 ETH

注:
gas used:284908
gas price:20 gwei
284908 * 20 = 5698160 gwei
5698160 / 1 000 000 000 = 0.005698160 eth
所以,total cost:0.005698160 eth

contract address,合約地址
account,合約帳戶

----------------------------------------------------------------------------------------------------------------

5

與智能合約交互

//進入Truffle控制檯,須要在項目文件夾下
//可使用命令與geth相似,web3
truffle console

//將合約實例賦給instance

let accounts = await web3.eth.getAccounts()
accounts
[ '0xf0f6e21B63D0c9eF5A5C6f3E3c3272ABdBC5815E',
  '0x21123d9E48693c7656B904B82e11e2Fa77D09F41',
  '0xd58bcc4Fbb5961FC7E96809B07c9e481981351D2',
  '0x225a3aa18c95e291A14fA25fee51dB6Ce87c1eb4',
  '0x40c8086a4AB47f547B452fF4C3651ab54F1d41Fb',
  '0x0Ce8832b9e9bD099CA0C5aC90cd0712e286B6C39',
  '0xBdF920b656ef5819C312F7575B26ebC035C30520',
  '0x697798292C73F3DfF1E9B9c9eE2A09dc0A4810b4',
  '0x5364d1566C10382E2880123989f006D9AAf4Fea5',
  '0xaCC3e08a27B56436bb4684B3d633d16f9e2456d2' ]

//建立合約實例,以{from: accounts[0]}設爲調用者
let instance = await Migrations.deployed({from: accounts[0]})

//查看狀態變量Owner
instance.getOwner()
'0xf0f6e21B63D0c9eF5A5C6f3E3c3272ABdBC5815E'

instance.setCompleted(1,{from: accounts[0]})
instance.upgrade(accounts[1],{from: accounts[0]})

----------------------------------------------------------------------------------------------------------------

6

使用在線工具,能夠直接編譯,點擊函數進行調用,自動部署到測試鏈上,無需關心測試鏈,只須要專心測試智能合約代碼
http://remix.ethereum.org/

----------------------------------------------------------------------------------------------------------------

7

在本身建立的以太坊本地區塊鏈私鏈上(用geth建立的),發佈智能合約

//啓動以太坊節點
geth --identity "newEth" --rpc --rpccorsdomain "*" --datadir .\data\ --port 30303 --rpcapi "db,eth,net,web3" --networkid 10 console
--identity,自定義節點名
--rpc,啓用HTTP-RPC服務器
--rpccorsdomain,容許跨域請求的域名列表(逗號分隔)(瀏覽器強制)
--rpcapi,基於HTTP-RPC接口提供的API
--networkid,網絡標識符(整型, 1=Frontier, 2=Morden (棄用), 3=Ropsten, 4=Rinkeby) (默認: 1)
保持啓動該節點的shell運行,下面操做在truffle項目另開shell窗口。
注:建立以太坊私鏈節點參考 以太坊又是啥? part7
https://my.oschina.net/feistel/blog/3007768

用truffle部署前先配置truffle-config.js文件,如part3所示
development: {
    host: "127.0.0.1",     // Localhost (default: none)
    port: 8545,            // Standard Ethereum port (default: none)
    network_id: "*",       // Any network (default: none)
    from: "0x5586fd7bb8b81992526bbf6bf707f0eba9ab3c7a",
    gas: 4712387
    }
注:1.須要指定部署合約的地址from,該地址是在私鏈節點上建立的,而且須要解鎖
    2.須要指定gas


//接着
truffle compile
truffle migrate

便可部署到私鏈上。

//若是節點沒有開始挖礦嗎,在節點shell上,能夠看到有一個待確認交易在交易池中
txpool.status
{
  pending: 1,
  queued: 0
}

//讓節點開始挖礦
miner.start(1)

要調用合約,回到part5

其中在truffle看到以下:
⚠️  Important ⚠️
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration will hang.


Starting migrations...
======================
> Network name:    'development'
> Network id:      10
> Block gas limit: 4712388


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x097f821a5cf84410a0c16d4987f86df061b154413e05d3efb0d5b55a3ea35732
   > Blocks: 0            Seconds: 0
   > contract address:    0xA920f6F9Aa3993e8cf11f70D3e3c581671B173e6
   > account:             0x5586fd7bb8b81992526BbF6bf707F0eba9ab3C7a
   > balance:             114.99242616
   > gas used:            321992
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00643984 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00643984 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.00643984 ETH

 

如需轉載請註明出處https://my.oschina.net/feistel/blog/3007913

相關文章
相關標籤/搜索