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.

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