smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.01k stars 2.91k forks source link

Lesson 12 unit test used the same contract #5381

Closed oldpride closed 1 year ago

oldpride commented 1 year ago

Lesson

Lesson 12

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

The video left this part as homework, therefore, no video. If it were there, it would be at 19:15:00 timestamp

Operating System

Windows

Describe the bug

In lesson 12, test/unit/ourToken-unit.test.js

ourToken = await ethers.getContract("OurToken", deployer)
...
playerToken = await ethers.getContract("OurToken", user1)

... I believe Patrick meant to create two different tokens to interact with each other. However, they turned out to be the same contract

    it("Should approve other address to spend token", async () => {
          const tokensToSpend = ethers.utils.parseEther("5")
          //Deployer is approving that user1 can spend 5 of their precious OT's
          console.log("deployer           : ", deployer)
          console.log("user1              : ", user1)
          console.log("ourToken.address   : ", ourToken.address)
          console.log("playerToken.address: ", playerToken.address)

          await ourToken.approve(user1, tokensToSpend)
          await playerToken.transferFrom(deployer, user1, tokensToSpend)
          expect(await playerToken.balanceOf(user1)).to.equal(tokensToSpend)
        })

this printed out the following when I ran 'yarn hardhat test'.

deployer           :  0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
user1              :  0x70997970C51812dc3A010C7d01b50e0d17dc79C8
ourToken.address   :  0x5FbDB2315678afecb367f032d93F642f64180aa3
playerToken.address:  0x5FbDB2315678afecb367f032d93F642f64180aa3

You can see ourToken and playerToken had the same address.

oldpride commented 1 year ago

ethers.getContract only returns the connection to the contract. As both were connecting to the same contract, therefore, the printed address were the same.