Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardocustodio committed Apr 28, 2024
1 parent d475cd7 commit 7ac940c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 14 deletions.
19 changes: 18 additions & 1 deletion bin/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,41 @@ async fn main() {
let (poll_job, sign_processor) = create_job_pair(
graphql_endpoint.clone(),
String::from(token.clone()),
String::from("matrix"),
Duration::from_millis(POLL_TRANSACTION_MS),
Arc::clone(&wallet_pair),
TRANSACTION_PAGE_SIZE.try_into().unwrap(),
);

let (poll_wallet_job, derive_wallet_processor) = create_wallet_job_pair(
graphql_endpoint,
graphql_endpoint.clone(),
String::from(token.clone()),
Duration::from_millis(POLL_ACCOUNT_MS),
Arc::clone(&wallet_pair),
ACCOUNT_PAGE_SIZE.try_into().unwrap(),
);

let (relay_pair, graphql, auth) =
load_relay_wallet::<DefaultConfig>(load_config().clone()).await;
let relay_pair = Arc::new(relay_pair);

let (relay_poll_job, relay_sign_processor) = create_job_pair(
graphql.clone(),
String::from(auth.clone()),
String::from("relay"),
Duration::from_millis(POLL_TRANSACTION_MS),
Arc::clone(&relay_pair),
TRANSACTION_PAGE_SIZE.try_into().unwrap(),
);

poll_job.start_job();
sign_processor.start_job();

poll_wallet_job.start_job();
derive_wallet_processor.start_job();

relay_poll_job.start_job();
relay_sign_processor.start_job();

signal::ctrl_c().await.expect("Failed to listen for ctrl c");
}
28 changes: 27 additions & 1 deletion lib/src/config_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ fn get_password(env_name: &str) -> SecretString {
let password = SecretString::new(
env::var(env_name).unwrap_or_else(|_| panic!("Password {} not loaded in memory", env_name)),
);
env::remove_var(env_name);
password
}

Expand Down Expand Up @@ -228,6 +227,33 @@ where
load_wallet_with_connections(config, context_provider, connection).await
}

pub async fn load_relay_wallet<T>(
config: Configuration,
) -> (
WalletConnectionPair<T, ChainConnector<T>, ChainConnector<T>>,
String,
String,
)
where
T: subxt::Config + Sync + Send,
T::Signature: From<<Sr25519Pair as Pair>::Signature> + Verify + From<sp_core::ecdsa::Signature>,
<T::Signature as Verify>::Signer:
From<<Sr25519Pair as Pair>::Public> + IdentifyAccount<AccountId = T::AccountId>,
T::AccountId: Into<<T as subxt::Config>::Address>
+ std::fmt::Display
+ From<subxt::sp_runtime::AccountId32>,
T::Address: From<T::AccountId> + Send + Sync,
T::Extrinsic: Send,
T::BlockNumber: Into<u64>,
{
let context_provider = Arc::new(Connector::<Client<T>, String, subxt::BasicError>::new(
config.relay_node.to_owned(),
));
let connection = Arc::clone(&context_provider);

load_wallet_with_connections(config, context_provider, connection).await
}

pub(crate) async fn load_wallet_with_connections<T, ChainConnection, ContextProvider>(
config: Configuration,
context_provider: Arc<ContextProvider>,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
mutation MarkAndListPendingTransactions($after: String, $first: Int) {
MarkAndListPendingTransactions(after: $after, first: $first) {
mutation MarkAndListPendingTransactions($network: String, $after: String, $first: Int) {
MarkAndListPendingTransactions(
network: $network
after: $after
first: $first
) {
edges {
cursor
node {
Expand Down
3 changes: 2 additions & 1 deletion lib/src/graphql_schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ type Query {
}

type Mutation {
MarkAndListPendingTransactions(after: String, first: Int): TransactionConnection

MarkAndListPendingTransactions(network: String, after: String, first: Int): TransactionConnection

UpdateTransaction(
id: Int!
Expand Down
29 changes: 21 additions & 8 deletions lib/src/jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,8 @@ pub struct PollJob<Client> {
url: Arc<String>,
/// Static token for authorization
token: Arc<String>,
///
network: Arc<String>,
/// Page size(pagination)
limit: i64,
}
Expand All @@ -343,6 +345,7 @@ pub struct PollJob<Client> {
pub fn create_job_pair<T>(
graphql_endpoint: String,
token: String,
network: String,
delay: Duration,
wallet_connection_pair: Arc<WalletConnectionPair<T, ChainConnector<T>, ChainConnector<T>>>,
page_size: i64,
Expand All @@ -364,6 +367,7 @@ pub fn create_job_pair<T>(
create_job_pair_with_executor(
graphql_endpoint,
token,
network,
delay,
wallet_connection_pair,
page_size,
Expand All @@ -375,6 +379,7 @@ pub fn create_job_pair<T>(
pub(crate) fn create_job_pair_with_executor<T, Client, ChainConnection, ContextProvider>(
graphql_endpoint: String,
token: String,
network: String,
delay: Duration,
wallet_connection_pair: Arc<WalletConnectionPair<T, ChainConnection, ContextProvider>>,
page_size: i64,
Expand Down Expand Up @@ -413,9 +418,12 @@ pub(crate) fn create_job_pair_with_executor<T, Client, ChainConnection, ContextP
let (tx, rx) = tokio::sync::mpsc::channel(50_000);
let graphql_endpoint = Arc::new(graphql_endpoint);
let token = Arc::new(token);
let network = Arc::new(network);

let poll_job = PollJob::new(
Arc::clone(&graphql_endpoint),
Arc::clone(&token),
Arc::clone(&network),
delay,
tx,
client.clone(),
Expand All @@ -441,8 +449,10 @@ where
{
#[cfg(not(tarpaulin_include))]
fn build_request(&self, limit: Option<i64>) -> Request {
let network = &*self.network;
let request_body = MarkAndListPendingTransactions::build_query(
mark_and_list_pending_transactions::Variables {
network: Some(network.clone().to_string()),
after: None,
first: limit,
},
Expand All @@ -460,6 +470,7 @@ where
pub fn new(
url: Arc<String>,
token: Arc<String>,
network: Arc<String>,
delay: Duration,
tx: Sender<Vec<SignRequest>>,
client: Arc<reqwest::Client>,
Expand All @@ -473,6 +484,7 @@ where
tx,
url,
token,
network,
limit,
}
}
Expand Down Expand Up @@ -1169,8 +1181,8 @@ where
let transaction = transaction.clone();
let wallet = &wallet_connection_pair.wallet;

/// This can be improved by making the daemon process the transactions in sequence
/// Since it process in parallel and we can have a transaction that deletes the account
// This can be improved by making the daemon process the transactions in sequence
// Since it process in parallel and we can have a transaction that deletes the account
Self::reset_wallet_nonce(wallet, &external_id).await;

let connection = &wallet_connection_pair.connection;
Expand Down Expand Up @@ -1288,7 +1300,7 @@ for SignRequest
.encoded_data
.split('x')
.nth(1)
.ok_or("No '0x' at the beggining")?;
.ok_or("No '0x' at the beginning")?;
let transaction = hex::decode(data).map_err(|e| {
tracing::error!("Error decoding: {:?}", e);
e
Expand Down Expand Up @@ -1350,12 +1362,13 @@ pub struct PaginationInput {
///
/// ## The query (Can be found in the `query_path`)
/// ```ignore
/// mutation MarkAndListPendingTransactions($after: String, $first: Int) {
/// MarkAndListPendingTransactions(after: $after, first: $first) {
/// mutation MarkAndListPendingTransactions($network: String, $after: String, $first: Int) {
/// MarkAndListPendingTransactions(network: $network, after: $after, first: $first) {
/// edges {
/// node {
/// encodedData
/// id
/// encodedData
/// network
/// wallet {
/// externalId
/// managed
Expand All @@ -1373,7 +1386,7 @@ pub struct PaginationInput {
)]
pub struct MarkAndListPendingTransactions;

/// GraphQL mutation to submit the received txHash.
/// GraphQL's mutation to submit the received txHash.
///
/// ## The query
/// ```ignore
Expand All @@ -1395,7 +1408,7 @@ pub struct MarkAndListPendingTransactions;
)]
pub struct UpdateTransaction;

/// GraphQL mutation to submit the received txHash.
/// GraphQL's mutation to submit the received txHash.
///
/// ## The query
/// ```ignore
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub(crate) use connection::{chain_connection, client_connection, connection_hand
pub(crate) use types::{SignedExtra, UncheckedExtrinsic};

pub use crate::wallet_trait::EfinityWallet;
pub use config_loader::{load_config, load_wallet};
pub use config_loader::{load_config, load_relay_wallet, load_wallet};
pub use jobs::{
create_job_pair, create_wallet_job_pair, DeriveWalletProcessor, PollJob, PollWalletJob,
SignProcessor,
Expand Down

0 comments on commit 7ac940c

Please sign in to comment.