From 239b15fd4e2c61c8ac2e50e1c67cea5bec1f1e3e Mon Sep 17 00:00:00 2001 From: John Cairns Date: Fri, 12 Nov 2021 14:11:42 -0600 Subject: [PATCH] 158: Provide working example for the README --- README.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7c5e720..ce1a36e 100644 --- a/README.md +++ b/README.md @@ -10,15 +10,21 @@ A lightweight JavaScript sdk to connect to the Blocknative backend Ethereum node ### Quick Start (client) +#### Transaction Monitor + ```javascript +import WebSocket from 'ws' import BlocknativeSdk from 'bnc-sdk' import Web3 from 'web3' -const web3 = new Web3(window.ethereum) +const wsapi_url = 'wss://api.blocknative.com/v0' + +const web3 = new Web3(wsapi_url) // create options object const options = { - dappId: 'Your dappId here', + dappId: process.env.BN_API_KEY, + ws: WebSocket, networkId: 1, transactionHandlers: [event => console.log(event.transaction)] } @@ -26,6 +32,11 @@ const options = { // initialize and connect to the api const blocknative = new BlocknativeSdk(options) +const txOptions = { + from: "0x7A132e43013cAA14a3744721EF179f1F3d2b3921", + to: "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", +} + // initiate a transaction via web3.js web3.eth.sendTransaction(txOptions).on('transactionHash', hash => { // call with the transaction hash of the transaction that you would like to receive status updates for @@ -44,8 +55,41 @@ web3.eth.sendTransaction(txOptions).on('transactionHash', hash => { emitter.on('all', transaction => { console.log(`Transaction event: ${transaction.eventCode}`) }) +}) + ``` +#### Address Listener + +```javascript +import WebSocket from 'ws' +import BlocknativeSdk from 'bnc-sdk' +import Web3 from 'web3' + +const wsapi_url = 'wss://api.blocknative.com/v0' + +const web3 = new Web3(wsapi_url) + +// create options object +const options = { + dappId: process.env.BN_API_KEY, + ws: WebSocket, + networkId: 1 +} + +// initialize and connect to the api +const blocknative = new BlocknativeSdk(options) + +const address = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D" + +const { emitter, details } = blocknative.account(address) + +emitter.on("all", transaction => { + console.log(transaction) +}) +``` + + ## Documentation For detailed documentation head to [docs.blocknative.com](https://docs.blocknative.com/notify-sdk)