MacBook-Pro:~ zhi$ mkdir -p ~/Ethereum/EncryptToken
MacBook-Pro:~ zhi$ cd ~/Ethereum/EncryptToken
MacBook-Pro:~ zhi$ truffle init
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile: truffle compile
Migrate: truffle migrate
Test contracts: truffle test
EncryptToken.sol
pragma solidity ^0.4.18;
contract EncryptToken{
uint256 INITIAL_SUPPLY = 100000000;//要发行的币总量
mapping(address=>uint256) balances;//存储地址以及币的字典
//初始化,将币的发行总量给到部署人的地址中
function EncryptToken() public{
balances[msg.sender] = INITIAL_SUPPLY;
}
// 转账到一个指定的地址
function tranfer(address _to, uint256 _amount) public {
assert(balances[msg.sender] >= _amount);//判断当前执行合约的地址中是否有足够的钱,如果不够就抛异常
balances[msg.sender] -= _amount;//将当前执行合约发送币的账户的币减少
balances[_to] += _amount;//接收地址的币额度增加
}
//查询执行账户的余额
function balanceOf(address _owner) constant public returns (uint256){
return balances[_owner];
}
}
truffle develop
MacBook-Pro:~ zhi$ truffle develop
Truffle Develop started at http://localhost:9545/
Accounts:
(0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
(1) 0xf17f52151ebef6c7334fad080c5704d77216b732
(2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
(3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
(4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
(5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
(6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
(7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
(8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
(9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de
Private Keys:
(0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3
(1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974d162f
(2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193c05875ef1
(3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71f772aa6c
(4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97bede61418
(5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917abc63
(6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914d58b8c8
(7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f39395618b7
(8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669ff12b7c4
(9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012b548b1e5
Mnemonic: candy maple cake sugar pudding cream honey rich smooth crumble sweet treat
编译,执行命令compile
truffle(develop)> compile
Compiling ./contracts/EncrypyToken.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts
在项目目录的migrations
文件夹中,创建部署文件2_deploy_EncryptToken.js
(文件名是随便取的,只要部署文件内容正确,并且在migrations
文件夹中即可),部署文件内容:
var EncryptToken = artifacts.require("./EncryptToken.sol");//合约文件
module.exports = function(deployer) {
deployer.deploy(EncryptToken);
};
部署,执行命令migrate
truffle(develop)> migrate
Using network 'develop'.
Running migration: 1_initial_migration.js
Deploying Migrations...
... 0x0b83eed16af8d7b98127c3883c30d887900ad09565be97a5c3e98a31090afcfb
Migrations: 0x8cdaf0cd259887258bc13a92c0a6da92698644c0
Saving successful migration to network...
... 0xd7bc86d31bee32fa3988f1c1eabce403a1b5d570340a3a9cdba53a472ee8c956
Saving artifacts...
Running migration: 2_deploy_EncryptToken.js
Deploying EncryptToken...
... 0x3790a04bf601165fcae94ff921da8e702b2633aea3e1ca1fb5812965801a2610
EncryptToken: 0x345ca3e014aaf5dca488057592ee47305d9b3e10
Saving successful migration to network...
... 0xf36163615f41ef7ed8f4a8f192149a0bf633fe1a2398ce001bf44c43dc7bdda0
Saving artifacts...
myToken
变量,执行命令EncryptToken.deployed().then(instance => myToken = instance)
truffle(develop)> let myToken;
undefined
truffle(develop)> EncryptToken.deployed().then(instance => myToken = instance)
TruffleContract {
constructor:
{ [Function: TruffleContract]
_static_methods:
{ setProvider: [Function: setProvider],
new: [Function: new],
at: [Function: at],
deployed: [Function: deployed],
defaults: [Function: defaults],
hasNetwork: [Function: hasNetwork],
isDeployed: [Function: isDeployed],
detectNetwork: [Function: detectNetwork],
setNetwork: [Function: setNetwork],
resetAddress: [Function: resetAddress],
link: [Function: link],
clone: [Function: clone],
addProp: [Function: addProp],
toJSON: [Function: toJSON] },
_properties:
{ contract_name: [Object],
contractName: [Object],
abi: [Object],
network: [Function: network],
networks: [Function: networks],
address: [Object],
links: [Function: links],
events: [Function: events],
binary: [Function: binary],
deployedBinary: [Function: deployedBinary],
unlinked_binary: [Object],
bytecode: [Object],
deployedBytecode: [Object],
sourceMap: [Object],
deployedSourceMap: [Object],
source: [Object],
sourcePath: [Object],
ast: [Object],
compiler: [Object],
schema_version: [Function: schema_version],
schemaVersion: [Function: schemaVersion],
updated_at: [Function: updated_at],
updatedAt: [Function: updatedAt] },
_property_values: {},
_json:
{ contractName: 'EncryptToken',
abi: [Object],
bytecode: '0x60606040526305f5e100600055341561001757600080fd5b600054600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555061023e8061006c6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806370a0823114610051578063c610bb071461009e575b600080fd5b341561005c57600080fd5b610088600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506100e0565b6040518082815260200191505060405180910390f35b34156100a957600080fd5b6100de600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610129565b005b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015151561017457fe5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555050505600a165627a7a7230582040d55a414ea6053dafc66a3b13adec755ee1e130c402118a90c56021f7de45780029',
deployedBytecode: '0x60606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806370a0823114610051578063c610bb071461009e575b600080fd5b341561005c57600080fd5b610088600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506100e0565b6040518082815260200191505060405180910390f35b34156100a957600080fd5b6100de600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610129565b005b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015151561017457fe5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555050505600a165627a7a7230582040d55a414ea6053dafc66a3b13adec755ee1e130c402118a90c56021f7de45780029',
sourceMap: '26:824:0:-;;;77:9;52:34;;249:78;;;;;;;;308:14;;285:8;:20;294:10;285:20;;;;;;;;;;;;;;;:37;;;;26:824;;;;;;',
deployedSourceMap: '26:824:0:-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;745:102;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;367:342;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;745:102;805:7;826:8;:16;835:6;826:16;;;;;;;;;;;;;;;;819:23;;745:102;;;:::o;367:342::-;458:7;434:8;:20;443:10;434:20;;;;;;;;;;;;;;;;:31;;427:39;;;;;;582:7;558:8;:20;567:10;558:20;;;;;;;;;;;;;;;;:31;;;;;;;;;;;665:7;648:8;:13;657:3;648:13;;;;;;;;;;;;;;;;:24;;;;;;;;;;;367:342;;:::o',
source: 'pragma solidity ^0.4.18;\n\ncontract EncryptToken{\n\n uint256 INITIAL_SUPPLY = 100000000;//要发行的币总量\n mapping(address=>uint256) balances;//存储地址以及币的字典\n\n //初始化,将币的发行总量给到部署人的地址中\n function EncryptToken() public{\n balances[msg.sender] = INITIAL_SUPPLY;\n }\n\n // 转账到一个指定的地址\n function tranfer(address _to, uint256 _amount) public {\n assert(balances[msg.sender] >= _amount);//判断当前执行合约的地址中是否有足够的钱,如果不够就抛异常\n balances[msg.sender] -= _amount;//将当前执行合约发送币的账户的币减少\n balances[_to] += _amount;//接收地址的币额度增加\n }\n\n //查询执行账户的余额\n function balanceOf(address _owner) constant public returns (uint256){\n return balances[_owner];\n }\n\n}\n',
sourcePath: '/Users/ zhi/Ethereum/EncrptToken/contracts/EncrypyToken.sol',
ast: [Object],
compiler: [Object],
networks: [Object],
schemaVersion: '1.0.1',
updatedAt: '2018-01-12T03:31:56.424Z' },
setProvider: [Function: bound setProvider],
new: [Function: bound new],
at: [Function: bound at],
deployed: [Function: bound deployed],
defaults: [Function: bound defaults],
hasNetwork: [Function: bound hasNetwork],
isDeployed: [Function: bound isDeployed],
detectNetwork: [Function: bound detectNetwork],
setNetwork: [Function: bound setNetwork],
resetAddress: [Function: bound resetAddress],
link: [Function: bound link],
clone: [Function: bound clone],
addProp: [Function: bound addProp],
toJSON: [Function: bound toJSON],
web3:
Web3 {
_requestManager: [Object],
currentProvider: [Object],
eth: [Object],
db: [Object],
shh: [Object],
net: [Object],
personal: [Object],
bzz: [Object],
settings: [Object],
version: [Object],
providers: [Object],
_extend: [Object] },
class_defaults:
{ from: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
gas: 6721975,
gasPrice: 100000000000 },
currentProvider:
HttpProvider {
host: 'http://localhost:9545/',
timeout: 0,
user: undefined,
password: undefined,
send: [Function],
sendAsync: [Function],
_alreadyWrapped: true },
network_id: '4447' },
abi:
[ { constant: true,
inputs: [Object],
name: 'balanceOf',
outputs: [Object],
payable: false,
stateMutability: 'view',
type: 'function' },
{ constant: false,
inputs: [Object],
name: 'tranfer',
outputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'function' },
{ inputs: [],
payable: false,
stateMutability: 'nonpayable',
type: 'constructor' } ],
contract:
Contract {
_eth:
Eth {
_requestManager: [Object],
getBalance: [Object],
getStorageAt: [Object],
getCode: [Object],
getBlock: [Object],
getUncle: [Object],
getCompilers: [Object],
getBlockTransactionCount: [Object],
getBlockUncleCount: [Object],
getTransaction: [Object],
getTransactionFromBlock: [Object],
getTransactionReceipt: [Object],
getTransactionCount: [Object],
call: [Object],
estimateGas: [Object],
sendRawTransaction: [Object],
signTransaction: [Object],
sendTransaction: [Object],
sign: [Object],
compile: [Object],
submitWork: [Object],
getWork: [Object],
coinbase: [Getter],
getCoinbase: [Object],
mining: [Getter],
getMining: [Object],
hashrate: [Getter],
getHashrate: [Object],
syncing: [Getter],
getSyncing: [Object],
gasPrice: [Getter],
getGasPrice: [Object],
accounts: [Getter],
getAccounts: [Object],
blockNumber: [Getter],
getBlockNumber: [Object],
protocolVersion: [Getter],
getProtocolVersion: [Object],
iban: [Object],
sendIBANTransaction: [Function: bound transfer] },
transactionHash: null,
address: '0x345ca3e014aaf5dca488057592ee47305d9b3e10',
abi: [ [Object], [Object], [Object] ],
balanceOf:
{ [Function: bound ]
request: [Function: bound ],
call: [Function: bound ],
sendTransaction: [Function: bound ],
estimateGas: [Function: bound ],
getData: [Function: bound ],
address: [Circular] },
tranfer:
{ [Function: bound ]
request: [Function: bound ],
call: [Function: bound ],
sendTransaction: [Function: bound ],
estimateGas: [Function: bound ],
getData: [Function: bound ],
'address,uint256': [Circular] },
allEvents: [Function: bound ] },
balanceOf:
{ [Function]
call: [Function],
sendTransaction: [Function],
request: [Function: bound ],
estimateGas: [Function] },
tranfer:
{ [Function]
call: [Function],
sendTransaction: [Function],
request: [Function: bound ],
estimateGas: [Function] },
sendTransaction: [Function],
send: [Function],
allEvents: [Function: bound ],
address: '0x345ca3e014aaf5dca488057592ee47305d9b3e10',
transactionHash: null }
执行完上面代码后,币就已经发布完成了。默认的发币账户是,之前进入truffle
命令行的时候,给出了10个默认账户:
(0) 0x627306090abab3a6e1400e9345bc60c78a8bef57
(1) 0xf17f52151ebef6c7334fad080c5704d77216b732
(2) 0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef
(3) 0x821aea9a577a9b44299b9c15c88cf3087f3b5544
(4) 0x0d1d4e623d10f9fba5db95830f7d3839406c6af2
(5) 0x2932b7a2355d6fecc4b5c0b6bd44cc31df247a2e
(6) 0x2191ef87e392377ec08e7c08eb105ef5448eced5
(7) 0x0f4f2ac550a1b4e2280d04c21cea7ebd822934b5
(8) 0x6330a553fc93768f612722bb8c2ec78ac90b3bbc
(9) 0x5aeda56215b167893e80b4fe645ba6d5bab767de
默认账户的第一个,即0x627306090abab3a6e1400e9345bc60c78a8bef57
就是发币的账户。
truffle(develop)> myToken.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57")
{ [String: '100000000'] s: 1, e: 8, c: [ 100000000 ] }
truffle(develop)>
truffle(develop)>
truffle(develop)> myToken.balanceOf("0xf17f52151ebef6c7334fad080c5704d77216b732")
{ [String: '0'] s: 1, e: 0, c: [ 0 ] }
向另一个账户转币
truffle(develop)> myToken.tranfer("0xf17f52151ebef6c7334fad080c5704d77216b732",10000)
{ tx: '0xcb922afb739f4299489201f55ac4f77f8efd870abae09c860519fabf439a639b',
receipt:
{ transactionHash: '0xcb922afb739f4299489201f55ac4f77f8efd870abae09c860519fabf439a639b',
transactionIndex: 0,
blockHash: '0x3765fd1e31ac0fa273d70380ea3ca1148be91f8ec16311d62dfc4e5f400f7315',
blockNumber: 5,
gasUsed: 49120,
cumulativeGasUsed: 49120,
contractAddress: null,
logs: [],
status: 1 },
logs: [] }
再次查询相应账户中的币
truffle(develop)> myToken.balanceOf("0x627306090abab3a6e1400e9345bc60c78a8bef57")
{ [String: '99990000'] s: 1, e: 7, c: [ 99990000 ] }
truffle(develop)>
truffle(develop)>
truffle(develop)> myToken.balanceOf("0xf17f52151ebef6c7334fad080c5704d77216b732")
{ [String: '10000'] s: 1, e: 4, c: [ 10000 ] }
参考:
如何编写智能合约(Smart Contract)?(II)建立加密代币