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.

90 lines
3.2 KiB

2 years ago
  1. // For Node >= v13 / es module environments
  2. import BlocknativeSdk from 'bnc-sdk'
  3. import WebSocket from 'ws' // only neccessary in server environments
  4. import dotenv from 'dotenv'
  5. dotenv.config()
  6. const API_URL = process.env.API_URL;
  7. import { createAlchemyWeb3 } from "@alch/alchemy-web3"
  8. const web3 = createAlchemyWeb3(API_URL);
  9. import contractABI from './MoonbirdPunks.json' assert { type: 'json' }
  10. // create options object
  11. const options = {
  12. dappId: 'd30463f1-eb29-42b8-8059-e5596e13d0fe',
  13. networkId: 3,
  14. system: 'ethereum', // optional, defaults to ethereum
  15. transactionHandlers: [event => console.log(event.transaction)],
  16. ws: WebSocket, // only neccessary in server environments
  17. name: 'Instance name here', // optional, use when running multiple instances
  18. onerror: (error) => {console.log(error)} //optional, use to catch errors
  19. }
  20. // initialize and connect to the api
  21. const blocknative = new BlocknativeSdk(options)
  22. const contractAddress = "0x90Bc839511e1a8b32e2ba27f37c7632D12E872B6";
  23. // const methodName = "ownerMint"
  24. const methodName = "flipSaleState"
  25. await blocknative.configuration({
  26. scope: contractAddress, // [required] - either 'global' or valid Ethereum address
  27. filters: [{"contractCall.methodName":methodName}],
  28. abi: contractABI.abi, // [optional] - valid contract ABI
  29. watchAddress: true // [optional] - Whether the server should automatically watch the "scope" value if it is an address
  30. })
  31. // returns a promise that resolves once the configuration has been applied
  32. // or rejects if there was a problem with the configuration
  33. // call with the address of the account that you would like to receive status updates for
  34. const {
  35. emitter, // emitter object to listen for status updates
  36. details // initial account details which are useful for internal tracking: address
  37. } = blocknative.account(contractAddress)
  38. console.log(`Watching for ${methodName} on ${contractAddress}...`)
  39. const nftContract = new web3.eth.Contract(contractABI.abi, contractAddress);
  40. async function mintPunk(maxFee, maxPriorityFee) {
  41. const nonce = await web3.eth.getTransactionCount(process.env.PUBLIC_KEY, 'latest'); //get latest nonce
  42. //the transaction
  43. const tx = {
  44. 'from': process.env.PUBLIC_KEY,
  45. 'to': contractAddress,
  46. 'nonce': nonce,
  47. 'gas': 100000,
  48. 'maxFeePerGas': maxFee,
  49. 'maxPriorityFeePerGas': maxPriorityFee,
  50. 'data': nftContract.methods.mintPunk(1).encodeABI()
  51. };
  52. const signedTx = await web3.eth.accounts.signTransaction(tx, process.env.PRIVATE_KEY);
  53. console.log("Minting punk...")
  54. const transactionReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
  55. console.log(`Transaction receipt: ${JSON.stringify(transactionReceipt)}`);
  56. }
  57. // listen to some events
  58. emitter.on('txPool', transaction => {
  59. // emitter.on('txSent', transaction => {
  60. console.log(`Sending ${transaction.value} wei to ${transaction.to}`)
  61. blocknative.unsubscribe(contractAddress)
  62. mintPunk(transaction.maxFeePerGas,
  63. transaction.maxPriorityFeePerGas)
  64. })
  65. // blocknative.unsubscribe(contractAddress)
  66. // emitter.on('txConfirmed', transaction => {
  67. // console.log('Transaction is confirmed!')
  68. // })
  69. // catch every other event that occurs and log it
  70. // emitter.on('all', transaction => {
  71. // console.log(`Transaction event: ${transaction.eventCode}`)
  72. // })
  73. //