-
Notifications
You must be signed in to change notification settings - Fork 3
Tips, Tricks, & Gotchas
Below is a list of common issues or tips that you will probably run into while developing DApps for the Ethereum using Solidity
All transactions on the blockchain must be confirmed through mining in the case of a PoW consensus algorithm, of which Ethereum and Bitcoin are currently using
[Miners try to find] A hash below a target value which can only be obtained, on average, by performing a certain amount of brute force work—therefore demonstrating proof of work. via bitcoin.org
What does this mean?
- Transactions take time to be processed. On average, you will wait 15-30 seconds waiting for a transaction to be confirmed on an public Ethereum network, like Rinkeby. Click here for current stats on the Rinkeby network
Every transaction on the Ethereum network costs a certain number of gas
Every operation that can be performed by a transaction or contract on the Ethereum platform costs a certain number of gas When it comes to actually paying for the gas, the transaction fee is charged as a certain number of ether via Ethereum Stack Exchange
What does this mean?
- You need to have an account, funded with ether, the cryptocurrency used on the Ethereum Platform, to transact on any public Ethereum Network. On public test networks you can use faucets, like this one to get ether transferred for free to your account for development purposes
- Because every line of code executed in a transaction costs some number of ether, contracts should be used to store raw data and simple business logic. You should avoid placing complex math or complex business logic, else you could easily run up costly transactions
To participate in a public Ethereum network, like calling methods on a solidity smart contract, you must be connected to an active node
What does this mean?
- You have to either run a full Ethereum node locally while developing using a CLI, like geth or use a service like Infura to act as your portal into a network
In documentation you will see references to TestRPC, this has been renamed to Ganaches, which is a command-line tool that allows you to create a local test ethereum network
Any updates to the blockchain, like updating the storage variables in a contract, need a transaction that must be confirmed through mining. However, if you are only reading data, you only need to invoke a function call, which is instantaneous and does not need a transaction.
What does this mean?
When you need to send transaction to a function:
- Takes time to be processed because it needs to be mined
- Returns a transaction id - hash string
- Costs gas to execute
- Use
send
on a function in for web3 instead ofcall
await inbox.methods.setMessage(newString).send({ gas: "5000000", from: accounts[0] });
await auctionFactory.methods.getDeployedAuctions().call();
When you call a read only function:
- It can return data
- It's instantaneous
- It's free
Every function execution in web3 are asynchronous
What does this mean?
- All functions return a promise
- Need to assign callbacks, else use cleaner async / await syntax
static async getInitialProps() {
const auctions = await auctionFactory.methods.getDeployedAuctions().call();
return { auctions };
}
You cannot return dynamic 2D arrays in your solidity smart contract (bytes[]
)
UnimplementedFeatureError: Nested dynamic arrays not implemented here.
What does this mean?
- You cannot return an array of strings (
string[]
) or any arrays containing dynamically sized types (bytes[]
) - Possible workaround in certain scenarios replace the dynamic sized type with a static sized type, like bytes with bytes32 for example.
Try to avoid looping through large arrays. They will cost more in gas as the array grows.
What does this mean?
- It is better to use the
mapping(address => uint)
type if constantly searching for values - Search times
- Array => linear time - x
- Mapping => constant search time - 1
Whenever you deploy your contract a new instance will be created at a new address. These instances have no connection to one another aside from containing the same deployed bytecode.
What does this mean?
- You must save the address everytime you deploy your contract.
- A common pattern to deal with this issue is to use the Factory Pattern
Functions that modify data cannot return any values