美文网首页
在testrpc上部署合约

在testrpc上部署合约

作者: zl_lz | 来源:发表于2016-12-03 16:54 被阅读0次

    通过geth在测试链上部署合约

    1.打开testrpc

    2.重开一个终端连接到RPC服务

    geth attach http://127.0.0.1:8545 

    3.新建一个合约  

    con = "pragma solidity ^0.4.2; contract test { function add(uint a,uint b) returns(uint c) { return a + b ; } }"

    4.进行编译,编译器会输出一个合约对象

    compilecon = eth.compile.solidity(con)  

    5.从合约对象中得到code和abi

    code = compilecon.code

    abi = compilecon.info.abiDefinition

    6.然后把合约部署到区块链上

    web3.eth.contract(abi).new({from: eth.accounts[0], data: code},function(err, contract){console.log(contract.address)} )

    测试时返回0x579f0c4f3bc489329f0e62ced3334215948cd246这是合约地址

    7.装载一个已部署的合约

    address = "0x579f0c4f3bc489329f0e62ced3334215948cd246"

    contract  = web3.eth.contract(abi) //创建一个合约对象,用来初始化合约

    mycontract = contract.at(address)

    8.与合约交互

    mycontract.add.call(1,2)

    相关文章

      网友评论

          本文标题:在testrpc上部署合约

          本文链接:https://www.haomeiwen.com/subject/cavqmttx.html