Skip to content

Commit 2d97629

Browse files
committed
block_txs:block_start_index
1 parent 2634175 commit 2d97629

File tree

3 files changed

+86
-36
lines changed

3 files changed

+86
-36
lines changed

src/args.rs

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ use std::process;
66

77
use crate::blocking;
88

9+
/// mempool-space_ARG
10+
/// mempool-space --arg
11+
/// mempool-space_ARG_STRING
12+
/// mempool-space --arg_string
913
pub fn generic_sys_call(option: &str, sub_string: &str) {
1014
use std::process::Command;
1115

@@ -33,36 +37,35 @@ pub fn generic_sys_call(option: &str, sub_string: &str) {
3337
print!("{}", result);
3438
}
3539
}
36-
40+
/// GET /api/v1/historical-price?currency=CURRENCY&timestamp=TIMESTAMP
3741
/// <https://mempool.space/docs/api/rest#get-historical-price>
3842
pub fn historical_price(currency: &str, timestamp: &str) {
39-
//REF: mempool-space --historical_price --currency EUR --timestamp 150000000
40-
//EXPECT: {"prices":[{"time":1279497600,"EUR":0,"USD":0}],"exchangeRates":{"USDEUR":0.92,"USDGBP":0.78,"USDCAD":1.38,"USDCHF":0.87,"USDAUD":1.52,"USDJPY":146.79}}
4143
let _res = blocking(&format!(
4244
"v1/historical-price?currency={}&timestamp={}",
4345
&format!("{:}", &currency),
4446
&format!("{:}", &timestamp)
4547
));
4648
}
47-
//GET /api/block/:hash/txid/:index
48-
//https://mempool.space/docs/api/rest#get-block-transaction-id
49-
pub fn block_tx_id(block_hash: &str, txindex: &str) {
50-
//REF: mempool-space --block_txid 000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce --txindex 218
51-
//EXPECT: 0fa6da60e484941f255cbb025c3d6440e5a7e970119e899b4065c7999360e406
49+
/// GET /api/block/:hash/txid/:index
50+
/// <https://mempool.space/docs/api/rest#get-block-transaction-id>
51+
pub fn block_txid(block_hash: &str, txindex: &str) {
5252
let _res = blocking(&format!("block/{}/txid/{}", block_hash, txindex));
5353
}
54-
//GET /api/block/:hash/txids
55-
//https://mempool.space/docs/api/rest#get-block-transaction-ids
56-
pub fn block_tx_ids(block_hash: &str) {
57-
//REF: mempool-space --block_txids 000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce
58-
//EXPECT:
59-
// [
60-
// "cfe624ccdd8010cf78dbedd1b25e1ff601b470c4d7d90fa9fc8c1bcc5cdc6e0e",
61-
// "a5ef89881bd5103f223a0fa285dfc75f4718974cb792cf85e623a7de05801bc9",
62-
// ...,
63-
// ]
54+
/// GET /api/block/:hash/txids
55+
/// <https://mempool.space/docs/api/rest#get-block-transaction-ids>
56+
pub fn block_txids(block_hash: &str) {
6457
let _res = blocking(&format!("block/{}/txids", block_hash));
6558
}
59+
/// GET /api/block/:hash/txs[/:start_index] (start_index % 25 = 0)
60+
/// <https://mempool.space/docs/api/rest#get-block-transactions>
61+
pub fn block_txs(block_hash: &str, start_index: &str) {
62+
let start_index_int = start_index.parse::<i32>().unwrap_or(0);
63+
if start_index_int % 25 == 0 {
64+
let _res = blocking(&format!("block/{}/txs/{}", block_hash, start_index));
65+
} else {
66+
let _res = blocking(&format!("block/{}/txs/{}", block_hash, &"0"));
67+
}
68+
}
6669

6770
/// <https://mempool.space/docs/api/rest>
6871
/// - [API/REST](https://mempool.space/docs/api/rest)
@@ -153,6 +156,8 @@ pub struct Args {
153156
/// - BLOCK <BLOCK_HASH> <TXS>
154157
/// `https://mempool.space/api/block/<BLOCK_HASH>/txs`
155158
pub block_txs: Option<String>,
159+
pub block_start_index: Option<String>,
160+
156161
/// - V1 BLOCKS <BLOCK_HEIGHT>
157162
/// `https://mempool.space/api/v1/blocks/<BLOCK_HEIGHT>`
158163
pub blocks: Option<String>,
@@ -244,9 +249,11 @@ impl Args {
244249

245250
opts.optopt("", "block_txid", "block txid api call", "BLOCK_TXID");
246251
opts.optopt("", "block_txindex", "block_txindex api call", "BLOCK_TXINDEX");
247-
248252
opts.optopt("", "block_txids", "block txids api call", "BLOCK_TXIDS");
249-
opts.optopt("", "block_txs", "block txids api call", "BLOCK_TXS");
253+
254+
opts.optopt("", "block_txs", "block txs api call", "BLOCK_TXS");
255+
opts.optopt("", "block_start_index", "block txs api call", "BLOCK_START_INDEX");
256+
250257
opts.optopt("", "blocks", "block txids api call", "BLOCKS");
251258
opts.optopt("", "blocks_bulk", "block txids api call", "BLOCKS_BULK");
252259

@@ -367,16 +374,23 @@ impl Args {
367374
std::process::exit(0);
368375
}
369376
if matches.opt_present("block_txid") {
370-
let block_txid = matches.opt_str("block_txid"); //expect a block_hash
371-
let block_txindex = matches.opt_str("block_txindex");
372-
block_tx_id(&block_txid.unwrap(), &block_txindex.unwrap());
377+
let arg_block_txid = matches.opt_str("block_txid"); //expect a block_hash
378+
let arg_block_txindex = matches.opt_str("block_txindex");
379+
block_txid(&arg_block_txid.unwrap(), &arg_block_txindex.unwrap());
373380
std::process::exit(0);
374381
}
375382
if matches.opt_present("block_txids") {
376-
let block_txids = matches.opt_str("block_txids"); //expect a block_hash
377-
block_tx_ids(&block_txids.unwrap());
383+
let arg_block_txids = matches.opt_str("block_txids"); //expect a block_hash
384+
block_txids(&arg_block_txids.unwrap());
378385
std::process::exit(0);
379386
}
387+
if matches.opt_present("block_txs") {
388+
let arg_block_txs = matches.opt_str("block_txs"); //expect a block_hash
389+
let arg_block_start_index = matches.opt_str("block_start_index");
390+
block_txs(&arg_block_txs.unwrap(), &arg_block_start_index.unwrap());
391+
std::process::exit(0);
392+
}
393+
380394
if matches.opt_present("h")
381395
|| (matches.free.is_empty()
382396
&& !matches.opt_present("u")
@@ -473,6 +487,7 @@ impl Args {
473487
// BLOCK BLOCK_HASH TXS
474488
// https://mempool.space/api/block/<block_hash>/<txs>
475489
block_txs: matches.opt_str("block_txs"),
490+
block_start_index: matches.opt_str("block_start_index"),
476491

477492
// V1 BLOCKS
478493
// https://mempool.space/api/v1/blocks/<BLOCK_HEIGHT>"

src/bin/mempool-space_block_txs.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use mempool_space::blocking;
2+
use std::env;
3+
4+
fn main() {
5+
{
6+
let args: Vec<String> = env::args().collect();
7+
let mut block = &String::from("");
8+
if args.len() > 1 {
9+
block = &args[1];
10+
} else {
11+
// silence is golden
12+
std::process::exit(0);
13+
}
14+
let _res = blocking(&format!("block/{}/txs", &block));
15+
}
16+
}

src/lib.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,13 @@ mod tests {
372372
// GET /api/block/:hash/txid/:index
373373
let binding =
374374
format!("block/000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txid/218").clone();
375-
let block_txid: &str = blocking(&binding).expect("returns current txid from block index");
375+
let get_block_txid: &str = blocking(&binding).expect("returns current txid from block index");
376376
let get_block_txid = generic_sys_call(
377377
"block_txid",
378378
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txid/218",
379379
);
380-
use crate::args::block_tx_id;
381-
let _ = block_tx_id(
380+
use crate::args::block_txid;
381+
block_txid(
382382
&"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce",
383383
&"218",
384384
);
@@ -387,21 +387,40 @@ mod tests {
387387
#[test]
388388
fn test_block_txids() {
389389
// GET /api/block/:hash/txids
390-
let binding =
391-
format!("block/000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txids").clone();
392-
let block_txid: &str = blocking(&binding).expect("returns current txids from block");
390+
let binding = format!("block/000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txids").clone();
391+
let get_block_txid: &str = blocking(&binding).expect("returns current txids from block");
393392
let get_block_txids = generic_sys_call(
394393
"block_txid",
395394
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txids",
396395
);
397-
use crate::args::block_tx_id;
398-
let _ = block_tx_id(
399-
&"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce",
400-
&"218",
401-
);
396+
use crate::args::block_txids;
397+
block_txids(&"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce");
402398
wait("1");
403399
}
404400
#[test]
401+
fn test_block_txs() {
402+
// GET /api/block/:hash/txs[/:start_index] (start_index % 25 = 0)
403+
// )
404+
let binding = format!("block/000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txs").clone();
405+
let block_txs: &str = blocking(&binding).expect("returns current txids from block");
406+
let get_block_txs = generic_sys_call(
407+
"block_txs",
408+
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txs",
409+
);
410+
let get_block_txs = generic_sys_call(
411+
"block_txs",
412+
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txs/0",
413+
);
414+
let get_block_txs = generic_sys_call(
415+
"block_txs",
416+
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txs/1", //test if start_index_int % 25 == 0
417+
);
418+
let get_block_txs = generic_sys_call(
419+
"block_txs",
420+
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txs/25",
421+
);
422+
}
423+
#[test]
405424
fn test_blockheight() {
406425
let blockheight = blockheight::blockheight();
407426
assert_ne!(0 as f64, blockheight.unwrap());

0 commit comments

Comments
 (0)