Lesson9: confused with why Interactions.s.sol:AddConsumer is working with forked spolia test #2013
-
In section 'Forked test environment and dynamic private keys', we learnt that how to test with forked Sepolia network. I am confused why the 'addingCustomer' function is working in sepolia network. vm.startBroadcast();
Raffle raffle = new Raffle(
config.enteranceFee,
config.interval,
config.vrfCoordinator,
config.keyHash,
config.subId,
config.callbackGasLimit
);
vm.stopBroadcast();
AddConsumer addConsumer = new AddConsumer();
addConsumer.addingConsumer(
address(raffle),
config.vrfCoordinator,
config.subId
); but inside the function 'addingConsumer', the 'VRFCoordinatorV2_5Mock' is used in both local and sepolia test, and both are working, why? function addingConsumer(
address contractToVrf,
address vrfCoodi,
uint256 subId
) public {
console.log("adding consumer contract: ", contractToVrf);
console.log("using vrfCoodinator:", vrfCoodi);
console.log("on chain id: ", block.chainid);
vm.startBroadcast();
VRFCoordinatorV2_5Mock(vrfCoodi).addConsumer(subId, contractToVrf);
vm.stopBroadcast();
} this also apply to 'createSubscription' when the subid is equal to 0. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hello @caodanbobo, So, to make a call to a contract on the blockchain, you need the address of the contract on the blockchain and the interface of the contract. So, let's understand why we need these two things. We need the address of the contract on the blockchain so that we can send the transaction directly to it. It's like if I want to come and visit you in your house, I need the address of your house to come to your house. We need the interface of the contract that we want to send a transaction to be able to call the correct function on the contract address that we have. Let's assume I am not just coming to your house to visit but to come to deliver a package. You have a very big house, and you can never know I have arrived to deliver the package except if I use the doorbell at your home. Now, let's assume you are using a special doorbell, which I don't know how to operate. I know I need to be able to use your doorbell if I want my delivery to be successful, so I will need to learn how to use your doorbell. Now the question is, do I need to come to your house to learn how to use your doorbell? I believe not! I just need to find the same doorbell in my environment and train with that. So, I found the same doorbell as yours in my environment, trained with it, and learned how to use it. Then I came to your house and used your doorbell, and you came to get the package, and my delivery was successful. We can say the same about the interface of a contract. You can use any interface that has the same functions as the contract at the address to which you are sending a transaction to wrap the address of the contract to make a transaction. Hence, why we are using Please ask questions if anything is confusing. |
Beta Was this translation helpful? Give feedback.
I asked ChatGPT this question, and here is the answer:
In Solidity, both calls to addConsumer will behave the same way if the contract at vrfAdd correctly implements addConsumer.
To understand this clearly, i ask it a similar question: Assume
foo
is a virtual function inSubscriptionAPI
,VRFCoordinatorV2_5
andVRFCoordinatorV2_5Mock
provide their own implementations of foo, what would happen ifVRFCoordinatorV2_5Mock(vrfAdd).foo()
is called?Answer: