-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: support for DInterfaces * support echo and stdout ifaces * Switch cli to debot dev sdk branch * Refactor supported interfaces * Add AddressInput iface * Add Terminal interface * Switch to SDK branch debot-getters * Update Terminal iface * Support for multiline input * Add Menu iface * FIx after rebase * remove printf from Terminal * Fix multiline input: Add new line char * Update ver * Fix cli after switching to master sdk
- Loading branch information
Showing
19 changed files
with
818 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::debot::term_browser::terminal_input; | ||
use crate::helpers::load_ton_address; | ||
use serde_json::Value; | ||
use ton_client::abi::Abi; | ||
use ton_client::debot::{DebotInterface, InterfaceResult}; | ||
use super::dinterface::decode_answer_id; | ||
use crate::config::Config; | ||
|
||
const ID: &'static str = "d7ed1bd8e6230871116f4522e58df0a93c5520c56f4ade23ef3d8919a984653b"; | ||
|
||
pub const ABI: &str = r#" | ||
{ | ||
"ABI version": 2, | ||
"header": ["time"], | ||
"functions": [ | ||
{ | ||
"name": "select", | ||
"inputs": [ | ||
{"name":"answerId","type":"uint32"} | ||
], | ||
"outputs": [ | ||
{"name":"value","type":"address"} | ||
] | ||
}, | ||
{ | ||
"name": "constructor", | ||
"inputs": [ | ||
], | ||
"outputs": [ | ||
] | ||
} | ||
], | ||
"data": [ | ||
], | ||
"events": [ | ||
] | ||
} | ||
"#; | ||
|
||
pub struct AddressInput { | ||
conf: Config | ||
} | ||
impl AddressInput { | ||
pub fn new(conf: Config) -> Self { | ||
Self {conf} | ||
} | ||
fn select(&self, args: &Value) -> InterfaceResult { | ||
let answer_id = decode_answer_id(args)?; | ||
let value = terminal_input("", |val| { | ||
let _ = load_ton_address(val, &self.conf).map_err(|e| format!("Invalid address: {}", e))?; | ||
Ok(()) | ||
}); | ||
Ok((answer_id, json!({ "value": value }))) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl DebotInterface for AddressInput { | ||
fn get_id(&self) -> String { | ||
ID.to_string() | ||
} | ||
|
||
fn get_abi(&self) -> Abi { | ||
Abi::Json(ABI.to_owned()) | ||
} | ||
|
||
async fn call(&self, func: &str, args: &Value) -> InterfaceResult { | ||
match func { | ||
"select" => self.select(args), | ||
_ => Err(format!("function \"{}\" is not implemented", func)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use super::address_input::AddressInput; | ||
use super::echo::Echo; | ||
use super::stdout::Stdout; | ||
use super::terminal::Terminal; | ||
use super::menu::Menu; | ||
use crate::config::Config; | ||
use crate::helpers::TonClient; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use ton_client::debot::{DebotInterface, DebotInterfaceExecutor}; | ||
|
||
pub struct SupportedInterfaces { | ||
client: TonClient, | ||
interfaces: HashMap<String, Arc<dyn DebotInterface + Send + Sync>>, | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl DebotInterfaceExecutor for SupportedInterfaces { | ||
fn get_interfaces<'a>(&'a self) -> &'a HashMap<String, Arc<dyn DebotInterface + Send + Sync>> { | ||
&self.interfaces | ||
} | ||
fn get_client(&self) -> TonClient { | ||
self.client.clone() | ||
} | ||
} | ||
|
||
impl SupportedInterfaces { | ||
pub fn new(client: TonClient, conf: &Config) -> Self { | ||
let mut interfaces = HashMap::new(); | ||
|
||
let iface: Arc<dyn DebotInterface + Send + Sync> = Arc::new(AddressInput::new(conf.clone())); | ||
interfaces.insert(iface.get_id(), iface); | ||
|
||
let iface: Arc<dyn DebotInterface + Send + Sync> = Arc::new(Stdout::new()); | ||
interfaces.insert(iface.get_id(), iface); | ||
|
||
let iface: Arc<dyn DebotInterface + Send + Sync> = Arc::new(Echo::new()); | ||
interfaces.insert(iface.get_id(), iface); | ||
|
||
let iface: Arc<dyn DebotInterface + Send + Sync> = Arc::new(Terminal::new()); | ||
interfaces.insert(iface.get_id(), iface); | ||
|
||
let iface: Arc<dyn DebotInterface + Send + Sync> = Arc::new(Menu::new()); | ||
interfaces.insert(iface.get_id(), iface); | ||
|
||
Self { client, interfaces } | ||
} | ||
} | ||
|
||
pub fn decode_answer_id(args: &Value) -> Result<u32, String> { | ||
u32::from_str_radix( | ||
args["answerId"] | ||
.as_str() | ||
.ok_or(format!("answer id not found in argument list"))?, | ||
10, | ||
) | ||
.map_err(|e| format!("{}", e)) | ||
} | ||
|
||
pub fn decode_arg(args: &Value, name: &str) -> Result<String, String> { | ||
args[name] | ||
.as_str() | ||
.ok_or(format!("\"{}\" not found", name)) | ||
.map(|x| x.to_string()) | ||
} | ||
|
||
pub fn decode_bool_arg(args: &Value, name: &str) -> Result<bool, String> { | ||
args[name] | ||
.as_bool() | ||
.ok_or(format!("\"{}\" not found", name)) | ||
} | ||
|
||
pub fn decode_string_arg(args: &Value, name: &str) -> Result<String, String> { | ||
let bytes = hex::decode(&decode_arg(args, name)?) | ||
.map_err(|e| format!("{}", e))?; | ||
std::str::from_utf8(&bytes) | ||
.map_err(|e| format!("{}", e)) | ||
.map(|x| x.to_string()) | ||
} | ||
|
||
pub fn decode_prompt(args: &Value) -> Result<String, String> { | ||
decode_string_arg(args, "prompt") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use serde_json::Value; | ||
use ton_client::debot::{DebotInterface, InterfaceResult}; | ||
use ton_client::abi::Abi; | ||
|
||
const ECHO_ID: &'static str = "f6927c0d4bdb69e1b52d27f018d156ff04152f00558042ff674f0fec32e4369d"; | ||
|
||
pub const ECHO_ABI: &str = r#" | ||
{ | ||
"ABI version": 2, | ||
"header": ["time"], | ||
"functions": [ | ||
{ | ||
"name": "echo", | ||
"inputs": [ | ||
{"name":"answerId","type":"uint32"}, | ||
{"name":"request","type":"bytes"} | ||
], | ||
"outputs": [ | ||
{"name":"response","type":"bytes"} | ||
] | ||
}, | ||
{ | ||
"name": "constructor", | ||
"inputs": [ | ||
], | ||
"outputs": [ | ||
] | ||
} | ||
], | ||
"data": [ | ||
], | ||
"events": [ | ||
] | ||
} | ||
"#; | ||
|
||
pub struct Echo {} | ||
impl Echo { | ||
|
||
pub fn new() -> Self { | ||
Self{} | ||
} | ||
|
||
fn echo(&self, args: &Value) -> InterfaceResult { | ||
let answer_id = u32::from_str_radix(args["answerId"].as_str().unwrap(), 10).unwrap(); | ||
let request_vec = hex::decode(args["request"].as_str().unwrap()).unwrap(); | ||
let request = std::str::from_utf8(&request_vec).unwrap(); | ||
Ok(( answer_id, json!({ "response": hex::encode(request.as_bytes()) }) )) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl DebotInterface for Echo { | ||
fn get_id(&self) -> String { | ||
ECHO_ID.to_string() | ||
} | ||
|
||
fn get_abi(&self) -> Abi { | ||
Abi::Json(ECHO_ABI.to_owned()) | ||
} | ||
|
||
async fn call(&self, func: &str, args: &Value) -> InterfaceResult { | ||
match func { | ||
"echo" => self.echo(args), | ||
_ => Err(format!("function \"{}\" is not implemented", func)), | ||
} | ||
} | ||
} |
Oops, something went wrong.