1

I want to use U256 in ink! but I've got some a error although I imported primitive_types/std.... how can I solve it?

use primitive_types::U256;

pub mod liquidity_num {
    pub const MIN_TICK: i32 = -887272;
    pub const MAX_TICK: i32 = -MIN_TICK;
    pub const MIN_SQRT_RATIO: u128 = 4295128739;
    pub const MAX_SQRT_RATIO: U256 = U256::from(1461446703485210103287273052203988822378723970342);
}

error

error: integer literal is too large
  --> uniswap-v3/logics/helpers/liquidity_helper.rs:13:49
   |
13 |     pub const MAX_SQRT_RATIO: U256 = U256::from(1461446703485210103287273052203988822378723970342);
   |                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0412]: cannot find type `U256` in this scope
  --> uniswap-v3/logics/helpers/liquidity_helper.rs:13:31
   |
13 |     pub const MAX_SQRT_RATIO: U256 = U256::from(1461446703485210103287273052203988822378723970342);
   |                               ^^^^ not found in this scope
   |
help: consider importing one of these items
   |
10 |     use crate::helpers::liquidity_helper::U256;
   |
10 |     use primitive_types::U256;
   |

error[E0433]: failed to resolve: use of undeclared type `U256`
  --> uniswap-v3/logics/helpers/liquidity_helper.rs:13:38
   |
13 |     pub const MAX_SQRT_RATIO: U256 = U256::from(1461446703485210103287273052203988822378723970342);
   |                                      ^^^^ not found in this scope
   |
help: consider importing one of these items
   |
10 |     use crate::helpers::liquidity_helper::U256;
   |
10 |     use primitive_types::U256;
   |

1 Answer 1

2

The largest size ink! has built in is a u128. ink! compiles to Wasm. The largest primitive Wasm has is 64bit (due to most computers using 64bit). So, there is no benefit to using any larger primitive over a collection.

From the ink! docs https://use.ink/ink-vs-solidity#uint256 the recommended way to treat with u256 is:

it is recommended to, with discretion, determine the range of the value, and choose the appropriate size (u8, u16, u32, u64, u128). If a 256-bit hash value is required, ink! has a Hash primitive available. In the event a value needs to be 256-bit, it is recommended to use an array (e.g. [u64; 4]).

See this old question on StackExchange too: Best way to handle Solidity's `uint256` in ink!

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.