Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add brc20 indexing and versioning #2

Merged
merged 1 commit into from
May 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add brc20 indexing and versioning
  • Loading branch information
nothing0012 committed May 17, 2023
commit daec1d4d07c61c4b0a3431826c457a106477574e
26 changes: 25 additions & 1 deletion src/index/updater/inscription_updater.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,21 @@ mod stream {
}
}

#[derive(Serialize, Deserialize)]
pub struct BRC20 {
p: String,
op: String,
tick: String,
max: Option<String>,
lim: Option<String>,
amt: Option<String>,
dec: Option<String>,
}

#[derive(Serialize)]
pub struct StreamEvent {
version: String,

// common fields
inscription_id: InscriptionId,
tx_value: u64,
Expand All @@ -349,6 +362,7 @@ mod stream {
content_length: Option<usize>,
content_media: Option<String>,
content_body: Option<String>,
brc20: Option<BRC20>,

// transfer fields
old_location: Option<SatPoint>,
Expand All @@ -364,6 +378,7 @@ mod stream {
block_hash: BlockHash,
) -> Self {
StreamEvent {
version: "2.0.0".to_owned(), // should match the ord-kafka docker image version
inscription_id,
new_location: new_satpoint,
block_timestamp,
Expand Down Expand Up @@ -396,11 +411,19 @@ mod stream {
content_length: None,
content_media: None,
content_body: None,
brc20: None,
old_location: None,
sat_details: None,
}
}

fn key(&self) -> String {
match self.brc20 {
Some(ref brc20) => brc20.tick.clone(),
None => self.inscription_id.to_string(),
}
}

fn get_network() -> Network {
Network::from_str(&env::var("NETWORK").unwrap_or("bitcoin".to_owned())).unwrap()
}
Expand Down Expand Up @@ -435,6 +458,7 @@ mod stream {
.parse::<usize>()
.unwrap();
if inscription.media() == Media::Text && body.len() < kafka_body_max_bytes {
self.brc20 = serde_json::from_slice(body).unwrap_or(None);
Some(general_purpose::STANDARD.encode(body))
} else {
None
Expand Down Expand Up @@ -467,7 +491,7 @@ mod stream {
}

pub fn publish(&mut self) -> Result {
let key = self.inscription_id.to_string();
let key = self.key();
let payload = serde_json::to_vec(&self)?;
let record = BaseRecord::to(&CLIENT.topic).key(&key).payload(&payload);
match CLIENT.producer.send(record) {
Expand Down