码迷,mamicode.com
首页 > 其他好文 > 详细

Solidity 没名字的function(){...}作用

时间:2018-03-19 16:45:05      阅读:521      评论:0      收藏:0      [点我收藏+]

标签:ons   imp   data   out   limited   SM   called   nsa   some   

官方解释:

这个叫做fallback function,当有人 1. 只发送以太币给合约而不带任何输入数据;2. 调用smart contract时调起了一个不存在的方法。会触发执行这个方法。

What is the deal with “function () { ... }” inside Solidity contracts? How can a function not have a name?

This function is called “fallback function” and it is called when someone just sent Ether to the contract without providing any data or if someone messed up the types so that they tried to call a function that does not exist.

The default behaviour (if no fallback function is explicitly given) in these situations is to just accept the call and do nothing. This is desireable in many cases, but should only be used if there is a way to pull out Ether from a contract.

If the contract is not meant to receive Ether with simple transfers, you should implement the fallback function as

function() { throw; }

this will cause all transactions to this contract that do not call an existing function to be reverted, so that all Ether is sent back.

Another use of the fallback function is to e.g. register that your contract received ether by using an event.

Attention: If you implement the fallback function take care that it uses as little gas as possible, because send() will only supply a limited amount.

Solidity 没名字的function(){...}作用

标签:ons   imp   data   out   limited   SM   called   nsa   some   

原文地址:https://www.cnblogs.com/huahuayu/p/8602590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!