本文由EOSUnion(eos.ren)优质内容计划赞助
版权声明
首发自微信公众号:新个体
作者:飞帅
这里是以太坊开发系列课程,我将带你从技术开发的角度理解以太坊,深入区块链底层,以最清晰的视角与区块链零距离亲密接触。
在这里,我将带你挖矿,开发自己的代币,编写智能合约还有其他有趣的Dapp应用!
前置课程:
04、编写第一个智能合约 : 投票合约的开发部署和执行
https://mp.weixin.qq.com/s/b7I4c8zdgJFmiEfZP83XSA
本期内容:
1、用代码创建账号,账号转账,解锁账号
2、用代码部署智能合约
3、用代码执行智能合约
一、用代码创建账号,账号转账,解锁账号
钱包中有一个主账号:"0xb41f9c14be264283bea8ee75016c1782a19b9ba9"
查看主账号余额:
eth.getBalance(eth.accounts[0])
创建新账号:
personal.newAccount('123456')
参数数账号密码:123456
查看账号列表:
eth.accounts
查看新账号余额:
eth.getBalance(eth.accounts[1])
钱包中可以看到两个账号和余额:
账号需要解锁,解锁操作:
personal.unlockAccount(eth.accounts[0],"1q2w3e4r");
账号转账:
eth.sendTransaction({from: '0xb41f9c14be264283bea8ee75016c1782a19b9ba9', to: '0xdd21b95398ec6e42328aec631160d02bf1b00e63', value: web3.toWei(1, "ether")})
钱包中可以看到账号2多了1个ether:
这里将新账号也解锁:
personal.unlockAccount(eth.accounts[1],"123456");
转账效果如下:
二、用代码部署智能合约
打开Browser-Solidity:https://ethereum.github.io/browser-solidity/
将下面智能合约代码拷贝到代码区:
pragma solidity ^0.4.18;
/**
* Author:Ethan.Zhou(飞帅)
* 公众号:xingeti(新个体)
*/
contract VoteContract {
//记录投票值
uint cnt = 1;
/* 构造函数 */
function construtor() pure public {
}
/*投票函数,将投票值加1*/
function vote() public {
cnt ++;
}
/*获取投票值*/
function getVote() constant public returns (uint) {
return cnt;
}
}
点击右边的detail:
找到web3deploy部分,复制代码:
复制代码如下,修改了两个变量的变量名:
var voteContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getVote","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"vote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"construtor","outputs":[],"payable":false,"stateMutability":"pure","type":"function"}]);
var voteContractDeploy = votecontractContract.new(
{
from: web3.eth.accounts[1],
data: '0x6080604052600160005534801561001557600080fd5b5060f2806100246000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630242f351146058578063632a9a5214608057806367fb2a66146094575b600080fd5b348015606357600080fd5b50606a60a8565b6040518082815260200191505060405180910390f35b348015608b57600080fd5b50609260b1565b005b348015609f57600080fd5b5060a660c4565b005b60008054905090565b6000808154809291906001019190505550565b5600a165627a7a72305820f12c095e03457a951742506b168ed4426792bd46cfc0d11d272e9c514c173b1b0029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
将代码拷贝到geth控制台,部署合约。
部署成功后,geth控制输出日志:
查看后台后台日志,可以看到合约创建成功!
|16:05:42] 🔨 mined potential block number=32 hash=77aa13…cdc066
|16:05:43] Successfully sealed new block number=33 hash=ad395e…c3eb18
|16:05:43] 🔗 block reached canonical chain number=28 hash=94a823…15c72c
|16:05:43] Commit new mining work number=34 txs=0 uncles=0 elapsed=0s
|16:05:43] 🔨 mined potential block number=33 hash=ad395e…c3eb18
|16:27:19] Submitted contract creation fullhash=0xc515d05b14a3e932f2f54ac966334d106c8b7c175fd4f135793ff481fea0417e contract=0x0137d67b4a894Dd75245D9A464109C9729Eaa55E
三、用代码执行智能合约
1、获取合约实例
contractInstance = votecontractContract.at(voteContractDeploy.address)
2、合约调用
更多精彩,欢迎关注公众号“新个体”