以太坊智能合约部署与交互

启动容器来执行geth命令

root@ubu-blockchain2:~# docker run -i blockchain101/ethereum-geth:1.6.5 geth attach http://45.32.252.88:8201
Welcome to the Geth JavaScript console!

instance: Geth/01/v1.6.5-stable/linux-amd64/go1.8
coinbase: 0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a
at block: 6064 (Wed, 02 Aug 2017 01:13:50 UTC)
 datadir: /ethcluster/779977/data/01
 modules: admin:1.0 eth:1.0 net:1.0 rpc:1.0 web3:1.0

查看我们的账户和余额


eth.getBalance(eth.accounts[0])

11000000000000000000
eth.getBalance(eth.accounts[1])
0
eth.accounts[0]
"0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a"
> eth.accounts[1]
"0xe82e2f0a5abd8774767b9751659976f9c4f59181"

发起一笔交易


eth.sendTransaction({
   from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(3,'ether')})

"0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec"

查看交易细节


>  eth.getTransaction("0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec")

{
  blockHash: "0x3115703894dc6015c96ef4de3e5615f416498ca1f985902b38cd70e27dab8871",
  blockNumber: 1250,
  from: "0x4c57e7e9c2f728046ddc6e96052056a241bdbd0a",
  gas: 90000,
  gasPrice: 18000000000,
  hash: "0x0075da712d26aea17d6647035107f509e13eaf3d113c1577db14d4cc4216caec",
  input: "0x",
  nonce: 0,
  r: "0x2aef2c1fa03a0fa4172d21e3383d8c0431d45ec855b9d16efdd5eb2de90c414c",
  s: "0xc4d530fb7902bf509fe56bfbea4861bf6cc16791afc9c9103c1a18f77407d1f",
  to: "0xe82e2f0a5abd8774767b9751659976f9c4f59181",
  transactionIndex: 0,
  v: "0x17cdb6",
  value: 3000000000000000000
}
> eth.getBalance(eth.accounts[1])

3000000000000000000

验证用户0的余额


> eth.getBalance(eth.accounts[0])

7999622000000000000

编写一个简单的合约



contract Sample {
    uint public value;

          function Sample(uint v){
                    value=v;
          }

          function set(uint v){
                    value=v;
          }

          function get() constant returns (uint){
                    return value;
          }
}

remix网页编译得到ABI接口和合约的二进制代码、

 abi=[{
  "constant":true,"inputs":[],"name":"value","outputs":[{
  "name":"","type":"uint256"}],"payable":false,"type":"function"},{
  "constant":false,"inputs":[{
  "name":"v","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{
  "constant":true,"inputs":[],"name":"get","outputs":[{
  "name":"","type":"uint256"}],"payable":false,"type":"function"},{
  "inputs":[{
  "name":"v","type":"uint256"}],"payable":false,"type":"constructor"}]



[{
    constant: true,
    inputs: [],
    name: "value",
    outputs: [{
        name: "",
        type: "uint256"
    }],
    payable: false,
    type: "function"
}, {
    constant: false,
    inputs: [{
        name: "v",
        type: "uint256"
    }],
    name: "set",
    outputs: [],
    payable: false,
    type: "function"
}, {
    constant: true,
    inputs: [],
    name: "get",
    outputs: [{
        name: "",
        type: "uint256"
    }],
    payable: false,
    type: "function"
}, {
    inputs: [{
        name: "v",
        type: "uint256"
    }],
    payable: false,
    type: "constructor"
}]

需要使用eth.contract来定义一个合约类


> sample=eth.contract(abi)

{
  abi: [{
      constant: true,
      inputs: [],
      name: "value",
      outputs: [{...}],
      payable: false,
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "set",
      outputs: [],
     
  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值