From 7e0c5d96b6ffd3f85ed833fc3df1075c3fab4824 Mon Sep 17 00:00:00 2001
From: Patrice Tisserand
Date: Mon, 22 Apr 2024 13:54:11 +0200
Subject: [PATCH] feat(indexer): refund sent are stored in database
---
apps/indexer/Cargo.lock | 1 +
apps/indexer/refund/Cargo.toml | 5 +-
apps/indexer/refund/src/bin/send_refund.rs | 94 ++++++++++++++++++----
3 files changed, 84 insertions(+), 16 deletions(-)
diff --git a/apps/indexer/Cargo.lock b/apps/indexer/Cargo.lock
index 38bbdec2..8c7a4134 100644
--- a/apps/indexer/Cargo.lock
+++ b/apps/indexer/Cargo.lock
@@ -3109,6 +3109,7 @@ dependencies = [
"env_logger",
"futures",
"log",
+ "mongodb",
"serde",
"serde_json",
"starklane_indexer",
diff --git a/apps/indexer/refund/Cargo.toml b/apps/indexer/refund/Cargo.toml
index 9fde5038..555a02c9 100644
--- a/apps/indexer/refund/Cargo.toml
+++ b/apps/indexer/refund/Cargo.toml
@@ -9,11 +9,12 @@ anyhow = "1.0"
clap = { version = "4.3.19", features = ["derive", "env", "string"] }
csv = "1.3.0"
env_logger = "0.10.0"
-log = "0.4.17"
futures = "0.3.28"
+log = "0.4.17"
+mongodb = "2.6.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
+starknet = "0.10.0"
tokio = { version = "1", features = ["full"] }
starklane_indexer = { version = "0.1.0", path= ".."}
-starknet = "0.10.0"
diff --git a/apps/indexer/refund/src/bin/send_refund.rs b/apps/indexer/refund/src/bin/send_refund.rs
index 3c4d65b1..56010aa6 100644
--- a/apps/indexer/refund/src/bin/send_refund.rs
+++ b/apps/indexer/refund/src/bin/send_refund.rs
@@ -3,7 +3,10 @@ use std::time::Duration;
use anyhow::{anyhow, Result};
use clap::Parser;
+use mongodb::{bson::doc, options::ClientOptions, Client, Collection};
use refund::Refund;
+use serde::{Deserialize, Serialize};
+use starklane_indexer::{storage::extract_database_name, utils::normalize_hex};
use starknet::{
accounts::{Account, Call, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount},
core::{
@@ -14,6 +17,49 @@ use starknet::{
signers::{LocalWallet, SigningKey},
};
+#[derive(Debug, Serialize, Deserialize)]
+struct RefundSend {
+ pub l1_hash: String,
+ pub l2_hash: String,
+ pub refund: Refund,
+}
+
+struct MongoStore {
+ refunded: Collection,
+}
+
+impl MongoStore {
+ pub async fn new(connection_string: &str, db_name: &str) -> Result {
+ let client_options = ClientOptions::parse(connection_string).await?;
+ let client = Client::with_options(client_options)?;
+ let db = client.database(db_name);
+ let refunded = db.collection::("refunded");
+
+ Ok(MongoStore { refunded })
+ }
+
+ pub async fn add(&self, l2_hash: String, refund: &Refund) -> Result<()> {
+ let doc = RefundSend {
+ l1_hash: refund.tx_hash.clone(),
+ l2_hash: normalize_hex(&l2_hash).unwrap(),
+ refund: refund.clone(),
+ };
+ self.refunded.insert_one(doc, None).await?;
+ Ok(())
+ }
+
+ pub async fn check(&self, l1_hash: String) -> Result