Developing for Ethereum requires several moving parts. Our goal is to kickstart your debugging experience. Instructions apply to both MacOSX and Windows.
Geth — A Local Ethereum Node
We prefer to use a Geth local Ethereum node. Installation packages are available for MacOSX and Windows.
Why Geth?
We have discovered that Ganace and ganache-cli while useful might deviate from the standard Ethereum node behavior.
Initializing Geth
Get the genesis block, genesis.json
Initialise Gets
geth --datadir=./datadir init genesis.json
Creating Accounts
Create several accounts:
geth --datadir=./datadir account new
Store passwords, one password per line in a file, say password.geth.remix.txt
Mine Only When There Are Transactions
If you enter Gets console, with:
geth --datadir=./datadir console
And do:
miner.start()
You will get a flood of messages due to mining that will disrupt your debugging experience.
So, get the ethtxminer.js script that will cause Geth to mine only when a transaction arrived.
Running Geth for Development
Run with:
geth --datadir=./datadir --nodiscover --rpc --rpcapi "db,personal,eth,net,web3,debug" --rpccorsdomain='*' --rpcaddr="localhost" --rpcport 8545 --unlock 0,1,2,3,4 --password password.geth.remix.txt --jspath . --preload ethtxminer.js console
Remix
Browser-based IDE — works in Safari and Chrome on MacOSX
http://remix.ethereum.org
Use the the http
to work with Geth.
In the Run tab, select Provider to be Web3 Provider. Then select your local Geth node http://localhost:8545
.
Remixd
Remixd connects Remix to local files so you can edit in Remix or in your IDE.
npm install -g remixd
Run with:
remixd -s contracts
where contracts
is your local contracts folder.
Open Local Files in Remix
In Remix, in the top left corner, click the link icon to connect to Remixd.
In the left sidebar, all your files appear under localhost
. Editing can be done either in IDE or in Remix. There is no save
action in Remix, so files are updated immediately in the file system.
Creating Accounts
Should be done with Geth console. No way to do it in Remix.
ABI and Bytecode
After compiling a contract, click on Details to copy them manually
Using Openzeppelin
OpenZeppelin is a well-known library for writing secure Smart Contracts on Ethereum. It provides a lot of standard contracts that you can inherit from.
Imports from Zeppelin in your contracts should be prefixed by:
github.com/OpenZeppelin/
So to import the Crowdsale
contract, your import should be:
github.com/OpenZeppelin/openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol
This way Remix is able to fetch them and compile your contracts.
Alternatively, you can download openzeppelin-solidity to a subfolder of your local contracts folder, and refer to them with relative paths, such as ./openzeppelin-solidity/contracts/crowdsale/Crowdsale.sol
Debugging
Most of your interaction with contracts, such as deployment, and calling of contract functions will be in Remix, using the Run tab. But, some things cannot be done in Remix, and some things do not work in Remix.
What Cannot be Done in Remix?
- Create new account
- Transfer Ether between accounts
What Does Not Work in Remix
We discovered events emitted by contracts sometimes do not appear in Remix transactions log.
Common Errors
Out of Gas in Remix
When Remix warns that a transaction will fail because of not enough gas, it usually, but not always mean that there is a bug in the contract function to be called. Or in the case of Deploy, in the contract constructor.
Oversized Data in Deploy
You should start by trying to refactor the contract code in order to reduce its size. This should be attempted before dealing with the block gas limit