无法将 HelloWorld 合同部署到我的网络 (testrpc)

Cannot deploy HelloWorld contract to my network (testrpc)

请原谅我的基本问题,我还需要什么来部署我的合约?

我有 简单的 Hello World 合约 定义如下 HelloWorld.sol:

pragma solidity ^0.4.17;

contract HelloWorld{  
    function sayHello() internal pure returns(string){
        return("Hello 2018!");
    }
}

初始migrations.js:

var Migrations = artifacts.require("./Migrations.sol");
var HelloWorld = artifacts.require("./HelloWorld.sol");

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

最后因为我使用松露,truffle.js:

module.exports = {

  networks: {
    development: {
      host: "localhost",
      port: 8545,
      network_id: "*" // Match any network id
    }
  }

};

当我运行命令时:

truffle compile
truffle migrate

然后转到控制台:

   truffle console

我使用 js:

var hw
HelloWorld.deployed().then(function(deployed){hw=deployed;});

我明白了

Error: HelloWorld has not been deployed to detected network (network/artifact mismatch) at C:\Users\Kofola\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:317116:17 at at process._tickDomainCallback (internal/process/next_tick.js:228:7)

如何正确部署我的合约并测试它的方法?

我还应该补充一点,我的 testrpc 服务器 运行ning 在 8545,一切似乎都很好。 (所有这些都是 运行ning 在本地主机上) 我已尝试指定网络 ID,我明确设置了 ID 并在 truffle.js 中指定,但仍然无法正常工作。我还尝试更改函数的可见性。

编辑"Output of truffle commands":

合同详情:

控制台输出跟踪所需结果:

PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle compile
PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle migrate
Using network 'development'.

Network up to date.
PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle console
truffle(development)> var hw
undefined
truffle(development)> HelloWorld.deployed().then(function(deployed){hw=deployed;});
undefined
truffle(development)> hw.sayHello.call()
''
truffle(development)> hw.SayHello.call()
TypeError: Cannot read property 'call' of undefined
    at evalmachine.<anonymous>:1:13
    at ContextifyScript.Script.runInContext (vm.js:59:29)
    at Object.runInContext (vm.js:120:6)
    at Console.interpret (C:\Users\Kofola\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:202629:17)
    at ReplManager.interpret (C:\Users\Kofola\AppData\Roaming\npm\node_modules\truffle\build\cli.bundled.js:203345:18)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:441:10)
    at emitOne (events.js:116:13)
    at REPLServer.emit (events.js:211:7)
truffle(development)> hw.sayHello.call()
''
truffle(development)> .exit
PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle migrate --reset
Could not connect to your Ethereum client. Please check that your Ethereum client:
    - is running
    - is accepting RPC connections (i.e., "--rpc" option is used in geth)
    - is accessible over the network
    - is properly configured in your Truffle configuration file (truffle.js)

PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle compile
Compiling .\contracts\HelloWorld.sol...
Compiling .\contracts\Migrations.sol...

Compilation warnings encountered:

/C/Users/Kofola/BlockChain/EthereumHelloWorld/contracts/HelloWorld.sol:5:5: Warning: No visibility specified. Defaulting to "public".
    function sayHello() returns(string){
    ^
Spanning multiple lines.
,/C/Users/Kofola/BlockChain/EthereumHelloWorld/contracts/HelloWorld.sol:5:5: Warning: Function state mutability can be restricted to pure
    function sayHello() returns(string){
    ^
Spanning multiple lines.

Writing artifacts to .\build\contracts

PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle migrate --reset
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x1ed08bc4bcbb9db244acd9edfe4cfc71ca1675d069e4f17dcb5780f1ae496da0
  Migrations: 0x2bcb6d8aade2239b14c17374d631be59c2847175
  Deploying HelloWorld...
  ... 0x0195732cdb2a7fae8896be02a86403c88fd45c5e793a0ab1de7b3fdf6a086a67
  HelloWorld: 0xf169c021288e56a0b9739fd959fe305a7f67f500
Saving successful migration to network...
  ... 0xed919bb06a5a83fbf7e8bae8b99ec2f398979b51bc3f41c152ef621e1c799561
Saving artifacts...
PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle migrate
Using network 'development'.

Network up to date.
PS C:\Users\Kofola\BlockChain\EthereumHelloWorld> truffle console
truffle(development)> var hw = HelloWorld.at(HelloWorld.address);
undefined
truffle(development)> hw.address
'0xf169c021288e56a0b9739fd959fe305a7f67f500'
truffle(development)> hw.sayHello();
{ tx: '0x694a532cffb398dd316c53c6ac44adde7d8a7c00e305bcd8b39746fb3d20acdc',
  receipt:
   { transactionHash: '0x694a532cffb398dd316c53c6ac44adde7d8a7c00e305bcd8b39746fb3d20acdc',
     transactionIndex: 0,
     blockHash: '0xdfd7a8637904ec0ed282acac34e2da4929f410325f7f37744955ce5b3195cc8b',
     blockNumber: 4,
     gasUsed: 21934,
     cumulativeGasUsed: 21934,
     contractAddress: null,
     logs: [],
     status: 1 },
  logs: [] }
truffle(development)> hw
TruffleContract {
  constructor:
   { [Function: TruffleContract]
     _static_methods:
      { setProvider: [Function: setProvider],
        new: [Function: new],
        at: [Function: at],
        deployed: [Function: deployed],
        defaults: [Function: defaults],
        hasNetwork: [Function: hasNetwork],
        isDeployed: [Function: isDeployed],
        detectNetwork: [Function: detectNetwork],
        setNetwork: [Function: setNetwork],
        resetAddress: [Function: resetAddress],
        link: [Function: link],
        clone: [Function: clone],
        addProp: [Function: addProp],
        toJSON: [Function: toJSON] },
     _properties:
      { contract_name: [Object],
        contractName: [Object],
        abi: [Object],
        network: [Function: network],
        networks: [Function: networks],
        address: [Object],
        links: [Function: links],
        events: [Function: events],
        binary: [Function: binary],
        deployedBinary: [Function: deployedBinary],
        unlinked_binary: [Object],
        bytecode: [Object],
        deployedBytecode: [Object],
        sourceMap: [Object],
        deployedSourceMap: [Object],
        source: [Object],
        sourcePath: [Object],
        ast: [Object],
        compiler: [Object],
        schema_version: [Function: schema_version],
        schemaVersion: [Function: schemaVersion],
        updated_at: [Function: updated_at],
        updatedAt: [Function: updatedAt] },
     _property_values: {},
     _json:
      { contractName: 'HelloWorld',
        abi: [Array],
        bytecode: '0x6060604052341561000f57600080fd5b6101578061001e6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063ef5fb05b14610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc610117565b6040805190810160405280600b81526020017f48656c6c6f203230313821000000000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a723058201d959a32cbfc1f3e235a39bee940f8e7653bd7d2a041268570ed5b2453d1a9430029',
        deployedBytecode: '0x606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063ef5fb05b14610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc610117565b6040805190810160405280600b81526020017f48656c6c6f203230313821000000000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a723058201d959a32cbfc1f3e235a39bee940f8e7653bd7d2a041268570ed5b2453d1a9430029',
        sourceMap: '28:114:0:-;;;;;;;;;;;;;;;;;',
        deployedSourceMap: '28:114:0:-;;;;;;;;;;;;;;;;;;;;;;;;62:75;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;23:1:-1;8:100;33:3;30:1;27:2;8:100;;;99:1;94:3;90;84:5;80:1;75:3;71;64:6;52:2;49:1;45:3;40:15;;8:100;;;12:14;3:109;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;62:75:0;90:6;;:::i;:::-;108:21;;;;;;;;;;;;;;;;;;;;62:75;:::o;28:114::-;;;;;;;;;;;;;;;:::o',
        source: 'pragma solidity ^0.4.17;\r\n\r\ncontract HelloWorld{  \r\n    \r\n    function sayHello() returns(string){\r\n        return("Hello 2018!");\r\n
 }\r\n\r\n}',
        sourcePath: 'C:\Users\Kofola\BlockChain\EthereumHelloWorld\contracts\HelloWorld.sol',
        ast: [Object],
        compiler: [Object],
        networks: [Object],
        schemaVersion: '1.0.1',
        updatedAt: '2017-12-31T19:58:12.425Z' },
     setProvider: [Function: bound setProvider],
     new: [Function: bound new],
     at: [Function: bound at],
     deployed: [Function: bound deployed],
     defaults: [Function: bound defaults],
     hasNetwork: [Function: bound hasNetwork],
     isDeployed: [Function: bound isDeployed],
     detectNetwork: [Function: bound detectNetwork],
     setNetwork: [Function: bound setNetwork],
     resetAddress: [Function: bound resetAddress],
     link: [Function: bound link],
     clone: [Function: bound clone],
     addProp: [Function: bound addProp],
     toJSON: [Function: bound toJSON],
     web3:
      Web3 {
        _requestManager: [Object],
        currentProvider: [Object],
        eth: [Object],
        db: [Object],
        shh: [Object],
        net: [Object],
        personal: [Object],
        bzz: [Object],
        settings: [Object],
        version: [Object],
        providers: [Object],
        _extend: [Object] },
     class_defaults:
      { from: '0x848375e1f2447bee3257b9afcac99af09d83f9f0',
        gas: 6721975,
        gasPrice: 100000000000 },
     currentProvider:
      HttpProvider {
        host: 'http://localhost:8545',
        timeout: 0,
        user: undefined,
        password: undefined,
        send: [Function],
        sendAsync: [Function],
        _alreadyWrapped: true },
     network_id: '1001' },
  abi:
   [ { constant: false,
       inputs: [],
       name: 'sayHello',
       outputs: [Array],
       payable: false,
       stateMutability: 'nonpayable',
       type: 'function' } ],
  contract:
   Contract {
     _eth:
      Eth {
        _requestManager: [Object],
        getBalance: [Object],
        getStorageAt: [Object],
        getCode: [Object],
        getBlock: [Object],
        getUncle: [Object],
        getCompilers: [Object],
        getBlockTransactionCount: [Object],
        getBlockUncleCount: [Object],
        getTransaction: [Object],
        getTransactionFromBlock: [Object],
        getTransactionReceipt: [Object],
        getTransactionCount: [Object],
        call: [Object],
        estimateGas: [Object],
        sendRawTransaction: [Object],
        signTransaction: [Object],
        sendTransaction: [Object],
        sign: [Object],
        compile: [Object],
        submitWork: [Object],
        getWork: [Object],
        coinbase: [Getter],
        getCoinbase: [Object],
        mining: [Getter],
        getMining: [Object],
        hashrate: [Getter],
        getHashrate: [Object],
        syncing: [Getter],
        getSyncing: [Object],
        gasPrice: [Getter],
        getGasPrice: [Object],
        accounts: [Getter],
        getAccounts: [Object],
        blockNumber: [Getter],
        getBlockNumber: [Object],
        protocolVersion: [Getter],
        getProtocolVersion: [Object],
        iban: [Object],
        sendIBANTransaction: [Function: bound transfer] },
     transactionHash: null,
     address: '0xf169c021288e56a0b9739fd959fe305a7f67f500',
     abi: [ [Object] ],
     sayHello:
      { [Function: bound ]
        request: [Function: bound ],
        call: [Function: bound ],
        sendTransaction: [Function: bound ],
        estimateGas: [Function: bound ],
        getData: [Function: bound ],
        '': [Circular] },
     allEvents: [Function: bound ] },
  sayHello:
   { [Function]
     call: [Function],
     sendTransaction: [Function],
     request: [Function: bound ],
     estimateGas: [Function] },
  sendTransaction: [Function],
  send: [Function],
  allEvents: [Function: bound ],
  address: '0xf169c021288e56a0b9739fd959fe305a7f67f500',
  transactionHash: null }
truffle(development)>
(To exit, press ^C again or type .exit)
truffle(development)> hw.sayHello.call()
'Hello 2018!'
truffle(development)>

解决方案:对于那些可能有同样问题的人阅读已接受答案的评论以获取所有信息。

工作代码,使用命令 运行 它: https://github.com/TomasBisciak/EthereumHelloWorldExample

您的 truffle migrate 命令显示未部署任何内容。您的输出应如下所示:

$ truffle migrate
Using network 'development'.

Running migration: 1_initial_migration.js
  Deploying Migrations...
  ... 0x92408ab65d6fb64dedcf1e3aaa99854cc5fc3eb3e15687e04df86d357cdf22d8
  Migrations: 0xbaae4238d6818704f09c253d4c7bc57a3585623a
  Deploying HelloWorld...
  ... 0x47d7237c07e819f28dd7e154cfef67fa6b854cc963b6562abe1861cbdb85c222
  HelloWorld: 0x0a926a36ec5bf177c7724d0d1a85aa15e965916c
Saving successful migration to network...
  ... 0x548eab4802922e75dedfbb63ce158c4863bddcc5e6fc14f0396f109be13d8794
Saving artifacts...
Running migration: 2_deploy_contracts.js
Saving successful migration to network...
  ... 0x308b6e327d3cbc901cd49d380f56d30eb422efc3232d54115a2ef025dfc68f0c
Saving artifacts...

一些尝试:

  1. Truffle 在 Windows 10 上有一个小问题 运行ning,其中 truffle.js 与主 truffle 命令有名称冲突。您需要将 truffle.js 重命名为 truffle-config.js 请参阅 this
  2. 清除之前的所有内容builds/migrations。删除build目录和运行truffle migrate --reset.
  3. 如果前 2 个不起作用,请重新启动 testrpc 以清除本地区块链上的所有先前迁移。然后再运行truffle migrate