Error while running test , (Ownable Unauthorized Account) #4857
-
|
I am running into this error while running test in MY DscEngineTest , what could be the reason error: test : |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
|
@anasqurreshhi can you share your DeployDsc contract? |
Beta Was this translation helpful? Give feedback.
-
|
@anasqurreshhi, run your test with the |
Beta Was this translation helpful? Give feedback.
-
|
@anasqurreshhi You're getting this error:
because your deployment script uses So during tests:
Fix:Do not broadcast when running inside tests. Only broadcast during real deployments. Use this pattern in you script and it will solve your issue. if (block.chainid != 31337) { // 31337 = Anvil , which is the default foundry test environment chain
vm.startBroadcast(deployerKey);
}
DecentralizedStableCoin dsc = new DecentralizedStableCoin();
DSCEngine engine = new DSCEngine(tokenAddresses, priceFeedAddresses, address(dsc));
dsc.transferOwnership(address(engine));
if (block.chainid != 31337) {
vm.stopBroadcast();
}Best of luck! |
Beta Was this translation helpful? Give feedback.
@anasqurreshhi You're getting this error:
solidity OwnableUnauthorizedAccount(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266)because your deployment script uses
vm.startBroadcast(), which makes all deployments and function calls originate from the broadcast signer, not from the Foundry test address.So during tests:
The DSC contract is deployed by the broadcast address.
That broadcast address becomes the owner.
The test continues running as the default Foundry test account
(0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266).When transferOwnership is executed, the caller is not the owner, so it reverts.
Fix:
Do not broadcast when running inside tests. Only broadcast during real deployments.