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

Ability to perform operations on TransactionData #5

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
max_width = 80
tab_spaces = 2
edition = "2018"
215 changes: 110 additions & 105 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use arweave_crypto::pkey::PrivateKey;
use arweave_crypto::Driver;
use arweave_crypto::PrivateKey;
use async_trait::async_trait;
use pretend::pretend;
use pretend::resolver::UrlResolver;
Expand All @@ -10,159 +10,164 @@ pub use pretend::Url;
use pretend_reqwest::Client as HttpClient;
use serde::Deserialize;

pub mod tx;

#[derive(Deserialize, Debug)]
pub struct NetworkInfo {
pub network: String,
pub version: usize,
pub release: usize,
pub height: usize,
pub current: String,
pub blocks: usize,
pub peers: usize,
pub queue_length: usize,
pub node_state_latency: usize,
pub network: String,
pub version: usize,
pub release: usize,
pub height: usize,
pub current: String,
pub blocks: usize,
pub peers: usize,
pub queue_length: usize,
pub node_state_latency: usize,
}

#[derive(Deserialize, Debug)]
pub struct Tag {
pub name: String,
pub value: String,
pub name: String,
pub value: String,
}

#[derive(Deserialize, Debug)]
pub struct TransactionData {
pub format: usize,
pub id: String,
pub last_tx: String,
pub owner: String,
pub tags: Vec<Tag>,
pub target: String,
pub quantity: String,
pub data: Vec<u8>,
pub reward: String,
pub signature: String,
pub data_size: String,
pub data_root: String,
pub format: usize,
pub id: String,
pub last_tx: String,
pub owner: String,
pub tags: Vec<Tag>,
pub target: String,
pub quantity: String,
pub data: Vec<u8>,
pub reward: String,
pub signature: String,
pub data_size: String,
pub data_root: String,
}

#[derive(Deserialize, Debug)]
pub struct TransactionConfirmedData {
block_indep_hash: String,
block_height: usize,
number_of_confirmations: usize,
block_indep_hash: String,
block_height: usize,
number_of_confirmations: usize,
}

#[derive(Deserialize, Debug)]
pub struct TransactionStatusResponse {
status: usize,
confirmed: Option<TransactionConfirmedData>,
status: usize,
confirmed: Option<TransactionConfirmedData>,
}

#[pretend]
trait ArweaveHttp {
// Network
#[request(method = "GET", path = "/info")]
async fn network_info(&self) -> Result<JsonResult<NetworkInfo, ()>>;
// Network
#[request(method = "GET", path = "/info")]
async fn network_info(&self) -> Result<JsonResult<NetworkInfo, ()>>;

#[request(method = "GET", path = "/peers")]
async fn peer_info(&self) -> Result<JsonResult<Vec<String>, ()>>;
#[request(method = "GET", path = "/peers")]
async fn peer_info(&self) -> Result<JsonResult<Vec<String>, ()>>;

// Transaction
#[request(method = "GET", path = "/price/{byte_size}")]
async fn tx_get_price(&self, byte_size: &str) -> Result<String>;
// Transaction
#[request(method = "GET", path = "/price/{byte_size}")]
async fn tx_get_price(&self, byte_size: &str) -> Result<String>;

#[request(method = "GET", path = "/tx/{id}")]
async fn tx_get(&self, id: &str) -> Result<JsonResult<TransactionData, ()>>;
#[request(method = "GET", path = "/tx/{id}")]
async fn tx_get(&self, id: &str) -> Result<JsonResult<TransactionData, ()>>;

#[request(method = "GET", path = "/tx/{id}/status")]
async fn tx_status(&self, id: &str) -> Result<JsonResult<TransactionStatusResponse, ()>>;
#[request(method = "GET", path = "/tx/{id}/status")]
async fn tx_status(
&self,
id: &str,
) -> Result<JsonResult<TransactionStatusResponse, ()>>;

// Wallet
#[request(method = "GET", path = "/wallet/{address}/balance")]
async fn wallet_balance(&self, address: &str) -> Result<String>;
// Wallet
#[request(method = "GET", path = "/wallet/{address}/balance")]
async fn wallet_balance(&self, address: &str) -> Result<String>;

#[request(method = "GET", path = "/wallet/{address}/last_tx")]
async fn wallet_last_tx_id(&self, address: &str) -> Result<String>;
#[request(method = "GET", path = "/wallet/{address}/last_tx")]
async fn wallet_last_tx_id(&self, address: &str) -> Result<String>;
}

pub struct Client(Pretend<HttpClient, UrlResolver>);

impl Client {
pub fn new(url: Url) -> Self {
let client = HttpClient::default();
let pretend = Pretend::for_client(client).with_url(url);
Self(pretend)
}

pub async fn network_info(&self) -> Result<NetworkInfo> {
let response = self.0.network_info().await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
pub fn new(url: Url) -> Self {
let client = HttpClient::default();
let pretend = Pretend::for_client(client).with_url(url);
Self(pretend)
}

pub async fn network_info(&self) -> Result<NetworkInfo> {
let response = self.0.network_info().await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
}

pub async fn peer_info(&self) -> Result<Vec<String>> {
let response = self.0.peer_info().await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
pub async fn peer_info(&self) -> Result<Vec<String>> {
let response = self.0.peer_info().await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
}
}

#[async_trait]
pub trait TxClient {
async fn get_price(&self, byte_size: &str) -> Result<String>;
async fn get(&self, id: &str) -> Result<TransactionData>;
async fn get_status(&self, id: &str) -> Result<TransactionStatusResponse>;
async fn get_price(&self, byte_size: &str) -> Result<String>;
async fn get(&self, id: &str) -> Result<TransactionData>;
async fn get_status(&self, id: &str) -> Result<TransactionStatusResponse>;
}

#[async_trait]
impl TxClient for Client {
async fn get_price(&self, byte_size: &str) -> Result<String> {
let response = self.0.tx_get_price(byte_size).await?;
Ok(response)
async fn get_price(&self, byte_size: &str) -> Result<String> {
let response = self.0.tx_get_price(byte_size).await?;
Ok(response)
}

async fn get(&self, id: &str) -> Result<TransactionData> {
let response = self.0.tx_get(id).await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
}

async fn get(&self, id: &str) -> Result<TransactionData> {
let response = self.0.tx_get(id).await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
}

async fn get_status(&self, id: &str) -> Result<TransactionStatusResponse> {
let response = self.0.tx_status(id).await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
async fn get_status(&self, id: &str) -> Result<TransactionStatusResponse> {
let response = self.0.tx_status(id).await?;
match response {
JsonResult::Ok(n) => Ok(n),
JsonResult::Err(_) => todo!(),
}
}
}

#[cfg(test)]
mod tests {
use super::Client;
use super::Url;
use tokio_test::block_on;

#[test]
fn test_network_info() {
let url = Url::parse("https://arweave.net/").unwrap();
let client = Client::new(url);
let network_info = block_on(client.network_info()).unwrap();

assert_eq!(network_info.network, "arweave.N.1".to_string());
}

#[test]
fn test_peer_info() {
let url = Url::parse("https://arweave.net/").unwrap();
let client = Client::new(url);
let peer_info = block_on(client.peer_info()).unwrap();

assert!(peer_info.len() > 0);
}
use super::Client;
use super::Url;
use tokio_test::block_on;

#[test]
fn test_network_info() {
let url = Url::parse("https://arweave.net/").unwrap();
let client = Client::new(url);
let network_info = block_on(client.network_info()).unwrap();

assert_eq!(network_info.network, "arweave.N.1".to_string());
}

#[test]
fn test_peer_info() {
let url = Url::parse("https://arweave.net/").unwrap();
let client = Client::new(url);
let peer_info = block_on(client.peer_info()).unwrap();

assert!(peer_info.len() > 0);
}
}
19 changes: 19 additions & 0 deletions core/src/tx.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use crate::TransactionData;

pub struct Transaction {
inner: TransactionData,
}

impl Transaction {
pub fn new(tx_data: TransactionData) -> Self {
Self { inner: tx_data }
}

// pub fn add_tag(&mut self, tag: Tag) {
// self.0.tags.push(tag);
// }

pub fn set_owner(&mut self, owner: String) {
self.inner.owner = owner;
}
}
Loading