Skip to main content
edited title
Link

Solidity Testing. 1 wei issue. How to handle rounding differences when testing balance changes in ethers.js contracts?

added 508 characters in body
Source Link
it("Should allow to buy and refund", async () => {
        const FEE = 10; 
        
        const oneEtherInWei = ethers.parseEther("1");  // 1 ETH = 1e18 wei
        const twoEtherInWei = ethers.parseEther("2");  // 2 ETH = 2e18 wei

        await expect(smartContract.connect(seller).createAuction(
            oneEtherInWei,  
            FEE,
            "test item",
            60
        ));
        
        const tx = await smartContract.connect(buyer).buy(0, { value: twoEtherInWei });
        await tx.wait();
        
        const feeAmount = (parseFloat(oneEtherInWei.toString()) * FEE) / 100; 
        console.log("feeAmount :", feeAmount);
        
        const moneyForSeller = parseFloat(oneEtherInWei.toString()) - feeAmount;
        console.log("moneyForSeller :", moneyForSeller);
        
        const moneyLeftOnContract = parseFloat(oneEtherInWei.toString()) - moneyForSeller;
        console.log("moneyLeftOnContract :", moneyLeftOnContract); 


        await expect(tx).to.changeEtherBalance(smartContract.target, moneyLeftOnContract.toString()) // Перевірка зміни балансу смарт-контракту
        await expect(tx).to.changeEtherBalance(buyer.address, (parseFloat(twoEtherInWei) * -1).toString()) // Перевірка зміни балансу покупця
        await expect(tx).to.changeEtherBalance(seller.address, moneyForSeller.toString()) // Перевірка зміни балансу продавця

        
        
        const price = await smartContract.auctions(0).finalPrice;
        expect(tx).to.emit(smartContract, "AuctionEnded")
            .withArgs(0, price, buyer);
    });

Test result screenshot

it("Should allow to buy and refund", async () => {
        const FEE = 10; 
        
        const oneEtherInWei = ethers.parseEther("1");  // 1 ETH = 1e18 wei
        const twoEtherInWei = ethers.parseEther("2");  // 2 ETH = 2e18 wei

        await expect(smartContract.connect(seller).createAuction(
            oneEtherInWei,  
            FEE,
            "test item",
            60
        ));
        
        const tx = await smartContract.connect(buyer).buy(0, { value: twoEtherInWei });
        await tx.wait();
        
        const feeAmount = (parseFloat(oneEtherInWei.toString()) * FEE) / 100; 
        console.log("feeAmount :", feeAmount);
        
        const moneyForSeller = parseFloat(oneEtherInWei.toString()) - feeAmount;
        console.log("moneyForSeller :", moneyForSeller);
        
        const moneyLeftOnContract = parseFloat(oneEtherInWei.toString()) - moneyForSeller;
        console.log("moneyLeftOnContract :", moneyLeftOnContract);

        
        
        const price = await smartContract.auctions(0).finalPrice;
        expect(tx).to.emit(smartContract, "AuctionEnded")
            .withArgs(0, price, buyer);
    });
it("Should allow to buy and refund", async () => {
        const FEE = 10; 
        
        const oneEtherInWei = ethers.parseEther("1");  // 1 ETH = 1e18 wei
        const twoEtherInWei = ethers.parseEther("2");  // 2 ETH = 2e18 wei

        await expect(smartContract.connect(seller).createAuction(
            oneEtherInWei,  
            FEE,
            "test item",
            60
        ));
        
        const tx = await smartContract.connect(buyer).buy(0, { value: twoEtherInWei });
        await tx.wait();
        
        const feeAmount = (parseFloat(oneEtherInWei.toString()) * FEE) / 100; 
        console.log("feeAmount :", feeAmount);
        
        const moneyForSeller = parseFloat(oneEtherInWei.toString()) - feeAmount;
        console.log("moneyForSeller :", moneyForSeller);
        
        const moneyLeftOnContract = parseFloat(oneEtherInWei.toString()) - moneyForSeller;
        console.log("moneyLeftOnContract :", moneyLeftOnContract); 


        await expect(tx).to.changeEtherBalance(smartContract.target, moneyLeftOnContract.toString()) // Перевірка зміни балансу смарт-контракту
        await expect(tx).to.changeEtherBalance(buyer.address, (parseFloat(twoEtherInWei) * -1).toString()) // Перевірка зміни балансу покупця
        await expect(tx).to.changeEtherBalance(seller.address, moneyForSeller.toString()) // Перевірка зміни балансу продавця

        
        
        const price = await smartContract.auctions(0).finalPrice;
        expect(tx).to.emit(smartContract, "AuctionEnded")
            .withArgs(0, price, buyer);
    });

Test result screenshot

edited title
Link
Jeremy Friesner
  • 73.9k
  • 17
  • 147
  • 257

Silidity Solidity Testing. How to handle rounding differences when testing balance changes in ethers.js contracts?

Source Link
Loading