Skip to content

Commit

Permalink
fix: wrong amount in token_event table
Browse files Browse the repository at this point in the history
  • Loading branch information
remiroyc committed Oct 8, 2024
1 parent 28ff5c0 commit 1268a2e
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 15 deletions.
27 changes: 23 additions & 4 deletions crates/diri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,48 @@ impl<S: Storage, E: EventHandler> Diri<S, E> {
}
};

let contract_address = ""; // TODO !
let broker_id = match self.storage.get_broker_id(contract_address).await {
Ok(id) => Some(id),
Err(e) => {
error!("Can't get broker id: {e}");
None
}
};

match orderbook_event {
Event::OrderPlaced(ev) => {
trace!("OrderPlaced found: {:?}", ev);
self.storage
.register_placed(block_number, block_timestamp, &ev.into())
.register_placed(block_number, block_timestamp, &ev.into(), broker_id)
.await?;
}
Event::OrderCancelled(ev) => {
trace!("OrderCancelled found: {:?}", ev);
self.storage
.register_cancelled(block_number, block_timestamp, &ev.into())
.register_cancelled(
block_number,
block_timestamp,
&ev.into(),
broker_id,
)
.await?;
}
Event::OrderFulfilled(ev) => {
trace!("OrderFulfilled found: {:?}", ev);
self.storage
.register_fulfilled(block_number, block_timestamp, &ev.into())
.register_fulfilled(
block_number,
block_timestamp,
&ev.into(),
broker_id,
)
.await?;
}
Event::OrderExecuted(ev) => {
trace!("OrderExecuted found: {:?}", ev);
self.storage
.register_executed(block_number, block_timestamp, &ev.into())
.register_executed(block_number, block_timestamp, &ev.into(), broker_id)
.await?;
}
Event::RollbackStatus(ev) => {
Expand Down
7 changes: 7 additions & 0 deletions crates/diri/src/storage/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use async_trait::async_trait;

pub mod types;
use starknet::contract;
use types::{CancelledData, ExecutedData, FulfilledData, PlacedData, RollbackStatusData};

pub type StorageResult<T> = Result<T, StorageError>;
Expand All @@ -20,27 +21,31 @@ pub trait Storage {
block_id: u64,
block_timestamp: u64,
order: &PlacedData,
broker_id: Option<String>,
) -> StorageResult<()>;

async fn register_cancelled(
&self,
block_id: u64,
block_timestamp: u64,
order: &CancelledData,
broker_id: Option<String>,
) -> StorageResult<()>;

async fn register_fulfilled(
&self,
block_id: u64,
block_timestamp: u64,
order: &FulfilledData,
broker_id: Option<String>,
) -> StorageResult<()>;

async fn register_executed(
&self,
block_id: u64,
block_timestamp: u64,
order: &ExecutedData,
broker_id: Option<String>,
) -> StorageResult<()>;

async fn status_back_to_open(
Expand All @@ -49,4 +54,6 @@ pub trait Storage {
block_timestamp: u64,
order: &RollbackStatusData,
) -> StorageResult<()>;

async fn get_broker_id(&self, contract_address: &str) -> StorageResult<String>;
}
4 changes: 2 additions & 2 deletions crates/pontos/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ impl<S: Storage, C: StarknetClient, E: EventHandler + Send + Sync> Pontos<S, C,
) -> Result<()> {
let mut token_sale_event = self
.event_manager
.format_element_sale_event(&event, block_timestamp)
.format_element_sale_event(&event, block_timestamp, chain_id)
.await?;

let contract_addr = FieldElement::from_hex_be(
Expand Down Expand Up @@ -430,7 +430,7 @@ impl<S: Storage, C: StarknetClient, E: EventHandler + Send + Sync> Pontos<S, C,

let mut token_sale_event = self
.event_manager
.format_ventory_sale_or_accepted_offer_event(&event, block_timestamp)
.format_ventory_sale_or_accepted_offer_event(&event, block_timestamp, chain_id)
.await?;

let contract_addr = FieldElement::from_hex_be(
Expand Down
8 changes: 6 additions & 2 deletions crates/pontos/src/managers/event_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl<S: Storage> EventManager<S> {
&self,
event: &EmittedEvent,
block_timestamp: u64,
chain_id: &str,
) -> Result<TokenSaleEvent> {
let _listing_counter = event
.data
Expand Down Expand Up @@ -120,14 +121,16 @@ impl<S: Storage> EventManager<S> {
currency_address: None,
marketplace_contract_address: to_hex_str(&event.from_address),
marketplace_name: "Ventory".to_string(),
price: price.to_big_decimal(0).to_string(),
price: to_hex_str(price),
chain_id: chain_id.to_string(),
})
}

pub async fn format_element_sale_event(
&self,
event: &EmittedEvent,
block_timestamp: u64,
chain_id: &str,
) -> Result<TokenSaleEvent> {
if event.keys.len() < 4 {
return Err(anyhow!("Can't find event data into this event"));
Expand Down Expand Up @@ -231,7 +234,8 @@ impl<S: Storage> EventManager<S> {
currency_address: Some(to_hex_str(currency_address)),
marketplace_contract_address: to_hex_str(&event.from_address),
marketplace_name: "Element".to_string(),
price: price.to_big_decimal(0).to_string(),
price: to_hex_str(price),
chain_id: chain_id.to_string(),
})
}

Expand Down
1 change: 1 addition & 0 deletions crates/pontos/src/storage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ pub struct TokenSaleEvent {
pub quantity: u64,
pub currency_address: Option<String>,
pub price: String,
pub chain_id: String,
}

impl Default for TokenTransferEvent {
Expand Down
2 changes: 1 addition & 1 deletion crates/sana/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ impl<S: Storage, C: StarknetClient, E: EventHandler + Send + Sync> Sana<S, C, E>

let mut token_sale_event = self
.event_manager
.format_ventory_sale_event(&event, block_timestamp)
.format_ventory_sale_event(&event, block_timestamp, chain_id)
.await?;

let contract_addr = FieldElement::from_hex_be(
Expand Down
7 changes: 4 additions & 3 deletions crates/sana/src/managers/event_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ impl<S: Storage> EventManager<S> {
&self,
event: &EmittedEvent,
block_timestamp: u64,
chain_id: &str,
) -> Result<TokenSaleEvent> {
let _listing_counter = event
.data
Expand Down Expand Up @@ -113,8 +114,8 @@ impl<S: Storage> EventManager<S> {
currency_address: None,
marketplace_contract_address: to_hex_str(&event.from_address),
marketplace_name: "Ventory".to_string(),
price: price.to_big_decimal(0).to_string(),
chain_id: "0x534e5f4d41494e".to_string(),
price: to_hex_str(price),
chain_id: chain_id.to_string(),
})
}

Expand Down Expand Up @@ -226,7 +227,7 @@ impl<S: Storage> EventManager<S> {
currency_address: Some(to_hex_str(currency_address)),
marketplace_contract_address: to_hex_str(&event.from_address),
marketplace_name: "Element".to_string(),
price: price.to_big_decimal(0).to_string(),
price: to_hex_str(price),
chain_id: chain_id.to_string(),
})
}
Expand Down
5 changes: 3 additions & 2 deletions crates/sana/src/storage/sqlx/default_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ impl Storage for PostgresStorage {
event.token_event_id
);
} else {
let insert_query = "INSERT INTO token_event (token_event_id, contract_address, chain_id, token_id, token_id_hex, event_type, block_timestamp, transaction_hash, to_address, from_address)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) ON CONFLICT (token_event_id) DO NOTHING";
let insert_query = "INSERT INTO token_event (token_event_id, contract_address, chain_id, broker_id, token_id, token_id_hex, event_type, block_timestamp, transaction_hash, to_address, from_address)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) ON CONFLICT (token_event_id) DO NOTHING";

let event_type = event.event_type.as_ref().map(|e| {
let res = self.to_title_case(&e.to_string().to_lowercase());
Expand All @@ -325,6 +325,7 @@ impl Storage for PostgresStorage {
.bind(event.token_event_id.clone())
.bind(event.contract_address.clone())
.bind(event.chain_id.clone())
.bind(event.broker_id.clone())
.bind(event.token_id.clone())
.bind(event.token_id_hex.clone())
.bind(event_type)
Expand Down
5 changes: 4 additions & 1 deletion crates/sana/src/storage/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub struct TokenTransferEvent {
pub to_address: String,
pub contract_address: String,
pub chain_id: String,
pub broker_id: Option<String>,
pub contract_type: String,
pub transaction_hash: String,
pub token_id: String,
Expand All @@ -183,7 +184,6 @@ pub struct TokenSaleEvent {
pub marketplace_contract_address: String,
pub marketplace_name: String,
pub transaction_hash: String,
pub chain_id: String,
pub token_id: String,
pub token_id_hex: String,
pub event_type: EventType,
Expand All @@ -193,6 +193,7 @@ pub struct TokenSaleEvent {
pub quantity: u64,
pub currency_address: Option<String>,
pub price: String,
pub chain_id: String,
}

impl Default for TokenTransferEvent {
Expand All @@ -211,6 +212,7 @@ impl Default for TokenTransferEvent {
block_number: None,
updated_at: None,
chain_id: "0x534e5f4d41494e".to_string(),
broker_id: None,
}
}
}
Expand Down Expand Up @@ -362,6 +364,7 @@ mod tests {
updated_at: Some(1625101200),
chain_id: "0x534e5f4d41494e".to_string(),
token_id_hex: "0x123".to_string(),
broker_id: None,
});

let serialized = serde_json::to_string(&event).expect("Failed to serialize TokenEvent");
Expand Down

0 comments on commit 1268a2e

Please sign in to comment.