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

GetTableRowsParams fixes, endianess for 256 key for RSDK and GetBlockResult deserialization #30

Merged
merged 9 commits into from
Sep 11, 2024
12 changes: 11 additions & 1 deletion crates/antelope/src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl Display for HTTPMethod {

#[async_trait::async_trait]
pub trait Provider: Debug + Default + Sync + Send {
fn set_debug(&mut self, debug: bool);
async fn post(&self, path: String, body: Option<String>) -> Result<String, String>;
async fn get(&self, path: String) -> Result<String, String>;
}
Expand All @@ -40,7 +41,16 @@ pub struct APIClient<P: Provider> {

impl<P: Provider> APIClient<P> {
pub fn default_provider(base_url: String) -> Result<APIClient<DefaultProvider>, String> {
let provider = DefaultProvider::new(base_url).unwrap();
Self::default_provider_debug(base_url, false)
}

pub fn default_provider_debug(
base_url: String,
debug: bool,
) -> Result<APIClient<DefaultProvider>, String> {
let mut provider = DefaultProvider::new(base_url).unwrap();
provider.set_debug(debug);

APIClient::custom_provider(provider)
}

Expand Down
45 changes: 40 additions & 5 deletions crates/antelope/src/api/default_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::api::client::Provider;

#[derive(Default, Clone)]
pub struct DefaultProvider {
debug: bool,
base_url: String,
client: Client,
}
Expand All @@ -25,6 +26,7 @@ impl DefaultProvider {
let url = base_url.trim_end_matches('/');

Ok(Self {
debug: false,
base_url: String::from(url),
client: client.unwrap(),
})
Expand All @@ -40,28 +42,61 @@ impl Debug for DefaultProvider {
#[async_trait::async_trait]
impl Provider for DefaultProvider {
async fn get(&self, path: String) -> Result<String, String> {
if self.debug {
println!("GET {}", self.base_url.to_string() + &path);
}

let res = self
.client
.get(self.base_url.to_string() + &path)
.send()
.await;
if res.is_err() {
return Err(res.err().unwrap().to_string());
let res_err = res.err().unwrap().to_string();
if self.debug {
println!("Error: {}", res_err);
}

return Err(res_err);
}

Ok(res.unwrap().text().await.unwrap())
let response = res.unwrap().text().await.unwrap();
if self.debug {
println!("Response: {}", response);
}

Ok(response)
}

async fn post(&self, path: String, body: Option<String>) -> Result<String, String> {
let mut builder = self.client.post(self.base_url.to_string() + &path);
if body.is_some() {
builder = builder.body(body.unwrap());
let body_str = body.unwrap();
if self.debug {
println!("POST {} {}", self.base_url.to_string() + &path, body_str);
}

builder = builder.body(body_str);
}
let res = builder.send().await;
if res.is_err() {
return Err(res.err().unwrap().to_string());
let err_str = res.err().unwrap().to_string();
if self.debug {
println!("Error: {}", err_str);
}

return Err(err_str);
}

Ok(res.unwrap().text().await.unwrap())
let response = res.unwrap().text().await.unwrap();
if self.debug {
println!("Response: {}", response);
}

Ok(response)
}

fn set_debug(&mut self, debug: bool) {
self.debug = debug;
}
}
2 changes: 1 addition & 1 deletion crates/antelope/src/api/system/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod structs;

use std::path::Path;
use crate::api::client::{APIClient, Provider};
use crate::api::system::structs::{
CreateAccountParams, DelegateBandwidthAction, NewAccountAction, SetAbiAction, SetCodeAction,
Expand All @@ -15,6 +14,7 @@ use crate::chain::private_key::PrivateKey;
use crate::name;
use crate::serializer::Encoder;
use sha2::{Digest, Sha256};
use std::path::Path;

#[derive(Debug, Default, Clone)]
pub struct SystemAPI<T: Provider> {
Expand Down
53 changes: 48 additions & 5 deletions crates/antelope/src/api/v1/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use std::fmt::Debug;

use serde_json::{self, Value};

use crate::api::v1::structs::{ABIResponse, EncodingError, GetBlockResponse, ServerError};
use crate::api::v1::structs::{
ABIResponse, EncodingError, GetBlockResponse, GetTransactionStatusResponse, ServerError,
};
use crate::chain::checksum::{Checksum160, Checksum256};
use crate::{
api::{
client::Provider,
Expand All @@ -21,7 +24,6 @@ use crate::{
serializer::formatter::{JSONObject, ValueTo},
util::hex_to_bytes,
};
use crate::chain::checksum::{Checksum160, Checksum256};

#[derive(Debug, Default, Clone)]
pub struct ChainAPI<T: Provider> {
Expand Down Expand Up @@ -124,7 +126,7 @@ impl<T: Provider> ChainAPI<T> {
Ok(response) => {
match serde_json::from_str::<GetBlockResponse>(&response) {
Ok(block_response) => Ok(block_response),
Err(_) => {
Err(_serr) => {
// Attempt to parse the error response
match serde_json::from_str::<ErrorResponse>(&response) {
Ok(error_response) => Err(ClientError::SERVER(ServerError {
Expand Down Expand Up @@ -190,6 +192,43 @@ impl<T: Provider> ChainAPI<T> {
}
}

pub async fn get_transaction_status(
&self,
trx_id: Checksum256,
) -> Result<GetTransactionStatusResponse, ClientError<ErrorResponse>> {
let payload = serde_json::json!({
"id": trx_id,
});

let result = self
.provider
.post(
String::from("/v1/chain/get_transaction_status"),
Some(payload.to_string()),
)
.await;

match result {
Ok(response) => {
match serde_json::from_str::<GetTransactionStatusResponse>(&response) {
Ok(status_response) => Ok(status_response),
Err(_) => {
// Attempt to parse the error response
match serde_json::from_str::<ErrorResponse>(&response) {
Ok(error_response) => Err(ClientError::SERVER(ServerError {
error: error_response,
})),
Err(_) => Err(ClientError::ENCODING(EncodingError {
message: "Failed to parse JSON".into(),
})),
}
}
}
}
Err(msg) => Err(ClientError::NETWORK(msg)),
}
}

pub async fn get_table_rows<P: Packer + Default>(
&self,
params: GetTableRowsParams,
Expand Down Expand Up @@ -232,10 +271,14 @@ impl<T: Provider> ChainAPI<T> {
next_key = Some(TableIndexType::UINT128(next_key_str.parse().unwrap()));
}
Some(TableIndexType::CHECKSUM160(_)) => {
next_key = Some(TableIndexType::CHECKSUM160(Checksum160::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap()));
next_key = Some(TableIndexType::CHECKSUM160(
Checksum160::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap(),
));
}
Some(TableIndexType::CHECKSUM256(_)) => {
next_key = Some(TableIndexType::CHECKSUM256(Checksum256::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap()));
next_key = Some(TableIndexType::CHECKSUM256(
Checksum256::from_bytes(hex_to_bytes(&next_key_str).as_slice()).unwrap(),
));
}
Some(TableIndexType::FLOAT64(_)) => {
next_key = Some(TableIndexType::FLOAT64(next_key_str.parse().unwrap()));
Expand Down
Loading
Loading