币安BSC智能链发币教程 remix+metamask实现币安BSC链上发币

一、准备:
1、使用在线的remix IDE或者搭建本地remixIDE都可以,优先选择在线的ide,中小项目足够了,但是在线remix的编译器版本加载有些慢,必要时需要KXSW
2、metamask钱包配置连接BSC主网,钱包中要有BNB,发币时消耗的BNB,这点区别于波场发币,波场消耗的是带宽和能量
metamask要充值足够的BNB否则合约发行失败,还会消耗掉你的BNB
3、合约接口要实现ERC20标准
4、部署合约时,要选择inject web3,连接上metamask钱包的用户来部署合约。
 
二、注意事项
1、发币完成后要记录交易的hash id,通过bscscan浏览器查询合约是否部署成功,并且获得发布的token的合约地址
2、bsc上发布合约后,暂时没有渠道可以上传通证logo,这点不如波场,在波场上可以自由上传更新logo
3、发币时建议首先在bsc的测试网上发布下合约,在钱包中转账,测试下是否满足合约中设定的模式。没有问题后再在bsc主网上发币。
三、具体发币步骤
1、打开remix在线IDE或者本地IDE环境
官网remix网址:https://remix.ethereum.org/,本示例使用的是本地remixIDE环境
币安BSC智能链发币教程——remix+metamask实现币安BSC链上发币【pdf+视频BSC发币教程下载】
2、创建合约文件
如果有多个文件,项目比较复杂的话,可以在根目录下创建文件夹,每发行一个代币保存到一个文件夹中,这样便于管理。
本示例以标准代币发行为例演示发币过程
在BSCS文件夹下创建五个sol文件:分别为IERC20.sol,SafeMath.sol,ERC20Detailed..sol,ERC20.sol,Tokenl.sol
对应的文件代码如下:
IERC20.sol文件:
pragma solidity ^0.5.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP. Does not include
 * the optional functions; to access them see {ERC20Detailed}.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

SafeMath.sol文件:

pragma solidity ^0.5.0;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

ERC20Detailed.sol 文件:

pragma solidity ^0.5.0;

import "./IERC20.sol";

/**
 * @dev Optional functions from the ERC20 standard.
 */
contract ERC20Detailed is IERC20 {
    string private _name;
    string private _symbol;
    uint8 private _decimals;

    /**
     * @dev Sets the values for `name`, `symbol`, and `decimals`. All three of
     * these values are immutable: they can only be set once during
     * construction.
     */
    constructor (string memory name, string memory symbol, uint8 decimals) public {
        _name = name;
        _symbol = symbol;
        _decimals = decimals;
    }

    /**
     * @dev Returns the name of the token.
     */
    function name() public view returns (string memory) {
        return _name;
    }

    /**
     * @dev Returns the symbol of the token, usually a shorter version of the
     * name.
     */
    function symbol() public view returns (string memory) {
        return _symbol;
    }

ERC20.sol 文件:

pragma solidity ^0.5.0;

import "./IERC20.sol";
import "./SafeMath.sol";

/**
 * @dev Implementation of the {IERC20} interface.
 *
 * This implementation is agnostic to the way tokens are created. This means
 * that a supply mechanism has to be added in a derived contract using {_mint}.
 * For a generic mechanism see {ERC20Mintable}.
 *
 * TIP: For a detailed writeup see our guide
 * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
 * to implement supply mechanisms].
 *
 * We have followed general OpenZeppelin guidelines: functions revert instead
 * of returning `false` on failure. This behavior is nonetheless conventional
 * and does not conflict with the expectations of ERC20 applications.
 *
 * Additionally, an {Approval} event is emitted on calls to {transferFrom}.
 * This allows applications to reconstruct the allowance for all accounts just
 * by listening to said events. Other implementations of the EIP may not emit
 * these events, as it isn't required by the specification.
 *
 * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
 * functions have been added to mitigate the well-known issues around setting
 * allowances. See {IERC20-approve}.
 */
contract ERC20 is IERC20 {
    using SafeMath for uint256;

    mapping (address => uint256) private _balances;

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

    uint256 private _totalSupply;

    /**
     * @dev See {IERC20-totalSupply}.
     */
    function totalSupply() public view returns (uint256) {
        return _totalSupply;
    }

Token.sol 文件:

// 0.5.1-c8a2
// Enable optimization
pragma solidity ^0.5.0;

import "./ERC20.sol";
import "./ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract Token is ERC20, ERC20Detailed {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor () public ERC20Detailed("Water Drop", "DROP", 18) {
        _mint(msg.sender, 10000 * (10 ** uint256(decimals())));
    }
}
修改token.sol文件的合约全称,简称,发行数量。本示例发行代币全称为Water Drop,简称为 DROP,发行总量为 10000
以上文件创建完成后保存会自动编译。
编译器版本选择0.5.15
币安BSC智能链发币教程——remix+metamask实现币安BSC链上发币【pdf+视频BSC发币教程下载】
3、部署合约
所有合约文件编译通过后,就可以部署合约了。
remix IDE支持以太坊,BSC,HECO链上部署合约,代码在所有链上完全一致,不需要任何针对平台的调整。
决定合约部署到哪个公链上,取决于metamask钱包当前连接到哪个公链上。
比如本示例想部署到BSC公链上,此时metamask就必须连接BSC主网:
然后在remix IDE上部署已经编译成功的合约:币安BSC智能链发币教程——remix+metamask实现币安BSC链上发币【pdf+视频BSC发币教程下载】
4、合约部署完成后,在BSC区块链浏览器上查询hash id,确认合约是否部署成功。
点击deploy执行合约部署后,在remix控制台输出如下:
币安BSC智能链发币教程——remix+metamask实现币安BSC链上发币【pdf+视频BSC发币教程下载】
其中最重要的信息是 transaction hash值,记录该值,到区块链浏览器上查询该值。
币安BSC智能链发币教程——remix+metamask实现币安BSC链上发币【pdf+视频BSC发币教程下载】
区块链浏览器显示该transcation hash对应的交易执行成功,合约成功部署,对应的合约地址为以上截图信息。
5、metamask钱包添加发行的代币
币安BSC智能链发币教程——remix+metamask实现币安BSC链上发币【pdf+视频BSC发币教程下载】
metamask钱包发币账户成功添加刚刚发行的代币,至此BSC主网发币完成。
区别于波场发币,波场发币完成后需要在tronscan上录入通证,同时上传logo。但是bsc链上不允许自定义logo,logo都是发币完成后自动生成的。
文章版权归原作者所有或来自互联网,未经允许请勿转载。如有侵权请联系我删除,谢谢!
THE END
分享
二维码
打赏
< <上一篇
下一篇>>