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: get fee for transactions for stratum transcoder #3571

Merged
Changes from 1 commit
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
59 changes: 59 additions & 0 deletions applications/tari_stratum_transcoder/src/proxy.rs
Original file line number Diff line number Diff line change
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just a note that this is a situation where a Vec::with_capacity() is a slight optimization

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