There is no way in solidity to check if an address is a contract. One of the goals of Ethereum is for humans and smart contracts to both be treated equally.2 ]/ g1 t" V- U) B- K
https://stackoverflow.com/a/37670490/72189121 J' ?# D. u0 `7 S
# U6 F3 I8 v# g' T2 W; n3 l
有时候确实有需要向智能合约转账:) u3 ]# f/ N! }: \$ w
但是大多数 smart contract 的 token 是不能转出的,只要合约里没写转出的逻辑,就不能转出。以太坊将智能合约看做是独立的个体,没人知道它的私钥,给不支持转出的智能合约转 token 就等于销毁了。8 C2 S) N/ P* ^3 K, `
当我们要转账时,会转到交易方的地址,不会闲着没事给智能合约转 token。但是这种事情仍然会发生,不断有人给 EOSTokenContract 转 EOS(见下图),EOSTokenContract 账户下现在有 154,834 EOS,价值 $1,176,738.4(价格 $7.6)。类似的,QtumTokenContract 账户下现在有 22,293 QTUM,价值 $158,280.3(价格 $7.1)。
这些 token 都都都都销毁了 (⊙?⊙)
Dexaran 认识到了这个问题,
Contracts that are not designed to work with tokens must reject incoming token transactions. Otherwise, each token becomes a potential token trap.2 k/ f, h' T5 G3 W& ^
ERC20 token standard issues.(google docs)3 J6 y; I1 W% S2 M! o% X( q
并在 github 创建了一个 issue。
tokenFallback 函数,当转账到不能转出的智能合约地址时,将自动取消。
contract ERC223 {/ N9 a# ?" c1 `" }
function transfer(address to, uint value, bytes data) {, b7 c9 N/ m7 P6 f
uint codeLength;- [2 E4 W1 L) G3 Q( D
assembly {
codeLength := extcodesize(_to)
}
balances[msg.sender] = balances[msg.sender].sub(_value);. |6 v8 I x) @# F6 O1 O
balances[_to] = balances[_to].add(_value);* @! O$ m7 N# U& E( T6 a! @- }
if(codeLength>0) {
// Require proper transaction handling.# P% H5 `1 d# S- P0 U/ V" l
ERC223Receiver receiver = ERC223Receiver(_to);
receiver.tokenFallback(msg.sender, _value, _data);+ V) I7 v/ N/ I6 h
}
}2 R9 |4 f7 A* X' h
}
【todo】:代码解释
The biggest change is that ERC223 no longer allow token to be transferred to a contract that does not allow token to be withdrawn.5 U7 o! l& b* d$ m, c S
https://medium.com/cryptomover/what-are-erc20-and-erc223-tokens-307badcca5a0 H9 z: z8 {8 N, C