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.# T; r' P/ F* R$ B8 ?; }' t6 W& @
https://stackoverflow.com/a/37670490/7218912
# ?" v+ {6 ? j! V, F3 @ X3 _
有时候确实有需要向智能合约转账:( x, h# `% y& g: J' i6 Z
但是大多数 smart contract 的 token 是不能转出的,只要合约里没写转出的逻辑,就不能转出。以太坊将智能合约看做是独立的个体,没人知道它的私钥,给不支持转出的智能合约转 token 就等于销毁了。4 S6 [: Q0 n5 f3 q9 C7 p: f8 T
当我们要转账时,会转到交易方的地址,不会闲着没事给智能合约转 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.
ERC20 token standard issues.(google docs)8 l1 V, n- K8 c; X) O
& b C- J: w$ ], |# O7 o
并在 github 创建了一个 issue。
tokenFallback 函数,当转账到不能转出的智能合约地址时,将自动取消。 _2 }, d# L) [1 Z
contract ERC223 {
function transfer(address to, uint value, bytes data) {
uint codeLength;
assembly {3 z2 `6 |& T5 L& b/ `: k W/ g
codeLength := extcodesize(_to)
}
balances[msg.sender] = balances[msg.sender].sub(_value);( h$ B3 l0 p' v9 u
balances[_to] = balances[_to].add(_value);
if(codeLength>0) {) B. e: g) ]+ Z i
// Require proper transaction handling." q* }/ n% p1 W# R3 ?
ERC223Receiver receiver = ERC223Receiver(_to);7 s3 K, T9 B7 K# i
receiver.tokenFallback(msg.sender, _value, _data);9 Y& E* h- j6 A4 {7 X4 Z* e% s
}
}
}
【todo】:代码解释; w8 X3 ^& f( Z: s) ~+ s
The biggest change is that ERC223 no longer allow token to be transferred to a contract that does not allow token to be withdrawn.
https://medium.com/cryptomover/what-are-erc20-and-erc223-tokens-307badcca5a