Skip to content

Instantly share code, notes, and snippets.

@Mleekko
Created January 8, 2024 22:04
Show Gist options
  • Save Mleekko/4aac938b002d62165d72ded9de02f948 to your computer and use it in GitHub Desktop.
Save Mleekko/4aac938b002d62165d72ded9de02f948 to your computer and use it in GitHub Desktop.
Invoke method by nodeid from omar
<@754961345123844136> If I understood correctly, then here is an example that does what you've described:
```rs
use scrypto::prelude::*;
#[derive(Clone, Debug, ScryptoSbor)]
pub enum Invocation {
Function {
package_address: PackageAddress,
blueprint_name: String,
function_name: String,
args: (ScryptoValue,),
},
Method {
node_id: NodeId,
module_id: ModuleId,
method_name: String,
args: (ScryptoValue,),
},
}
#[blueprint]
mod dao {
struct Dao {
invocation: Invocation,
}
impl Dao {
pub fn add_voting_item(&mut self, invocation: Invocation) {
self.invocation = invocation
}
pub fn perform_invocation(&mut self) -> Vec<u8> {
match &self.invocation {
Invocation::Function {
package_address,
blueprint_name,
function_name,
args,
} => ScryptoVmV1Api::blueprint_call(
*package_address,
blueprint_name,
function_name,
scrypto_encode(&args.0).expect("Must succeed!"),
),
Invocation::Method {
node_id,
module_id,
method_name,
args,
} => {
let attached_module_id = match module_id {
ModuleId::Main => None,
ModuleId::Metadata => Some(AttachedModuleId::Metadata),
ModuleId::Royalty => Some(AttachedModuleId::Royalty),
ModuleId::RoleAssignment => Some(AttachedModuleId::RoleAssignment),
};
if let Some(attached_module_id) = attached_module_id {
ScryptoVmV1Api::object_call_module(
node_id,
attached_module_id,
method_name,
scrypto_encode(&args.0).expect("Must succeed!"),
)
} else {
ScryptoVmV1Api::object_call(
node_id,
method_name,
scrypto_encode(&args.0).expect("Must succeed!"),
)
}
}
}
}
}
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment