Skip to content

Commit

Permalink
Merge pull request #51 from near/daniyar/send-transactions
Browse files Browse the repository at this point in the history
feat: send txs to rpc
  • Loading branch information
volovyks authored Apr 14, 2023
2 parents e59211e + 7dd503d commit 83b5b7c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
47 changes: 45 additions & 2 deletions mpc-recovery/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use near_jsonrpc_client::{methods, JsonRpcClient};
use near_jsonrpc_client::{methods, JsonRpcClient, MethodCallResult};
use near_jsonrpc_primitives::types::query::QueryResponseKind;
use near_primitives::hash::CryptoHash;
use near_primitives::transaction::SignedTransaction;
use near_primitives::types::{AccountId, Finality};
use near_primitives::views::{AccessKeyView, QueryRequest};
use near_primitives::views::{AccessKeyView, FinalExecutionOutcomeView, QueryRequest};

#[derive(Clone)]
pub struct NearRpcClient {
Expand Down Expand Up @@ -41,6 +42,35 @@ impl NearRpcClient {
}
}

async fn query_broadcast_tx(
&self,
method: &methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest,
) -> MethodCallResult<
FinalExecutionOutcomeView,
near_jsonrpc_primitives::types::transactions::RpcTransactionError,
> {
let result = self.rpc_client.call(method).await;
match &result {
Ok(response) => {
tracing::debug!(
target: "client",
"Submitting transaction with actions {:?} succeeded with status {:?}",
method.signed_transaction.transaction.actions,
response.status
);
}
Err(error) => {
tracing::error!(
target: "client",
"Calling RPC method {:?} resulted in error {:?}",
method,
error
);
}
};
result
}

pub async fn access_key_nonce(
&self,
account_id: AccountId,
Expand All @@ -59,6 +89,19 @@ impl NearRpcClient {
.await?;
Ok(block_view.header.hash)
}

pub async fn send_tx(
&self,
signed_transaction: SignedTransaction,
) -> anyhow::Result<FinalExecutionOutcomeView> {
let result = self
.query_broadcast_tx(&methods::broadcast_tx_commit::RpcBroadcastTxCommitRequest {
signed_transaction,
})
.await?;

Ok(result)
}
}

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions mpc-recovery/src/leader_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ async fn process_new_account(
);

// Sign the transaction
let _signed_create_acc_tx =
let signed_create_acc_tx =
sign_transaction(create_acc_tx, account_creator_id, account_creator_sk);

//TODO: Send transaction to the relayer
state.client.send_tx(signed_create_acc_tx).await?;

Ok((StatusCode::OK, Json(NewAccountResponse::Ok)))
}
Expand Down Expand Up @@ -200,9 +200,9 @@ async fn process_add_key(
// Sign the transaction
// TODO: use key derivation or other techniques to generate a key
let mpc_recovery_user_sk: SecretKey = "".parse().unwrap();
let _signed_add_key_tx = sign_transaction(add_key_tx, user_account_id, mpc_recovery_user_sk);
let signed_add_key_tx = sign_transaction(add_key_tx, user_account_id, mpc_recovery_user_sk);

//TODO: Send transaction to the relayer
state.client.send_tx(signed_add_key_tx).await?;

Ok((StatusCode::OK, Json(AddKeyResponse::Ok)))
}
Expand Down

0 comments on commit 83b5b7c

Please sign in to comment.