smart contract 知识点

知识点

memory vs storage vs stack

  • storage , where all the contract state variables reside. Every contract has its own storage and it is persistent between function calls and quite expensive to use.

  • memory , this is used to hold temporary values. It is erased between (external) function calls and is cheaper to use.

  • stack , which is used to hold small local variables. It is almost free to use, but can only hold a limited amount of values.

There are defaults for the storage location depending on which type of variable it concerns:

state variables are always in storage
function arguments are in memory by default
local variables of struct, array or mapping type reference storage by default
local variables of value type (i.e. neither array, nor struct nor mapping) are stored in the stack

constanst vs view vs pure

  • constant: Starting with solc 0.4.17, constant is deprecated in favor of two new and more specific modifiers.
  • View: This is generally the replacement for constant. It indicates that the function will not alter the storage state in any way.
  • Pure: This is even more restrictive, indicating that it won't even read the storage state.
  function returnTrue() public pure returns(bool response) {
    return true;
  }

ref

require vs assert vs revert vs throw(if)

简化:
require() and revert() will cause refund the remaining gas
assert() will used up all the remaining gas

Use require() to:

  • generally it will be used at the begining of a function, validate user inputs : such as invalid input
  • should be used more often

use assert() to :

  • prevent condition that should never, ever be possible : such as fatal error
  • generally it will be used at the end of a function
  • should be used less often

revert() it was said that revert can handle more complex logic.(emit that, I think we should use require() instead)
throw : deprecated

ref

ref

todo:

lib
use external source(local sol or git)
call from another contract
msg.sender what's in msg

ide 调试

string compare
return struct
testing

more useful ref:
best practise

FAQ about Error

Error: VM Exception while processing transaction: invalid opcode
-- out of index

Error: Invalid number of arguments to Solidity function
-- 参数不对

good REF

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值