Solidity

如何使用 web3.js 將合約部署到 Ropsten 測試網

  • June 1, 2018

我是新手。我不明白在Ropsten測試網中部署契約必須執行哪些步驟。

這是我的契約:

pragma solidity ^0.4.15;

import './ERC20.sol';
import './SafeMath.sol';

contract StepanToken2 is ERC20{
   using SafeMath for uint256;

   address public ownerFirst;
   address public ownerSecond;

   string public constant name = "Stepan Token";
   string public constant symbol = "SPT";
   uint8 public constant decimals = 8; 
   uint totalTokens = 100000000000000;


   //balances for accaunts
   mapping(address => uint) balances;

   //Owner of account approves the transfer of an amount to another account
   mapping(address => mapping(address => uint)) allowed;


   function StepanToken2 (address _ownerFirst, address _ownerSecond){
      ownerFirst = _ownerFirst;
      ownerSecond = _ownerSecond;

      balances[ownerFirst] = SafeMath.div(totalTokens, 3);
      balances[ownerSecond] = SafeMath.sub(totalTokens, balances[ownerFirst]);
   }

   function totalSupply() constant returns (uint256 totalSupply){
        return totalTokens;
   }

   function balanceOf(address _owner) constant returns (uint256 balance){
        return balances[_owner];
   }


   function transfer(address _to, uint _value)  returns (bool success){
           require(balances[msg.sender] >= _value && _value > 0 && SafeMath.add(balances[_to], _value) > balances[_to]);
               balances[msg.sender] = SafeMath.sub(balances[msg.sender],_value);
               balances[_to] = SafeMath.add(balances[_to], _value);
               Transfer(msg.sender,  _to, _value);
               return true;     
   }

   function transferFrom(address _from, address _to, uint _value) returns (bool success){
       require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0 && SafeMath.add(balances[_to], _value) > 0);
           balances[_from] = SafeMath.sub(balances[_from], _value);
           allowed[_from][msg.sender] = SafeMath.sub(allowed[_from][msg.sender], _value);
           balances[_to] = SafeMath.add(balances[_to], _value);
           Transfer(_from, _to, _value);
           return true;
   }


   function approve(address _spender, uint _value)  returns (bool success){
       allowed[msg.sender][_spender] = _value;
       Approval(msg.sender, _spender,  _value);
       return true;
   }

   function allowance(address _owner, address _spender) constant returns (uint256 remaining){
       return allowed[_owner][_spender];
   }

}

它編譯和遷移正常。當契約將在 ropsten 中編譯時,我應該在建構子契約中設置參數。我為此契約創建了一個帶有 JS 的 HTML 文件:

var Web3 = 需要 (‘web3’); var web3 = 新的 Web3 ();

   function initConnectWeb3() {

           web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/[infura_api_key]"));


       console.log(web3);
   }

   initConnectWeb3();

   primaryAddress = web3.eth.accounts[0];
   var MyContract = web3.eth.contract([{
           "constant": true,
           "inputs": [],
           "name": "name",
           "outputs": [{
               "name": "",
               "type": "string"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": false,
           "inputs": [{
                   "name": "_spender",
                   "type": "address"
               },
               {
                   "name": "_value",
                   "type": "uint256"
               }
           ],
           "name": "approve",
           "outputs": [{
               "name": "success",
               "type": "bool"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [],
           "name": "totalSupply",
           "outputs": [{
               "name": "totalSupply",
               "type": "uint256"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": false,
           "inputs": [{
                   "name": "_from",
                   "type": "address"
               },
               {
                   "name": "_to",
                   "type": "address"
               },
               {
                   "name": "_value",
                   "type": "uint256"
               }
           ],
           "name": "transferFrom",
           "outputs": [{
               "name": "success",
               "type": "bool"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [],
           "name": "decimals",
           "outputs": [{
               "name": "",
               "type": "uint8"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [],
           "name": "ownerFirst",
           "outputs": [{
               "name": "",
               "type": "address"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [{
               "name": "_owner",
               "type": "address"
           }],
           "name": "balanceOf",
           "outputs": [{
               "name": "balance",
               "type": "uint256"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [],
           "name": "symbol",
           "outputs": [{
               "name": "",
               "type": "string"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": false,
           "inputs": [{
                   "name": "_to",
                   "type": "address"
               },
               {
                   "name": "_value",
                   "type": "uint256"
               }
           ],
           "name": "transfer",
           "outputs": [{
               "name": "success",
               "type": "bool"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [{
                   "name": "_owner",
                   "type": "address"
               },
               {
                   "name": "_spender",
                   "type": "address"
               }
           ],
           "name": "allowance",
           "outputs": [{
               "name": "remaining",
               "type": "uint256"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "constant": true,
           "inputs": [],
           "name": "ownerSecond",
           "outputs": [{
               "name": "",
               "type": "address"
           }],
           "payable": false,
           "type": "function"
       },
       {
           "inputs": [{
                   "name": "_ownerFirst",
                   "type": "address"
               },
               {
                   "name": "_ownerSecond",
                   "type": "address"
               }
           ],
           "payable": false,
           "type": "constructor"
       },
       {
           "anonymous": false,
           "inputs": [{
                   "indexed": true,
                   "name": "_from",
                   "type": "address"
               },
               {
                   "indexed": true,
                   "name": "_to",
                   "type": "address"
               },
               {
                   "indexed": false,
                   "name": "_value",
                   "type": "uint256"
               }
           ],
           "name": "Transfer",
           "type": "event"
       },
       {
           "anonymous": false,
           "inputs": [{
                   "indexed": true,
                   "name": "_owner",
                   "type": "address"
               },
               {
                   "indexed": true,
                   "name": "_spender",
                   "type": "address"
               },
               {
                   "indexed": false,
                   "name": "_value",
                   "type": "uint256"
               }
           ],
           "name": "Approval",
           "type": "event"
       }
   ]);

   // contact = MyContract.new(0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B, 0x4E90a36B45879F5baE71B57Ad525e817aFA54890, {from: primaryAddress,  gas: 50000});
   contract = MyContract.new("0xC0b4ec83028307053Fbe8d00ba4372384fe4b52B", "0x4E90a36B45879F5baE71B57Ad525e817aFA54890", {
       from: primaryAddress,
       gas: 50000
       // data: evmCode
   }, function (err, contract) {
       if (!err && contract.address)
           console.log(contract.address);
   });

我在腳本中添加了更改

現在我有問題: 在此處輸入圖像描述

你可以試試這個 NodeJS 解決方案:

module.paths.push('/usr/lib/node_modules');

var fs = require('fs');
const contract_data = JSON.parse(
   fs.readFileSync('path to your JSON file')
);


var openKey = "open Key (address)";
var privateKey = "privateKey";


var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/ "));

web3.eth.accounts.wallet.add("0x" + privateKey);

var contract = new web3.eth.Contract(contract_data.abi);

contract.deploy({
   data: contract_data.unlinked_binary,
   arguments: []
}).send({
   from: openKey,
   gas: 1500000,
   gasPrice: '80000000'
}, function (error, transactionHash) {

}).on('error', function (error) {
   console.log('error', error);
}).on('transactionHash', function (transactionHash) {
   console.log('transactionHash', transactionHash);
}).on('receipt', function (receipt) {
   console.log('receipt', receipt.contractAddress);
}).on('confirmation', function (confirmationNumber, receipt) {
   console.log('confirmation', confirmationNumber);
});

引用自:https://ethereum.stackexchange.com/questions/27097