3

I have a question about ink!. Is it possible from a smart contract A to manage the transfer of tokens of contract B IF contract B will be deployed later ?

I want a user to call a function of smart contract A (deployed and that we can't modity), put as argument the hash of contract B and make the transfer of tokens of the contract B (from the smart contract A). And this without put use smartcontractB in the smartcontract A.

2
  • From my understanding you want to call contract A which takes as argument another contract B. Contract A will then transfer its tokens to contract B. What kind of token do you want to transfer? Apr 18, 2023 at 14:11
  • Yes, you understood correctly ! It could be ERC20 but aswell erc721 and erc 1155 ! I want it rather wide ! Does it makes sense ?
    – Matthiew
    Apr 18, 2023 at 15:12

1 Answer 1

4

Yes this is possible.

When instantiating a smart contract it gets an account id, just like a user has an account id. This means that transfering tokens to/from smart contracts works the same.

Therefore, e.g. for the ERC20 contract, you can call fn transfer with parameter to the account id of contract B.

I'm not sure if you still need contract A with this knowledge. But if you do, you can do something similar like:

        fn transfer_from_contract_a(&self, to: AccountId, value: Balance) {
            build_call::<DefaultEnvironment>()
                .call(self.erc20_contract)
                .gas_limit(0)
                .transferred_value(0)
                .call_flags(CallFlags::default().set_tail_call(true))
                .exec_input(
                    ExecutionInput::new(Selector::new(ink::selector_bytes!("transfer")))
                        .push_arg(caller),
                        .push_arg(value),
                )
                .returns::<()>()
                .try_invoke();
             .unreachable!("set_tail_call = true");

In this example the erc20 contract that it interacts with is stored in contract A. This can, however, be given as an argument as well:

        fn transfer_from_contract_a(&self, to: AccountId, value: Balance, erc20_as_argument: AccountId) {
            build_call::<DefaultEnvironment>()
                .call(erc20_as_argument)
                ...
        }

1
  • Thanks you very Daan ! It is really clear, I will implement that ! I appreciated your time :)
    – Matthiew
    Apr 18, 2023 at 17:07

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.