Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need an example to show how to use trait-contract as a storage in contract. #590

Closed
atenjin opened this issue Nov 22, 2020 · 4 comments
Closed

Comments

@atenjin
Copy link

atenjin commented Nov 22, 2020

Is there a way to load a contract from an AccountId directly?

Describe the details
Example delegator and multisig_plain show how to do a contract call another contract. However I'm interesting in that whether ink! allow to load a contract from an AccountId?
In example delegator, the contract call should pass the contract type, and in example multisig_plain, the contract allow to pass an AccountId, but call some function need to pass the selector and encoded params.

So is there a way to load a contract from an AccountId? I look though the code and find the trait:

pub trait FromAccountId<T>
where
    T: Environment,
{
    /// Creates the contract instance from the account ID of the already instantiated contract.
    fn from_account_id(account_id: <T as Environment>::AccountId) -> Self;
}

But there is no example to show how to use this trait. And in expanded code for a contract, this trait is not implemented for the contract.
Thus, I don't know whether this thing "load a contract from an AccountId" is allowed?
If this thing is allowed, then, how can I use it?
(
Sorry for I don't understand the doc comments in create_builder.rs file:
https://github.com/paritytech/ink/blob/3803a2662e89dfa97b6f8b17e87c0cce2d873f48/crates/env/src/call/create_builder.rs#L146-L164
)

In my mind, what I expect is like:

// load a contract from an AccountId
let erc20_instance: Erc20 = Erc20::from_account_id(account_id);

And it seems that the trait-* design like the example "trait-erc20", "trait-flipper" is a way to solve this situation? May be we need more example to show how to use trait-* contract to design a contract specification.

@atenjin
Copy link
Author

atenjin commented Nov 22, 2020

for example the trait-contract may could be used like(I guess):

    #[ink(storage)]
    pub struct ExampleContract {
        holder: Lazy<dyn BaseErc20>,
    }

to receive different erc20 implementation. (But I can't compile it now).

Thus the final thing what I expect is like the interface in solidity. The caller contract could load an Address to an interface and call the real contract.

But now I don't know how to use from_account_id to those things, and there is no example to show how to use trait-contract in a contract.

@atenjin
Copy link
Author

atenjin commented Dec 2, 2020

I think I figure out how to use FromAccountId::from_account_id.
This method is only used in the condition like delegator example.

contract A crate is depended by contract B crate, like accumulator is depended by delegator in delegator example. In B cargo.toml, the A must open feature ink-as-dependency, then in conditional compilation for A, the macro would generate the implementation FromAccountId::from_account_id for struct A, and then, in contract B, could use

            use ink_env::call::FromAccountId;
            let instance: A = A::from_account_id(address);

to load contract A from an address/account_id.

But now I still don't know how to use trait-contract as a storage.

@atenjin atenjin changed the title Is there a way to load a contract from an AccountId directly? Need an example to show how to use trait-contract as a storage in contract. Dec 2, 2020
@Robbepop
Copy link
Collaborator

Robbepop commented Dec 2, 2020

for example the trait-contract may could be used like(I guess):

    #[ink(storage)]
    pub struct ExampleContract {
        holder: Lazy<dyn BaseErc20>,
    }

to receive different erc20 implementation. (But I can't compile it now).

Thus the final thing what I expect is like the interface in solidity. The caller contract could load an Address to an interface and call the real contract.

But now I don't know how to use from_account_id to those things, and there is no example to show how to use trait-contract in a contract.

This has not yet been implemented for ink! but we are aware that this is needed and are already working out some possible designs for this feature.

@Robbepop
Copy link
Collaborator

Closed since superseeded by #631.
Please continue discussion in the linked issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants