-
Hi everyone, Runnig the unit test below, I observed that all 3 new instances of As 1. Shouldn't the addresses be different if 3 new instances of I added 2. Secondly, these 3 new instances act on behalf of new When these new instances are connected to different I mean, I'm not able to wrap my head around how we are only calling the functions using Isn't How could we select winner when all the 4 instances are connected to different Tried my best to convey my concern but if it's still unclear, sorry, I can try rephrasing. Let me know please. !developmentChains.includes(network.name) ? describe.skip : describe("Raffle", function () {
let raffle, vrfCoordinatorV2Mock, raffleEntranceFee, deployer, interval
const chainId = network.config.chainId
beforeEach(async function() {
deployer = (await getNamedAccounts()).deployer
await deployments.fixture(["all"])
raffle = await ethers.getContract("Raffle", deployer)
console.log(`Address of original Raffle_instance in beforeEach# 1: ${raffle.address}`)
vrfCoordinatorV2Mock = await ethers.getContract("VRFCoordinatorV2Mock", deployer)
raffleEntranceFee = await raffle.getEntranceFee()
interval = await raffle.getInterval()
})
describe ("fulfillRandomWords", function (){
beforeEach(async function(){
await raffle.enterRaffle({value: raffleEntranceFee})
await network.provider.send("evm_increaseTime", [interval.toNumber() + 1])
await network.provider.send("evm_mine", [])
})
it("Should pick up a random Winner, resets the Lottery, and sends the money to the Winner.", async function(){
const additionalEntrants = 3
const startingAccountIndex = 1
const accounts = await ethers.getSigners()
for (let i=startingAccountIndex; i < startingAccountIndex + additionalEntrants; i++) {
const accountConnectedRaffle = raffle.connect(accounts[i])
// checking all addresses for clarity...
console.log(`Address of new Raffle_instance: ${accountConnectedRaffle.address}`)
await accountConnectedRaffle.enterRaffle({value: raffleEntranceFee})
}
const startingTimestamp = await raffle.getLatestTimestamp()
await new Promise(async (resolve, reject) => {
raffle.once("WinnerPicked", async () => {
console.log("Found the emitted event - Winner Picked")
try {
const recentWinner = await raffle.getRecentWinner()
console.log(`Recent Winner : ${recentWinner}`)
console.log(`Deployer : ${deployer}`)
console.log(`AdditionalEntrant# 1 : ${accounts[1].address}`)
console.log(`AdditionalEntrant# 2 : ${accounts[2].address}`)
console.log(`AdditionalEntrant# 3 : ${accounts[3].address}`)
// testing 3 variables that should be reset by now
const raffleState = await raffle.getRaffleState()
const numPlayers = await raffle.getNumOfPlayers()
const endingTimestamp = await raffle.getLatestTimestamp()
// assert statements to check all 3
assert.equal(raffleState.toString(), "0")
assert.equal(numPlayers.toString(), "0")
assert(endingTimestamp > startingTimestamp)
} catch(e) {
reject(e)
}
resolve()
})
const tx = await raffle.performUpkeep([])
const txReceipt = await tx.wait(1)
await vrfCoordinatorV2Mock.fulfillRandomWords(txReceipt.events[1].args.requestId, raffle.address)
})
})
}) 3. Also, why am I always getting the same account as the winner if the selection is random. I've run the test 8 times. So much so, even Patrick got the same winner. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 9 replies
-
Based on my understanding to your questions
for (let i=startingAccountIndex; i < startingAccountIndex + additionalEntrants; i++) {
const accountConnectedRaffle = raffle.connect(accounts[i])
// checking all addresses for clarity...
console.log(`Address of new Raffle_instance: ${accountConnectedRaffle.address}`)
await accountConnectedRaffle.enterRaffle({value: raffleEntranceFee})
} We are simply connecting the same contract with different accounts which is evident as with this line: console.log(`Address of new Raffle_instance: ${accountConnectedRaffle.address}`) you are getting address of same contract. await accountConnectedRaffle.enterRaffle({value: raffleEntranceFee}) we are making a transection from the same contract which is currently connected to account[i] (i being 0,1,2,3) and with next iteration of the loop connecting to the next account and repeating the process again.
Tried my best to explain. |
Beta Was this translation helpful? Give feedback.
-
@adityabhattad2021 explains well, here I am going to explain my way. Your first question: Q1. Shouldn't the addresses be different if 3 new instances of Raffle.sol are getting created? Ans: Raffle instance is not created again, it is only created once when we run this command in the await deployments.fixture(["all"]); Now we get those deployed contracts with the help of hardhat ether and connect it deployer, like this: raffle = await ethers.getContract("Raffle", deployer) Now, anything we do with the contract, Now, I am not able to understand your second question properly, sorry for that, but I am going to explain it further so it will make sense to you. So now we want to test that when multiple players enter into the raffle, our contract should select the winner and transfer the amount to him/her. For that, the first requirement is to enter multiple players into the raffle, right? But if we directly call the const accountConnectedRaffle = raffle.connect(accounts[i])
await accountConnectedRaffle.enterRaffle({value: raffleEntranceFee}) The first line is connecting our contract with other players' accounts (in every iteration of the loop), so the And the second line is calling This way we achieved what we wanted to enter multiple players into the raffle contract, further in the test we select a winner and send money to the winner's account simply. Hope it helps you understand the process. |
Beta Was this translation helpful? Give feedback.
-
Thanks @alymurtazamemon and @adityabhattad2021 for taking this up. The root cause of my "multiple instances got created" doubt is the ethers.js documentation below: It says, That's what led me to think that So, am I misunderstanding the ether.js documentation ? |
Beta Was this translation helpful? Give feedback.
Thanks @alymurtazamemon and @adityabhattad2021 for taking this up.
The root cause of my "multiple instances got created" doubt is the ethers.js documentation below:
It says,
contract.connect()
returns new instance of the contract.That's what led me to think that
const accountConnectedRaffle = raffle.connect(accounts[i])
is returning a new instance in every iterationSo, am I misunderstanding the ether.js documentation ?