Skip to content

Latest commit

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Simple Eth RPC-JSON Websocket client

Get started

npminstalleth-ws
constWsEth=require('eth-ws')constwsEth=newWsEth('wss://mainnet.infura.io/ws/v3/YOUR-PROJECT-ID')wsEth.call('eth_blockNumber').then(blockNr=>console.log(blockNr))// parseInt(0x4b7) = 1207

Requests and responses are HEX encoded

Real-time Events

Supports PubSub subscriptions
https://geth.ethereum.org/docs/rpc/pubsub

wsEth.subscribe('newHeads')// Gets executed on every new blockwsEth.on('newHeads',(block)=>{console.log('New block:',parseInt(block.number))})

Methods

https://eth.wiki/json-rpc/API#json-rpc-methods

constgasPrice=awaitwsEth.call('eth_gasPrice')console.log(gasPrice/1e9,'Gwei')
// or provide a callback to websocket `open` eventwsEth.on('open',async()=>{console.log('WebSocket connection is now open!')consttx=awaitwsEth.call('eth_getTransactionByHash',TX_HASH)console.log(tx.from)})wsEth.on('error',err=>console.error(err))

Signing and Sending transactions

// wsEth.setCommon({ chain: 'ropsten' })consttxData={from: SENDER_ADDRESSto: RECEIVER_ADDRESS,gasPrice: '0x'+1e9.toString(16),// 1 Gweivalue: '0x0',// 0nonce: awaitwsEth.call('eth_getTransactionCount',SENDER_ADDRESS,'pending')}constrawTx=wsRpc.signTransaction(txData,PRIVATEKEY)// Call returns a Promise combined with Event emitter// 1) Using Promise for getting only the TransactionHashconsttxHash=awaitwsEth.call('eth_sendRawTransaction',rawTransaction)console.log('Transaction submitted:',txHash)
// 2) Using event emitter for TransactionHash and Receipt// Can use .once to receive Receipt just once, otherwise gets emitted on every new block confirmationwsEth.call('eth_sendRawTransaction',rawTransaction).once('txHash',txHash=>console.log('Transaction submitted': txHash)).once('receipt',({error, receipt})=>{if(error)returnconsole.error(error)const{status, gasUsed, transactionHash, blocksSince}=receiptif(parseInt(status)===0){console.log('Transaction was submitted and mined but failed')}else{console.log('Transaction was successful')}console.log(`Transaction was confirmed in ${blocksSince} blocks`)console.log('Gas used by tx:',gasUsed)})

Default block parameter

The following methods have an extra Default Block parameter:

  • eth_getBalance
  • eth_getCode
  • eth_getTransactionCount
  • eth_getStorageAt
  • eth_call

latest | pending | earliest | Block nr HEX string

wsEth.call('eth_getTransactionCount',ADDRESS,'pending')

For example, executing smart-contract functions on pending or past state of blockchain:

constabi=require('web3-eth-abi');constcontractABI=[{name: 'myMethod',type: 'function',inputs: [{type: 'uint256',name: 'myNumber'},{type: 'string',name: 'myString'}]}]// myMethod(2345675643, 'Hello!%')consttxParams={to: CONTRACT_ADDRESS,from: SENDER_ADDRESS,data: abi.encodeFunctionCall(contractABI[0],['2345675643','Hello!%']),}// pass defaultBlock - 'pending' or block number hexwsEth.call('eth_estimateGas',txParams,'pending').then(gas=>{console.log('Estimated gas required:',parseInt(gas))// "0x5208" -> 21000}).catch(err=>{// for example 'execution reverted:'console.log(err.message)})

Signing on Custom Chains and Testnets

You can either

  • pass the Chain common object to signTransaction
  • or use setCommon to set it once for signing all future transactions
wsEth.setCommon({chain: 'ropsten'})
wsEth.setCommon({chain: 'ropsten',hardfork: 'byzantium'})
wsEth.setCommon({chain: 'ropsten',hardfork: 'byzantium'})
wsEth.setCommon({customChain: {name: 'polygon-mainnet',chainId: 137,networkId: 137},baseChain: 'mainnet',hardfork: 'petersburg'})

About

Eth JSON-RPC WebSocket client

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages