Skip to content
This repository was archived by the owner on Jan 5, 2026. It is now read-only.
Open
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
1 change: 1 addition & 0 deletions programs/soroswap/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
[dependencies]
zephyr-sdk = { version = "0.2.1" }
stellar-strkey = "0.0.8"
thiserror = "1.0"
soroban-sdk = { version = "21.5.0" }

[lib]
Expand Down
149 changes: 109 additions & 40 deletions programs/soroswap/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@


use zephyr_sdk::{prelude::*, soroban_sdk::{xdr::{ScVal, Hash }, String as SorobanString, Address}, PrettyContractEvent, EnvClient, DatabaseDerive};
use std::env;

pub mod router;
pub mod factory;
pub mod pairs;
pub mod providers;

#[derive(DatabaseDerive, Clone)]
#[with_name("ssw_rt_ev")]
Expand Down Expand Up @@ -38,6 +38,43 @@ struct ReservesChangeTable {
timestamp: ScVal,
}

#[derive(DatabaseDerive, Clone)]
#[with_name("ssw_providers")]
pub struct ProvidersTable {
provider_address: ScVal,
pool_address: ScVal,
shares: ScVal,
share_percentage: f64,
token_a_amount: ScVal,
token_b_amount: ScVal,
timestamp: ScVal,
action: String,
tx_hash: ScVal,
}

impl ProvidersTable {
pub fn put(&self, env: &EnvClient) {
let default_limits = soroban_sdk::xdr::Limits::none();

env.db_write(
"ssw_providers",
&[
"provider_address", "pool_address", "shares", "share_percentage", "token_a_amount", "token_b_amount", "timestamp", "action", "tx_hash",
],
&[
&self.provider_address.to_xdr(default_limits.clone()).unwrap_or_default(),
&self.pool_address.to_xdr(default_limits.clone()).unwrap_or_default(),
&self.shares.to_xdr(default_limits.clone()).unwrap_or_default(),
&self.share_percentage.to_le_bytes(),
&self.token_a_amount.to_xdr(default_limits.clone()).unwrap_or_default(),
&self.token_b_amount.to_xdr(default_limits.clone()).unwrap_or_default(),
&self.timestamp.to_xdr(default_limits.clone()).unwrap_or_default(),
&self.action.as_bytes(),
&self.tx_hash.to_xdr(default_limits.clone()).unwrap_or_default(),
],
).unwrap();
}
}
#[test]
fn test() {
let factory_address_str: &'static str = env!("SOROSWAP_FACTORY");
Expand All @@ -50,22 +87,44 @@ fn test() {
pub extern "C" fn on_close() {
let env = EnvClient::new();

let factory_address_str: &'static str = env!("SOROSWAP_FACTORY");
let router_address_str: &'static str = env!("SOROSWAP_ROUTER");

env.log().debug(format!("Indexing SSW Factory: {:?}", &factory_address_str), None);
env.log().debug(format!("Indexing SSW Router: {:?}", &router_address_str), None);

let factory_address_bytes: [u8; 32]=stellar_strkey::Contract::from_string(factory_address_str).unwrap().0;
let router_address_bytes: [u8; 32]=stellar_strkey::Contract::from_string(router_address_str).unwrap().0;

let contract_events_with_txhash: Vec<(PrettyContractEvent, [u8; 32])> = env.reader().pretty().soroban_events_and_txhash();

let filtered_router_events_with_txhash: Vec<(PrettyContractEvent, [u8; 32])> = contract_events_with_txhash.clone().into_iter()
.filter(|(event, _txhash)| event.raw.contract_id == Some(Hash(router_address_bytes))).collect();

let filtered_factory_events_with_txhash: Vec<(PrettyContractEvent, [u8; 32])> = contract_events_with_txhash.clone().into_iter()
.filter(|(event, _txhash)| event.raw.contract_id == Some(Hash(factory_address_bytes))).collect();
let factory_address_str =
env::var("SOROSWAP_FACTORY").expect("SOROSWAP_FACTORY environment variable not set");
let router_address_str =
env::var("SOROSWAP_ROUTER").expect("SOROSWAP_ROUTER environment variable not set");

env.log().debug(
format!("Indexing SSW Factory: {:?}", &factory_address_str),
None,
);
env.log().debug(
format!("Indexing SSW Router: {:?}", &router_address_str),
None,
);

let factory_address_bytes: [u8; 32] =
stellar_strkey::Contract::from_string(&factory_address_str)
.unwrap()
.0;
let router_address_bytes: [u8; 32] = stellar_strkey::Contract::from_string(&router_address_str)
.unwrap()
.0;

let contract_events_with_txhash: Vec<(PrettyContractEvent, [u8; 32])> =
env.reader().pretty().soroban_events_and_txhash();

let filtered_router_events_with_txhash: Vec<(PrettyContractEvent, [u8; 32])> =
contract_events_with_txhash
.clone()
.into_iter()
.filter(|(event, _txhash)| event.raw.contract_id == Some(Hash(router_address_bytes)))
.collect();

let filtered_factory_events_with_txhash: Vec<(PrettyContractEvent, [u8; 32])> =
contract_events_with_txhash
.clone()
.into_iter()
.filter(|(event, _txhash)| event.raw.contract_id == Some(Hash(factory_address_bytes)))
.collect();

//handle the events from the router contract
router::events::handle_contract_events(&env, filtered_router_events_with_txhash);
Expand All @@ -75,35 +134,45 @@ pub extern "C" fn on_close() {

// At this point our PairsTable is up to date. Now we can handle the events from the Pairs
let pairs_rows = env.read::<PairsTable>();

env.log().debug(format!("Before pairs_rows iteration"), None);

for pair in pairs_rows {
env.log()
.debug(format!("Before pairs_rows iteration"), None);

for pair in pairs_rows {
let pair_address: Address = env.from_scval(&pair.address);
env.log().debug(format!("Checking pair: {:?}", &pair_address), None);


env.log()
.debug(format!("Checking pair: {:?}", &pair_address), None);

// We filter to see if there is any event related to our Pair
let pair_contract_events: Vec<(PrettyContractEvent, [u8; 32])> = contract_events_with_txhash.clone().into_iter()
.filter(|(event, _txhash)| {
let contract_id_str = SorobanString::from_str(&env.soroban(), &stellar_strkey::Contract(event.raw.contract_id.as_ref().unwrap().0).to_string());
contract_id_str == pair_address.to_string()
})
.collect();

env.log().debug(format!("We identified a number of events: {:?}", &pair_contract_events.len()), None);
env.log().debug(format!("pair_contract_events: {:?}", &pair_contract_events), None);

let pair_contract_events: Vec<(PrettyContractEvent, [u8; 32])> =
contract_events_with_txhash
.clone()
.into_iter()
.filter(|(event, _txhash)| {
let contract_id_str = SorobanString::from_str(
&env.soroban(),
&stellar_strkey::Contract(event.raw.contract_id.as_ref().unwrap().0)
.to_string(),
);
contract_id_str == pair_address.to_string()
})
.collect();

env.log().debug(
format!(
"We identified a number of events: {:?}",
&pair_contract_events.len()
),
None,
);
env.log().debug(
format!("pair_contract_events: {:?}", &pair_contract_events),
None,
);

// Handle the Event
pairs::events::handle_contract_events(&env, pair_contract_events, pair);

}

env.log().debug(format!("After pairs_rows iteration"), None);


}


}
Loading