Ethereum Foundry script: equilibrium error
As a developer who works with the intelligent contracts of Ethereum, it is important to ensure that your scripts are correctly and precisely executed and pursue accounts. However, there is often an supervision that can lead to problems such as incompatible balance balance. In this article, we will deal with the mistake to compensate for the incompatibility in the foundry and to give guidelines for reproduction and solution.
What is an incompatibility of the balance?
Incompatibility of the balance occurs when the actual balance of a blockchain account differs from the balance, which is used by calling the function to pursue the balance of this account. This can happen for several reasons, including:
- False transactions are transferred
- Contradictory credit between accounts (e.g. due to break attacks)
- Wrong or logical contract code
Foundry script reproduction
The following Foundry script shows a balance of equilibrium compatibility:
`Rost
Mycontract {contract
Fn myfunction () -> Balance {
// simulate some transactions
Be tx1 = tx :: new (& [address :: von (0x123456789)), 100);
Se tx2 = tx :: new (& [address :: von (0x234567890)], 200);
Foundry_script {
Vm.StartBoadcast (address :: von (0x345678901)));
Vm.gather ();
Balance :: new (). Balance () // must return the caller’s credit
.If_eq (waiting -Fault);
Waited
}
}
Fn myfunction2 () -> Balance {
Se tx1 = tx :: new (& [address :: von (0x123456789)], 100);
Foundry_script {
Vm.StartBoadcast (address :: von (0x234567890);
Vm.gather ();
Leave the remaining amount = the expected;
Claim! (Balance.balance ().
balance
}
}
Fn myfunction3 () -> Balance {
Se tx1 = tx :: new (& [address :: von (0x123456789)], 100);
Foundry_script {
Vm.StartBoadcast (address :: von (0x234567890);
Vm.gather ();
Leave the remaining amount = the expected;
Claim! (Balance.balance ().
balance
}
}
FN Expendation Care () -> Balance {
// return the expected balance based on a logic (e.g. a fixed value)
Balance :: new (). Balance ()
}
}
`
In this script we have three functions “MyFunction”, “MyFunction2” and “MyFunction3”, which simulate transactions and pursue their balance. The error occurs when these functions of the Foundry_sript
occurs. In particular, the function VM.StartBoadcast ()
with an address is called up as a parameter, but the drawer of this function does not have the same balance.
Why does this mistake occur?
The question arises because the ‘Fundy_sriptexpects the account to be in a certain state (e.g. not yet transmitted). The calling address has already been transferred by calling these functions
Foundry_sript. As a result,
Vm.StartBoadcast ()does not give back an empty address, which leads to false balances.
Solution: Use VM.StartCall
To correct this error, we have to usevm.Startcallinstead of VM.StartBoadcast. Here is the updated script:
Rost
Mycontract {contract
Fn myfunction () -> Balance {
// simulate some transactions
Se tx1 = tx :: new (& [address :: von (0x123456789)], 100);
Se tx2 = tx :: new (& [address :: von (0x234567890)], 200);
Vm.startcall (address :: von (0x345678901)))))
.Withag (TX1)
. With (TX2)
.Build (). Examination ();
}
Fn myfunction2 () -> Balance {
Se tx1 = tx :: new (& [address :: von (0x123456789)], 100);
Vm.startcall (address :: von (0x234567890)))))
.Withag (TX1)
.build().
دیدگاهها