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.

59 lines
2.2 KiB

2 years ago
  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. filters: [{"contractCall.methodName":"ownerMint"}],
  21. abi: contractABI.abi, // [optional] - valid contract ABI
  22. watchAddress: true // [optional] - Whether the server should automatically watch the "scope" value if it is an address
  23. })
  24. // returns a promise that resolves once the configuration has been applied
  25. // or rejects if there was a problem with the configuration
  26. // initialize web3
  27. // const web3 = new Web3(window.ethereum)
  28. // get current account
  29. // const accounts = await web3.eth.getAccounts();
  30. const address = "0x7d82a88Efb60Bb83B3f1665757c20D07E5C0f3ED"
  31. // call with the address of the account that you would like to receive status updates for
  32. const {
  33. emitter, // emitter object to listen for status updates
  34. details // initial account details which are useful for internal tracking: address
  35. } = blocknative.account(address)
  36. console.log(`Watching for events on ${address}...`)
  37. // listen to some events
  38. emitter.on('txPool', transaction => {
  39. console.log(`Sending ${transaction.value} wei to ${transaction.to}`)
  40. })
  41. // emitter.on('txConfirmed', transaction => {
  42. // console.log('Transaction is confirmed!')
  43. // })
  44. // catch every other event that occurs and log it
  45. // emitter.on('all', transaction => {
  46. // console.log(`Transaction event: ${transaction.eventCode}`)
  47. // })