在账户和托盘之间转移资产

问题描述

我正在尝试创建一个托盘,用户可以将资产存入和取出。 我已经编写了以下代码,但由于每个运行时都可以访问 frame_system::RawOrigin::Root.into(),因此我不确定这是最好的处理方式。

我对 Substrate 还是很陌生,不确定这到底是如何工作的,希望得到一些关于最佳设计选择的指导。

利用资产托盘存入:

<Assets::Module<T>>::transfer(origin,asset_id,RawOrigin::Root.into(),amount);

退出

<Assets::Module<T>>::transfer(RawOrigin::Root.into(),origin,amount);

编辑

solidity 编写的类似想法:

contract DepositWithdrawSend {
    using SafeMath for uint256;
    mapping (address => mapping (address => uint256)) public depositInfo;
    address public sendPallet;

    constructor(address _sendPallet) public {
        sendPallet = _sendPallet;
    }

    function deposit(address _token,uint256 _amount) public {
        IERC20(_token).transferFrom(msg.sender,address(this),amount);
        depositInfo[_token][msg.sender] = depositInfo[_token][msg.sender].add(_amount);
    }

    function withdraw(address _token,uint256 _amount) public {
        require(depositInfo[_token][msg.sender] >= _amount,"Over withdraw");
        require(IERC20(_token).balanceOf(address(this)) >= _amount,"Not enough");
        IERC20(_token).transfer(msg.sender,amount);
        depositInfo[_token][msg.sender] = depositInfo[_token][msg.sender].sub(_amount);
    }

    function send(address _token,uint256 _amount) public {
        require(IERC20(_token).balanceOf(address(this)) >= _amount,"Not enough");
        IERC20(_token).transfer(sendPallet,amount);
    }
}

解决方法

我们遵循一个非常简单的模式,为托盘提供自己的“帐户”,用于将余额转移到其他任何地方。

首先,您创建一个唯一的 PalletId 代表您的托盘:

use frame_support::PalletId;
const MyPalletId: PalletId = PalletId(*b"replace_");

然后从这里,您可以从此 AccountId 生成一个 PalletId

use sp_runtime::traits::AccountIdConversion;

/// These actually do computation. If you need to keep using them,/// then make sure you cache the value and only call them once.

pub fn account_id() -> T::AccountId {
    T::PalletId::get().into_account()
}

pub fn sub_account(seed: u16) -> T::AccountId {
    // only use two byte prefix to support 16 byte account id (used by test)
    // "modl" ++ "replace_" ++ "hi" is 14 bytes,and two bytes remaining for bounty index
    T::PalletId::get().into_sub_account(("hi",id))
}

此模式用于 Treasury Pallet 等。