ERC20 token在转移时都需要有以太币做为燃料费,GUSD也是ERC20代币,但GUSD提供了一种不需要以太币进行资金归集的方法,简单来看一看。
授权资金归集
function enableSweep(uint8[] _vs, bytes32[] _rs, bytes32[] _ss, address _to) public onlySweeper {
require(_to != address(0));
require((_vs.length == _rs.length) && (_vs.length == _ss.length));
uint256 numSignatures = _vs.length;
uint256 sweptBalance = 0;
for (uint256 i=0; i<numSignatures; ++i) {
address from = ecrecover(sweepMsg, _vs[i], _rs[i], _ss[i]);
// ecrecover returns 0 on malformed input
if (from != address(0)) {
sweptSet[from] = true;
uint256 fromBalance = erc20Store.balances(from);
if (fromBalance > 0) {
sweptBalance += fromBalance;
erc20Store.setBalance(from, 0);
erc20Proxy.emitTransfer(from, _to, fromBalance);
}
}
}
if (sweptBalance > 0) {
erc20Store.addBalance(_to, sweptBalance);
}
}
如果要允许地址余额被归集,需要用这个地址对合约中变量“sweepMsg”进行签名,把签名数据给Sweeper。
Sweeper用签名后的数据调用enableSweep()函数,波函数中会通过“ecrecover”验证签名数据,验证通过则把这个地址放入可归集地址中(sweptSet[from]),同时将地址余额转移到函数参数“_to”指定的地址。
可以看出,只要签名同意过Sweeper进行资金归集,这个授权就是永久的,Sweeper有权将地址余额转移到任何地址。
重新归集
function replaySweep(address[] _froms, address _to) public onlySweeper {
require(_to != address(0));
uint256 lenFroms = _froms.length;
uint256 sweptBalance = 0;
for (uint256 i=0; i<lenFroms; ++i) {
address from = _froms[i];
if (sweptSet[from]) {
uint256 fromBalance = erc20Store.balances(from);
if (fromBalance > 0) {
sweptBalance += fromBalance;
erc20Store.setBalance(from, 0);
erc20Proxy.emitTransfer(from, _to, fromBalance);
}
}
}
if (sweptBalance > 0) {
erc20Store.addBalance(_to, sweptBalance);
}
}
这个函数比较简单,Sweeper可以调用这个函数,传入一堆地址,如果这些地址是授权可以被归集的,则将地址余额转移到函数参数“_to”指定的地址中。
结论
GUSD通过一次签名永久授权的方式,提供了一种ERC20代币资金归集的方法,可以借鉴。
遗憾的是只能自己写的ERC20合约才可以这样做,已经发布的ERC20代币进行资金归集还是需要以太币的,不知道ERC新标准里有没有类似的方法。
路过
Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!
Reply !stop to disable the comment. Thanks!