This repository was archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
This repository was archived by the owner on Mar 11, 2024. It is now read-only.
Documentation not complete for bootstrapping nodejs example - missing critical item #191
Copy link
Copy link
Open
Description
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 if
from` 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, tarrencev and pirtim
Metadata
Metadata
Assignees
Labels
No labels