Skip to content

Commit

Permalink
Merge branch 'dev' into wc/rpc-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cwbhhjl committed May 9, 2023
2 parents f301ba0 + 79c1c46 commit bdc673c
Show file tree
Hide file tree
Showing 10 changed files with 1,135 additions and 966 deletions.
8 changes: 4 additions & 4 deletions src/brc20/error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::brc20::Ledger;
use crate::brc20::LedgerRead;
use crate::{brc20::num::Num, InscriptionId};
use serde::{Deserialize, Serialize};

#[derive(Debug, thiserror::Error)]
pub enum Error<L: Ledger> {
pub enum Error<L: LedgerRead> {
#[error("brc20 error: {0}")]
BRC20Error(BRC20Error),

#[error("ledger error: {0}")]
LedgerError(<L as Ledger>::Error),
LedgerError(<L as LedgerRead>::Error),
}

#[derive(Debug, PartialEq, thiserror::Error)]
Expand Down Expand Up @@ -77,7 +77,7 @@ pub enum BRC20Error {
TransferableOwnerNotMatch(InscriptionId),
}

impl<L: Ledger> From<BRC20Error> for Error<L> {
impl<L: LedgerRead> From<BRC20Error> for Error<L> {
fn from(e: BRC20Error) -> Self {
Self::BRC20Error(e)
}
Expand Down
42 changes: 20 additions & 22 deletions src/brc20/ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,42 @@ use crate::InscriptionId;
use bitcoin::Txid;
use std::fmt::{Debug, Display};

pub trait Ledger {
pub trait LedgerRead {
type Error: Debug + Display;

fn get_balances(&self, script_key: &ScriptKey) -> Result<Vec<(Tick, Balance)>, Self::Error>;

fn get_balance(
&self,
script_key: &ScriptKey,
tick: &Tick,
) -> Result<Option<Balance>, Self::Error>;

fn get_token_info(&self, tick: &Tick) -> Result<Option<TokenInfo>, Self::Error>;
fn get_tokens_info(&self) -> Result<Vec<TokenInfo>, Self::Error>;

fn get_transaction_receipts(&self, txid: &Txid) -> Result<Vec<ActionReceipt>, Self::Error>;

fn get_transferable(&self, script: &ScriptKey) -> Result<Vec<TransferableLog>, Self::Error>;
fn get_transferable_by_tick(
&self,
script: &ScriptKey,
tick: &Tick,
) -> Result<Vec<TransferableLog>, Self::Error>;
fn get_transferable_by_id(
&self,
script: &ScriptKey,
inscription_id: &InscriptionId,
) -> Result<Option<TransferableLog>, Self::Error>;
}

pub trait LedgerReadWrite: LedgerRead {
fn update_token_balance(
&self,
script_key: &ScriptKey,
tick: &Tick,
new_balance: Balance,
) -> Result<(), Self::Error>;

fn get_token_info(&self, tick: &Tick) -> Result<Option<TokenInfo>, Self::Error>;

fn get_tokens_info(&self) -> Result<Vec<TokenInfo>, Self::Error>;

fn insert_token_info(&self, tick: &Tick, new_info: &TokenInfo) -> Result<(), Self::Error>;

fn update_mint_token_info(
Expand All @@ -34,28 +48,12 @@ pub trait Ledger {
minted_block_number: u64,
) -> Result<(), Self::Error>;

fn get_transaction_receipts(&self, txid: &Txid) -> Result<Vec<ActionReceipt>, Self::Error>;

fn save_transaction_receipts(
&self,
txid: &Txid,
receipts: &[ActionReceipt],
) -> Result<(), Self::Error>;

fn get_transferable(&self, script: &ScriptKey) -> Result<Vec<TransferableLog>, Self::Error>;

fn get_transferable_by_tick(
&self,
script: &ScriptKey,
tick: &Tick,
) -> Result<Vec<TransferableLog>, Self::Error>;

fn get_transferable_by_id(
&self,
script: &ScriptKey,
inscription_id: &InscriptionId,
) -> Result<Option<TransferableLog>, Self::Error>;

fn insert_transferable(
&self,
script: &ScriptKey,
Expand Down
2 changes: 1 addition & 1 deletion src/brc20/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub use self::{
updater::{Action, BRC20Updater, InscribeAction, InscriptionData, TransferAction},
};

use ledger::Ledger;
use ledger::{LedgerRead, LedgerReadWrite};

pub fn deserialize_brc20_operation(inscription: Inscription) -> Result<Operation> {
Ok(deserialize_brc20(std::str::from_utf8(
Expand Down
45 changes: 38 additions & 7 deletions src/brc20/types/tick.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::super::error::BRC20Error;
use crate::brc20::params::TICK_BYTE_COUNT;
use serde::{Deserialize, Serialize};
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use std::str::FromStr;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone)]
pub struct Tick([u8; TICK_BYTE_COUNT]);

impl FromStr for Tick {
Expand All @@ -30,6 +30,12 @@ impl PartialEq for Tick {
}

impl Tick {
pub fn as_str(&self) -> &str {
// NOTE: Tick comes from &str by from_str,
// so it could be calling unwrap when convert to str
std::str::from_utf8(self.0.as_slice()).unwrap()
}

pub fn to_lowercase(&self) -> Tick {
Self::from_str(self.as_str().to_lowercase().as_str()).unwrap()
}
Expand All @@ -51,11 +57,22 @@ impl Tick {
}
}

impl Tick {
pub fn as_str(&self) -> &str {
// NOTE: Tick comes from &str by from_str,
// so it could be calling unwrap when convert to str
std::str::from_utf8(self.0.as_slice()).unwrap()
impl Serialize for Tick {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.as_str().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Tick {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Self::from_str(&String::deserialize(deserializer)?)
.map_err(|e| de::Error::custom(format!("deserialize tick error: {}", e)))
}
}

Expand All @@ -69,4 +86,18 @@ mod tests {

assert_ne!(Tick::from_str("aBc1"), Tick::from_str("aBc2"));
}

#[test]
fn test_tick_serialize() {
let obj = Tick::from_str("Ab1;").unwrap();
assert_eq!(serde_json::to_string(&obj).unwrap(), r##""Ab1;""##);
}

#[test]
fn test_tick_deserialize() {
assert_eq!(
serde_json::from_str::<Tick>(r##""Ab1;""##).unwrap(),
Tick::from_str("Ab1;").unwrap()
);
}
}
11 changes: 6 additions & 5 deletions src/brc20/updater.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::str::FromStr;

use super::{
ActionReceipt, BRC20Event, Balance, Deploy, DeployEvent, Error, Ledger, Mint, MintEvent, Num,
Operation, Tick, TokenInfo, Transfer, TransferPhase1Event, TransferPhase2Event, TransferableLog,
ActionReceipt, BRC20Event, Balance, Deploy, DeployEvent, Error, LedgerRead, LedgerReadWrite,
Mint, MintEvent, Num, Operation, Tick, TokenInfo, Transfer, TransferPhase1Event,
TransferPhase2Event, TransferableLog,
};
use crate::{
brc20::{error::BRC20Error, params::MAX_DECIMAL_WIDTH, ScriptKey},
Expand Down Expand Up @@ -45,11 +46,11 @@ pub struct InscriptionData {
pub action: Action,
}

pub struct BRC20Updater<'a, L: Ledger> {
pub struct BRC20Updater<'a, L: LedgerReadWrite> {
ledger: &'a L,
network: Network,
}
impl<'a, L: Ledger> BRC20Updater<'a, L> {
impl<'a, L: LedgerReadWrite> BRC20Updater<'a, L> {
pub fn new(ledger: &'a L, network: Network) -> Self {
Self { ledger, network }
}
Expand All @@ -60,7 +61,7 @@ impl<'a, L: Ledger> BRC20Updater<'a, L> {
block_time: u32,
txid: Txid,
operations: Vec<InscriptionData>,
) -> Result<usize, <L as Ledger>::Error> {
) -> Result<usize, <L as LedgerRead>::Error> {
let mut receipts = Vec::new();
for operation in operations {
let result = match operation.action {
Expand Down
2 changes: 1 addition & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::brc20::ledger::Ledger;
use crate::brc20::ledger::LedgerRead;
use crate::brc20::ScriptKey;
use {
self::{
Expand Down
3 changes: 2 additions & 1 deletion src/index/updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ impl Updater {

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_updater.index_transaction(self.height, block.header.time, txid, brc20_transaction)?
as u64;
}

statistic_to_count.insert(&Statistic::LostSats.key(), &lost_sats)?;
Expand Down
Loading

0 comments on commit bdc673c

Please sign in to comment.