web3.js编译Solidity,发布,调用全部流程

在Ubuntu环境下,创建了一个名为web3test的项目,并通过npm初始化了package.json。接着安装了web3库并保存为依赖。然后在index.js中引入web3并设置提供者为本地的以太坊节点。最后展示了Solidity合约源代码,但未提及编译、发布和调用的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

root@i-colbyo7v:/home/ubuntu# mkdir web3test && cd web3test
root@i-colbyo7v:/home/ubuntu/web3test# npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See npm help json for definitive documentation on these fields
and exactly what they do.

Use npm install <pkg> afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (web3test) test
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to /home/ubuntu/web3test/package.json:

{
“name”: “test”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1”
},
“author”: “”,
“license”: “ISC”
}

Is this ok? (yes) yes
\root@i-colbyo7v:/home/ubuntu/web3test# npm install web3 –save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN test@1.0.0 No description
npm WARN test@1.0.0 No repository field.

  • web3@0.19.1
    added 6 packages in 16.859s
    root@i-colbyo7v:/home/ubuntu/web3test# vim index.js

var Web3 = require(“web3”);
var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider(“http://localhost:8545“));
~
~~let source = “pragma solidity ^0.4.0;contract Calc{ uint count; function add(uint a, uint b) returns(uint){ count++; return a + b; } function getCount() returns (uint){ return count; }}”;

geth –identity “haha” –datadir ./data/00 –networkid 12345 –rpcapi “db,eth,net,web3” –port 61910 –rpcport 8200 console

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值