基于OpenZepplin上线交易所的安全token合约

因为代币合约的安全问题,造成好多交易所和持币公司被黑客利用漏洞攻击,造成大量的损失,所以现在很多交易所 对代币都会进行审核,审核不通过的代币则不允许上线。

OpenZepplin库是一个写只能合约的安全库,利用可以简化我们的开发,而且它内置了很多东西,包括安全等方面。

只是做交易代币的话,有两种方法部署可以通过交易所审查的加密代币,第一种只说方法(过程以前的教程有),对于大部分人我推荐第二种(方便,快捷不写代码,复制我整理的代码,按步骤操作就行):

1.利用

使用OpenZeppelin对于智能合约开发会快速很多,安装truffle框架npminstallzeppelin-solidity安装,然后继承里面的已有的智能合约就可以了。

打开truffle框架,contrancts目录,穿件一个只能合约TetsCion.sol文件,

pragma solidity ^0.4.4;

import "../node_modules/_openzeppelin-solidity@1.9.0@openzeppelin-solidity/contracts/token/StandardToken.sol";(引入openzeppelin安全库合约)

contract NenmoCoin is StandardToken {

......(里面写合约内容)

}

}

如图:

truffle框架目录 (windows删除truffle.js文件)

node模块

openzeppelin库:

openzeppelin合约模块:

token合约文件:

里面有几种代币的合约,我们做erc20的合约

openzeppelin内置的安全的ERC20合约:

写合约的时候,为了防止漏洞可以参考里面的已经审查过得安全合约

剩下的就是撸合约代码,编译部署,用truffle还是开发比较快的(但是不利于迭代),但是如果想深入了解以太坊的话,建议用WEB3,进行编译部署,可以写一个属于自己的部署框架。

2.傻瓜式操作,按照步骤来就好:

1)打开remix在线编译器(怎么用我以前的教程里面有):

https://ethereum.github.io/browser-solidity/

2)登录metamask钱包:

3)复制下面代码到remix编译器:

pragma solidity ^0.4.21;

/**

* @title SafeMath

* @dev Math operations with safety checks that throw on error

*/

library SafeMath {

/**

* @dev Multiplies two numbers, throws on overflow.

*/

function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {

if (a == 0) {

return 0;

}

c = a * b;

assert(c / a == b);

return c;

}

/**

* @dev Integer division of two numbers, truncating the quotient.

*/

function div(uint256 a, uint256 b) internal pure returns (uint256) {

// assert(b > 0); // Solidity automatically throws when dividing by 0

// uint256 c = a / b;

// assert(a == b * c + a % b); // There is no case in which this doesn't hold

return a / b;

}

/**

* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).

*/

function sub(uint256 a, uint256 b) internal pure returns (uint256) {

assert(b

return a - b;

}

/**

* @dev Adds two numbers, throws on overflow.

*/

function add(uint256 a, uint256 b) internal pure returns (uint256 c) {

c = a + b;

assert(c >= a);

return c;

}

}

/**

* @title ERC20Basic

* @dev Simpler version of ERC20 interface

* @dev see https://github.com/ethereum/EIPs/issues/179

*/

contract ERC20Basic {

function totalSupply() public view returns (uint256);

function balanceOf(address who) public view returns (uint256);

function transfer(address to, uint256 value) public returns (bool);

event Transfer(address indexed from, address indexed to, uint256 value);

}

/**

* @title Basic token

* @dev Basic version of StandardToken, with no allowances.

*/

contract BasicToken is ERC20Basic {

using SafeMath for uint256;

mapping(address => uint256) balances;

uint256 totalSupply_;

/**

* @dev total number of tokens in existence

*/

function totalSupply() public view returns (uint256) {

return totalSupply_;

}

/**

* @dev transfer token for a specified address

* @param _to The address to transfer to.

* @param _value The amount to be transferred.

*/

function transfer(address _to, uint256 _value) public returns (bool) {

require(_to != address(0));

require(_value

balances[msg.sender] = balances[msg.sender].sub(_value);

balances[_to] = balances[_to].add(_value);

emit Transfer(msg.sender, _to, _value);

return true;

}

/**

* @dev Gets the balance of the specified address.

* @param _owner The address to query the the balance of.

* @return An uint256 representing the amount owned by the passed address.

*/

function balanceOf(address _owner) public view returns (uint256) {

return balances[_owner];

}

}

/**

* @title ERC20 interface

* @dev see https://github.com/ethereum/EIPs/issues/20

*/

contract ERC20 is ERC20Basic {

function allowance(address owner, address spender) public view returns (uint256);

function transferFrom(address from, address to, uint256 value) public returns (bool);

function approve(address spender, uint256 value) public returns (bool);

event Approval(address indexed owner, address indexed spender, uint256 value);

}

/**

* @title Standard ERC20 token

*

* @dev Implementation of the basic standard token.

* @dev https://github.com/ethereum/EIPs/issues/20

* @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol

*/

/**

* @title Standard ERC20 token

*

* @dev Implementation of the basic standard token.

* @dev https://github.com/ethereum/EIPs/issues/20

* @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol

*/

contract StandardToken is ERC20, BasicToken {

mapping (address => mapping (address => uint256)) internal allowed;

/**

* @dev Transfer tokens from one address to another

* @param _from address The address which you want to send tokens from

* @param _to address The address which you want to transfer to

* @param _value uint256 the amount of tokens to be transferred

*/

function transferFrom(address _from, address _to, uint256 _value) public returns (bool) {

require(_to != address(0));

require(_value

require(_value

balances[_from] = balances[_from].sub(_value);

balances[_to] = balances[_to].add(_value);

allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);

emit Transfer(_from, _to, _value);

return true;

}

/**

* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.

*

* 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

* @param _spender The address which will spend the funds.

* @param _value The amount of tokens to be spent.

*/

function approve(address _spender, uint256 _value) public returns (bool) {

allowed[msg.sender][_spender] = _value;

emit Approval(msg.sender, _spender, _value);

return true;

}

/**

* @dev Function to check the amount of tokens that an owner allowed to a spender.

* @param _owner address The address which owns the funds.

* @param _spender address The address which will spend the funds.

* @return A uint256 specifying the amount of tokens still available for the spender.

*/

function allowance(address _owner, address _spender) public view returns (uint256) {

return allowed[_owner][_spender];

}

/**

* @dev Increase the amount of tokens that an owner allowed to a spender.

*

* approve should be called when allowed[_spender] == 0. To increment

* allowed value is better to use this function to avoid 2 calls (and wait until

* the first transaction is mined)

* From MonolithDAO Token.sol

* @param _spender The address which will spend the funds.

* @param _addedValue The amount of tokens to increase the allowance by.

*/

function increaseApproval(address _spender, uint _addedValue) public returns (bool) {

allowed[msg.sender][_spender] = allowed[msg.sender][_spender].add(_addedValue);

emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);

return true;

}

/**

* @dev Decrease the amount of tokens that an owner allowed to a spender.

*

* approve should be called when allowed[_spender] == 0. To decrement

* allowed value is better to use this function to avoid 2 calls (and wait until

* the first transaction is mined)

* From MonolithDAO Token.sol

* @param _spender The address which will spend the funds.

* @param _subtractedValue The amount of tokens to decrease the allowance by.

*/

function decreaseApproval(address _spender, uint _subtractedValue) public returns (bool) {

uint oldValue = allowed[msg.sender][_spender];

if (_subtractedValue > oldValue) {

allowed[msg.sender][_spender] = 0;

} else {

allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);

}

emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);

return true;

}

}

contract TestCoin is StandardToken {

string public name = "TestCoin";

string public symbol = "TTB";

uint8 public decimals = 18;

uint256 public totalSupply;

function TestCoin() public{

totalSupply = 100000000* 10 ** uint256(decimals);

balances[msg.sender] = 100000000 * 10 ** uint256(decimals);

}

}

PS:最下的name symbol 和totalSupply分别对应,数字货币的名称、符号和数量,根据需求自定义修改,至于自动兑换、管理冻结等功能可参考我以前的教程,都有

4.选择合约名称,点击create部署

有很多个合约,但是TestCoin继承了上面的所有合约

5)matemask会弹出是否部署,但是submit提交:

7.显示如下,说明已经部署到主链上:

红圈为我们创建的token代币合约地址

8.你部署合约的那个账户,现在已经有的你部署的代币数量,可以去进行流通交易了,我部署TTB代币的数量是1亿,转了8888给其他账户了,所以剩下这么多

9.去验证你的合约,属于你自己,怎么验证参考以前的教程,现在你已经有了一个可以上线到交易所的安全代币,go!!

  • 发表于:
  • 原文链接:https://kuaibao.qq.com/s/20180603G1I6E200?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。

扫码关注云+社区

领取腾讯云代金券