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$ w, j, Z9 x+ l1 i( K
https://stackoverflow.com/a/37670490/72189121 R2 b) q* z$ s4 Q5 @8 y/ Z% v# d
有时候确实有需要向智能合约转账:* H% s5 R( f! s ^2 u8 X: H) D! T
但是大多数 smart contract 的 token 是不能转出的,只要合约里没写转出的逻辑,就不能转出。以太坊将智能合约看做是独立的个体,没人知道它的私钥,给不支持转出的智能合约转 token 就等于销毁了。
当我们要转账时,会转到交易方的地址,不会闲着没事给智能合约转 token。但是这种事情仍然会发生,不断有人给 EOSTokenContract 转 EOS(见下图),EOSTokenContract 账户下现在有 154,834 EOS,价值 $1,176,738.4(价格 $7.6)。类似的,QtumTokenContract 账户下现在有 22,293 QTUM,价值 $158,280.3(价格 $7.1)。2 v1 ~6 N2 Q' g7 o- z2 p
这些 token 都都都都销毁了 (⊙?⊙)
Dexaran 认识到了这个问题,0 O( d2 S% W- n* T/ |
Contracts that are not designed to work with tokens must reject incoming token transactions. Otherwise, each token becomes a potential token trap.$ D. N% K% g5 c4 C0 p2 a& [3 d
ERC20 token standard issues.(google docs)& M5 g, p* |4 ?
+ R$ W1 H4 N- F' |/ H
并在 github 创建了一个 issue。
tokenFallback 函数,当转账到不能转出的智能合约地址时,将自动取消。
contract ERC223 {
function transfer(address to, uint value, bytes data) {. N& H0 R% T) [, x7 ^9 M+ [
uint codeLength;
assembly {4 d9 [& `$ V' Y2 ~
codeLength := extcodesize(_to)5 }. L3 j/ x: m7 Y/ D8 [& c
}+ g# E4 b& ]7 b. k# d- x
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);! G9 A2 K+ i3 {( w$ f
if(codeLength>0) {
// Require proper transaction handling., A" Z/ p6 W Q# f) l! R
ERC223Receiver receiver = ERC223Receiver(_to);) \' A) x& c% V
receiver.tokenFallback(msg.sender, _value, _data);: P$ p, J1 }( Y1 }9 n9 H
}2 d# z* e& h% r7 ?: B/ h( o
}
}
【todo】:代码解释4 p5 y, X. }4 ]. B% \/ X4 N& c
The biggest change is that ERC223 no longer allow token to be transferred to a contract that does not allow token to be withdrawn. [; G; R2 Q2 N* X/ b8 H! F: ]
https://medium.com/cryptomover/what-are-erc20-and-erc223-tokens-307badcca5a! @; O* a2 S5 {/ N- z+ i% Z8 N, b