You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.1 KiB

  1. // For Node >= v13 / es module environments
  2. import BlocknativeSdk from 'bnc-sdk'
  3. import Web3 from 'web3'
  4. import WebSocket from 'ws' // only neccessary in server environments
  5. import contractABI from './MoonbirdPunks.json' assert { type: 'json' }
  6. // create options object
  7. const options = {
  8. dappId: 'd30463f1-eb29-42b8-8059-e5596e13d0fe',
  9. networkId: 3,
  10. system: 'ethereum', // optional, defaults to ethereum
  11. transactionHandlers: [event => console.log(event.transaction)],
  12. ws: WebSocket, // only neccessary in server environments
  13. name: 'Instance name here', // optional, use when running multiple instances
  14. onerror: (error) => {console.log(error)} //optional, use to catch errors
  15. }
  16. // initialize and connect to the api
  17. const blocknative = new BlocknativeSdk(options)
  18. await blocknative.configuration({
  19. scope: "0x7d82a88Efb60Bb83B3f1665757c20D07E5C0f3ED", // [required] - either 'global' or valid Ethereum address
  20. abi: contractABI.abi, // [optional] - valid contract ABI
  21. watchAddress: true // [optional] - Whether the server should automatically watch the "scope" value if it is an address
  22. })
  23. // returns a promise that resolves once the configuration has been applied
  24. // or rejects if there was a problem with the configuration
  25. // initialize web3
  26. // const web3 = new Web3(window.ethereum)
  27. // get current account
  28. // const accounts = await web3.eth.getAccounts();
  29. const address = "0x7d82a88Efb60Bb83B3f1665757c20D07E5C0f3ED"
  30. // call with the address of the account that you would like to receive status updates for
  31. const {
  32. emitter, // emitter object to listen for status updates
  33. details // initial account details which are useful for internal tracking: address
  34. } = blocknative.account(address)
  35. console.log(`Watching for events on ${address}...`)
  36. // listen to some events
  37. emitter.on('txPool', transaction => {
  38. console.log(`Sending ${transaction.value} wei to ${transaction.to}`)
  39. })
  40. emitter.on('txConfirmed', transaction => {
  41. console.log('Transaction is confirmed!')
  42. })
  43. // catch every other event that occurs and log it
  44. emitter.on('all', transaction => {
  45. console.log(`Transaction event: ${transaction.eventCode}`)
  46. })