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

1.

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

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

 for (var 0; 2000; i++) ... }

---

Solidity Types - 整型(Integer)

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

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

如果用二进制表示:

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

 

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

The  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。 

转载于:https://www.cnblogs.com/x-poior/p/10188177.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值