【区块链学习】(4)以太坊智能合约部署及发布ICO代币

首先通过geth启动节点进行交互模式

geth --datadir data console

查询账号

> eth.accounts
["0x53099d033d847921e44812067d720d17b133a2d8", "0x6917c3b6eb6f4df7c80d3c3167bec2e4959d9152"]
> eth.getBalance(eth.accounts[0])
75000000000000000000 // 如果没钱就执行挖矿
> miner.start()
1. 代币的智能合约代码
pragma solidity ^0.4.2;
contract Token {
             address issuer;
             mapping (address => uint) balances;

             event Issue(address account, uint amount);
             event Transfer(address from, address to,uint amount);

             function Token() {
                 issuer = msg.sender;
             }

             function issue(address account, uint amount) {
                 if (msg.sender != issuer) throw;
                 balances[account] += amount;
             }

             function transfer(address to, uint amount){
                 if (balances[msg.sender] < amount)throw;

                 balances[msg.sender] -= amount;
                 balances[to] += amount;

                 Transfer(msg.sender, to, amount);
             }

             function getBalance(address account)constant returns (uint) {
                 return balances[account];
             }
         }

在线编译网站 remix

2.合约代码编译 选择WEB3DEPLOY的编码,编译代码如下
var tokenContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"account","type":"address"},{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[],"name":"Token","outputs":[],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function","stateMutability":"view"},{"anonymous":false,"inputs":[{"indexed":false,"name":"account","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}]);
var token = tokenContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000c57fe5b5b61042a8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b41461005c578063a9059cbb1461009b578063c2412676146100da578063f8b2cb4f146100ec575bfe5b341561006457fe5b610099600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610136565b005b34156100a357fe5b6100d8600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101e5565b005b34156100e257fe5b6100ea610370565b005b34156100f457fe5b610120600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506103b4565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156101935760006000fd5b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102325760006000fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b5050565b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b9190505600a165627a7a7230582001290f3be2be06979e6b7120c8b0e6b5440aeee14a5fade1d3a161c4599425490029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })
3. 定义合约

注:编码后的第一行 本人测试需要去掉var 及结尾的分号; 才能执行成功

> tokenContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"account","type":"address"},{"name":"amount","type":"uint256"}],"name":"issue","outputs":[],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[{"name":"to","type":"address"},{"name":"amount","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":false,"inputs":[],"name":"Token","outputs":[],"payable":false,"type":"function","stateMutability":"nonpayable"},{"constant":true,"inputs":[{"name":"account","type":"address"}],"name":"getBalance","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function","stateMutability":"view"},{"anonymous":false,"inputs":[{"indexed":false,"name":"account","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Issue","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"from","type":"address"},{"indexed":false,"name":"to","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"Transfer","type":"event"}])
4. 合约部署上交易链

其实就是执行编码后的第二行代码

> token = tokenContract.new(
...    {
......      data: '0x6060604052341561000c57fe5b5b61042a8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b41461005c578063a9059cbb1461009b578063c2412676146100da578063f8b2cb4f146100ec575bfe5b341561006457fe5b610099600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610136565b005b34156100a357fe5b6100d8600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101e5565b005b34156100e257fe5b6100ea610370565b005b34156100f457fe5b610120600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506103b4565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156101935760006000fd5b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102325760006000fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b5050565b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b9190505600a165627a7a7230582001290f3be2be06979e6b7120c8b0e6b5440aeee14a5fade1d3a161c4599425490029',
......      gas: '4700000'
......    }, function (e, contract){
......     console.log(e, contract);
......     if (typeof contract.address !== 'undefined') {
.........          console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
.........     }
......  })
Error: authentication needed: password or unlock undefined
undefined
>

这里报了异常,原因是账户没有解锁进行解锁

> personal.unlockAccount(eth.accounts[0])
Unlock account 0x53099d033d847921e44812067d720d17b133a2d8
Passphrase:
true

再次执行

> var token = tokenContract.new(   {     from: web3.eth.accounts[0],      data: '0x6060604052341561000c57fe5b5b61042a8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b41461005c578063a9059cbb1461009b578063c2412676146100da578063f8b2cb4f146100ec575bfe5b341561006457fe5b610099600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610136565b005b34156100a357fe5b6100d8600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080> token = tokenContract.new(   {     from: web3.eth.accounts[0],      data: '0x6060604052341561000c57fe5b5b61042a8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b41461005c578063a9059cbb1461009b578063c2412676146100da578063f8b2cb4f146100ec575bfe5b341561006457fe5b610099600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610136565b005b34156100a357fe5b6100d8600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101e5565b005b34156100e257fe5b6100ea610370565b005b34156100f457fe5b610120600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506103b4565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156101935760006000fd5b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102325760006000fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b5050565b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b9190505600a165627a7a7230582001290f3be2be06979e6b7120c8b0e6b5440aeee14a5fade1d3a161c4599425490029',      gas: '4700000'   }, function (e, contract){    console.log(e, contract);    if (typeof contract.address !== 'undefined') {         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);    } })
INFO [08-31|17:06:15.864] Submitted contract creation              fullhash=0xe8f5aadb7c35abf8808996b84a3679db2282659d19e422949d405b9d6ff066c8 contract=0x8ED7065Cc990Fbb38C8f5A80739722ECC8ab9e25
null [object Object]

这里再次执行还是失败,原因需要去掉换行符及var变量关健字

再次执行

> token = tokenContract.new(   {     from: web3.eth.accounts[0],      data: '0x6060604052341561000c57fe5b5b61042a8061001c6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063867904b41461005c578063a9059cbb1461009b578063c2412676146100da578063f8b2cb4f146100ec575bfe5b341561006457fe5b610099600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610136565b005b34156100a357fe5b6100d8600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506101e5565b005b34156100e257fe5b6100ea610370565b005b34156100f457fe5b610120600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506103b4565b6040518082815260200191505060405180910390f35b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415156101935760006000fd5b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102325760006000fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b5050565b33600060006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505b565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490505b9190505600a165627a7a7230582001290f3be2be06979e6b7120c8b0e6b5440aeee14a5fade1d3a161c4599425490029',      gas: '4700000'   }, function (e, contract){    console.log(e, contract);    if (typeof contract.address !== 'undefined') {         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);    } })

如出现交易记录的hash值.代表部署成功

  address: undefined,
  transactionHash: "0xe8f5aadb7c35abf8808996b84a3679db2282659d19e422949d405b9d6ff066c8"

如需要部署成功需要进行挖矿确认

> miner.start()
INFO [08-31|17:11:17.651] Updated mining threads                   threads=0
INFO [08-31|17:11:17.651] Transaction pool price threshold updated price=18000000000
INFO [08-31|17:11:17.651] Starting mining operation
INFO [08-31|17:11:17.654] Commit new mining work                   number=253 txs=1 uncles=0 elapsed=2.507ms
INFO [08-31|17:11:18.517] Successfully sealed new block            number=253 hash=508765…aedfbb
INFO [08-31|17:11:18.519] ? block reached canonical chain          number=248 hash=a31bb8…40e316
INFO [08-31|17:11:18.519] ? mined potential block                  number=253 hash=508765…aedfbb
INFO [08-31|17:11:18.520] Commit new mining work                   number=254 txs=1 uncles=0 elapsed=385.673µs

根据交易记录hash查询详细信息

> eth.getTransactionReceipt("0xe8f5aadb7c35abf8808996b84a3679db2282659d19e422949d405b9d6ff066c8")
{
  blockHash: "0x48fbf85484c083b808fecf3cc75c7e200187369f24f2dbeeaca0b244274ba117",
  blockNumber: 256,
  contractAddress: "0x8ed7065cc990fbb38c8f5a80739722ecc8ab9e25",
  cumulativeGasUsed: 336109,
  from: "0x53099d033d847921e44812067d720d17b133a2d8",
  gasUsed: 336109,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0xca618cac97da3ff654524bfe70d51359f41bc58526c996405ca43f19472edb58",
  to: null,
  transactionHash: "0xe8f5aadb7c35abf8808996b84a3679db2282659d19e422949d405b9d6ff066c8",
  transactionIndex: 0
}
>
5. 发布代币
> token.issue.sendTransaction(eth.accounts[0],500,{from:eth.accounts[0]})
INFO [08-31|17:16:24.403] Submitted transaction                    fullhash=0xa4bedeb003ad77dc943c0cde9044f02dc880b48be5324f919ca3b137493b413f recipient=0x8ED7065Cc990Fbb38C8f5A80739722ECC8ab9e25
"0xa4bedeb003ad77dc943c0cde9044f02dc880b48be5324f919ca3b137493b413f"
>

如出现异常需要解锁账户:

Error: authentication needed: password or unlock
    at web3.js:3143:20
    at web3.js:6347:15
    at web3.js:5081:36
    at web3.js:4137:16
    at <anonymous>:1:1

设置发币账户

> token.Token({from:eth.accounts[0]})
INFO [08-31|17:49:01.149] Submitted transaction                    fullhash=0x306833e2870653723b320aa771e41d495d67d62c1a62ea8bb92b146d282993f3 recipient=0x8ED7065Cc990Fbb38C8f5A80739722ECC8ab9e25
"0x306833e2870653723b320aa771e41d495d67d62c1a62ea8bb92b146d282993f3"
>

充值并进行挖矿交易确认

> token.issue(eth.accounts[0],100,{from:eth.accounts[0]})
INFO [08-31|17:49:11.393] Submitted transaction                    fullhash=0xb5c46bc08695075863e58e5befe1b19340b1a7760c61b713a58a3044750ca52b recipient=0x8ED7065Cc990Fbb38C8f5A80739722ECC8ab9e25
"0xb5c46bc08695075863e58e5befe1b19340b1a7760c61b713a58a3044750ca52b"
> miner.start()
INFO [08-31|17:49:27.068] Updated mining threads                   threads=0
INFO [08-31|17:49:27.068] Transaction pool price threshold updated price=18000000000
INFO [08-31|17:49:27.068] Starting mining operation

查询代码已经拥有100个代币了

> token.getBalance(eth.accounts[0])
100
>
## 交易
> token.transfer(eth.accounts[1],20,{from:eth.accounts[0]})
INFO [08-31|17:53:33.240] Submitted transaction                    fullhash=0x5d397234eee995918901a6266e00ab0af4a1223f82a2ab6fb725f37ef0b3423f recipient=0x8ED7065Cc990Fbb38C8f5A80739722ECC8ab9e25
"0x5d397234eee995918901a6266e00ab0af4a1223f82a2ab6fb725f37ef0b3423f"
## 挖矿 进行交易确认
> miner.start()
INFO [08-31|17:53:41.179] Updated mining threads                   threads=0
INFO [08-31|17:53:41.179] Transaction pool price threshold updated 
price=18000000000
....

## 代币查询发现交易已经成功.账户代币数量已经变更
> token.getBalance(eth.accounts[0])
80
> token.getBalance(eth.accounts[1])
20
>

你可能感兴趣的:(区块链)