简体   繁体   中英

Cannot deploy HelloWorld contract to my network (testrpc)

pardon my basic question, bud what else do i need to deploy my contract?

I have simple Hello World contract defined as follows HelloWorld.sol:

pragma solidity ^0.4.17;

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

Initial migrations.js:

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

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

And finally since im using truffle, truffle.js:

module.exports = {

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

};

When I run commands:

truffle compile
truffle migrate

Then go to console:

   truffle console

and i use js:

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

I get

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)

How do i correctly deploy my contract and test its method?

I should also add that my testrpc server is running at 8545 and everything seems to be alright. (all of this is running on localhost) I have tried to specify id of the network, i explicitly set id and specified in truffle.js, still not working. I also tried to change visibility of the function.

Edit "Output of truffle commands": 在此处输入图片说明

Contract detail: 在此处输入图片说明

Console output trail to desired result:

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)>

Solution : To those who might have same problem READ COMMENTS from accepted answer to get all the info.

Working code, with commands to run it: https://github.com/TomasBisciak/EthereumHelloWorldExample

Your truffle migrate command is showing nothing was deployed. Your output should look something like this:

 $ 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... 

Some things to try:

  1. Truffle has a minor issue running on Windows 10 where the truffle.js has name conflicts with the main truffle command. You'll want to rename truffle.js to truffle-config.js See this .
  2. Clear out all previous builds/migrations. Delete the build directory and run truffle migrate --reset .
  3. If the previous 2 don't work, restart testrpc to clear out any previous migrations on your local blockchain. Then rerun truffle migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM