Skip to content

Commit c29c7db

Browse files
committed
block_txid:block_txids
1 parent 25a1333 commit c29c7db

File tree

4 files changed

+109
-6
lines changed

4 files changed

+109
-6
lines changed

src/args.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,25 @@ pub fn historical_price(currency: &str, timestamp: &str) {
4343
&format!("{:}", &currency),
4444
&format!("{:}", &timestamp)
4545
));
46-
// let _res = blocking(&format!(
47-
// "v1/historical-price?currency={}&timestamp={}",
48-
// &format!("{:}", "EUR"),
49-
// &format!("{:}", "1500000000")
50-
// ));
46+
}
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
52+
let _res = blocking(&format!("block/{}/txid/{}", block_hash, txindex));
53+
}
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+
// ]
64+
let _res = blocking(&format!("block/{}/txids", block_hash));
5165
}
5266

5367
/// <https://mempool.space/docs/api/rest>
@@ -130,6 +144,8 @@ pub struct Args {
130144
/// - BLOCK <BLOCK_HASH> <TXID> <INDEX>
131145
/// `https://mempool.space/api/block/<TXID>/<INDEX>`
132146
pub block_txid: Option<String>,
147+
pub block_txindex: Option<String>,
148+
133149
/// - BLOCK <BLOCK_HASH> <TXIDS>
134150
/// `https://mempool.space/api/block/<TXID>`
135151
pub block_txids: Option<String>,
@@ -226,6 +242,14 @@ impl Args {
226242
opts.optflag("", "blocks_tip_height", "GET /api/blocks/tip/height api call");
227243
opts.optflag("", "blocks_tip_hash", "GET /api/blocks/tip/hash api call");
228244

245+
opts.optopt("", "block_txid", "block txid api call", "BLOCK_TXID");
246+
opts.optopt("", "block_txindex", "block_txindex api call", "BLOCK_TXINDEX");
247+
248+
opts.optopt("", "block_txids", "block txids api call", "BLOCK_TXIDS");
249+
opts.optopt("", "block_txs", "block txids api call", "BLOCK_TXS");
250+
opts.optopt("", "blocks", "block txids api call", "BLOCKS");
251+
opts.optopt("", "blocks_bulk", "block txids api call", "BLOCKS_BULK");
252+
229253
//OPTOPT
230254
opts.optopt("c", "config", "sets the configuration file", "CONFIG");
231255
opts.optopt("s", "server", "sets the address of the rustypaste server", "SERVER");
@@ -342,7 +366,17 @@ impl Args {
342366
generic_sys_call("blocks_tip_hash", &"extraneous_arg");
343367
std::process::exit(0);
344368
}
345-
369+
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());
373+
std::process::exit(0);
374+
}
375+
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());
378+
std::process::exit(0);
379+
}
346380
if matches.opt_present("h")
347381
|| (matches.free.is_empty()
348382
&& !matches.opt_present("u")
@@ -432,6 +466,7 @@ impl Args {
432466
// BLOCK BLOCK_HASH TXID INDEX
433467
// https://mempool.space/api/block/<block_hash>/<txid>/<index>
434468
block_txid: matches.opt_str("block_txid"),
469+
block_txindex: matches.opt_str("block_txindex"),
435470
// BLOCK BLOCK_HASH TXIDS
436471
// https://mempool.space/api/block/<block_hash>/<txids>
437472
block_txids: matches.opt_str("block_txids"),

src/bin/mempool-space_block_txid.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
let mut index = &String::from("");
9+
if args.len() == 3 {
10+
block = &args[1];
11+
index = &args[2];
12+
} else {
13+
// silence is golden
14+
std::process::exit(0);
15+
}
16+
let _res = blocking(&format!("block/{}/txid/{}", &block, &index));
17+
}
18+
}

src/bin/mempool-space_block_txids.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/{}/txids", &block));
15+
}
16+
}

src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,40 @@ mod tests {
368368
wait("1");
369369
}
370370
#[test]
371+
fn test_block_txid() {
372+
// GET /api/block/:hash/txid/:index
373+
let binding =
374+
format!("block/000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txid/218").clone();
375+
let block_txid: &str = blocking(&binding).expect("returns current txid from block index");
376+
let get_block_txid = generic_sys_call(
377+
"block_txid",
378+
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txid/218",
379+
);
380+
use crate::args::block_tx_id;
381+
let _ = block_tx_id(
382+
&"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce",
383+
&"218",
384+
);
385+
wait("1");
386+
}
387+
#[test]
388+
fn test_block_txids() {
389+
// 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");
393+
let get_block_txids = generic_sys_call(
394+
"block_txid",
395+
"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce/txids",
396+
);
397+
use crate::args::block_tx_id;
398+
let _ = block_tx_id(
399+
&"000000000000000015dc777b3ff2611091336355d3f0ee9766a2cf3be8e4b1ce",
400+
&"218",
401+
);
402+
wait("1");
403+
}
404+
#[test]
371405
fn test_blockheight() {
372406
let blockheight = blockheight::blockheight();
373407
assert_ne!(0 as f64, blockheight.unwrap());

0 commit comments

Comments
 (0)