以太坊私链部署智能合约

以太坊私链部署智能合约,第1张

centos安装npm和solc

1、安装gcc

yum install gcc gcc-c++

2、下载node国内镜像

wget https://npm.taobao.org/mirrors/node/v10.14.1/node-v10.14.1-linux-x64.tar.gz

3、解压node文件并进行重命名

tar -xvf  node-v10.14.1-linux-x64.tar.gz
mv node-v10.14.1-linux-x64 node

4、添加环境变量

echo "export NODE_HOME=/root/nodejs/node" > /etc/profile
echo "export PATH=$NODE_HOME/bin:$PATH" > /etc/profile 

5、刷新系统配置

source /etc/profile

6、查看版本

node -v
npm -v

7、安装solc

npm install -g solc

部署智能合约

1、使用remix编译.sol文件,获取abi参数和bytecode参数

pragma solidity ^0.4.0;

contract testETH{
    string abc = "hello ethereum";
    function getabc() public view returns(string){
        return abc;
    }
}

2、在以太坊控制台输入abi参数,将abi参数放在web3.eth.contract()中

testabi=web3.eth.contract([{"constant": true,"inputs": [],"name": "getabc","outputs": [{"name": "","type": "string"}],"payable": false,"stateMutability": "view","type": "function"}])

3、在以太坊控制台输入bin参数

testbin="0x60806040526040805190810160405280600e81526020017f68656c6c6f20657468657265756d0000000000000000000000000000000000008152506000908051906020019061004f929190610062565b5034801561005c57600080fd5b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b6101a4806101166000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063f3f26af714610046575b600080fd5b34801561005257600080fd5b5061005b6100d6565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009b578082015181840152602081019050610080565b50505050905090810190601f1680156100c85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561016e5780601f106101435761010080835404028352916020019161016e565b820191906000526020600020905b81548152906001019060200180831161015157829003601f168201915b50505050509050905600a165627a7a72305820d4bf7d2d9be16172398dd1bdd3d44784e6247aedaf938c888bcde96bd6e384150029"

4、设置计算需要的gas费用

testgas="300000"

5、解锁账户并部署合约

personal.unlockAccount(eth.accounts[0],"123456")

var testeth = testabi.new({from:web3.eth.accounts[0],data:testbin,gas:testgas})
// 同时可以看到交易池中有一笔记录,开启挖矿即可讲合约部署成功

6、挖矿实现合约成功部署
miner.start(1);admin.sleepBlocks(1);miner.stop();

合约交互

1、获取合约对象

myheyue = eth.contract(testabi)
// 图中是用的abi,其中abi内容与testabi内容一样

2、实例化合约

myname = myheyue.at(testeth.address)

3、调用合约函数

myname.getabc.call()

// 也可使用
testeth.getabc()

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/796577.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存