合約代碼
pragma solidity ^0.4.8;
contract Token{
// token總量,默認(rèn)會(huì)為public變量生成一個(gè)getter函數(shù)接口,名稱為totalSupply().
uint256 public totalSupply;
/// 獲取賬戶_owner擁有token的數(shù)量
function balanceOf(address _owner) constant returns (uint256 balance);
//從消息發(fā)送者賬戶中往_to賬戶轉(zhuǎn)數(shù)量為_value的token
function transfer(address _to, uint256 _value) returns (bool success);
//從賬戶_from中往賬戶_to轉(zhuǎn)數(shù)量為_value的token,與approve方法配合使用
function transferFrom(address _from, address _to, uint256 _value) returns
(bool success);
//消息發(fā)送賬戶設(shè)置賬戶_spender能從發(fā)送賬戶中轉(zhuǎn)出數(shù)量為_value的token
function approve(address _spender, uint256 _value) returns (bool success);
//獲取賬戶_spender可以從賬戶_owner中轉(zhuǎn)出token的數(shù)量
function allowance(address _owner, address _spender) constant returns
(uint256 remaining);
//發(fā)生轉(zhuǎn)賬時(shí)必須要觸發(fā)的事件
event Transfer(address indexed _from, address indexed _to, uint256 _value);
//當(dāng)函數(shù)approve(address _spender, uint256 _value)成功執(zhí)行時(shí)必須觸發(fā)的事件
event Approval(address indexed _owner, address indexed _spender, uint256
_value);
}
contract StandardToken is Token {
function transfer(address _to, uint256 _value) returns (bool success) {
//默認(rèn)totalSupply 不會(huì)超過最大值 (2^256 - 1).
//如果隨著時(shí)間的推移將會(huì)有新的token生成,則可以用下面這句避免溢出的異常
//require(balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]);
require(balances[msg.sender] >= _value);
balances[msg.sender] -= _value;//從消息發(fā)送者賬戶中減去token數(shù)量_value
balances[_to] += _value;//往接收賬戶增加token數(shù)量_value
Transfer(msg.sender, _to, _value);//觸發(fā)轉(zhuǎn)幣交易事件
return true;
}
function transferFrom(address _from, address _to, uint256 _value) returns
(bool success) {
//require(balances[_from] >= _value && allowed[_from][msg.sender] >=
// _value && balances[_to] + _value > balances[_to]);
require(balances[_from] >= _value && allowed[_from][msg.sender] >= _value);
balances[_to] += _value;//接收賬戶增加token數(shù)量_value
balances[_from] -= _value; //支出賬戶_from減去token數(shù)量_value
allowed[_from][msg.sender] -= _value;//消息發(fā)送者可以從賬戶_from中轉(zhuǎn)出的數(shù)量減少_value
Transfer(_from, _to, _value);//觸發(fā)轉(zhuǎn)幣交易事件
return true;
}
function balanceOf(address _owner) constant returns (uint256 balance) {
return balances[_owner];
}
function approve(address _spender, uint256 _value) returns (bool success)
{
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
return allowed[_owner][_spender];//允許_spender從_owner中轉(zhuǎn)出的token數(shù)
}
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
}
contract HumanStandardToken is StandardToken {
address public owner;
/* Public variables of the token */
string public name; //名稱: eg Simon Bucks
uint8 public decimals; //最多的小數(shù)位數(shù),How many decimals to show. ie. There could 1000 base units with 3 decimals. Meaning 0.980 SBX = 980 base units. It's like comparing 1 wei to 1 ether.
string public symbol; //token簡稱: eg SBX
string public version = 'H0.1'; //版本
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function HumanStandardToken(uint256 _initialAmount, string _tokenName, uint8 _decimalUnits, string _tokenSymbol) {
balances[msg.sender] = _initialAmount; // 初始token數(shù)量給予消息發(fā)送者
totalSupply = _initialAmount; // 設(shè)置初始總量
name = _tokenName; // token名稱
decimals = _decimalUnits; // 小數(shù)位數(shù)
symbol = _tokenSymbol; // token簡稱
owner = msg.sender;
}
/* Approves and then calls the receiving contract */
function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) {
allowed[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
//call the receiveApproval function on the contract you want to be notified. This crafts the function signature manually so one doesn't have to include a contract in here just for this.
//receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData)
//it is assumed that when does this that the call *should* succeed, otherwise one would use vanilla approve instead.
require(_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData));
return true;
}
// 增發(fā),給某個(gè)地址增加代幣金額
function mintToken(address target, uint256 mintedAmount) public onlyOwner returns (bool success){
balances[target] += mintedAmount;
totalSupply += mintedAmount;
Transfer(0, owner, mintedAmount);
Transfer(owner, target, mintedAmount);
return true;
}
}
測試
發(fā)布后使用代碼測試:
var Web3 = require("web3");
var Tx = require('ethereumjs-tx');
var coin = require('../conf/coin');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("https://ropsten.infura.io/v3/18b6909fdc9b4ba0af57772357f035a9"));
web3.eth.defaultAccount = "0x2b547f3098408f0632a4063eb1c86595efbaf470";
var token_name = 'CHUSDT10';
var token_contract = new web3.eth.Contract(coin[token_name].abi, coin[token_name].contractId);
// 0x3945aD33d03f9fe2C19131Dc5366b1FBe186
// 0x2b547f3098408f0632a4063eb1c86595f470
var account_addr = '0x3945aD33d03f9fe2C19131Dc5366b1FBe186';
getbalance();
// mind();
function getbalance() {
token_contract.methods.balanceOf('0x3945aD33d03f9fe2C19131Dc5366b1FBe186').call(function(err, result) {
if (err) {
console.log({ success: false, error: err });
} else {
console.log({ success: true, result: result });
}
});
}
function mind() {
// 0x3945aD33d03f9fe2C19131Dc5366b1FBe186 的密鑰
var privKey = new Buffer.from('406ab1f237d6168413d8ee21dbe777158979fe8eb39a5edb394b3068c9', 'hex');
web3.eth.getTransactionCount('0x3945aD33d03f9fe2C19131Dc5366b1FBe186', (err, txCount) => {
if (err) {
res.json({ success: false, error: err });
return;
}
const txObject = {
nonce: web3.utils.toHex(txCount),
gasLimit: web3.utils.toHex(800000),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
to: coin[token_name].contractId,
data: token_contract.methods.mintToken('0x3945aD33d03f9fe2C19131Dc5366b1FBe186', 1000).encodeABI()
}
const tx = new Tx(txObject)
tx.sign(privKey)
const serializedTx = tx.serialize()
web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'), (err, txHash) => {
if (err) {
console.log('err', err);
return;
}
console.log('ok', txHash);
})
})
}