测试文件下新建market.js文件
扁平化,将所有依赖放在tmp.sol,可以去给他人使用
npx hardhat flatten > tmp.sol
测试文件
const {expect} = require('chai');
const {ethers} = require('hardhat');
describe('Market',async function(){
//定义三个合约,两个账户
let usdt,nft,market,accountA,accountB;
beforeEach(async () =>{
//hardhat提供两个账户
[accountA,accountB] = await ethers.getSigners();
//部署三个合约
const USDT = await ethers.getContractFactory('cUSDT');
usdt = await USDT.deploy();
const MyNFT = await ethers.getContractFactory('NFTM');
nft = await MyNFT.deploy(accountA.address);
const Market = await ethers.getContractFactory('Market');
market = await Market.deploy(usdt.target,nft.target);
//给账户1挖1usdt的 erc20 部署erc20合约是就给账户A了1e26的erc20
//给账户2挖两个nft
await nft.safeMint(accountB.address);
await nft.safeMint(accountB.address);
await nft.connect(accountB).setApprovalForAll(accountA.address,true);
//给market授权花费A的钱(默认连接的就是第一个用户)
//await usdt.connect(accountA).approve(market.target,"1e26");
await usdt.approve(market.target,"100000000000000000000000000");
});
//验证erc20变量==usdt的地址
it('its erc20 address should be usdt',async function(){
expect(await market.erc20()).to.equal(usdt.target);
});
it('its erc721 address should be MyNft',async function(){
expect(await market.erc721()).to.equal(nft.target);
});
it('accountB should have two nfts',async function(){
expect(await nft.balanceOf(accountB.address)).to.equals(2);
});
it('accountA should have 1e26 usdt',async function(){
expect(await usdt.balanceOf(accountA.address)).to.equals("100000000000000000000000000");
});
it('accountB can list 2 nft to market',async function(){
const price = "0x0000000000000000000000000000000000000000000000000001c6bf52634000";
//await nft.connect(accountB).safeTransferFrom(accountB.address,market.target,0,price);
//报错
//TypeError: ambiguous function description (i.e. matches "safeTransferFrom(address,address,uint256)",
//"safeTransferFrom(address,address,uint256,bytes)") (argument="key", value="safeTransferFrom", code=INVALID_ARGUMENT, version=6.9.0)
//ambiguous 混淆同名函数,即使参数不一样,也分不出来,所以用下面的方法,明确选择器的名字
//这里应该是B来调用进行上架,但是默认是a,老师忘记怎么连接了
//于是在上面定义 await nft.connect(accountB).setApprovalForAll(accountA.address,true);
//b允许a使用它的nft
expect(await nft['safeTransferFrom(address,address,uint256,bytes)'](accountB.address,market.target,0,price))
.to.emit(market,"NewOrder");
expect(await nft['safeTransferFrom(address,address,uint256,bytes)'](accountB.address,market.target,1,price))
.to.emit(market,"NewOrder");
expect(await nft.balanceOf(accountB.address)).to.equal(0);
expect(await nft.balanceOf(market.target)).to.equal(2);
expect(await market.isListed(0)).to.equal(true);
expect(await market.isListed(1)).to.equal(true);
expect((await market.getAllNFTs())[0][0]).to.equal(accountB.address);
expect((await market.getAllNFTs())[0][1]).to.equal(0);
expect((await market.getAllNFTs())[0][2]).to.equal(price);
expect((await market.getAllNFTs())[1][0]).to.equal(accountB.address);
expect((await market.getAllNFTs())[1][1]).to.equal(1);
expect((await market.getAllNFTs())[1][2]).to.equal(price);
expect(await market.getOrderLength()).to.equal(2);
expect((await market.connect(accountB).getMyNFTs())[0][0]).to.equal(accountB.address);
expect((await market.connect(accountB).getMyNFTs())[0][1]).to.equal(0);
expect((await market.connect(accountB).getMyNFTs())[0][2]).to.equal(price);
expect((await market.connect(accountB).getMyNFTs())[1][0]).to.equal(accountB.address);
expect((await market.connect(accountB).getMyNFTs())[1][1]).to.equal(1);
expect((await market.connect(accountB).getMyNFTs())[1][2]).to.equal(price);
});
});