Skip to content

Commit

Permalink
Merge pull request #41 from okx/fix/btc20_inscription_number
Browse files Browse the repository at this point in the history
fix brc20 zero inscription number
  • Loading branch information
wanyvic authored May 15, 2023
2 parents 57078e8 + b265d9c commit 13977a7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
7 changes: 5 additions & 2 deletions src/brc20/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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")]
Expand All @@ -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}")]
Expand Down
4 changes: 2 additions & 2 deletions src/brc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
44 changes: 29 additions & 15 deletions src/brc20/updater.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -26,20 +27,20 @@ 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,
pub to_script: Option<ScriptKey>,
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(
Expand All @@ -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;
Expand All @@ -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(),
)
}
Expand All @@ -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(),
)
}
Expand All @@ -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,
Expand All @@ -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())
}
Expand Down
11 changes: 6 additions & 5 deletions src/index/updater.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::brc20::{BRC20Updater, InscriptionData};
use crate::brc20::{updater::BRC20Updater, InscriptionData};
use crate::okx::BRC20Database;

use {
Expand Down Expand Up @@ -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)?;
Expand Down
7 changes: 0 additions & 7 deletions src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 13977a7

Please sign in to comment.