Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: send txs to rpc #51

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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