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

Refund L1 transactions #196

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
2,022 changes: 1,140 additions & 882 deletions apps/indexer/Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions apps/indexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[workspace]
members = ["refund"]

[package]
name = "starklane_indexer"
version = "0.1.0"
Expand Down
4 changes: 2 additions & 2 deletions apps/indexer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ COPY Starklane_ABI.json .
COPY Cargo.toml Cargo.toml
COPY Cargo.lock Cargo.lock
COPY src src
COPY refund refund
# COPY doesn't check if file is present when matching is used
COPY git-versio[n] git-version

RUN cargo install --path .

RUN cargo install --path .
#####
FROM debian:bookworm-slim

Expand Down
20 changes: 20 additions & 0 deletions apps/indexer/refund/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "refund"
version = "0.1.0"
edition = "2021"


[dependencies]
anyhow = "1.0"
clap = { version = "4.3.19", features = ["derive", "env", "string"] }
csv = "1.3.0"
env_logger = "0.10.0"
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= ".."}
112 changes: 112 additions & 0 deletions apps/indexer/refund/src/bin/extract_refund.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
use anyhow::{anyhow, Result};
use clap::Parser;

use starklane_indexer::{
price::moralis::MoralisPrice,
storage::{
extract_database_name,
mongo::MongoStore,
store::{EventStore, RequestStore},
},
};

use refund::Refund;

const ENV_PREFIX: &str = "INDEXER";
const ENV_SEPARATOR: &str = "__"; // "_" can't be used since we have key with '_' in json

#[derive(Parser, Debug)]
#[clap(about = "Extract refund")]
struct Args {
#[clap(long, help = "Mongo db connection string", env = format!("{}{}MONGODB_URI", ENV_PREFIX, ENV_SEPARATOR))]
mongodb: String,

#[clap(long, help = "Max amount to refund")]
amount_max: u64,

#[clap(long, help = "CSV output file")]
output: String,

#[clap(long, help = "Ceil amount (false by default)", default_value_t = false)]
ceil: bool,
}

const STRK_ADDR_ETH: &str = "0xCa14007Eff0dB1f8135f4C25B34De49AB0d42766";
const STRK_ADDR_STRK: &str = "0x04718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d";

async fn get_strk_price() -> Result<f64> {
let price = MoralisPrice::new(None)
.get_price(STRK_ADDR_ETH, None)
.await?;
match price.parse::<f64>() {
Ok(p) => Ok(p),
Err(e) => Err(anyhow!("Failed to parse STRK price: {:?}", e)),
}
}

fn compute_amount_strk(amount_usd: f64, strk_price: f64, amount_max: f64, ceil: bool) -> f64 {
let amount_usd = if amount_usd > amount_max {
amount_max
} else {
amount_usd
};

let amount = amount_usd / strk_price;
if ceil {
amount.ceil()
} else {
amount
}
}

#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let strk_price = get_strk_price().await?;
log::info!("STRK price: {}", strk_price);
let args = Args::parse();
let amount_max = args.amount_max;
let output = args.output;

let dbname = extract_database_name(&args.mongodb)
.expect("Database name couldn't be extracted from the connection string");
let mongo_store = MongoStore::new(&args.mongodb, dbname).await?;

let mut wtr = csv::Writer::from_path(output)?;

if let Ok(events) = mongo_store
.events_by_label(
starklane_indexer::storage::EventLabel::DepositInitiatedL1,
true,
)
.await
{
for event in events {
let req = mongo_store.req_by_hash(&event.req_hash).await?;
if req.is_some() {
let req = req.unwrap();
let refund = if event.price.is_some() {
event.price.unwrap().usd_price
} else {
"-1".to_owned()
};
if refund != "-1" {
let amount_usd = refund.parse::<f64>().unwrap();
let amount =
compute_amount_strk(amount_usd, strk_price, amount_max as f64, args.ceil);
let refund_info = Refund {
token_address: STRK_ADDR_STRK.to_owned(),
dest: req.to,
amount,
amount_usd,
tx_hash: event.tx_hash,
};
wtr.serialize(refund_info.clone())?;
log::debug!("{:?}", refund_info);
}
}
}
}
wtr.flush()?;
Ok(())
}
207 changes: 207 additions & 0 deletions apps/indexer/refund/src/bin/send_refund.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
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::{
types::{ExecutionResult, FieldElement, StarknetError},
utils::get_selector_from_name,
},
providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider, ProviderError, Url},
signers::{LocalWallet, SigningKey},
};

#[derive(Debug, Serialize, Deserialize)]
struct RefundSend {
pub l1_hash: String,
pub l2_hash: String,
pub refund: Refund,
}

struct MongoStore {
refunded: Collection<RefundSend>,
}

impl MongoStore {
pub async fn new(connection_string: &str, db_name: &str) -> Result<MongoStore> {
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::<RefundSend>("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<Option<RefundSend>> {
match self
.refunded
.find_one(doc! { "l1_hash": l1_hash}, None)
.await
{
Ok(r) => Ok(r),
Err(e) => Err(anyhow!("Failed to query DB: {:?}", e)),
}
}
}

#[derive(Parser, Debug)]
#[clap(about = "Send refund")]
struct Args {
#[clap(long, help = "Mongo db connection string", env = "REFUND_MONGODB_URI")]
mongodb: String,

#[clap(long, help = "CSV input file")]
input: String,

#[clap(long, help = "Starknet RPC", env = "STARKNET_RPC")]
rpc: String,

#[clap(
long,
help = "Starknet account address",
env = "STARKNET_ACCOUNT_ADDRESS"
)]
address: String,

#[clap(
long,
help = "Starknet account private key",
env = "STARKNET_PRIVATE_KEY"
)]
private_key: String,
}

fn refund_to_call(refund: &Refund) -> Call {
let selector = get_selector_from_name("transfer").unwrap();
let token_address = FieldElement::from_hex_be(&refund.token_address).unwrap();
let dest = FieldElement::from_hex_be(&refund.dest).unwrap();
let token_decimals = 18;
let decimals = 8;
let amount = refund.amount * (10_u64.pow(decimals) as f64);
let amount = (amount as u128) * (10_u64.pow(token_decimals - decimals) as u128);
let amount = FieldElement::from(amount);
Call {
to: token_address,
selector,
calldata: vec![dest, amount, FieldElement::ZERO],
}
}

// From starkli
pub async fn watch_tx<P>(
provider: P,
transaction_hash: FieldElement,
poll_interval: Duration,
) -> Result<()>
where
P: Provider,
{
loop {
match provider.get_transaction_receipt(transaction_hash).await {
Ok(receipt) => match receipt.execution_result() {
ExecutionResult::Succeeded => {
log::info!(
"Transaction {} confirmed",
format!("{:#064x}", transaction_hash)
);

return Ok(());
}
ExecutionResult::Reverted { reason } => {
return Err(anyhow::anyhow!("transaction reverted: {}", reason));
}
},
Err(ProviderError::StarknetError(StarknetError::TransactionHashNotFound)) => {
log::debug!("Transaction not confirmed yet...");
}
Err(err) => return Err(err.into()),
}

tokio::time::sleep(poll_interval).await;
}
}

#[tokio::main]
async fn main() -> Result<()> {
env_logger::init();
let args = Args::parse();

let input = args.input;
let rpc = args.rpc;
let address = args.address;
let private_key = args.private_key;

let dbname = extract_database_name(&args.mongodb)
.expect("Database name couldn't be extracted from the connection string");

let provider = JsonRpcClient::new(HttpTransport::new(Url::parse(&rpc).unwrap()));
let signer = LocalWallet::from(SigningKey::from_secret_scalar(
FieldElement::from_hex_be(&private_key).unwrap(),
));
let address = FieldElement::from_hex_be(&address).unwrap();
let chain_id = provider.chain_id().await?;

let account =
SingleOwnerAccount::new(provider, signer, address, chain_id, ExecutionEncoding::New);

log::debug!("Account address: {:?}", account.address());
let mut calls: Vec<Call> = vec![];
let mut refunds: Vec<Refund> = vec![];

let mongo = MongoStore::new(&args.mongodb, dbname).await?;

let mut rdr = csv::Reader::from_path(input)?;
for elem in rdr.deserialize() {
let refund: Refund = elem?;
if let Ok(Some(_check)) = mongo.check(refund.clone().tx_hash).await {
log::debug!("Already send: {:?}", refund);
continue;
}
log::debug!("To send: {:?}", refund);
let call = refund_to_call(&refund);
calls.push(call);
refunds.push(refund);
}

if calls.is_empty() {
log::info!("No refund to send");
return Ok(());
}

let invoke = account.execute(calls).send().await.unwrap();
let provider = account.provider();
log::debug!("Wait for transaction: {:?}", invoke.transaction_hash);
let result = watch_tx(
provider,
invoke.transaction_hash,
Duration::from_millis(10000),
)
.await;
match result {
Ok(_) => {
for refund in refunds {
let l2_hash = format!("{:#064x}", invoke.transaction_hash);
let l2_hash = normalize_hex(&l2_hash).unwrap();
mongo.add(l2_hash, &refund).await?
}
Ok(())
}
Err(e) => Err(anyhow!("Transaction failed! {:?}", e)),
}
}
15 changes: 15 additions & 0 deletions apps/indexer/refund/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct Refund {
#[serde(rename = "Token Address")]
pub token_address: String,
#[serde(rename = "Recipient")]
pub dest: String,
#[serde(rename = "Amount")]
pub amount: f64,
#[serde(rename = "USD")]
pub amount_usd: f64,
#[serde(rename = "Transaction Hash")]
pub tx_hash: String,
}
1 change: 0 additions & 1 deletion apps/indexer/src/ethereum_indexer/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ where
xchain_txor_config: XchainTxConfig,
) -> Result<EthereumIndexer<T>> {
let client = EthereumClient::new(config.clone()).await?;
/// TODO: should we add moralis api key to configuration file?
let pricer = MoralisPrice::new(None);
Ok(EthereumIndexer {
client,
Expand Down
Loading
Loading