GUSD资金归集

in #gusd6 years ago

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新标准里有没有类似的方法。

Sort:  

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!

吃了吗?欢迎在steemauto里设置跟赞 @cnbuddy 给整个cn区点赞假如我的留言打扰到你,请回复“取消”。

Coin Marketplace

STEEM 0.27
TRX 0.11
JST 0.031
BTC 68853.42
ETH 3708.63
USDT 1.00
SBD 3.65