2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > Solidity语言基础 和 Etherum ERC20合约基础

Solidity语言基础 和 Etherum ERC20合约基础

时间:2021-10-10 00:44:00

相关推荐

Solidity语言基础 和 Etherum ERC20合约基础

1.

类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的,

原因是``i`` 的类型是uint8,而这个类型变量的最大值比2000小。

for(vari=0;i<2000;i++){...}

---

Solidity Types - 整型(Integer)

无符号整型(uint)是计算机编程中的一种数值资料型别。有符号整型(int)可以表示任何规定范围内的整数,无符号整型只能表示非负数(0及正数)。

有符号整型能够表示负数的代价是其能够存储正数的范围的缩小,因为其约一半的数值范围要用来表示负数。如:uint8的存储范围为0 ~ 255,而int8的范围为-127 ~ 127

如果用二进制表示:

uint8:0b00000000~0b11111111,每一位都存储值,范围为0 ~ 255int80b11111111~ob01111111,最左一位表示符号,1表示0表示,范围为-127 ~ 127

2.Solidity中移除 constant关键字,为什么?

Theconstantmodifier 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 ofviewandpureconveys the meaning of their usage.

viewcan be considered as the subset ofconstantthat 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; } }

purecan be considered as the subset ofconstantwhere 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。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。