diff --git a/src/brc20/error.rs b/src/brc20/error.rs index b423fdddb3..04c21b9b10 100644 --- a/src/brc20/error.rs +++ b/src/brc20/error.rs @@ -9,6 +9,9 @@ pub enum Error<L: LedgerRead> { #[error("ledger error: {0}")] LedgerError(<L as LedgerRead>::Error), + + #[error("others: {0}")] + Others(anyhow::Error), } #[derive(Debug, PartialEq, thiserror::Error)] @@ -40,7 +43,7 @@ pub enum BRC20Error { #[error("tick invalid supply {0}")] InvalidSupply(Num), - #[error("tick has been existed")] + #[error("tick: {0} has been existed")] DuplicateTick(String), #[error("tick: {0} not found")] @@ -67,7 +70,7 @@ pub enum BRC20Error { #[error("insufficient balance: {0} {1}")] InsufficientBalance(Num, Num), - #[error("amout exceed limit: {0}")] + #[error("amount exceed limit: {0}")] AmountExceedLimit(Num), #[error("transferable inscriptionId not found: {0}")] diff --git a/src/brc20/mod.rs b/src/brc20/mod.rs index 648de0fa29..4f0651384d 100644 --- a/src/brc20/mod.rs +++ b/src/brc20/mod.rs @@ -7,14 +7,14 @@ mod num; mod operation; mod params; mod types; -mod updater; +pub mod updater; pub use self::{ error::{BRC20Error, Error}, num::Num, operation::{deserialize_brc20, Deploy, Mint, Operation, Transfer}, types::*, - updater::{Action, BRC20Updater, InscribeAction, InscriptionData}, + updater::{Action, InscribeAction, InscriptionData}, }; use ledger::{LedgerRead, LedgerReadWrite}; diff --git a/src/brc20/updater.rs b/src/brc20/updater.rs index 3175f412a5..aa33192c61 100644 --- a/src/brc20/updater.rs +++ b/src/brc20/updater.rs @@ -1,15 +1,16 @@ use std::str::FromStr; use super::{ - ActionReceipt, BRC20Event, Balance, Deploy, DeployEvent, Error, EventType, LedgerRead, - LedgerReadWrite, Mint, MintEvent, Num, Operation, Tick, TokenInfo, Transfer, TransferPhase1Event, - TransferPhase2Event, TransferableLog, + ActionReceipt, BRC20Event, Balance, Deploy, DeployEvent, Error, EventType, LedgerReadWrite, Mint, + MintEvent, Num, Operation, Tick, TokenInfo, Transfer, TransferPhase1Event, TransferPhase2Event, + TransferableLog, }; use crate::brc20::params::BIGDECIMAL_TEN; use crate::{ brc20::{error::BRC20Error, params::MAX_DECIMAL_WIDTH, ScriptKey}, - InscriptionId, SatPoint, Txid, + Index, InscriptionId, SatPoint, Txid, }; +use anyhow::anyhow; use bigdecimal::num_bigint::Sign; #[derive(Clone)] @@ -26,7 +27,6 @@ pub struct InscribeAction { pub struct InscriptionData { pub txid: Txid, pub inscription_id: InscriptionId, - pub inscription_number: u64, pub old_satpoint: SatPoint, pub new_satpoint: Option<SatPoint>, pub from_script: ScriptKey, @@ -34,12 +34,13 @@ pub struct InscriptionData { pub action: Action, } -pub struct BRC20Updater<'a, L: LedgerReadWrite> { +pub(crate) struct BRC20Updater<'a, L: LedgerReadWrite> { ledger: &'a L, + index: &'a Index, } impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> { - pub fn new(ledger: &'a L) -> Self { - Self { ledger } + pub fn new(ledger: &'a L, index: &'a Index) -> Self { + Self { ledger, index } } pub fn index_transaction( @@ -48,11 +49,21 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> { block_time: u32, txid: Txid, operations: Vec<InscriptionData>, - ) -> Result<usize, <L as LedgerRead>::Error> { + ) -> Result<usize, Error<L>> { let mut receipts = Vec::new(); for operation in operations { let op: EventType; - let result = match operation.action { + + let inscription_number = self + .index + .get_inscription_entry(operation.inscription_id) + .map_err(|e| Error::Others(e))? + .ok_or(Error::Others(anyhow!(format!( + "inscription number not found for {}", + operation.inscription_id + ))))? + .number; + let result: Result<BRC20Event, Error<L>> = match operation.action { Action::Inscribe(inscribe) => match inscribe.operation { Operation::Deploy(deploy) => { op = EventType::Deploy; @@ -61,7 +72,7 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> { block_number, block_time, operation.inscription_id, - operation.inscription_number, + inscription_number, operation.to_script.clone(), ) } @@ -74,7 +85,7 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> { self.process_inscribe_transfer( transfer, operation.inscription_id, - operation.inscription_number, + inscription_number, operation.to_script.clone(), ) } @@ -92,14 +103,14 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> { let result = match result { Ok(event) => Ok(event), Err(Error::BRC20Error(e)) => Err(e), - Err(Error::LedgerError(e)) => { + Err(e) => { return Err(e); } }; receipts.push(ActionReceipt { inscription_id: operation.inscription_id, - inscription_number: operation.inscription_number, + inscription_number, op, old_satpoint: operation.old_satpoint, new_satpoint: operation.new_satpoint, @@ -109,7 +120,10 @@ impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> { }); } if !receipts.is_empty() { - self.ledger.save_transaction_receipts(&txid, &receipts)?; + self + .ledger + .save_transaction_receipts(&txid, &receipts) + .map_err(|e| Error::LedgerError(e))?; } Ok(receipts.len()) } diff --git a/src/index/updater.rs b/src/index/updater.rs index 85b51286ea..224993128d 100644 --- a/src/index/updater.rs +++ b/src/index/updater.rs @@ -1,4 +1,4 @@ -use crate::brc20::{BRC20Updater, InscriptionData}; +use crate::brc20::{updater::BRC20Updater, InscriptionData}; use crate::okx::BRC20Database; use { @@ -586,12 +586,13 @@ impl Updater { } inscription_collects.pop(); } - let mut brc20_updater = BRC20Updater::new(&brc20_database); + let mut brc20_updater = BRC20Updater::new(&brc20_database, &index); for (txid, brc20_transaction) in inscription_collects { - brc20_action_count += - brc20_updater.index_transaction(self.height, block.header.time, txid, brc20_transaction)? - as u64; + brc20_action_count += brc20_updater + .index_transaction(self.height, block.header.time, txid, brc20_transaction) + .map_err(|e| anyhow!("failed to parse brc20 protocol for {txid} reason {e}"))? + as u64; } statistic_to_count.insert(&Statistic::LostSats.key(), &lost_sats)?; diff --git a/src/index/updater/inscription_updater.rs b/src/index/updater/inscription_updater.rs index 4f6dac3980..02bd8e49c1 100644 --- a/src/index/updater/inscription_updater.rs +++ b/src/index/updater/inscription_updater.rs @@ -133,7 +133,6 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { InscriptionData { txid, inscription_id, - inscription_number: 0, old_satpoint, new_satpoint: None, from_script: ScriptKey::from_script( @@ -197,7 +196,6 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { InscriptionData { txid, inscription_id: txid.into(), - inscription_number: 0, old_satpoint: SatPoint { outpoint: tx.input.get(0).unwrap().previous_output, offset: 0, @@ -250,11 +248,6 @@ impl<'a, 'db, 'tx> InscriptionUpdater<'a, 'db, 'tx> { }) .map(|value| &mut value.1) { - inscription_data.inscription_number = Index::get_inscription_number_by_inscription_id( - self.id_to_entry, - inscription_data.inscription_id, - )?; - inscription_data.to_script = Some(ScriptKey::from_script( &tx_out.script_pubkey, self.index.get_chain_network(),