ประเภทหนัง
ตัวอย่างหนัง Ethereum: Foundry script: balance mismatch of tx caller
Script Zlievárne Ethereum: Error compensation balance
As a developer who works with intelligent contracts from Ethereum, it must be ensured that your scripts pursue properly and exactly your account income. However, there is often an supervision that can lead to problems such as balance. In this article we will immerse ourselves in the “Balance of Balance” error in the foundry and give instructions on how to reproduce and solve.
What is the inconsistency of balance?
The remaining amount of the balance occurs when the actual account of the account on the blockchain differs from the displayed balance, which shows the caller function with which the credit of this account is pursued. This can happen from a variety of reasons, including:
- False transactions are broadcast
- Conflict equations between accounts (e.g. as a result of Relycia attacks)
- Wrong contract code or logic
Reproduction of the foundry
The following foundation shows a mistake of the inconsistency of the balance:
`Rost
MyContract {
Fn myfunction () -> bilant {
// simulate some transactions
Be tx1 = tx :: new (& [address :: from 0x123456789)), 100);
Be tx2 = tx :: new (& [address :: from 0x234567890)], 200);
fleyry_script {
vm.startbroadcast (address :: von (0x345678901)));
Vm.gather ();
Balance :: new (). Bilant () // should return the remaining amount of the caller
.if_eq (expected balance);
balance
}
}
Fn myfunction2 () -> bilant {
Be tx1 = tx :: new (& [address :: von (0x123456789)], 100);
fleyry_script {
vm.startbroadcast (address :: von (0x234567890)));
Vm.gather ();
Leave the remaining amount = expected balance;
Apply! (Balance.balance (). IF_EQ (expected balance));
compensate
}
}
Fn myfunction3 () -> bilant {
Be tx1 = tx :: new (& [address :: von (0x123456789)], 100);
fleyry_script {
vm.startbroadcast (address :: von (0x234567890)));
Vm.gather ();
Leave the remaining amount = expected balance;
Apply! (Balance.balance (). IF_EQ (expected balance));
compensate
}
}
FN expected balance () -> Balance {
// give the expected balance based on a certain logic (e.g. fixed value)
Balance :: new (). Balance ()
}
}
`
In this script we have three functions of “MyFunction”, “MyFunction2” AMyFunction3 ", which simulate transactions and monitor their balances. The error occurs when calling these functions of
Foundry_Script. In particular, the function
vm.startbroadcast ()The address calls the address as a parameter, but the caller of this function does not have the same balance.
Why does this mistake occur?
The problem occurs becauseFoundry_Script ‘expects the account to be in a certain state (e.g. it is not transferred). When calling these functions of Foundry_Script
, the address of the caller has already been broadcast. As a result, VM.Startbroadcast ()
does not return an empty address, which leads to the monitoring of false credit.
Solution: Use V.StartCall
To correct this error, we have to use “Vm.StartCall” instead of “VM.Startbroadcast”. Here is an updated script:
`Rost
MyContract {
Fn myfunction () -> bilant {
// simulate some transactions
Be tx1 = tx :: new (& [address :: von (0x123456789)], 100);
Be tx2 = tx :: new (& [address :: from 0x234567890)], 200);
vm.startcall (address :: von (0x345678901))
.Witharg (TX1)
.Witharg (TX2)
.build ().
}
Fn myfunction2 () -> bilant {
Be tx1 = tx :: new (& [address :: von (0x123456789)], 100);
vm.startcall (address :: von (0x234567890)))
.Witharg (TX1)
.build ().