Skip to content

Commit

Permalink
feat: get fee for transactions for stratum transcoder (#3571)
Browse files Browse the repository at this point in the history
Description
---
Adds the ability for a fee to be requested for a transaction from the console wallet via the tari stratum transcoder. It makes use of the existing grpc call get_transaction_info and essentially returns a (TxId, Fee) collection of json objects.

Motivation and Context
---
Primarily used in the Tari implementation for Miningcore.

How Has This Been Tested?
---
Manually.
  • Loading branch information
StriderDM committed Nov 16, 2021
1 parent 51b6121 commit ccf1da0
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions applications/tari_stratum_transcoder/src/proxy.rs
Expand Up @@ -480,6 +480,64 @@ impl InnerService {
proxy::json_response(StatusCode::OK, &json_response)
}

async fn handle_get_fee(
&self,
request: Request<json::Value>,
) -> Result<Response<Body>, StratumTranscoderProxyError> {
let request = request.body();
let transactions = match request["params"]["transactions"].as_array() {
Some(v) => v,
None => {
return proxy::json_response(
StatusCode::OK,
&json_rpc::error_response(
request["id"].as_i64(),
1,
"`transactions` field is empty or an invalid type for transfer request.",
None,
),
)
},
};

let mut grpc_transaction_info = Vec::new();
for transaction in transactions.iter() {
grpc_transaction_info.push(
transaction["transaction_id"]
.as_str()
.unwrap()
.to_string()
.parse::<u64>()
.unwrap(),
);
}

let mut client = self.wallet_client.clone();

let transaction_info_results = client
.get_transaction_info(grpc::GetTransactionInfoRequest {
transaction_ids: grpc_transaction_info,
})
.await?
.into_inner();
let info_results = &transaction_info_results.transactions;

let mut results = Vec::new();
for info_result in info_results.iter() {
let result = json!({
"transaction_id": info_result.tx_id,
"fee": info_result.fee,
});
results.push(result.as_object().unwrap().clone());
}

let json_response = json!({
"jsonrpc": "2.0",
"result": {"fee_results" : results},
});
proxy::json_response(StatusCode::OK, &json_response)
}

async fn handle_transfer(
&self,
request: Request<json::Value>,
Expand Down Expand Up @@ -586,6 +644,7 @@ impl InnerService {
"getlastblockheader" | "get_last_block_header" => self.handle_get_last_block_header(request).await,
"transfer" => self.handle_transfer(request).await,
"getbalance" | "get_balance" => self.handle_get_balance(request).await,
"getfee" | "get_fee" => self.handle_get_fee(request).await,
_ => {
let request = request.body();
proxy_resp = Response::new(standard_error_response(
Expand Down

0 comments on commit ccf1da0

Please sign in to comment.