Skip to content

Feat: add return type assertion #43

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

Merged
merged 5 commits into from
Sep 25, 2024
Merged

Conversation

indirection42
Copy link
Contributor

Design

  • Generate multiple instances with specific types for a single "polymorphic" XCQ program.

For example:

#[xcq_api::program]
#[xcq_api::extern_type(AssetId,AccountId,Balance)]
mod sum_balance {
    #[xcq::call_def(extension_id = 10588899351449456541u64, call_index = 1, )]
    fn balance(asset: AssetId, who: AccountId) -> Balance {}

    #[xcq::entrypoint]
    fn sum_balance(calls: Vec<BalanceCall>) -> Balance {
        let mut sum = 0;
        for call in calls {
            sum += call.call();
        }
        sum
    }
}

will generate multiple XCQ program instances including the following instance:

#[xcq_api::program]
mod sum_balance {
    #[xcq::call_def(extension_id = 10588899351449456541u64, call_index = 1)]
    fn balance(asset: u32, who: [u8; 32]) -> u64 {}
    #[xcq::entrypoint]
    fn sum_balance(calls: Vec<BalanceCall>) -> u64 {
        let mut sum = 0;
        for call in calls {
            sum += call.call();
        }
        sum
    }
}
  • Front-end codes will choose the correct prebuilt XCQ program according to the host chain's type, and construct corresponding data.

Future work:

  • A script to generate multiple instances of a single XCQ program
  • Frontend codes to detect the host chain and choose the correct XCQ program instance.

@@ -5,9 +5,9 @@ static GLOBAL: polkavm_derive::LeakingAllocator = polkavm_derive::LeakingAllocat
use alloc::vec::Vec;
#[xcq_api::program]
mod sum_balance {
#[xcq::call_def]
#[xcq::call_def(extension_id = 10588899351449456541u64, call_index = 1)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe is better to specify extension_id by hex? e.g. 0x12345u64 so that it is shorter

.segments
.last()
.ok_or_else(|| syn::Error::new_spanned(path, "expected function return type to be a path"))?;
if last_segment.ident == "u8" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use match instead of multiple if else if

@indirection42 indirection42 merged commit fb4e3d1 into master Sep 25, 2024
2 checks passed
@indirection42 indirection42 deleted the program-type-annotation branch September 25, 2024 03:57
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

Successfully merging this pull request may close these issues.

2 participants