Skip to content

Commit

Permalink
Merge branch 'main' into db-refine
Browse files Browse the repository at this point in the history
  • Loading branch information
fluiderson committed Sep 30, 2024
2 parents 26eeda6 + 10d616e commit f6b16cc
Show file tree
Hide file tree
Showing 35 changed files with 2,097 additions and 1,557 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust-cargo-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
access_token: ${{ github.token }}

- name: Checkout sources
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
with:
fetch-depth: 50
submodules: 'recursive'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-cargo-deny.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

steps:
- name: Checkout Sources
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0

- name: Run cargo-deny
uses: EmbarkStudios/cargo-deny-action@v1
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/rust-check-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ jobs:
override: true

- name: Checkout base
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
if: github.event_name == 'pull_request'
with:
ref: ${{ github.event.pull_request.base.ref }}
fetch-depth: 50
submodules: 'recursive'

- name: Checkout before push
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
if: github.event_name != 'pull_request'
with:
ref: ${{ github.event.before }}
Expand All @@ -53,7 +53,7 @@ jobs:
)" >> $GITHUB_ENV
- name: Checkout sources
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
with:
fetch-depth: 50
submodules: 'recursive'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
access_token: ${{ github.token }}

- name: Checkout sources
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
with:
fetch-depth: 50
submodules: 'recursive'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-fmt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
access_token: ${{ github.token }}

- name: Checkout sources
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
with:
fetch-depth: 50
submodules: 'recursive'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/rust-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
access_token: ${{ github.token }}

- name: Checkout sources
uses: actions/checkout@v4.1.1
uses: actions/checkout@v4.2.0
with:
fetch-depth: 50
submodules: 'recursive'
Expand Down
12 changes: 5 additions & 7 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "kalatori"
authors = ["Alzymologist Oy <[email protected]>"]
version = "0.2.0-rc4"
version = "0.2.1"
edition = "2021"
description = "A gateway daemon for Kalatori."
license = "GPL-3.0-or-later"
Expand Down Expand Up @@ -68,7 +68,6 @@ axum = { version = "0.7", default-features = false, features = [
] }
ureq = { version = "2", default-features = false, features = ["json"] }
axum-macros = "0.4"
primitive-types = "0.12"

substrate_parser = { git = "https://github.com/Alzymologist/substrate-parser" }
substrate-constructor = { git = "https://github.com/Alzymologist/substrate-constructor" }
Expand Down
2 changes: 1 addition & 1 deletion shoot.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
curl 127.0.0.1:16726/order/1337/price/5
curl 127.0.0.1:16726/order/1111/price/30
57 changes: 54 additions & 3 deletions src/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@
//! Contains everything related to CLI arguments, environment variables, and the config.
use crate::{
chain_wip::definitions::Decimals,
chain_wip::definitions::{H256, HEX_PREFIX},
database::definitions::{AssetId, Timestamp},
error::{ChainIntervalError, ConfigError},
error::{AccountParseError, ChainIntervalError, ConfigError},
logger,
server::definitions::new::{Decimals, SS58Prefix, SubstrateAccount},
utils::PathDisplay,
};
use clap::{Arg, ArgAction, Parser};
use serde::Deserialize;
use std::{
borrow::Cow,
env,
ffi::OsString,
ffi::{OsStr, OsString},
fmt::{Debug, Display, Formatter, Result as FmtResult},
fs::File,
io::{ErrorKind, Read, Write},
net::{IpAddr, Ipv4Addr, SocketAddr},
path::Path,
str,
sync::Arc,
};
use substrate_crypto_light::common::{AccountId32, AsBase58};
use toml_edit::de;

shadow_rs::shadow!(shadow);
Expand Down Expand Up @@ -371,3 +374,51 @@ pub struct AssetInfo {
pub name: String,
pub id: AssetId,
}

#[derive(Clone, Copy)]
pub enum Account {
Hex(AccountId32),
Substrate(SubstrateAccount),
}

impl Account {
pub fn from_os_str(string: impl AsRef<OsStr>) -> Result<Self, AccountParseError> {
let s = string.as_ref();

Ok(
if let Some(stripped) = s.as_encoded_bytes().strip_prefix(HEX_PREFIX.as_bytes()) {
H256::from_hex(stripped).map(|hash| Self::Hex(AccountId32(hash.to_be_bytes())))?
} else {
AccountId32::from_base58_string(
s.to_str().ok_or(AccountParseError::InvalidUnicode)?,
)
.map(|(account, p)| Self::Substrate(SubstrateAccount(SS58Prefix(p), account)))?
},
)
}
}

impl Display for Account {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match self {
Self::Hex(a) => Display::fmt(&H256::from_be_bytes(a.0), f),
Self::Substrate(SubstrateAccount(p, a)) => {
let s = &a.to_base58_string(p.0);

if f.alternate() {
Debug::fmt(s, f)
} else {
Display::fmt(s, f)
}
}
}
}
}

impl From<Account> for AccountId32 {
fn from(value: Account) -> Self {
match value {
Account::Hex(a) | Account::Substrate(SubstrateAccount(_, a)) => a,
}
}
}
8 changes: 5 additions & 3 deletions src/callback.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::definitions::api_v2::OrderStatus;
use crate::server::definitions::api_v2::OrderStatus;
use tokio::task;

pub const MODULE: &str = module_path!();

pub async fn callback(path: &str, order_status: OrderStatus) {
let req = ureq::post(path);
// TODO: This will be used once we setup callback functionality
#[allow(dead_code)]
pub async fn callback(path: String, order_status: OrderStatus) {
let req = ureq::post(&path);

task::spawn_blocking(move || {
let _d = req.send_json(order_status);
Expand Down
19 changes: 9 additions & 10 deletions src/chain.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
//! Everything related to the actual interaction with a blockchain.
use crate::{
arguments::Chain,
error::{ChainError, Error},
server::definitions::api_v2::OrderInfo,
signer::Signer,
state::State,
utils::task_tracker::TaskTracker,
};
use std::{collections::HashMap, sync::Arc};
use substrate_crypto_light::common::AccountId32;
use tokio::{
Expand All @@ -8,15 +16,6 @@ use tokio::{
};
use tokio_util::sync::CancellationToken;

use crate::{
arguments::Chain,
definitions::api_v2::OrderInfo,
error::{ChainError, Error},
signer::Signer,
utils::task_tracker::TaskTracker,
State,
};

pub mod definitions;
pub mod payout;
pub mod rpc;
Expand All @@ -40,7 +39,7 @@ pub struct ChainManager {

impl ChainManager {
/// Run once to start all chain connections; this should be very robust, if manager fails
/// - all modules should be restarted probably.
/// - all modules should be restarted, probably.
pub fn ignite(
chain: Vec<Chain>,
state: State,
Expand Down
30 changes: 3 additions & 27 deletions src/chain/definitions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Common objects for chain interaction system
use jsonrpsee::ws_client::WsClient;
use primitive_types::H256;
use substrate_crypto_light::common::{AccountId32, AsBase58};
use tokio::sync::oneshot;

Expand All @@ -10,35 +9,12 @@ use crate::{
rpc::{asset_balance_at_account, system_balance_at_account},
tracker::ChainWatcher,
},
chain_wip::definitions::BlockHash,
database::definitions::Timestamp,
definitions::{api_v2::OrderInfo, Balance},
error::{ChainError, NotHex},
utils::unhex,
error::ChainError,
server::definitions::{api_v2::OrderInfo, Balance},
};

/// Abstraction to distinguish block hash from many other H256 things
#[derive(Clone)]
pub struct BlockHash(pub H256);

impl BlockHash {
/// Convert block hash to RPC-friendly format
pub fn to_string(&self) -> String {
format!("0x{}", const_hex::encode(&self.0 .0.as_slice()))
}

/// Convert string returned by RPC to typesafe block
///
/// TODO: integrate nicely with serde
pub fn from_str(s: &str) -> Result<Self, crate::error::ChainError> {
let block_hash_raw = unhex(&s, NotHex::BlockHash)?;
Ok(BlockHash(H256(
block_hash_raw
.try_into()
.map_err(|_| ChainError::BlockHashLength)?,
)))
}
}

#[derive(Debug)]
pub struct EventFilter<'a> {
pub pallet: &'a str,
Expand Down
2 changes: 1 addition & 1 deletion src/chain/payout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::{
BalanceTransferConstructor,
},
},
definitions::api_v2::TokenKind,
error::ChainError,
server::definitions::api_v2::TokenKind,
signer::Signer,
state::State,
};
Expand Down
Loading

0 comments on commit f6b16cc

Please sign in to comment.