Skip to content

Commit

Permalink
Merge pull request #2 from EyeOfPython/fix/chunk_requests
Browse files Browse the repository at this point in the history
Chunk all requests for listing trades into chunks of sizes 20.
  • Loading branch information
EyeOfPython authored Apr 26, 2019
2 parents 230f6a0 + bf3b79b commit e95dabe
Showing 1 changed file with 35 additions and 19 deletions.
54 changes: 35 additions & 19 deletions src/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,22 +336,31 @@ pub fn accept_trades_interactive(wallet: &Wallet) -> Result<(), Box<std::error::
hex::encode(&trade.tx_id.iter().cloned().rev().collect::<Vec<_>>())
}).collect::<Vec<_>>();

let trades_validity: Vec<SlpTxValidity> = reqwest::Client::new()
.post("https://rest.bitcoin.com/v2/slp/validateTxid")
.json(&vec![("txids", tx_hashes)].into_iter().collect::<HashMap<_, _>>())
.send()?
.json()?;
let trades_validity = tx_hashes.chunks(20).flat_map(|chunk| {
reqwest::Client::new()
.post("https://rest.bitcoin.com/v2/slp/validateTxid")
.json(&vec![("txids", chunk)].into_iter().collect::<HashMap<_, _>>())
.send().unwrap()
.json::<Vec<SlpTxValidity>>().unwrap()
}).collect::<Vec<_>>();

let valid_txs = trades_validity.into_iter()
.filter(|validity| validity.valid)
.map(|validity| validity.txid)
.collect::<HashSet<_>>();

let tx_details: Vec<TxDetails> = reqwest::Client::new()
.post("https://rest.bitcoin.com/v2/transaction/details")
.json(&vec![("txids", &valid_txs)].into_iter().collect::<HashMap<_, _>>())
.send()?
.json()?;
let tx_details = valid_txs
.iter()
.collect::<Vec<_>>()
.chunks(20)
.flat_map(|chunk| {
reqwest::Client::new()
.post("https://rest.bitcoin.com/v2/transaction/details")
.json(&vec![("txids", chunk)].into_iter().collect::<HashMap<_, _>>())
.send().unwrap()
.json::<Vec<TxDetails>>().unwrap()
})
.collect::<Vec<_>>();

let token_ids = tx_details.into_iter().filter_map(|tx| {
let mut p2sh_amount = None;
Expand Down Expand Up @@ -388,16 +397,23 @@ pub fn accept_trades_interactive(wallet: &Wallet) -> Result<(), Box<std::error::
Some((tx_id?, (token_id?, p2sh_amount?)))
}).collect::<HashMap<_, _>>();

let token_details = reqwest::Client::new()
.post("https://rest.bitcoin.com/v2/slp/list")
.json(&vec![(
"tokenIds",
token_ids.values().map(|(x, _)| x.clone()).collect::<HashSet<_>>(),
)].into_iter().collect::<HashMap<_, _>>())
.send()?
.json::<Vec<TokenEntry>>()?
let token_id_set = token_ids.values().map(|(x, _)| x).collect::<HashSet<_>>();
let token_details = token_id_set
.into_iter()
.map(|token_details| (token_details.id.clone(), token_details))
.collect::<Vec<_>>()
.chunks(20)
.flat_map(|chunk| {
reqwest::Client::new()
.post("https://rest.bitcoin.com/v2/slp/list")
.json(&vec![(
"tokenIds",
chunk,
)].into_iter().collect::<HashMap<_, _>>())
.send().unwrap()
.json::<Vec<TokenEntry>>().unwrap()
.into_iter()
.map(|token_details| (token_details.id.clone(), token_details))
})
.collect::<HashMap<_, _>>();

let valid_trades = trades.into_iter()
Expand Down

0 comments on commit e95dabe

Please sign in to comment.