Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
37ng committed Jan 19, 2024
1 parent 7f93c79 commit f47b404
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 6 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
WALLET_ADDRESS="0x"
2 changes: 2 additions & 0 deletions xps-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jsonrpsee = { version = "0.21", features = ["macros", "server", "client-core"] }
anyhow = "1"
thiserror = "1"
ctor = "0.2"
dotenvy = "0.15.7"
envy = "0.4.2"

[dev-dependencies]
jsonrpsee = { version = "0.21", features = ["macros", "server", "client"] }
Expand Down
26 changes: 25 additions & 1 deletion xps-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,43 @@ mod util;

use anyhow::Result;
use jsonrpsee::server::Server;
use serde::Deserialize;

pub use crate::rpc::{XpsMethods, XpsServer};

pub const SERVER_HOST: &str = "127.0.0.1:0";
pub const DEFAULT_WALLET_ADDRESS: &str = "0x0000000000000000000000000000000000000000";

#[derive(Deserialize)]

Check warning on line 14 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L14

Added line #L14 was not covered by tests
struct XpsGatewayOptions {
/// wallet address
#[serde(default = "default_wallet_address")]
wallet_address: String,
}

fn default_wallet_address() -> String {
DEFAULT_WALLET_ADDRESS.to_string()
}

Check warning on line 23 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L21-L23

Added lines #L21 - L23 were not covered by tests

/// Entrypoint for the xps Gateway
pub async fn run() -> Result<()> {
crate::util::init_logging();
match dotenvy::dotenv() {
Ok(path) => {

Check warning on line 29 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L28-L29

Added lines #L28 - L29 were not covered by tests
// .env file successfully loaded.
log::debug!("Env file {} was loaded successfully", path.display());

Check warning on line 31 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L31

Added line #L31 was not covered by tests
}
Err(err) => {

Check warning on line 33 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L33

Added line #L33 was not covered by tests
// Error handling for the case where dotenv() fails
log::info!("Unable to load env file(s) : {err}");

Check warning on line 35 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L35

Added line #L35 was not covered by tests
}
}
let opts: XpsGatewayOptions = envy::from_env()?;

Check warning on line 38 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L38

Added line #L38 was not covered by tests

// a port of 0 allows the OS to choose an open port
let server = Server::builder().build(SERVER_HOST).await?;
let addr = server.local_addr()?;
let handle = server.start(rpc::XpsMethods.into_rpc());
let handle = server.start(XpsMethods::new(opts.wallet_address.as_str()).into_rpc());

Check warning on line 43 in xps-gateway/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/lib.rs#L43

Added line #L43 was not covered by tests

log::info!("Server Started at {addr}");
handle.stopped().await;
Expand Down
3 changes: 3 additions & 0 deletions xps-gateway/src/rpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,7 @@ pub trait Xps {
/// - The system should have proper error handling to deal with invalid requests, unauthorized access, and other potential issues.
#[method(name = "status")]
async fn status(&self) -> Result<String, ErrorObjectOwned>;

#[method(name = "walletAddress")]
async fn wallet_address(&self) -> Result<String, ErrorObjectOwned>;
}
17 changes: 13 additions & 4 deletions xps-gateway/src/rpc/methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@
use super::api::*;
use jsonrpsee::types::error::ErrorCode;

use super::super::DEFAULT_WALLET_ADDRESS;
use async_trait::async_trait;
use jsonrpsee::types::ErrorObjectOwned;

use crate::types::Message;

/// Gateway Methods for XPS
pub struct XpsMethods;
pub struct XpsMethods {
pub wallet_address: String,
}

impl XpsMethods {
/// Create a new instance of the XpsMethods struct
pub fn new() -> Self {
Self {}
pub fn new(wallet_address: &str) -> Self {
Self {
wallet_address: wallet_address.to_string(),
}
}
}

impl Default for XpsMethods {
fn default() -> Self {
Self::new()
Self::new(DEFAULT_WALLET_ADDRESS)

Check warning on line 28 in xps-gateway/src/rpc/methods.rs

View check run for this annotation

Codecov / codecov/patch

xps-gateway/src/rpc/methods.rs#L28

Added line #L28 was not covered by tests
}
}

Expand All @@ -36,4 +41,8 @@ impl XpsServer for XpsMethods {
log::debug!("xps_status called");
Ok("OK".to_string())
}

async fn wallet_address(&self) -> Result<String, ErrorObjectOwned> {
Ok(self.wallet_address.clone())
}
}
13 changes: 12 additions & 1 deletion xps-gateway/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use tokio::time::timeout as timeout_tokio;
use xps_gateway::{rpc::XpsClient, types::Message, XpsMethods, XpsServer, SERVER_HOST};

const TEST_TIMEOUT: Duration = Duration::from_secs(10);
const TEST_WALLET_ADDRESS: &str = "0x000000000000000000000000000000000000dEaD";

#[cfg(test)]
mod it {
Expand Down Expand Up @@ -42,6 +43,16 @@ mod it {
})
.await
}

#[tokio::test]
async fn test_wallet_address() -> Result<(), Error> {
with_xps_client(None, |client| async move {
let result = client.wallet_address().await?;
assert_eq!(result, TEST_WALLET_ADDRESS);
Ok(())
})
.await
}
}

async fn with_xps_client<F, R, T>(timeout: Option<Duration>, f: F) -> Result<T, Error>
Expand All @@ -51,7 +62,7 @@ where
{
let server = Server::builder().build(SERVER_HOST).await.unwrap();
let addr = server.local_addr().unwrap();
let handle = server.start(XpsMethods::new().into_rpc());
let handle = server.start(XpsMethods::new(TEST_WALLET_ADDRESS).into_rpc());
let client = WsClientBuilder::default()
.build(&format!("ws://{addr}"))
.await
Expand Down

0 comments on commit f47b404

Please sign in to comment.