1.
类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的,
原因是``i`` 的类型是 uint8
,而这个类型变量的最大值比 2000
小。
for (var i = 0; i < 2000; i++) { ... }
---
Solidity Types - 整型(Integer)
无符号整型(uint)是计算机编程中的一种数值资料型别。有符号整型(int)可以表示任何规定范围内的整数,无符号整型只能表示非负数(0及正数)。
有符号整型能够表示负数的代价是其能够存储正数的范围的缩小,因为其约一半的数值范围要用来表示负数。如:uint8
的存储范围为0 ~ 255
,而int8
的范围为-127 ~ 127
如果用二进制表示:
- uint8: 0b
00000000
~ 0b11111111
,每一位都存储值,范围为0 ~ 255 - int8:0b
11111111
~ ob01111111
,最左一位表示符号,1
表示负
,0
表示正
,范围为-127 ~ 127
2.Solidity中移除 constant关键字,为什么?
constant
modifier has the meaning that the function wont modify the contract storage (But the the word constant didn't actually convey the meaning that it is used for).The new replacements of view
and pure
conveys the meaning of their usage.
view
can be considered as the subset of constant
that will read the storage(hence viewing). However the storage will not be modified.
eg:
contract viewExample {
string state;
// other contract functions
function viewState() public view returns(string) {
//read the contract storage
return state;
}
}
pure
can be considered as the subset of constant
where the return value will only be determined by it's parameters(input values) . There will be no read or write to storage and only local variable will be used (has the concept of pure functions in functional programming)
eg:
contract pureExample {
// other contract functions
function pureComputation(uint para1 , uint para2) public pure returns(uint result) {
// do whatever with para1 and para2 and assign to result as below
result = para1 + para2;
return result;
}
}
3.
ERC20的 approve和allowance,transferFrom用法?
approve 和allowance的用法
账户A有1000个ETH,想允许B账户随意调用100个ETH。A账户按照以下形式调用approve函数approve(B,100)。当B账户想用这100个ETH中的10个ETH给C账户时,则调用transferFrom(A, C, 10)。这时调用allowance(A, B)可以查看B账户还能够调用A账户多少个token。