Skip to content

Web3Connection

Aritz Beobide-Cardinal edited this page Oct 16, 2020 · 10 revisions

This is the bridge between your plebeian centralized computer and the Godly Ethereum network. (Node not included)

Right now HTTP, HTTPS, and WebSocket, and whatever happens to be in window.ethereum is supported.

const Web3Connection = require("arc-web3").Web3Connection;
const web3 = new Web3Connection("http://192.168.5.5:8545");

Methods:


Web3Connection.closeConnection()

Closes this connection and stops listening to any events. You should really call this if you're terminating your program otherwise it'll hang.

// No example. (Feel free to suggest one!)

Promise() Web3Connection.listen(AddressString address, Array topics, Number fromBlock, Number toBlock, Function callback)

Listens for events happening on the blockchain.

  • address can be null, you'll just be listening to every address.
  • topics an array encoded like the following examples. If an event with indexed topics [A, B] is emitted,the following topic arrays will match:
    • [] "anything"
    • [A] "A in first position (and anything after)"
    • [null, B] "anything in first position AND B in second position (and anything after)"
    • [A, B] "A in first position AND B in second position (and anything after)"
    • [[A, B], [A, B]] "(A OR B) in first position AND (A OR B) in second position (and anything after)"
  • fromBlock if not null, then all events since this block will be fetched and passed to the callback.
  • toBlock if null, then the event will be listened to until unlisten is called. If not null, then the callback will stop being called after this block was reached. This parameter is ignored if fromBlock is null.
  • callback a function that will be called with an EthereumEvent as its first and only parameter. If this function is asynchronous, then it won't be called again until it resolves. Events will be placed on a queue. Mind you that if your callback never resolves, then you'll lock up the library. Also, make sure your callback doesn't throw! Handle your Errors!

This function resolved when the search range between fromBlock and toBlock or between fromBlock and the current block is finished.

// No example. (Feel free to suggest one!)

Web3Connection.unlisten(AddressString address, Array topics, Function callback)

Stops calling the callback

// No example. (Feel free to suggest one!)

Promise(String) Web3Connection.clientVersion()

Returns a string containing whatever the endpoint feels like. Usually its name and version.

const Web3Connection = require("arc-web3").Web3Connection;
const web3 = new Web3Connection("http://192.168.5.5:8545");
console.log(await web3.clientVersion()); // "Geth/v1.8.14-stable-316fc7ec/linux-amd64/go1.10"

Promise(Buffer) Web3Connection.keccak256(Buffer data)

Hashes the given data. You should probably use the js-sha3 library... Unless making the endpoint do the work and suffering from the consequences of latency is really what you want.

const Web3Connection = require("arc-web3").Web3Connection;
const web3 = new Web3Connection("http://192.168.5.5:8545");
console.log((await web3.keccak256(Buffer.from("haha yes"))).toString("hex")); // "ff93c554f2f838caaf4977ac245122da303634f09870b0e430187637232cf782"

Promise(Number) Web3Connection.networkID()

Returns the current network ID. Common values are:

  • 1: Ethereum Mainnet (Or an improperly configured custom network)
  • 2: Morden Testnet (deprecated)
  • 3: Ropsten Testnet
  • 4: Rinkeby Testnet
  • 42: Kovan Testnet
const Web3Connection = require("arc-web3").Web3Connection;
const web3 = new Web3Connection("http://192.168.5.5:8545");
console.log(await web3.networkID()); // 1

Promise(Number) Web3Connection.networkPeerCount()

Returns number of peers currently connected to the node endpoint.

// No example. (Feel free to suggest one!)

Promise(Number) Web3Connection.protocolVersion()

Returns number the current ethereum protocol version.

// No example. (Feel free to suggest one!)

Promise(Boolean || SyncStatus) Web3Connection.isSyncing()

If the end point is syncing, it will return SyncStatus. Otherwise, it will return false.

// No example. (Feel free to suggest one!)

Promise(Boolean) Web3Connection.isMining()

Returns true if the node is mining, false if it isn't.

// No example. (Feel free to suggest one!)

Promise(AddressString) Web3Connection.coinbase()

If the connected endpoint is mining, this will return the address all the block payments are going to.

// No example. (Feel free to suggest one!)

Promise(BigNumber) Web3Connection.gasPrice()

Returns the current gas price. (Actual return value depends on the configuration of the endpoint)

// No example. (Feel free to suggest one!)

Promise(Number) Web3Connection.blockNumber()

The most recent block number.

// No example. (Feel free to suggest one!)

Promise(BigNumber) Web3Connection.getBalance(AddressString address [,Number blockNumber])

Returns the balance of the specified account in wei. Divide the returned value by 1e18 if you want the value in Ether.

Second argument is the block number, which is useful for getting someone's past balance. If no block number is specified, the latest one will be used.

// No example. (Feel free to suggest one!)

Promise(Number) Web3Connection.getTransactionCount(AddressString address [,Number blockNumber])

Returns the number of transactions sent from the specified address.

Second argument is the block number, which is useful for getting someone's past data. If no block number is specified, the latest one will be used.

// No example. (Feel free to suggest one!)

Promise(Number) Web3Connection.getBlockTransactionCount([String blockHash || Number blockNumber])

Returns the amount of transactions that are in the specified block. This function can accept a block's hash, or its number. If no arguments are given, the latest block will be used.

// No example. (Feel free to suggest one!)

Promise(Number) Web3Connection.getUncleCount([String blockHash || Number blockNumber])

Returns the amount of uncles associated with the specified block. This function can accept a block's hash, or its number. If no arguments are given, the latest block will be used.

// No example. (Feel free to suggest one!)

Promise(Buffer) Web3Connection.getCode(AddressString address)

Returns the EVM byte code at the specified address.

// No example. (Feel free to suggest one!)

Promise(TransactionHash Web3Connection.sendRawTransaction(Buffer rawSignedTx)

Returns a transaction hash representing the transaction you just sent

// No example. (Feel free to suggest one!)

Promise(EthereumBlock) Web3Connection.getBlock([String blockHash || Number blockNumber])

Returns a bunch of data describing a block in the Ethereum blockchain. This function can accept a block's hash, or its number. If no arguments are given, the latest block will be used.

// No example. (Feel free to suggest one!)

Promise(EthereumTransaction) Web3Connection.getTransaction(TransactionHash txHash || Number blockNumber [,Number blockIndex])

Returns a bunch of data describing a transaction in the Ethereum blockchain. See the EthereumTransaction page for more info.

  • If you're looking up a transaction by its hash, only use the first argument.
  • If you're looking up a transaction by its block and index, you may use a blocks hash or number as the first argument, and the numerical index as the second argument.
// No example. (Feel free to suggest one!)

Promise(EthereumTransactionReceipt) Web3Connection.getTransactionReceipt(TransactionHash tx)

This function only accepts a transaction hash. See the EthereumTransactionReceipt page for more info.

// No example. (Feel free to suggest one!)

Promise(EthereumBlock) Web3Connection.getUncle(String blockHash || Number blockNumber, Number index)

Gets an uncle block. First argument is a block's number or hash. Second argument is a numerical index.

// No example. (Feel free to suggest one!)
Clone this wiki locally