Skip to content

Commit

Permalink
Merge pull request #20 from okx/wc/rpc-dev
Browse files Browse the repository at this point in the history
brc20 rpc
  • Loading branch information
wanyvic authored May 9, 2023
2 parents 9ddc4bb + ca8ab52 commit e635ac4
Show file tree
Hide file tree
Showing 4 changed files with 297 additions and 71 deletions.
79 changes: 55 additions & 24 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -953,19 +953,26 @@ impl Index {
}

pub(crate) fn brc20_get_tick_info(&self, name: &String) -> Result<Option<brc20::TokenInfo>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let info = brc20_db.get_token_info(&brc20::Tick::from_str(name)?)?;
Ok(info)
}

pub(crate) fn brc20_get_all_tick_info(&self) -> Result<Vec<brc20::TokenInfo>> {
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let info = brc20_db.get_tokens_info()?;
Ok(info)
}

pub(crate) fn brc20_get_balance_by_address(
&self,
tick: &str,
address: &bitcoin::Address,
) -> Result<Option<brc20::Balance>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let bal = brc20_db.get_balance(
&brc20::ScriptKey::from_address(address.clone()),
&brc20::Tick::from_str(tick)?,
Expand All @@ -977,49 +984,73 @@ impl Index {
&self,
address: &bitcoin::Address,
) -> Result<Vec<(brc20::Tick, brc20::Balance)>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let all_balance = brc20_db.get_balances(&brc20::ScriptKey::from_address(address.clone()))?;
Ok(all_balance)
}

pub(crate) fn brc20_get_tx_events_by_txid(
&self,
txid: &bitcoin::Txid,
) -> Result<Vec<brc20::ActionReceipt>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
) -> Result<Option<Vec<brc20::ActionReceipt>>> {
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let res = brc20_db.get_transaction_receipts(txid)?;
return Ok(res);

if res.len() == 0 {
let tx = self.client.get_raw_transaction_info(txid, None)?;
if let Some(tx_blockhash) = tx.blockhash {
let tx_bh = self.client.get_block_header_info(&tx_blockhash)?;
let parsed_height = self.height()?;
if parsed_height.is_none() || tx_bh.height as u64 > parsed_height.unwrap().0 {
return Ok(None);
}
} else {
return Err(anyhow!("can't get tx block hash: {txid}"));
}
}

return Ok(Some(res));
}

pub(crate) fn brc20_get_block_events_by_blockhash(
&self,
blockhash: bitcoin::BlockHash,
) -> Result<Vec<(bitcoin::Txid, Vec<brc20::ActionReceipt>)>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
) -> Result<Option<Vec<(bitcoin::Txid, Vec<brc20::ActionReceipt>)>>> {
let parsed_height = self.height()?;
if parsed_height.is_none() {
return Ok(None);
}
let parsed_height = parsed_height.unwrap().0;
let block = self.client.get_block_info(&blockhash)?;
if block.height as u64 > parsed_height {
return Ok(None);
}

let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);

let block = self.client.get_block(&blockhash)?;
let mut result = Vec::new();

for tx in &block.txdata {
result.push((
tx.txid().clone(),
brc20_db.get_transaction_receipts(&tx.txid())?,
));
for txid in &block.tx {
let tx_events = brc20_db.get_transaction_receipts(txid)?;
if tx_events.len() == 0 {
continue;
}
result.push((txid.clone(), tx_events));
}

Ok(result)
Ok(Some(result))
}

pub(crate) fn brc20_get_tick_transferable_by_address(
&self,
tick: &str,
address: &bitcoin::Address,
) -> Result<Vec<brc20::TransferableLog>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let res = brc20_db.get_transferable_by_tick(
&ScriptKey::from_address(address.clone()),
&brc20::Tick::from_str(tick)?,
Expand All @@ -1032,8 +1063,8 @@ impl Index {
&self,
address: &bitcoin::Address,
) -> Result<Vec<brc20::TransferableLog>> {
let wtx = self.database.begin_write().unwrap();
let brc20_db = crate::okx::BRC20Database::new(&wtx);
let wtx = self.database.begin_read().unwrap();
let brc20_db = crate::okx::BRC20DatabaseReader::new(&wtx);
let res = brc20_db.get_transferable(&ScriptKey::from_address(address.clone()))?;

Ok(res)
Expand Down
1 change: 1 addition & 0 deletions src/okx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod brc20_db;

pub use self::brc20_db::BRC20Database;
pub use self::brc20_db::BRC20DatabaseReader;
2 changes: 2 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ impl Server {
.route("/status", get(Self::status))
.route("/tx/:txid", get(Self::transaction))
.route("/brc20/tick/:tick", get(brc20_tick_info))
.route("/brc20/tick", get(brc20_all_tick_info))
.route(
"/brc20/tick/:tick/address/:address/balance",
get(brc20_balance),
Expand Down Expand Up @@ -2559,6 +2560,7 @@ mod tests {

for url in [
"/brc20/tick/🍎",
"/brc20/tick",
"/brc20/tick/ordi/address/bc1pjdmfs5lvqfl6qmzpc0e4ewfdgfmdyz2t79scrsaz8ep98374wwnsywz7t4/balance",
"/brc20/address/bc1pjdmfs5lvqfl6qmzpc0e4ewfdgfmdyz2t79scrsaz8ep98374wwnsywz7t4/balance",
"/brc20/tx/b61b0172d95e266c18aea0c624db987e971a5d6d4ebc2aaed85da4642d635735",
Expand Down
Loading

0 comments on commit e635ac4

Please sign in to comment.