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

Generate ink traits out of contract's metadata. #53

Open
deuszx opened this issue Aug 4, 2023 · 0 comments
Open

Generate ink traits out of contract's metadata. #53

deuszx opened this issue Aug 4, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@deuszx
Copy link
Collaborator

deuszx commented Aug 4, 2023

use-ink/ink#1673 introduced macro (ink::contract_ref!) that generates a type-safe API of a contract out of its trait. The dependant trait has to be available during compilation for the macro to work. This is difficult for projects that want to depend on others without having access to all of their repository. In the future where on-chain contracts are verified, it will be easier to download contract's metadata files and generate API than pulling in all of the contracts code as our dependency.

This issue is a feature request to generate ink-compatible trait definitions out of contract's metadata.


A more concrete example: imagine a project my-contract that needs to call something with PSP22 interface. To do that right now, the project would have to look like this:

- psp22-traits
- - Cargo.toml
- - lib.rs
- my-contract
- - Cargo.toml
- - lib.rs

where psp22-traits/lib.rs defines the PSP22 trait:

#[ink::trait_definition]
trait PSP22 {
  #[ink(message)]
  pub fn balance_of(&self, account: AccountId) -> Balance 
  
  // other methods
}

my-contract/Cargo.toml needs to specify it as dependency:

psp22-traits = { path = "../psp22-traits" , default-features = false }

so that it can use type-safe API for PSP22:

let psp22_ref: ink::contract_ref!(PSP22) = (*reward_token).into();
let balance: Balance = psp22_ref.balance_of(Self::env().account_id());

If ink-wrapper was capable of generating ink trait definitions out of contract's metadata, the first part of the setup would go away and we'd be left with:

- my-contract
- - Cargo.toml
- - lib.rs
- - resources
- - - psp22_metadata.json

and in my-contract/lib.rs generate the ref

#[ink_wrapper::contract_ref("resources/psp22_metadata.json")]
mod psp22_trait;
// ...
let psp22_ref = psp22_trait::PSP22::from_account(*reward_token);
@deuszx deuszx added the enhancement New feature or request label Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant