首页>>资讯>>产业

通过metamask跨链桥兑换matic代币

2024-01-19 16:14:15 94

在polygon链上部署合约时需要消耗matic作为WETH,因此需要提前获取matic代币。目前最常见的方法是通过跨链桥直接兑换matic。可以选择从以太坊eth链或者BSC币安链上兑换相关的代币为matic代币,用于在polygon链上部署合约。


以下演示兑换BSC链上BNB为matic代币的操作流程


选择metamask跨链桥 https://portfolio.metamask.io/bridge

选项兑换代币对应的网络和币种

1.png

兑换完成后切换钱包到polygon网络,查看当前matic余额


附:polygon马蹄链上IERC20接口类型合约部署源码示例:


contract MaticToken is IERC20, Ownable, BaseToken {

    using SafeMath for uint256;


    uint256 public constant VERSION = 1;


    mapping(address => uint256) private _balances;

    mapping(address => mapping(address => uint256)) private _allowances;


    string private _name;

    string private _symbol;

    uint8 private _decimals;

    uint256 private _totalSupply;


    constructor(

        string memory name_,

        string memory symbol_,

        uint8 decimals_,

        uint256 totalSupply_

    ) payable {

        _name = name_;

        _symbol = symbol_;

        _decimals = decimals_;

        _mint(owner(), totalSupply_ * 10 ** decimals_);


        emit TokenCreated(owner(), address(this), TokenType.standard, VERSION);

    }


    function name() public view virtual returns (string memory) {

        return _name;

    }


    function symbol() public view virtual returns (string memory) {

        return _symbol;

    }


    function decimals() public view virtual returns (uint8) {

        return _decimals;

    }


    function totalSupply() public view virtual override returns (uint256) {

        return _totalSupply;

    }


    function balanceOf(address account)

        public

        view

        virtual

        override

        returns (uint256)

    {

        return _balances[account];

    }


    function transfer(address recipient, uint256 amount)

        public

        virtual

        override

        returns (bool)

    {

        _transfer(_msgSender(), recipient, amount);

        return true;

    }


    function allowance(address owner, address spender)

        public

        view

        virtual

        override

        returns (uint256)

    {

        return _allowances[owner][spender];

    }


    function approve(address spender, uint256 amount)

        public

        virtual

        override

        returns (bool)

    {

        _approve(_msgSender(), spender, amount);

        return true;

    }


    function transferFrom(

        address sender,

        address recipient,

        uint256 amount

) public virtual override returns (bool) {

        _transfer(sender, recipient, amount);

        _approve(

            sender,

            _msgSender(),

            _allowances[sender][_msgSender()].sub(

                amount,

                "ERC20: transfer amount exceeds allowance"

            )

        );

        return true;

    }


    function increaseAllowance(address spender, uint256 addedValue)

        public

        virtual

        returns (bool)

    {

        _approve(

            _msgSender(),

            spender,

            _allowances[_msgSender()][spender].add(addedValue)

        );

        return true;

    }


    function decreaseAllowance(address spender, uint256 subtractedValue)

        public

        virtual

        returns (bool)

    {

        _approve(

            _msgSender(),

            spender,

            _allowances[_msgSender()][spender].sub(

                subtractedValue,

                "ERC20: decreased allowance below zero"

            )

        );

        return true;

    }


    function _transfer(

        address sender,

        address recipient,

        uint256 amount

    ) internal virtual {

        require(sender != address(0), "ERC20: transfer from the zero address");

        require(recipient != address(0), "ERC20: transfer to the zero address");


        _beforeTokenTransfer(sender, recipient, amount);


        _balances[sender] = _balances[sender].sub(

            amount,

            "ERC20: transfer amount exceeds balance"

        );

        _balances[recipient] = _balances[recipient].add(amount);

        emit Transfer(sender, recipient, amount);

    }


    function _mint(address account, uint256 amount) internal virtual {

        require(account != address(0), "ERC20: mint to the zero address");


        _beforeTokenTransfer(address(0), account, amount);


        _totalSupply = _totalSupply.add(amount);

        _balances[account] = _balances[account].add(amount);

        emit Transfer(address(0), account, amount);

    }


    function _burn(address account, uint256 amount) internal virtual {

        require(account != address(0), "ERC20: burn from the zero address");


        _beforeTokenTransfer(account, address(0), amount);


        _balances[account] = _balances[account].sub(

            amount,

            "ERC20: burn amount exceeds balance"

        );

        _totalSupply = _totalSupply.sub(amount);

        emit Transfer(account, address(0), amount);

    }


    function _approve(

        address owner,

        address spender,

        uint256 amount

    ) internal virtual {

        require(owner != address(0), "ERC20: approve from the zero address");

        require(spender != address(0), "ERC20: approve to the zero address");


        _allowances[owner][spender] = amount;

        emit Approval(owner, spender, amount);

    }


    function _setupDecimals(uint8 decimals_) internal virtual {

        _decimals = decimals_;

    }


    function _beforeTokenTransfer(

        address from,

        address to,

        uint256 amount

    ) internal virtual {}

}


至此,完成通过metamask跨链桥兑换matic代币所有操作流程。

声明:本网站所有相关资料如有侵权请联系站长删除,资料仅供用户学习及研究之用,不构成任何投资建议!