在账户和 pallet 之间转移资产

Transferring assets between accounts and pallet

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

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

利用资产托盘进行充值:

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

提款:

<Assets::Module<T>>::transfer(RawOrigin::Root.into(), asset_id, 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_");

然后从这里,您可以从这个 PalletId:

生成一个 AccountId
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 和其他人。