Interaction scripts to interact with the NFT marketplace sample code
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
///////////////////// | |
// BUY ITEM // | |
///////////////////// | |
const { ethers } = require("hardhat") | |
const TOKEN_ID = 0 // SET THIS BEFORE RUNNING SCRIPT | |
async function buyItem() { | |
const accounts = await ethers.getSigners() | |
const [deployer, owner, buyer1] = accounts | |
const IDENTITIES = { | |
[deployer.address]: "DEPLOYER", | |
[owner.address]: "OWNER", | |
[buyer1.address]: "BUYER_1", | |
} | |
const nftMarketplaceContract = await ethers.getContract("NftMarketplace") | |
const basicNftContract = await ethers.getContract("BasicNft") | |
const listing = await nftMarketplaceContract.getListing(basicNftContract.address, TOKEN_ID) | |
const price = listing.price.toString() | |
const tx = await nftMarketplaceContract | |
.connect(buyer1) | |
.buyItem(basicNftContract.address, TOKEN_ID, { | |
value: price, | |
}) | |
await tx.wait(1) | |
console.log("NFT Bought!") | |
const newOwner = await basicNftContract.ownerOf(TOKEN_ID) | |
console.log( | |
`New owner of Token ID ${TOKEN_ID} is ${newOwner} with identity of ${IDENTITIES[newOwner]} ` | |
) | |
} | |
buyItem() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
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
///////////////////// | |
// CANCEL LISTING // | |
///////////////////// | |
const { ethers } = require("hardhat") | |
const TOKEN_ID = 2 | |
async function cancelListing() { | |
const accounts = await ethers.getSigners() | |
const [deployer, owner] = accounts | |
const nftMarketplaceContract = await ethers.getContract("NftMarketplace") | |
const basicNftContract = await ethers.getContract("BasicNft") | |
const tx = await nftMarketplaceContract | |
.connect(owner) | |
.cancelListing(basicNftContract.address, TOKEN_ID) | |
const cancelTxReceipt = await tx.wait(1) | |
const args = cancelTxReceipt.events[0].args | |
console.log(`NFT with ID ${TOKEN_ID} Canceled...`) | |
// Check cancellation. | |
const canceledListing = await nftMarketplaceContract.getListing( | |
basicNftContract.address, | |
TOKEN_ID | |
) | |
console.log("Seller is Zero Address (i.e no one!)", canceledListing.seller) | |
} | |
cancelListing() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
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
///////////////////// | |
// CHECK SELLER PROCEEDS // | |
///////////////////// | |
const { ethers, network } = require("hardhat") | |
async function getProceeds() { | |
const accounts = await ethers.getSigners() | |
const [deployer, owner] = accounts | |
const nftMarketplaceContract = await ethers.getContract("NftMarketplace") | |
const basicNftContract = await ethers.getContract("BasicNft") | |
const proceeds = await nftMarketplaceContract.getProceeds(owner.address) | |
const proceedsWei = ethers.utils.formatEther(proceeds.toString()) | |
console.log(`Seller ${owner.address} has ${proceedsWei} eth!`) | |
} | |
getProceeds() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
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
///////////////////// | |
// MINT AND LIST // | |
///////////////////// | |
const { ethers } = require("hardhat") | |
const PRICE = ethers.utils.parseEther("0.1") | |
async function mintAndList() { | |
const accounts = await ethers.getSigners() | |
const [deployer, owner, buyer1] = accounts | |
const IDENTITIES = { | |
[deployer.address]: "DEPLOYER", | |
[owner.address]: "OWNER", | |
[buyer1.address]: "BUYER_1", | |
} | |
const nftMarketplaceContract = await ethers.getContract("NftMarketplace") | |
const basicNftContract = await ethers.getContract("BasicNft") | |
console.log(`Minting NFT for ${owner.address}`) | |
const mintTx = await basicNftContract.connect(owner).mintNft() | |
const mintTxReceipt = await mintTx.wait(1) | |
const tokenId = mintTxReceipt.events[0].args.tokenId | |
console.log("Approving Marketplace as operator of NFT...") | |
const approvalTx = await basicNftContract | |
.connect(owner) | |
.approve(nftMarketplaceContract.address, tokenId) | |
await approvalTx.wait(1) | |
console.log("Listing NFT...") | |
const tx = await nftMarketplaceContract | |
.connect(owner) | |
.listItem(basicNftContract.address, tokenId, PRICE) | |
await tx.wait(1) | |
console.log("NFT Listed with token ID: ", tokenId.toString()) | |
const mintedBy = await basicNftContract.ownerOf(tokenId) | |
console.log( | |
`NFT with ID ${tokenId} minted and listed by owner ${mintedBy} with identity ${IDENTITIES[mintedBy]}.` | |
) | |
} | |
mintAndList() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) | |
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
///////////////////// | |
// UPDATE LISTING // | |
///////////////////// | |
const { ethers } = require("hardhat") | |
const TOKEN_ID = 1 | |
async function updateListing() { | |
const accounts = await ethers.getSigners() | |
const [deployer, owner, buyer1] = accounts | |
const IDENTITIES = { | |
[deployer.address]: "DEPLOYER", | |
[owner.address]: "OWNER", | |
[buyer1.address]: "BUYER_1", | |
} | |
const nftMarketplaceContract = await ethers.getContract("NftMarketplace") | |
const basicNftContract = await ethers.getContract("BasicNft") | |
console.log(`Updating listing for token ID ${TOKEN_ID} with a new price`) | |
const updateTx = await nftMarketplaceContract | |
.connect(owner) | |
.updateListing(basicNftContract.address, TOKEN_ID, ethers.utils.parseEther("0.5")) | |
const updateTxReceipt = await updateTx.wait(1) | |
const updatedPrice = updateTxReceipt.events[0].args.price | |
console.log("updated price: ", updatedPrice.toString()) | |
// Confirm the listing is updated. | |
const updatedListing = await nftMarketplaceContract.getListing( | |
basicNftContract.address, | |
TOKEN_ID | |
) | |
console.log(`Updated listing has price of ${updatedListing.price.toString()}`) | |
} | |
updateListing() | |
.then(() => process.exit(0)) | |
.catch((error) => { | |
console.error(error) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment