How to Handle and Prevent ‘BalanceOverflow’ Errors in Sway Contracts?

I have been developing a smart contract on the Fuel Network using the Sway language and encountered an issue related to balance overflow (BalanceOverflow). During the execution of the contract, when trying to store an asset amount exceeding the maximum value of u64, the contract triggered a BalanceOverflow error, leading to the transaction being reverted.

Here is the specific error message:

Error: Transaction(Reverted { reason: "BalanceOverflow", revert_id: 0, receipts: [Panic { id: 0000000000000000000000000000000000000000000000000000000000000000, reason: PanicInstruction { reason: BalanceOverflow, instruction: CALL { target_struct: 0x10, fwd_coins: 0x11, asset_id_addr: 0x12, fwd_gas: 0x13 } (bytes: 2d 41 14 93) }, pc: 10392, is: 10368, contract_id: None }, ScriptResult { result: Panic, gas_used: 27232 }] })

The scenario involved attempting to increase a user’s token count to a value greater than u64::MAX. I understand that u64::MAX is 18,446,744,073,709,551,615, which obviously exceeded in my operations.

I would like to ask the community:

1.	Are there any standard methods or best practices for handling this type of overflow issue?
2.	How can we effectively prevent such issues in contract logic?

Can you share the repo or codebase to reproduce this?
also, please share the screenshot of fuelup show from your terminal?

    fn mint_asset_to_self()  {
        mint(ZERO_B256, u64::max());
    }

When I run the tests and try to call the function twice, I receive an error message.

Error: Transaction(Reverted { reason: "BalanceOverflow", revert_id: 0, receipts: [Call { id: 0000000000000000000000000000000000000000000000000000000000000000, to: 2f12797ff2b53bc474f479c972d907b59bfdf580237d9c5f582b5b658be8c1ad, amount: 0, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, gas: 858, param1: 10480, param2: 10493, pc: 11752, is: 11752 }, Panic { id: 2f12797ff2b53bc474f479c972d907b59bfdf580237d9c5f582b5b658be8c1ad, reason: PanicInstruction { reason: BalanceOverflow, instruction: MINT { amount: 0x10, sub_id_addr: 0x11 } (bytes: 35 41 10 00) }, pc: 19112, is: 11752, contract_id: None }, ScriptResult { result: Panic, gas_used: 1253 }] })
test can_get_contract_id ... FAILED

So, I assume that the amount of assets that can be stored in a contract is limited to u64::max(). Could this limitation potentially lead to undesirable outcomes for certain products, such as a dex

would you mind sharing the complete codebase or at least the whole contract for me to debug? also, an output for the command fuelup show would be required to help me to debug

Hey @0x0077, it’s unsupported. This is a VM limitation afaik, so if it’s intentional you can’t use native assets. You can use contracts for that instead, like you would in Ethereum.