0

I'm working on a smart contract designed to facilitate swaps, and given that values in Solidity are expressed in wei, I encounter exceedingly large numbers. Even when using uint256, I've run into integer overflow errors.

I'm currently employing Solidity version 0.8.0, which has integrated SafeMath functionalities.

How can I efficiently store and handle numbers larger than the uint256 capacity in Solidity?

2
  • Typical data structure you would use is a linked list, i.e. each node has a uint256 value and points to another node with uint256 value. In solidity, you could implement this by having two mappings: 1) a mapping of address to address. 2) a mapping of address to uint. A linked list would then be given by one address. You would look this up in the first mapping to get the next "node". The node's value is looked-up in the second mapping. Jan 1, 2022 at 23:55
  • You might need to use a fixed-point math library. I recommend taking a look at PRBMath. Sep 23, 2022 at 10:48

2 Answers 2

0

Normally uint256 are big enough for everything. The maximum you can represent is 2^256 which should be sufficient to represent any swap.

If you want to represent larger numbers you can use a combination of multiple bytes32 but this would mean that you have to implement your own math for it.

Can you share code where you get an overflow? Then it is easier to provide a detailed answer.

0

If you need to store integers larger than can be represented with uint256, one option is to use the solidity-BigNumber library.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.