DAPP开发【09】NFT交易市场开发(hardhat测试)

测试文件下新建market.js文件

扁平化,将所有依赖放在tmp.sol,可以去给他人使用

npx hardhat flatten > tmp.sol

测试文件

cpp 复制代码
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);
        
    });

});
相关推荐
王苏安说钢材A4 小时前
无锡卖无缝管厂家推荐@无锡佳钛合不锈钢有限公司
区块链
程序员李程峰1 天前
基础知识——区块链钱包
web3·去中心化·区块链·同态加密·零知识证明·共识算法·分布式账本
区块block1 天前
Infinity Alpha(无限阿尔法)即将发布纯链上AI收益引擎通证IA
人工智能·区块链
Datakeji1 天前
维恩波特Vairnport商业逻辑
大数据·人工智能·区块链
m0_380167141 天前
清算热力图怎么看?如何用来判断行情走向
大数据·人工智能·区块链
长安链开源社区1 天前
2025 长安链开发大赛正式启动!
web3·区块链
麦麦大数据2 天前
基于以太坊区块链+Spring Boot+Solidity智能合约的投票系统设计与实现
spring boot·后端·区块链·智能合约·投票系统
Bczheng12 天前
二十三.交易数据之签名(2)--选币逻辑
区块链
zhglhy2 天前
交易支付/证券/数字货币交易所交易引擎核心功能对比
区块链
长安链开源社区2 天前
学者观察 | 基于区块链的隐私计算技术——北京理工大学教授祝烈煌
运维·区块链