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

// For Node >= v13 / es module environments
import BlocknativeSdk from 'bnc-sdk'
import WebSocket from 'ws' // only neccessary in server environments
import dotenv from 'dotenv'
dotenv.config()
const API_URL = process.env.API_URL;
import { createAlchemyWeb3 } from "@alch/alchemy-web3"
const web3 = createAlchemyWeb3(API_URL);
// change:
// networkId
// ABI
// contract address
// method name
// mint gas
// mint method data
// public/private key
import contractABI from './MoonbirdPunks.json' assert { type: 'json' }
// create options object
const options = {
dappId: 'd30463f1-eb29-42b8-8059-e5596e13d0fe',
networkId: 3,
system: 'ethereum', // optional, defaults to ethereum
transactionHandlers: [event => console.log(event.transaction)],
ws: WebSocket, // only neccessary in server environments
name: 'Instance name here', // optional, use when running multiple instances
onerror: (error) => {console.log(error)} //optional, use to catch errors
}
// initialize and connect to the api
const blocknative = new BlocknativeSdk(options)
const contractAddress = "0x90Bc839511e1a8b32e2ba27f37c7632D12E872B6";
const methodName = "flipSaleState"
await blocknative.configuration({
scope: contractAddress, // [required] - either 'global' or valid Ethereum address
filters: [{"contractCall.methodName":methodName}],
abi: contractABI.abi, // [optional] - valid contract ABI
watchAddress: true // [optional] - Whether the server should automatically watch the "scope" value if it is an address
})
// returns a promise that resolves once the configuration has been applied
// or rejects if there was a problem with the configuration
// call with the address of the account that you would like to receive status updates for
const {
emitter, // emitter object to listen for status updates
details // initial account details which are useful for internal tracking: address
} = blocknative.account(contractAddress)
console.log(`Watching for ${methodName} on ${contractAddress}...`)
const nftContract = new web3.eth.Contract(contractABI.abi, contractAddress);
async function mintNFT(maxFee, maxPriorityFee) {
const nonce = await web3.eth.getTransactionCount(process.env.PUBLIC_KEY, 'latest'); //get latest nonce
//the transaction
const tx = {
'from': process.env.PUBLIC_KEY,
'to': contractAddress,
'nonce': nonce,
'gas': 100000,
'maxFeePerGas': maxFee,
'maxPriorityFeePerGas': maxPriorityFee,
'data': nftContract.methods.mintPunk(1).encodeABI()
};
const signedTx = await web3.eth.accounts.signTransaction(tx, process.env.PRIVATE_KEY);
console.log("Minting NFT...")
const transactionReceipt = await web3.eth.sendSignedTransaction(signedTx.rawTransaction);
console.log(`Transaction receipt: ${JSON.stringify(transactionReceipt)}`);
}
// listen to some events
emitter.on('txPool', transaction => {
// emitter.on('txSent', transaction => {
console.log(`Sending ${transaction.value} wei to ${transaction.to}`)
blocknative.unsubscribe(contractAddress)
mintNFT(transaction.maxFeePerGas,
transaction.maxPriorityFeePerGas)
})
// blocknative.unsubscribe(contractAddress)
// emitter.on('txConfirmed', transaction => {
// console.log('Transaction is confirmed!')
// })
// catch every other event that occurs and log it
// emitter.on('all', transaction => {
// console.log(`Transaction event: ${transaction.eventCode}`)
// })
//