1. Use the struct structure
Use the struct structure to realize the structure storage space, and the compiler will automatically optimize the storage of the struct. For example, the uint type, whether it is uint32 or uint256, will use 256 storage by default. Two uint32s also take up two 256 storage units. But if it is two uint32 inside the struct, it will only occupy a 256 storage unit.
2. For functions that do not need to modify the content on the chain, add the view keyword.
When a player calls a view
function from the outside, there is no need to pay a gas.
This is because view
functions don't actually change any data on the blockchain - they just read. So view
marking a function with means to tell that web3.js
running this function only needs to query your local Ethereum node, without creating a transaction on the blockchain (the transaction needs to be run on every node, so it costs gas).
We'll cover how to set up web3.js on your own node later. But now, the key thing for you to remember is to mark the "read-only" external view
statement on the function that can be read-only, and you can reduce the gas usage in the DApp for your players.
Note: If a
view
function is called inside another function, and the calling functionview
does not belong to the same contract as the function, a call cost will also be incurred. This is because if the calling function creates a transaction in Ethereum, it still needs to be verified node by node. Soview
functions marked with are free only when called externally.