用Remix部署Solidity合约

    Remix是一个用于开发和部署Solidity合约的线上IDE,该IDE含有各个版本的Solidity,当一个工程有多个Solidity版本时,就需要使用Remix来进行分开编译和部署。

1、安装相关的编译与部署插件

1.1)安装Solidity编译插件

    点击Remix左侧页边栏里的两脚插头图标,进入Remix的搜索栏,输入solidity关键字,按[Active]依次激活如下插件
    SOLIDITY COMPILER
    SOLIDITY STATIC ANALYSYS
    SOLIDITY UINT TESTING

1.2)安装Solidity部署插件

    点击Remix左侧页边栏里的两脚插头图标,进入Remix是搜索栏,输入run关键字,点击[Active]即可安装

1.3)安装Debug插件和SOURCIFY代码检查插件

    同样,进入Remix是搜索栏,输入Debug关键字,点击[Active]即可安装;
    输入SOURCIFY关键字,点击[Active]即可安装;
    最后,一共6个插件,如下:

用Remix部署Solidity合约_第1张图片

图(1) 安装编译和部署等插件

2、设置Remix的页面颜色

    点页面左下角的齿轮按钮 -->Themes --> 颜色如下:
      Dark(dark) 深蓝色
      Light(ligt) 白色
      Midcentury(light) 浅棕色
      Black(dark) 深黑色
      Candy(light) 糖果色
      Cerulen(light) 浅青色
      Flaty(light) 淡白色
      Spacelab(ligth) 蛋白色
      Cybory (dark) 亮黑色
    建议选择第四个Black(dark),如图(2)所示:

用Remix部署Solidity合约_第2张图片

图(2)设置Remix的主题和颜色

3、新建一个Solidity文件,名称为HelloWorldToken.sol

//HelloWorldToken.sol

pragma solidity ^0.4.24;

library SafeMath {

  function mul(uint256 a, uint256 b) internal pure returns (uint256) {
    if (a == 0) {
      return 0;
    }
    uint256 c = a * b;
    require(c / a == b);
    return c;
  }
  function div(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b > 0); 
    uint256 c = a / b;
    return c;
  }
  function sub(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b <= a);
    uint256 c = a - b;
    return c;
  }
  function add(uint256 a, uint256 b) internal pure returns (uint256) {
    uint256 c = a + b;
    require(c >= a);
    return c;
  }
  function mod(uint256 a, uint256 b) internal pure returns (uint256) {
    require(b != 0);
    return a % b;
  }
}

library Roles {
    
  struct Role {
    mapping (address => bool) bearer;
  }
  function add(Role storage role, address account) internal {
    require(account != address(0));
    require(!has(role, account));
    role.bearer[account] = true;
  }
  function remove(Role storage role, address account) internal {
    require(account != address(0));
    require(has(role, account));
    role.bearer[account] = false;
  }
  function has(Role storage role, address account) internal view returns (bool) {
    require(account != address(0));
    return role.bearer[account];
  }
}

contract Ownable {

  address private _owner;
  event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  constructor() internal {
    _owner = msg.sender;
    emit OwnershipTransferred(address(0), _owner);
  }
  function owner() public view returns(address) {
    return _owner;
  }
  modifier onlyOwner() {
    require(isOwner());
    _;
  }
  function isOwner() public view returns(bool) {
    return msg.sender == _owner;
  }
  function renounceOwnership() public onlyOwner {
    emit OwnershipTransferred(_owner, address(0));
    _owner = address(0);
  }
  function transferOwnership(address newOwner) public onlyOwner {
    _transferOwnership(newOwner);
  }
  function _transferOwnership(address newOwner) internal {
    require(newOwner != address(0));
    emit OwnershipTransferred(_owner, newOwner);
    _owner = newOwner;
  }
}

contract ERC223ReceivingContract {

    function tokenFallback(address _from, uint256 _value, bytes _data) public;
}

interface IERC20 {
    
  function totalSupply() external view returns (uint256);
  function balanceOf(address who) external view returns (uint256);
  function allowance(address owner, address spender) external view returns (uint256);
  function transfer(address to, uint256 value) external returns (bool);
  function approve(address spender, uint256 value) external returns (bool);
  function transferFrom(address from, address to, uint256 value) external returns (bool);
  event Approval(address indexed owner, address indexed spender, uint256 value);

  //ERC223
  function transfer(address to, uint256 value, bytes data) external returns (bool success);
  event Transfer(address indexed from, address indexed to, uint256 value);
}

contract ERC20 is IERC20, Ownable {
    
  using SafeMath for uint256;
  mapping (address => uint256) private _balances;
  mapping (address => mapping (address => uint256)) private _allowed;
  mapping (address => bool) public frozenAccount;
  event frozenFunds(address account, bool freeze);
  uint256 private _totalSupply;
  function totalSupply() public view returns (uint256) {
    return _totalSupply;
  }
  function balanceOf(address owner) public view returns (uint256) {
    return _balances[owner];
  }
  function allowance(address owner, address spender) public view returns (uint256) {
    return _allowed[owner][spender];
  }
  function transfer(address to, uint256 value) public returns (bool) {
    _transfer(msg.sender, to, value);
    return true;
  }
  //ERC223
  function transfer(address to, uint256 value, bytes data) external returns (bool) {
    require(transfer(to, value));

   uint codeLength;

   assembly {
    // Retrieve the size of the code on target address, this needs assembly.
    codeLength := extcodesize(to)
  }

  if (codeLength > 0) {
    ERC223ReceivingContract receiver = ERC223ReceivingContract(to);
    receiver.tokenFallback(msg.sender, value, data);
    }
  return true;
  
  }
  function approve(address spender, uint256 value) public returns (bool) {
    require(spender != address(0));
    _allowed[msg.sender][spender] = value;
    emit Approval(msg.sender, spender, value);
    return true;
  }
  function transferFrom(address from, address to, uint256 value) public returns (bool) {
    require(value <= _allowed[from][msg.sender]);
    require(!frozenAccount[from]);
    require(from != address(0));
    _allowed[from][msg.sender] = _allowed[from][msg.sender].sub(value);
    _transfer(from, to, value);
    return true;
  }
  function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
    require(spender != address(0));
    _allowed[msg.sender][spender] = (
      _allowed[msg.sender][spender].add(addedValue));
    emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
    return true;
  }
  function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
    require(spender != address(0));
    _allowed[msg.sender][spender] = (
      _allowed[msg.sender][spender].sub(subtractedValue));
    emit Approval(msg.sender, spender, _allowed[msg.sender][spender]);
    return true;
  }
  function _transfer(address from, address to, uint256 value) internal {
    require(value <= _balances[from]);
    require(to != address(0));
    require(!frozenAccount[msg.sender]);
    _balances[from] = _balances[from].sub(value);
    _balances[to] = _balances[to].add(value);
    emit Transfer(from, to, value);
  }
  function _issue(address account, uint256 value) internal {
    require(account != 0);
    _totalSupply = _totalSupply.add(value);
    _balances[account] = _balances[account].add(value);
    emit Transfer(address(0), account, value);
  }
  function _burn(address account, uint256 value) internal {
    require(account != 0);
    require(!frozenAccount[account]);
    require(value <= _balances[account]);
    _totalSupply = _totalSupply.sub(value);
    _balances[account] = _balances[account].sub(value);
    emit Transfer(account, address(0), value);
  }
  function _burnFrom(address account, uint256 value) internal {
    require(value <= _allowed[account][msg.sender]);
    require(!frozenAccount[msg.sender]);
    _allowed[account][msg.sender] = _allowed[account][msg.sender].sub(value);
    _burn(account, value);
  }
}

contract PauserRole {
    
  using Roles for Roles.Role;
  event PauserAdded(address indexed account);
  event PauserRemoved(address indexed account);
  Roles.Role private pausers;
  constructor() internal {
    _addPauser(msg.sender);
  }
  modifier onlyPauser() {
    require(isPauser(msg.sender));
    _;
  }
  function isPauser(address account) public view returns (bool) {
    return pausers.has(account);
  }
  function addPauser(address account) public onlyPauser {
    _addPauser(account);
  }
  function renouncePauser() public {
    _removePauser(msg.sender);
  }
  function _addPauser(address account) internal {
    pausers.add(account);
    emit PauserAdded(account);
  }
  function _removePauser(address account) internal {
    pausers.remove(account);
    emit PauserRemoved(account);
  }
}

contract Pausable is PauserRole {
    
  event Paused(address account);
  event Unpaused(address account);
  bool private _paused;
  constructor() internal {
    _paused = false;
  }
  function paused() public view returns(bool) {
    return _paused;
  }
  modifier whenNotPaused() {
    require(!_paused);
    _;
  }
  modifier whenPaused() {
    require(_paused);
    _;
  }
  function pause() public onlyPauser whenNotPaused {
    _paused = true;
    emit Paused(msg.sender);
  }
  function unpause() public onlyPauser whenPaused {
    _paused = false;
    emit Unpaused(msg.sender);
  }
}

contract ERC20Burnable is ERC20 {

  function burn(uint256 value) public {
    _burn(msg.sender, value);
  }
  function burnFrom(address from, uint256 value) public {
    _burnFrom(from, value);
  }
}

contract ERC20Pausable is ERC20, Pausable {

  function transfer(address to, uint256 value) public whenNotPaused returns (bool) {
    return super.transfer(to, value);
  }
  function transferFrom(address from, address to, uint256 value) public whenNotPaused returns (bool) {
    return super.transferFrom(from, to, value);
  }
  function approve(address spender, uint256 value) public whenNotPaused returns (bool) {
    return super.approve(spender, value);
  }
  function increaseAllowance(address spender, uint addedValue) public whenNotPaused returns (bool success) {
    return super.increaseAllowance(spender, addedValue);
  }
  function decreaseAllowance(address spender, uint subtractedValue) public whenNotPaused returns (bool success) {
    return super.decreaseAllowance(spender, subtractedValue);
  }
 
}

contract ERC20Frozen is ERC20 {
    
  function freezeAccount (address target, bool freeze) onlyOwner public {
    require(target != address(0));  
    frozenAccount[target]=freeze;
    emit frozenFunds(target, freeze);
  }
}

contract HelloWorldToken is ERC20Burnable, ERC20Pausable, ERC20Frozen {

  string public constant name = "HelloWorldToken";
  string public constant symbol ="HWT";
  uint8 public constant decimals = 18;
  uint256 public constant INITIAL_SUPPLY = 100000000 * (10 ** uint256(decimals));

  constructor() public {
    _issue(msg.sender, INITIAL_SUPPLY);
  }
}

4、设置编译选项

    COMPILER: 0.4.26
    EVM VERSION: compiler default
    COMPILER CONFIGRURATION:
      √ Auto compile
      √ Enable optimization
    点击Compile按钮,如图(3)所示:

用Remix部署Solidity合约_第3张图片

图(3) 设置编译选项

5、设置部署选项

    ENVIRIONMENT: JavaScript VM
    ACCOUTNT: 默认即可
    GAS LIMIT: 3000000
    VALUE: 0 wei
    CONTRACT: HelloWorldToken - browser/HelloWorldToken.sol
    点击[Deploy]按钮,即可部署,如图(4)所示:

用Remix部署Solidity合约_第4张图片

图(4)设置部署选项

    等约半分钟,部署完成后,左侧Deploy Contracts下会弹出HelloWorldToken这一栏,点它的头部,会展开列出里面的操作方法,点它右括号后的尾巴会复制该Token地址,如图(5)所示:

用Remix部署Solidity合约_第5张图片

图(5) 点头部查看函数列表, 点右括号的尾巴得到合约地址

6、验证部署

    点击[name] 按钮,得到Token的全称;
    点击[owner]按钮,得到Token的主人地址;
    点击[symbol]按钮,得到Token的简称;
    点击[totalSupply]按钮,得到Token的总量。
    如图(6)所示:

用Remix部署Solidity合约_第6张图片

    点击[name]按钮,得到Token的全称为"HelloWorldToken",与代码里保持一致,这说明HelloWorldToken部署成功,它部署在本地JavaScript虚拟机里。 如果想要部署在测试网上,需要将部署选项参数ENVIRIONMENT设置为Injected Web3,并连上对应的MetaMask账户。

你可能感兴趣的:(区块链,Remix,Solidity合约,智能合约,以太坊,部署)