Created
July 18, 2022 14:24
-
-
Save dharniel45/4467996f7f5fb7e9ae90ad5eb179cda1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CONTRACT = require("./../artifacts/contracts/AfterApe.sol/AfterApeNFT.json"); | |
require("dotenv").config(); | |
const { ALCHEMY_URL, PUBLIC_KEY, PRIVATE_KEY, CONTRACT_ADDRESS, CID } = process.env; | |
const web3 = createAlchemyWeb3(`${ALCHEMY_URL}`); | |
const nftContract = new web3.eth.Contract(CONTRACT.abi, CONTRACT_ADDRESS); | |
async function mintNFT(tokenURI) { | |
const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, "latest"); | |
//the transaction | |
const tx = { | |
"from": PUBLIC_KEY, | |
"to": CONTRACT_ADDRESS, | |
"nonce": nonce, | |
"gas": 500000, | |
"data": nftContract.methods.mintNFT(PUBLIC_KEY, tokenURI).encodeABI() | |
}; | |
const signPromise = web3.eth.accounts.signTransaction(tx, `0x${PRIVATE_KEY}`); | |
signPromise | |
.then((signedTx) => { | |
web3.eth.sendSignedTransaction( | |
signedTx.rawTransaction, | |
function(err, hash) { | |
if (!err) { | |
console.log( | |
"The hash of your transaction is: ", hash , | |
"\nCheck Alchemy's Mempool to view the status of your transaction!" | |
) | |
} else { | |
console.log( | |
"Something went wrong when submitting your transaction:", err) | |
} | |
} | |
) | |
}) | |
.catch((err) => { | |
console.log("Promise failed:", err) | |
}); | |
} | |
mintNFT(`ipfs://${CID}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment