Skip to content

Commit

Permalink
add transfers monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroShkvorets committed Mar 23, 2023
1 parent 74fde2d commit 05f5033
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 26 deletions.
4 changes: 2 additions & 2 deletions pomelo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ info:

.PHONY: run
run:
substreams run -e eos.firehose.eosnation.io:9001 entity_out -s 296787657 -t +100000
substreams run -e eos.firehose.eosnation.io:9001 entity_out -s 300090411 -t +100000

.PHONY: gui
gui:
substreams gui -e eos.firehose.eosnation.io:9001 entity_out -s 296787657 -t +100000
substreams gui -e eos.firehose.eosnation.io:9001 entity_out -s 300090411 -t +100000

.PHONY: sink
sink:
Expand Down
56 changes: 47 additions & 9 deletions pomelo/src/abi.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,58 @@
use substreams_antelope::ActionTrace;
// use antelope::Asset;
use serde::{Deserialize, Serialize};

macro_rules! impl_from_str {
($type:ty) => {
impl From<&str> for $type {
#[inline]
#[must_use]
fn from(str: &str) -> Self {
serde_json::from_str(str).unwrap()
}
}
};
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SetGrant {
pub author_id: String,
pub project_id: String,
pub struct GrantsRow {
pub id: String,
pub r#type: String,
pub author_user_id: String,
pub funding_account: String,
pub accepted_tokens: Vec<String>,
pub status: String,
pub created_at: String,
pub updated_at: String,
}
impl_from_str!(GrantsRow);


pub fn parse_setgrant(action_trace: &ActionTrace) -> Option<SetGrant> {
let action = action_trace.action.as_ref()?;
if action.name != "setgrant" { return None; }
let data: SetGrant = serde_json::from_str(&action.json_data).unwrap();
Some(data)

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct ExtendedQuantity {
pub quantity: String,
pub contract: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct TransfersRow {
pub transfer_id: u64,
pub from: String,
pub to: String,
pub ext_quantity: ExtendedQuantity,
pub fee: String,
pub memo: String,
pub user_id: String,
pub season_id: u16,
pub round_id: u16,
pub project_type: String,
pub project_id: String,
pub value: String,
pub trx_id: String,
pub created_at: String,
}
impl_from_str!(TransfersRow);

55 changes: 41 additions & 14 deletions pomelo/src/sinks.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
use substreams::{log};
use substreams::errors::Error;
use substreams_antelope::{Block};
use substreams_entity_change::pb::entity::{entity_change::Operation, EntityChanges};
use substreams_entity_change::pb::entity::{EntityChanges};

use crate::abi;
use crate::utils::from_dbop_to_entityop;

use crate::abi::SetGrant;

#[substreams::handlers::map]
pub fn entity_out( block: Block) -> Result<EntityChanges, Error> {
let mut entity_changes: EntityChanges = Default::default();

for trx in block.clone().all_transaction_traces() {
for trace in &trx.action_traces {
let action_trace = trace.action.as_ref().unwrap().clone();
if action_trace.account != "app.pomelo" { continue; }
if action_trace.name == "setgrant" {
let data: SetGrant = serde_json::from_str(&action_trace.json_data).unwrap();
log::info!("data={:?}", data);
entity_changes.push_change("Grant", &data.project_id, 1, Operation::Create)
.change::<&str, String>("author_id", data.author_id)
.change::<&str, String>("project_id", data.project_id)
.change::<&str, String>("funding_account", data.funding_account)
.change::<&str, String>("accepted_tokens", data.accepted_tokens.join(","));
for trx in block.all_transaction_traces() {
for db_op in &trx.db_ops {
if db_op.code == "app.pomelo" {
match db_op.table_name.as_str() {
"grants" => {
let grant = abi::GrantsRow::from(db_op.new_data_json.as_str());
let op = from_dbop_to_entityop(&db_op.operation());
log::info!("Op: {:?}, Grant={:?}", op, grant);
entity_changes.push_change("Grant", &grant.id, 1, op)
.change("id", grant.id)
.change("author_user_id", grant.author_user_id)
.change("funding_account", grant.funding_account)
.change("status", grant.status)
.change("accepted_tokens", grant.accepted_tokens)
.change("updated_at", grant.updated_at)
.change("trx_id", trx.id.clone());
}
"transfers" => {
let transfer = abi::TransfersRow::from(db_op.new_data_json.as_str());
let op = from_dbop_to_entityop(&db_op.operation());
let transfer_id = format!("{}-{}", transfer.round_id, transfer.transfer_id);
log::info!("Op: {:?}, Transfer={:?}", op, transfer);
entity_changes.push_change("Transfer", &transfer_id, 1, op)
.change("user_id", transfer.user_id)
.change("from", transfer.from)
.change("to", transfer.to)
.change("quantity", transfer.ext_quantity.quantity)
.change("contract", transfer.ext_quantity.contract)
.change("fee", transfer.fee)
.change("value",transfer.value.to_string())
.change("memo",transfer.memo)
.change("project_id", transfer.project_id)
.change("created_at", transfer.created_at)
.change("trx_id", transfer.trx_id);
}
_ => continue,
}
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion pomelo/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
use substreams_antelope::db_op;
use substreams_entity_change::pb::entity::entity_change;


pub fn from_dbop_to_entityop(op: &db_op::Operation) -> entity_change::Operation {
match op {
db_op::Operation::Insert => entity_change::Operation::Create,
db_op::Operation::Update => entity_change::Operation::Update,
db_op::Operation::Remove => entity_change::Operation::Delete,
db_op::Operation::Unknown => entity_change::Operation::Unset,
}
}

0 comments on commit 05f5033

Please sign in to comment.