Incorrect Bits And Cells Storage Constants In `gas.fc`
Introduction
The TEP-74 implementation uses precomputed constants for storage cell and bits to calculate storage fees in the gas.fc
file. However, we believe that some of these values are incorrect. In this article, we will discuss the issue, its impact, and the proposed solution.
The Issue
The incorrect values are stored in the following constants:
;;- `JETTON_WALLET_BITS` [/sandbox_tests/StateInit.spec.ts#L92](L92)
const JETTON_WALLET_BITS = 1033;
;;- `JETTON_WALLET_CELLS`: [/sandbox_tests/StateInit.spec.ts#L92](L92)
const JETTON_WALLET_CELLS = 3;
These values were calculated using the @ton/core
library, which parses the blockchain state for a particular account. However, upon closer inspection, we found that the @ton/core
sources load the storage stats differently than the code that parses the given slice.
The Correct Calculation
The correct calculation can be found in the @ton/core
sources:
// Source: https://github.com/ton-blockchain/ton/blob/24dc184a2ea67f9c47042b4104bbb4d82289fac1/crypto/block/block.tlb#L222
// storage_used$_ cells:(VarUInteger 7) bits:(VarUInteger 7)
// public_cells:(VarUInteger 7) = StorageUsed;
export function loadStorageUsed(cs: Slice): StorageUsed {
return {
cells: cs.loadVarUintBig(7),
bits: cs.loadVarUintBig(7),
publicCells: cs.loadVarUintBig(7),
}
}
As we can see, the VarUInteger
values are actually of size 7, not 3. This discrepancy leads to incorrect storage constants calculations, which eventually ends with wrong storage fees in the production contract.
Impact
The incorrect storage constants have a significant impact on the production contract. The wrong storage fees can lead to:
- Incorrect contract behavior: The contract may behave incorrectly, leading to unexpected outcomes.
- Financial losses: The incorrect storage fees can result in financial losses for users and the contract itself.
- Reputation damage: The incorrect storage fees can damage the reputation of the contract and its developers.
Conclusion
In conclusion, the incorrect bits and cells storage constants in gas.fc
are a critical issue that needs to be addressed. The proposed solution is to update the constants to reflect the correct calculation. We recommend that the developers of the TEP-74 implementation review and update the constants to ensure accurate storage fees.
Recommendations
To resolve this issue, we recommend the following:
- Update the constants: Update the
JETTON_WALLET_BITS
andJETTON_WALLET_CELLS
constants to reflect the correct calculation. - Review the code: Review the code that parses the given slice to ensure that it accurately loads the storage stats.
- Test the contract: Test the contract thoroughly to ensure that it behaves correctly and accurately calculates storage fees.
Contact Us
If you have any questions or concerns, please do not hesitate to contact us. You can reach us at here.
Related Issues
Q: What is the issue with the storage constants in gas.fc
?
A: The issue is that the storage constants JETTON_WALLET_BITS
and JETTON_WALLET_CELLS
are calculated incorrectly. The correct calculation can be found in the @ton/core
sources, which load the storage stats differently than the code that parses the given slice.
Q: What is the impact of the incorrect storage constants?
A: The incorrect storage constants can lead to incorrect contract behavior, financial losses, and reputation damage. The wrong storage fees can result in unexpected outcomes, financial losses for users and the contract itself, and damage to the reputation of the contract and its developers.
Q: How can I update the storage constants to reflect the correct calculation?
A: To update the storage constants, you need to update the JETTON_WALLET_BITS
and JETTON_WALLET_CELLS
constants to reflect the correct calculation. You can find the correct calculation in the @ton/core
sources.
Q: What is the correct calculation for the storage constants?
A: The correct calculation for the storage constants can be found in the @ton/core
sources. The loadStorageUsed
function loads the storage stats as follows:
export function loadStorageUsed(cs: Slice): StorageUsed {
return {
cells: cs.loadVarUintBig(7),
bits: cs.loadVarUintBig(7),
publicCells: cs.loadVarUintBig(7),
}
}
Q: How can I ensure that the contract behaves correctly and accurately calculates storage fees?
A: To ensure that the contract behaves correctly and accurately calculates storage fees, you need to review the code that parses the given slice to ensure that it accurately loads the storage stats. You also need to test the contract thoroughly to ensure that it behaves correctly and accurately calculates storage fees.
Q: What are the recommended steps to resolve this issue?
A: The recommended steps to resolve this issue are:
- Update the constants to reflect the correct calculation.
- Review the code that parses the given slice to ensure that it accurately loads the storage stats.
- Test the contract thoroughly to ensure that it behaves correctly and accurately calculates storage fees.
Q: Where can I find more information about this issue?
A: You can find more information about this issue in the following resources:
- This issue in
stablecoin-contract
. - This article about the incorrect bits and cells storage constants in
gas.fc
. - This FAQ about the issue.
Q: How can I contact the developers of the TEP-74 implementation?
A: You can contact the developers of the TEP-74 implementation at here.