Skip to content

Commit

Permalink
refator: add env
Browse files Browse the repository at this point in the history
  • Loading branch information
Guo Hong committed Jun 21, 2024
1 parent 76e3ed1 commit d952747
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 20 deletions.
2 changes: 2 additions & 0 deletions members/xiangnuans/task3/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INFURA_PROJECT_ID=""
PRIVATE_KEY="
2 changes: 1 addition & 1 deletion members/xiangnuans/task3/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ npx hardhat ignition deploy ./ignition/modules/Lock.ts
- Contract MyToken deployed successfully: [0xC2F6FE1FCb070abE231CA0f9c2d148183740E4a8](https://sepolia.etherscan.io/address/0xC2F6FE1FCb070abE231CA0f9c2d148183740E4a8)
- Contract MyNFT deployed successfully: [0x4ECEd2DEAfC38a647675F650c8ee0abbc52Db26e](https://sepolia.etherscan.io/address/0x4ECEd2DEAfC38a647675F650c8ee0abbc52Db26e)
- Contract NFTMarket deployed successfully: [0x53b72dE55e80B4569EfDA5C96393b26FBDd27eF8](https://sepolia.etherscan.io/address/0x53b72dE55e80B4569EfDA5C96393b26FBDd27eF8)
- Purchase NFT's transaction hash: [0xf87b7bbf97f47920944c4941fa087703c0f3f10876431f05b7de50d9b9bc6e93](https://sepolia.etherscan.io/tx/0xf87b7bbf97f47920944c4941fa087703c0f3f10876431f05b7de50d9b9bc6e93)
- Purchase NFT's transaction hash: [0x60ee944f5619c5f7d1315f2d7e0cafb0f354f4a8dd3cf295709232415da08085](https://sepolia.etherscan.io/tx/0x60ee944f5619c5f7d1315f2d7e0cafb0f354f4a8dd3cf295709232415da08085)
40 changes: 27 additions & 13 deletions members/xiangnuans/task3/scripts/buy-nft.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { marketAddress, tokenAddress } from "./config";

import { ethers } from "hardhat";
import { marketAddress } from "./config";

async function main() {
const [buyer] = await ethers.getSigners();
console.log("Buyer address:", buyer.address);

const market = await ethers.getContractAt("NFTMarket", marketAddress);
console.log("Market contract attached:", marketAddress);
console.log("Market contract attached:", marketAddress, market);


// 获取列表中的第一个NFT信息
const listing = await market.listings(0);
// 获取列表中的第3个NFT信息
const listing = await market.listings(2);
console.log("Market listing:", listing);

const seller = listing.seller;
Expand All @@ -23,12 +25,20 @@ async function main() {
console.log("Price:", price.toString());

// 获取支付代币的合约地址
const tokenContractAddress = await market.paymentToken();
console.log("Payment Token Contract Address:", tokenContractAddress);
console.log("Payment Token Contract Address:", tokenAddress);
const Token = await ethers.getContractAt("IERC20", tokenAddress);
console.log("Token Contract Address:", Token.target);


// 模拟买家授权市场合约访问代币
const Token = await ethers.getContractAt("IERC20", tokenContractAddress);
console.log("Token Contract Address:", Token);
// 获取当前用户拥有的代币数量
const balance = await Token.balanceOf(buyer.address);
console.log("Balance:", ethers.formatEther(balance), "tokens");

// 检查用户代币余额是否足够购买 NFT
if (balance < price) {
console.log("Insufficient balance to purchase the NFT.");
process.exit(1);
}

const allowance = await Token.allowance(buyer.address, marketAddress);
console.log("Allowance:", allowance.toString());
Expand All @@ -40,11 +50,15 @@ async function main() {
}

// 购买NFT
console.log("Buying NFT...");
const buyTx = await market.connect(buyer).buyNFT(0);
await buyTx.wait();
try {
console.log("Buying NFT...");
const buyTx = await market.connect(buyer).buyNFT(2);
await buyTx.wait();

console.log(`NFT with tokenId ${tokenId} purchased from ${seller} by ${buyer.address}`);
console.log(`NFT with tokenId ${tokenId} purchased from ${seller} by ${buyer.address}`);
} catch (error) {
console.log("Buying NFT Error", error)
}
}

main().catch((error) => {
Expand Down
6 changes: 1 addition & 5 deletions members/xiangnuans/task3/scripts/config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// export const tokenAddress = '0xe07eB366A984Aac071D8cf4e2FAc2f3baF8026B0';
// export const nftAddress = '0x825035A3EABb9c74Dcf470eBafA5a9c8B05A2C91';
// export const marketAddress = '0x05b062cBCe45226D7d357f9b92533E143E71Df02';


// etherscan的合约地址
export const tokenAddress = "0xC2F6FE1FCb070abE231CA0f9c2d148183740E4a8";
export const nftAddress = '0x4ECEd2DEAfC38a647675F650c8ee0abbc52Db26e';
export const marketAddress = '0x53b72dE55e80B4569EfDA5C96393b26FBDd27eF8';
2 changes: 1 addition & 1 deletion members/xiangnuans/task3/scripts/list-nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ethers } from "hardhat";

async function main() {
const [deployer] = await ethers.getSigners();
const tokenId = 1; // 你想要上架的NFT的Token ID
const tokenId = 3; // 你想要上架的NFT的Token ID
const price = ethers.parseEther("10"); // 上架价格
const tokenURI = "https://my-json-server.typicode.com/abcoathup/samplenft/tokens/1"; // NFT的元数据URI

Expand Down

0 comments on commit d952747

Please sign in to comment.