trufflesuite / trufflesuite.github.io

Staging Repo of Build Artifacts for Truffle Suite. Find the source at https://github.com/trufflesuite/trufflesuite.com
https://www.trufflesuite.com
34 stars 161 forks source link

Documentation not complete for bootstrapping nodejs example - missing critical item #191

Open pszabop opened 6 years ago

pszabop commented 6 years ago

The bootstrap nodejs documentation is missing a critical item that gives a very obscure error, and it took two days to resolve with help from @gnidan on gitter. (thanks @gnidan!)

The documentation shows this.

// Step 1: Get a contract into my application
var json = require("./build/contracts/MyContract.json");

// Step 2: Turn that contract into an abstraction I can use
var contract = require("truffle-contract");
var MyContract = contract(json);

// Step 3: Provision the contract with a web3 provider
MyContract.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));

// Step 4: Use the contract!
MyContract.deployed().then(function(deployed) {
  return.deployed.someFunction();  // this throws with obscure message, see below
});

To make it function correctly, you need to create a web3 provider and use the accounts from it. For the mocha test framework this happens automatically, but if you are making your nodejs application it does not:

// Step 1:  Create a web3 instance with a default account.  This example works with testrpc
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3.defaultAccount = web3.eth.accounts[0];

// Step 2: Get a contract into my application
var json = require("./build/contracts/MyContract.json");

// Step 3: Turn that contract into an abstraction I can use
var contract = require("truffle-contract");
var MyContract = contract(json);

// Step 4: Provision the contract with a web3 provider and an account
MyContract.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));
Mycontract.defaults({ from: web3.eth.accounts[0] });

// Step 5: Use the contract!
MyContract.deployed().then(function(deployed) {
  return.deployed.someFunction();
});

And alas the error if the defaults.from:is not set is very obscure. I'll file a separate issue for truffle-contract, it should throw a useful error iffrom` is not set. The error:

    operator: error
    expected: |-
      undefined
    actual: |-
      [Error: invalid address: undefined]
    at: process._tickCallback (internal/process/next_tick.js:109:7)
    stack: |-
      Error: invalid address: undefined
          at inputAddressFormatter (/mnt/node_modules/web3/lib/web3/formatters.js:273:11)
          at inputTransactionFormatter (/mnt/node_modules/web3/lib/web3/formatters.js:99:20)
          at /mnt/node_modules/web3/lib/web3/method.js:89:28
          at Array.map (native)
          at Method.formatInput (/mnt/node_modules/web3/lib/web3/method.js:88:32)
          at Method.toPayload (/mnt/node_modules/web3/lib/web3/method.js:114:23)
          at Eth.send [as sendTransaction] (/mnt/node_modules/web3/lib/web3/method.js:139:30)
          at SolidityFunction.sendTransaction (/mnt/node_modules/web3/lib/web3/function.js:173:15)
          at SolidityFunction.execute (/mnt/node_modules/web3/lib/web3/function.js:256:37)
          at /mnt/node_modules/truffle-contract/contract.js:188:16
          at /mnt/node_modules/truffle-contract/contract.js:154:18
          at process._tickCallback (internal/process/next_tick.js:109:7)
rfaulhaber commented 6 years ago

I've been having the same issue. I had no idea I had to set the .defaults value, but when I did I definitely got something different. The documentation is very unclear on this.

bmmpxf commented 6 years ago

Thanks @pszabop for raising this issue. @gnidan can you look this over and make sure this is good to go as-is with the current version? Once you give the thumbs up, I can update the docs.

MagRelo commented 6 years ago

Thanks for raising the issue, and upcoming update to docs! I had the same problem