use script! macro in a terminal script

This commit is contained in:
dr-frmr 2024-07-23 00:13:43 +03:00
parent 15557a65c7
commit bfd0267f66
No known key found for this signature in database

View File

@ -1,19 +1,16 @@
use kinode_process_lib::{await_next_message_body, call_init, eth, println, Address};
use kinode_process_lib::{eth, script, Address};
wit_bindgen::generate!({
path: "target/wit",
world: "process-v0",
});
call_init!(init);
fn init(_our: Address) {
let Ok(args) = await_next_message_body() else {
println!("failed to get args");
return;
};
// incoming args bytes are a string we parse to u64, if none provided, default to 1
let chain_id = std::str::from_utf8(&args)
script!(init);
fn init(_our: Address, args: String) -> String {
// call get_block with the chain id provided in the args
let chain_id = args
.split_whitespace()
.next()
.unwrap_or("1")
.parse::<u64>()
.unwrap_or(1);
@ -22,11 +19,7 @@ fn init(_our: Address) {
let provider = eth::Provider::new(chain_id, 5);
match provider.get_block_number() {
Ok(block_number) => {
println!("latest block number: {block_number}");
}
Err(e) => {
println!("failed to get block number: {e:?}");
}
Ok(block_number) => format!("latest block number: {block_number}"),
Err(e) => format!("failed to get block number: {e:?}"),
}
}