How to convert u8 into u64 in sway v0.45?

How to convert u8 into u64 in sway v0.45?

Hello guys! In continuing to rewrite all the composability labs code on the fuel testnet beta4 toolchain I ran into another problem, in this case, it’s our predicate-based central limit orderbook spark.

We have a mechanism in CLOB to cancel orders by inputting zero tokens from the order creator with an output to the creator’s address. To test this case we need to check if the predicate creator is the owner of at least one of the inputs.

To do this, we get the total number of inputs using the function std::inputs::input_count, it returns a number of type u8. After that we check the address of the owner of the order with the address of a particular input in a loop using the function std::inputs::input_owner.

fn main() -> bool {
    let mut i = 0;
    let inputs = input_count();
    while i < inputs  {
        if input_owner(i).unwrap() == Address::from(MAKER) {
            return true;
        i += 1;

The problem is that std::inputs::input_owner accepts u64 type as an argument, and the counter i can only be of u8 type (it became mandatory with beta4). I don’t understand how to cast u8 to u64, conversion methods like as u64 or .into() don’t work

The error:

  --> /Users/alexey/Desktop/spark-rs/limit-order-predicate/src/main.sw:30:15
28 | 
29 |     let inputs = input_count();
30 |     while i < inputs  {
   |               ^^^^^^ Mismatched types.
expected: u64
found:    u8.
help: This argument's type is not castable to the declared parameter type.
31 |         if input_owner(i).unwrap() == Address::from(MAKER) {
32 |             return true;

  Aborting due to 1 error.
Error: Failed to compile limit-order-predicate

How to reproduce the problem

git clone  
cd spark-rs   
git checkout beta-4  
cd limit-order-predicate 
forc build   


Default host: aarch64-apple-darwin
fuelup home: /Users/alexey/.fuelup

installed toolchains
latest-aarch64-apple-darwin (default)

active toolchain
latest-aarch64-apple-darwin (default)
  forc : 0.44.1
    - forc-client
      - forc-deploy : 0.44.1
      - forc-run : 0.44.1
    - forc-doc : 0.44.1
    - forc-explore : 0.28.1
    - forc-fmt : 0.44.1
    - forc-index : 0.20.7
    - forc-lsp : 0.44.1
    - forc-tx : 0.44.1
    - forc-wallet : 0.3.0
  fuel-core : 0.20.4
  fuel-core-keygen : Error getting version string
  fuel-indexer : 0.20.7

fuels versions
forc : 0.45
forc-wallet : 0.45

hello :palm_tree:

Just use primitive conversions by importing form from core::primitive_conversions::*

You should be able to convert any u8 just doing .as_u64().

1 Like

@sandusky @stacio
That works
Thanks a lot guys

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.