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

Pass Reference of External Call Context #1141

Merged
merged 7 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions x/programs/rust/examples/counter-external/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ use wasmlanche_sdk::{public, types::Address, Context, ExternalCallContext, Progr

#[public]
pub fn inc(_: Context, external: Program, address: Address) {
let ctx = ExternalCallContext::new(external, 1_000_000, 0);
counter::inc(ctx, address, 1);
let ctx = ExternalCallContext::new(&external, 1_000_000, 0);
counter::inc(&ctx, address, 1);
}

#[public]
pub fn get_value(_: Context, external: Program, address: Address) -> u64 {
let ctx = ExternalCallContext::new(external, 1_000_000, 0);
counter::get_value(ctx, address)
let ctx = ExternalCallContext::new(&external, 1_000_000, 0);
counter::get_value(&ctx, address)
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion x/programs/rust/sdk-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ pub fn public(_: TokenStream, item: TokenStream) -> TokenStream {
move |mut arg| {
if first {
first = false;
arg.ty = Box::new(parse_quote!(wasmlanche_sdk::ExternalCallContext));
arg.ty = Box::new(parse_quote!(&wasmlanche_sdk::ExternalCallContext));
}

FnArg::Typed(arg)
Expand Down
13 changes: 8 additions & 5 deletions x/programs/rust/wasmlanche-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,29 +91,32 @@ impl<K> BorshDeserialize for Context<K> {
}

/// Special context that is passed to external programs.
pub struct ExternalCallContext {
program: Program,
pub struct ExternalCallContext<'a> {
program: &'a Program,
max_units: Gas,
value: u64,
}

impl ExternalCallContext {
pub fn new(program: Program, max_units: Gas, value: u64) -> Self {
impl<'a> ExternalCallContext<'a> {
pub fn new(program: &'a Program, max_units: Gas, value: u64) -> Self {
Self {
program,
max_units,
value,
}
}

#[must_use]
pub fn program(&self) -> &Program {
&self.program
self.program
}

#[must_use]
pub fn max_units(&self) -> Gas {
self.max_units
}

#[must_use]
pub fn value(&self) -> u64 {
self.value
}
Expand Down
Loading