1

I would like to know how to achieve native token transfers between different accounts in ink? For example, I would like to initiate transfer from Alice(which will be the caller) to Bob within a method, how should I do? The transferred value should be calculated within the method which cannot be predetermined, thus cannot be included with the call and cannot be read by self.env().transferred_value(). So far I could only find self.env().transfer() for transfers from contract to an address. It should be something similar to to.call{value}(“”) in solidity.

1 Answer 1

2

If you talk about ink! that means the contract needs to be involved in the fund transfer.

  1. Alice signes the tx and includes funds with the call
  2. The contract reads the value used in tx with: self::env().transferred_value()
  3. The contract sends funds to Bob by using: self.env().transfer()

something like this code:

#[ink(message, payable)]
fn send_funds(&mut self, to: AccountId)-> Result<(), ink_env::Error>{
    let balance = Self::env().transferred_value();
    Self::env().transfer(to, balance)?;
    Ok(())
}
4
  • How transferred_value() is determined while contract contract execution?
    – Ganesh11
    Dec 11, 2022 at 2:02
  • Is there a way to initiate transfer inside the method? I mean in my project's logic, the transferred value will be calculated within the method which cannot be predetermined, thus cannot be included with the call and cannot be read by self.env().transferred_value()
    – Hikari
    Dec 11, 2022 at 11:09
  • You can only transfer value that is on the contract's account. If you add balance on the contract with some other payable method, you can still use transfer(to, balance)to send funds. To check contract balance use ink_env::balance()
    – Maario
    Dec 12, 2022 at 16:01
  • @Maario ink_env::balance(), returns executed contract balance, if we send this balance using transfer(to, ink_env::balance()), does all balance of the contract transferred? if so, will contract still exists with 0 balance?
    – Ganesh11
    Dec 13, 2022 at 1:24

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.