You will need Truffle and Ganache
To install truffle globally first make sure you are using the right node version:
nvm use
npm install -g truffle
Install your deps so compilations work:
yarn
Make sure Ganache is running and then compile and migrate the contracts:
truffle compile
truffle migrate
Symlink contracts because create-react-app says so and there is no gosh
cd node_modules
ln -s ../build/contracts contracts
Note: If you have restarted ganache and redeployed contracts you must delete
.cache
fromnode_modules
. You must also delete the symlinked contracts and redo the symlink. Also, don't forget to restore your MetaMask with the new seed.
You did it, you can now start the webserver
yarn start
You will need MetaMask. From the network dropdown choose Custom RPC and enter the RPC server displayed in Ganache. You can also use the Mnemonic from Ganache as the seed to view your accounts.
When you run into issues pray and tell yourself everything will be alright
- Did you choose the right network?
- Did you enter your mnemonic for the most recent time you restarted Ganache?
- When in doubt go to Settings > Reset Account
truffle console
SimpleStorage.deployed() // return the deployed contract...
.then(function(instance) {
return instance.set(7); // call a method of the contract...
}).then(function(value) {
return value;
});
SimpleStorage.deployed()
.then(function(instance) {
return instance.get.call();
}).then(function(value) {
return value.toString()
});
Copy Paste Friendly:
SimpleStorage.deployed().then(function(instance) {return instance.set(2);}).then(function(value) {return value;});
// and
SimpleStorage.deployed().then(function(instance) {return instance.get.call();}).then(function(value) {return value.toString()});
I need to wrap my head around working in Ethereum so hopefully writing it down helps.
Ganache is just a local ethereum node with an RPC server @ http://127.0.0.1:7545
MetaMask injects their own provider (using the provider engine). They do this to intercept certain RPC calls for example, where they intercept the sendTransaction
call and do their own signing and stuff on MetaMask.
Also worth noting this github comment explaining the state of things with subproviders and why MetaMask doesn't support websockets yet
React App connects to a web3 instance provided by MetaMask usually (src/utils/getWeb3.js
). Though one could manually connect to the node like so:
// RANDOM JSON RPC TEST
// Create a web3 provider: the Ganache node (but this could be your own full node or whatever)
const clientProvider = new Web3.providers.HttpProvider('http://127.0.0.1:7545')
// Create a client using the given provider: this is gives us the abstractions
// of the Javascript JSON RPC API (https://github.com/ethereum/wiki/wiki/JSON-RPC#javascript-api)
const client = new Web3(clientProvider)
// Use the abstractions to do stuff...
client.eth.getAccounts().then(accounts => console.log(accounts))
Or somehow modify the above to use MetaMask's provider engine to do fancier stuff.
Yeah right...