#Cast i128 into Val

2 messages · Page 1 of 1 (latest)

eager rapids
#

I am trying to call a mint function on a token contract from another contract. While passing mint function args as :

          amount:u32) {
         let mut mint_fn_args: Vec<Val> = Vec::new(&env);
         mint_fn_args.push_back(contract_addr.to_val());
         mint_fn_args.push_back(i128::from(amount).as_val());
       
         let mint_fn : Symbol = Symbol::new(&env, "mint");
         // call mint function on token contract
         env.invoke_contract::<Val>(&token_address, &mint_fn, mint_fn_args);

}```

getting below error :
error[E0599]: no method named `as_val` found for type `i128` in the current scope
  --> src/lib.rs:51:55
   |
51 |             mint_fn_args.push_back(i128::from(amount).as_val());
   |                                                       ^^^^^^ method not found in `i128
uneven kraken
#

i128 can't be converted to Val directly. only simple types (up to 32-bit integers IIRC) can be represented with Val without involving the host. otherwise, Val represents a reference to a host object. I would give you the following recommendations on this function:

  • use into_val on tuple to quickly convert a sequence of values to a host vector, e.g. let args: Vec<Val> = (contract_addr, 123_i128).into_val(&env)
  • use i128 to represent token amounts - there is no reason to do a cast from u32
  • use token client if possible instead of invoking directly through env. env.invoke is there for dynamic dispatch (e.g. when you need to call arbitrary contract functions), here you have a well-defined interface. so you'd just call token_client.mint(recepient, amount)
  • don't forget require_auth for the token admin (test/preflight will complain about that anyway when you make this compile)