web3.js简单介绍[区块链]
发布于 8 个月前 作者 leiwei1991 5126 次浏览 来自 分享

##介绍

  1. node web3模块: This is the Ethereum compatible JavaScript API which implements the Generic JSON RPC spec

  2. 通过web3能够方便的与以太坊交互,开发者不用关心JSON RPC实现细节。

  3. geth提供了以下的api模块: “admin”:“1.0”, “db”:“1.0”, “debug”:“1.0”, “eth”:“1.0”, “miner”:“1.0”, “net”:“1.0”, “personal”:“1.0”, “shh”:“1.0”, “txpool”:“1.0”, “web3”:“1.0”

  4. By default Geth enables all APIs over the IPC (ipc) interface and only the db, eth, net and web3 APIs over the HTTP and WebSocket interfaces.

###使用

  • 注意事项

    1. 默认用同步的方式,同时也支持异步。 As this API is designed to work with a local RPC node, all its functions use synchronous HTTP requests by default. If you want to make an asynchronous request, you can pass an optional callback as the last parameter to most functions.
    2. bignumber 依赖bignumber这个模块 You will always get a BigNumber object for number values as JavaScript is not able to handle big numbers correctly.

    设置provider(以太坊节点提供的HTTP-RPC服务)

    ```
    var Web3 = require('web3');
     var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))
    web3.eth.getTransaction('0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b');
    

    //eg: 获取一笔交易的信息:
    { “hash”: “0x9fc76417374aa880d4449a1f7f31ec597f00b1f6f3dd2d66f4c9c6c445836d8b”, “nonce”: 2, “blockHash”: “0xef95f2f1ed3ca60b048b4bf67cde2195961e0bba6f70bcbea9a2c4e133e34b46”, “blockNumber”: 3, “transactionIndex”: 0, “from”: “0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b”, “to”: “0x6295ee1b4f6dd65047762f924ecd367c17eabf8f”, “value”: BigNumber, “gas”: 314159, “gasPrice”: BigNumber, “input”: “0x57cb2fc4” }

#参考文档 1. https://github.com/ethereum/go-ethereum/wiki/Management-APIs 2. https://github.com/ethereum/wiki/wiki/JSON-RPC 3. https://github.com/ethereum/wiki/wiki/JavaScript-API

回到顶部