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

Allow for ignoring some intents based on domain #5

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,13 @@ BRIDGE_TOKEN="USDT" # USDT
COMISSION="10" # if COMISSION == "1"-> 0.01%
SOLVER_ID="" # Given by Composable
COMPOSABLE_ENDPOINT="" # ws IP address Given by Composable
ALLOWED_TRANSFERS="ethereum,solana,ethereum-solana,solana-ethereum"
```

In the example above `ALLOWED_TRANSFER` allows for all possible domains at the moment.
If you want to ignore some incoming intents (e.g. because you cannot handle them well),
remove the appropriate elements from the list.

## Step 2: Run the Solver
To run the solver, use the following command:
```sh
Expand Down
3 changes: 2 additions & 1 deletion example_solver/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ SOLANA_KEYPAIR=""
BRIDGE_TOKEN="USDT" # USDT
COMISSION="200" # if COMISSION == "1"-> 0.01%
SOLVER_ID="" # Given by Composable
COMPOSABLE_ENDPOINT="" # ws IP address Given by Composable
COMPOSABLE_ENDPOINT="" # ws IP address Given by Composable
ALLOWED_TRANSFERS="ethereum,solana,ethereum-solana,solana-ethereum"
53 changes: 51 additions & 2 deletions example_solver/src/chains/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,62 @@ pub struct PostIntentInfo {
pub outputs: OperationOutput,
}

#[derive(Debug, PartialEq, Eq, Hash, EnumString, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, EnumString, Serialize, Deserialize)]
#[strum(serialize_all = "lowercase")]
enum Blockchain {
pub enum Blockchain {
Ethereum,
Solana,
}

#[derive(Debug, PartialEq, Eq)]
pub struct TransferDir {
pub src: Blockchain,
pub dst: Blockchain,
}

impl<'a> TryFrom<&'a str> for TransferDir {
type Error = &'a str;

fn try_from(value: &'a str) -> Result<Self, Self::Error> {
let parts: Vec<&str> = value.split("-").collect();
match parts.len() {
1 => {
let chn = Blockchain::from_str(parts[0]).map_err(|_| parts[0])?;
Ok(TransferDir { src: chn.clone(), dst: chn })
},
2 => {
let src = Blockchain::from_str(parts[0]).map_err(|_| parts[0])?;
let dst = Blockchain::from_str(parts[1]).map_err(|_| parts[1])?;
Ok(TransferDir { src, dst })
},
_ => Err(value),
}
}
}

impl TransferDir {
pub fn all() -> Vec<TransferDir> {
vec![
Self {
src: Blockchain::Solana,
dst: Blockchain::Solana,
},
Self {
src: Blockchain::Solana,
dst: Blockchain::Ethereum,
},
Self {
src: Blockchain::Ethereum,
dst: Blockchain::Solana,
},
Self {
src: Blockchain::Ethereum,
dst: Blockchain::Ethereum,
},
]
}
}

#[derive(Debug, PartialEq, Eq, Hash, EnumString, Serialize, Deserialize)]
#[strum(serialize_all = "UPPERCASE")]
enum Token {
Expand Down
37 changes: 29 additions & 8 deletions example_solver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use crate::chains::SOLVER_ADDRESSES;
use crate::chains::SOLVER_ID;
use crate::chains::SOLVER_PRIVATE_KEY;
use crate::routers::get_simulate_swap_intent;
use chains::create_keccak256_signature;
use chains::{create_keccak256_signature, Blockchain, TransferDir};
use ethers::types::U256;
use futures::{SinkExt, StreamExt};
use serde_json::json;
use serde_json::Value;
use spl_associated_token_account::get_associated_token_address;
use std::env;
use std::str::FromStr;
use tokio_tungstenite::connect_async;
use tokio_tungstenite::tungstenite::protocol::Message;

Expand All @@ -37,6 +38,12 @@ async fn main() {
"solver_addresses": SOLVER_ADDRESSES,
}
});
let allowed_transfers: Vec<TransferDir> = env::var("ALLOWED_TRANSFERS")
.map(|s| {
let strs = s.split(',');
strs.map(|s| TransferDir::try_from(s).unwrap()).collect()
})
.unwrap_or(TransferDir::all());

create_keccak256_signature(&mut json_data, SOLVER_PRIVATE_KEY.to_string())
.await
Expand Down Expand Up @@ -79,6 +86,17 @@ async fn main() {
let intent_value: Value = serde_json::from_str(&intent_str).unwrap();
let intent_info: PostIntentInfo = serde_json::from_value(intent_value).unwrap();

let trasfer_dir = TransferDir {
src: Blockchain::from_str(&intent_info.src_chain)
.expect("Unrecognized source chain"),
dst: Blockchain::from_str(&intent_info.dst_chain)
.expect("Unrecognized destination chain"),
};
if ! allowed_transfers.contains(&trasfer_dir) {
println!("Transfer not allowed: {:?}", intent_info);
continue;
}

// calculate best quote
let final_amount = get_simulate_swap_intent(
&intent_info,
Expand All @@ -87,7 +105,7 @@ async fn main() {
&String::from("USDT"),
)
.await;

// decide if participate or not
let mut amount_out_min = U256::zero();
if let OperationOutput::SwapTransfer(transfer_output) = &intent_info.outputs {
Expand Down Expand Up @@ -158,13 +176,16 @@ async fn main() {
.await
.unwrap();
} else if intent.dst_chain == "ethereum" {
handle_ethereum_execution(&intent, U256::from_dec_str(intent_id).unwrap(), amount, intent.src_chain == intent.dst_chain)
.await
.unwrap();
handle_ethereum_execution(
&intent,
U256::from_dec_str(intent_id).unwrap(),
amount,
intent.src_chain == intent.dst_chain,
)
.await
.unwrap();
} else if intent.dst_chain == "mantis" {
handle_mantis_execution(&intent, intent_id)
.await
.unwrap();
handle_mantis_execution(&intent, intent_id).await.unwrap();
}

// ws_sender.send(Message::text(msg)).await.expect("Failed to send message");
Expand Down