Skip to content

Commit

Permalink
Merge pull request #1 from Astraly-Labs/feat/poc
Browse files Browse the repository at this point in the history
🎉 Proof of Concept
  • Loading branch information
EvolveArt authored Oct 16, 2023
2 parents ac8e2f0 + 77ffc86 commit 8a3bf4d
Show file tree
Hide file tree
Showing 31 changed files with 2,971 additions and 247 deletions.
1,757 changes: 1,565 additions & 192 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "axum-diesel-real-world"
name = "pragma-node"
version = "0.1.0"
edition = "2021"

Expand All @@ -8,7 +8,11 @@ edition = "2021"
axum = { version = "0.6", features = ["macros"] }
axum-macros = "0.3"
chrono = { version = "0.4.26", features = ["serde"] }
diesel = { version = "2.1", features = ["postgres", "extras"] }
diesel = { version = "2.1", features = [
"postgres",
"extras",
"postgres_backend",
] }
diesel_migrations = "2"
deadpool-diesel = { version = "0.4", features = ["postgres"] }
dotenvy = "0.15"
Expand All @@ -19,3 +23,8 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
uuid = { version = "1.4", features = ["fast-rng", "v4", "serde"] }
bigdecimal = { version = "0.4.1", features = ["serde"] }
starknet = "0.6.0"
thiserror = "1.0.49"

[dev-dependencies]
rstest = "0.18.2"
7 changes: 5 additions & 2 deletions migrations/2023-10-11-223513_create_entries/up.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
-- Your SQL goes here
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

CREATE TABLE entries (
id SERIAL PRIMARY KEY,
id uuid DEFAULT uuid_generate_v4(),
pair_id VARCHAR NOT NULL,
publisher TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL,
price NUMERIC NOT NULL
price NUMERIC NOT NULL,
PRIMARY KEY (id)
)
3 changes: 3 additions & 0 deletions migrations/2023-10-12-005433_add_source/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE entries
DROP COLUMN source;
3 changes: 3 additions & 0 deletions migrations/2023-10-12-005433_add_source/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Your SQL goes here
ALTER TABLE entries
ADD COLUMN source VARCHAR NOT NULL;
2 changes: 2 additions & 0 deletions migrations/2023-10-12-232125_add_publishers_table/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This file should undo anything in `up.sql`
DROP TABLE publishers
9 changes: 9 additions & 0 deletions migrations/2023-10-12-232125_add_publishers_table/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Your SQL goes here
CREATE TABLE publishers (
id uuid DEFAULT uuid_generate_v4(),
name VARCHAR NOT NULL,
master_key VARCHAR NOT NULL,
active_key VARCHAR NOT NULL,
active BOOLEAN NOT NULL,
PRIMARY KEY (id)
);
3 changes: 3 additions & 0 deletions migrations/2023-10-16-233310_add_account_address/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
ALTER TABLE publishers
DROP COLUMN account_address;
3 changes: 3 additions & 0 deletions migrations/2023-10-16-233310_add_account_address/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- Your SQL goes here
ALTER TABLE publishers
ADD COLUMN account_address VARCHAR NOT NULL DEFAULT '';
5 changes: 5 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[toolchain]
channel = "nightly-2023-08-24"
components = ["rustfmt", "clippy", "rust-analyzer"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
20 changes: 19 additions & 1 deletion src/domain/models/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@ use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
use serde_json::json;
use starknet::core::crypto::EcdsaVerifyError;
use uuid::Uuid;

use crate::infra::errors::InfraError;

use super::publisher::PublisherError;

#[derive(Clone, Debug, PartialEq)]
pub struct EntryModel {
pub id: Uuid,
pub pair_id: String,
pub publisher: String,
pub source: String,
pub timestamp: u64,
pub price: u128,
}

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum EntryError {
#[error("internal server error")]
InternalServerError,
#[error("entry not found: {0}")]
NotFound(String),
#[error("infra error: {0}")]
InfraError(InfraError),
#[error("invalid signature")]
InvalidSignature(EcdsaVerifyError),
#[error("unauthorized request")]
Unauthorized,
#[error("publisher error: {0}")]
PublisherError(PublisherError),
}

impl IntoResponse for EntryError {
Expand All @@ -33,6 +46,11 @@ impl IntoResponse for EntryError {
StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal server error: {}", db_error),
),
Self::InvalidSignature(err) => (
StatusCode::BAD_REQUEST,
format!("Invalid signature: {}", err),
),
Self::Unauthorized => (StatusCode::UNAUTHORIZED, format!("Unauthorized publisher")),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
String::from("Internal server error"),
Expand Down
1 change: 1 addition & 0 deletions src/domain/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod entry;
pub mod publisher;
57 changes: 57 additions & 0 deletions src/domain/models/publisher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::Json;
use serde_json::json;
use uuid::Uuid;

use crate::infra::errors::InfraError;

#[derive(Clone, Debug, PartialEq)]
pub struct PublisherModel {
pub id: Uuid,
pub name: String,
pub master_key: String,
pub active_key: String,
pub account_address: String,
pub active: bool,
}

#[derive(Debug, thiserror::Error)]
pub enum PublisherError {
#[error("internal server error")]
InternalServerError,
#[error("publisher not found: {0}")]
NotFound(String),
#[error("infra error: {0}")]
InfraError(InfraError),
#[error("invalid key : {0}")]
InvalidKey(String),
#[error("invalid address : {0}")]
InvalidAddress(String),
}

impl IntoResponse for PublisherError {
fn into_response(self) -> axum::response::Response {
let (status, err_msg) = match self {
Self::NotFound(name) => (
StatusCode::NOT_FOUND,
format!("PublisherModel with name {} has not been found", name),
),
Self::InfraError(db_error) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Internal server error: {}", db_error),
),
_ => (
StatusCode::INTERNAL_SERVER_ERROR,
String::from("Internal server error"),
),
};
(
status,
Json(
json!({"resource":"PublisherModel", "message": err_msg, "happened_at" : chrono::Utc::now() }),
),
)
.into_response()
}
}
7 changes: 7 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ use axum::response::IntoResponse;
use axum::Json;
use serde_json::json;

use crate::domain::models::entry::EntryError;

#[derive(Debug)]
pub enum AppError {
InternalServerError,
BodyParsingError(String),
Entry(EntryError),
}

pub fn internal_error<E>(_err: E) -> AppError {
Expand All @@ -24,6 +27,10 @@ impl IntoResponse for AppError {
StatusCode::BAD_REQUEST,
format!("Bad request error: {}", message),
),
Self::Entry(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Entry error: {}", err),
),
};
(status, Json(json!({ "message": err_msg }))).into_response()
}
Expand Down
Loading

0 comments on commit 8a3bf4d

Please sign in to comment.