___|  _ \   |  |    |   |_ _|\ \     / ____|
 |     |   |  |  |    |   |  |  \ \   /  __|
 |   | |   | ___ __|  ___ |  |   \ \ /   |
\____|\___/     _|   _|  _|___|   \_/   _____| 

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

GUSD资金归集

BY: @chaimyu | CREATED: Oct. 31, 2018, 8:09 a.m. | VOTES: 26 | PAYOUT: $6.93 | [ VOTE ]

[IMAGE: https://cdn.steemitimages.com/DQmPZuAYp98cQTwMJPhnxqtbzVkjdaCpr3vdTm3sAPbKuBD/image.png]

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

TAGS: [ #gusd ] [ #erc20 ] [ #cn ] [ #programming ] [ #token ]

Replies

@ilovecoding | Oct. 31, 2018, 8:09 a.m. | Votes: 1 | [ VOTE ]

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!
[IMAGE: https://codingforspeed.com/images/i-love-coding.jpg]
Reply !stop to disable the comment. Thanks!

@luymm | Oct. 31, 2018, 8:10 a.m. | Votes: 1 | [ VOTE ]

路过

@cnbuddy | Oct. 31, 2018, 1:58 p.m. | Votes: 0 | [ VOTE ]

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

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>