Merge branch 'main' into encoder/hr/ENG-4083-Validate-SplitSwap

This commit is contained in:
Harsh Vardhan Roy
2025-02-05 00:17:21 +05:30
committed by GitHub
20 changed files with 383 additions and 243 deletions

View File

@@ -1,3 +1,17 @@
## [0.30.1](https://github.com/propeller-heads/tycho-execution/compare/0.30.0...0.30.1) (2025-02-04)
### Bug Fixes
* deprecated signature ([576f89d](https://github.com/propeller-heads/tycho-execution/commit/576f89d24ca25ab37ae59b4db97cbff946d6da58))
## [0.30.0](https://github.com/propeller-heads/tycho-execution/compare/0.29.1...0.30.0) (2025-02-04)
### Features
* Refactor Registries ([23875b8](https://github.com/propeller-heads/tycho-execution/commit/23875b8b02396690b3f028c7696ea5b95e17e40e))
## [0.29.1](https://github.com/propeller-heads/tycho-execution/compare/0.29.0...0.29.1) (2025-02-04) ## [0.29.1](https://github.com/propeller-heads/tycho-execution/compare/0.29.0...0.29.1) (2025-02-04)

176
Cargo.lock generated
View File

@@ -47,9 +47,9 @@ checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]] [[package]]
name = "alloy" name = "alloy"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea8ebf106e84a1c37f86244df7da0c7587e697b71a0d565cce079449b85ac6f8" checksum = "bbcc41e8a11a4975b18ec6afba2cc48d591fa63336a4c526dacb50479a8d6b35"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-core", "alloy-core",
@@ -82,20 +82,35 @@ dependencies = [
[[package]] [[package]]
name = "alloy-consensus" name = "alloy-consensus"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41ed961a48297c732a5d97ee321aa8bb5009ecadbcb077d8bec90cb54e651629" checksum = "f4138dc275554afa6f18c4217262ac9388790b2fc393c2dfe03c51d357abf013"
dependencies = [ dependencies = [
"alloy-eips", "alloy-eips",
"alloy-primitives", "alloy-primitives",
"alloy-rlp", "alloy-rlp",
"alloy-serde", "alloy-serde",
"alloy-trie",
"auto_impl", "auto_impl",
"c-kzg", "c-kzg",
"derive_more", "derive_more",
"serde", "serde",
] ]
[[package]]
name = "alloy-consensus-any"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fa04e1882c31288ce1028fdf31b6ea94cfa9eafa2e497f903ded631c8c6a42c"
dependencies = [
"alloy-consensus",
"alloy-eips",
"alloy-primitives",
"alloy-rlp",
"alloy-serde",
"serde",
]
[[package]] [[package]]
name = "alloy-core" name = "alloy-core"
version = "0.8.19" version = "0.8.19"
@@ -140,9 +155,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-eip7702" name = "alloy-eip7702"
version = "0.3.2" version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ffc577390ce50234e02d841214b3dc0bea6aaaae8e04bbf3cb82e9a45da9eb" checksum = "cabf647eb4650c91a9d38cb6f972bb320009e7e9d61765fb688a86f1563b33e8"
dependencies = [ dependencies = [
"alloy-primitives", "alloy-primitives",
"alloy-rlp", "alloy-rlp",
@@ -152,9 +167,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-eips" name = "alloy-eips"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b69e06cf9c37be824b9d26d6d101114fdde6af0c87de2828b414c05c4b3daa71" checksum = "52dd5869ed09e399003e0e0ec6903d981b2a92e74c5d37e6b40890bad2517526"
dependencies = [ dependencies = [
"alloy-eip2930", "alloy-eip2930",
"alloy-eip7702", "alloy-eip7702",
@@ -170,12 +185,14 @@ dependencies = [
[[package]] [[package]]
name = "alloy-genesis" name = "alloy-genesis"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dde15e14944a88bd6a57d325e9a49b75558746fe16aaccc79713ae50a6a9574c" checksum = "e7d2a7fe5c1a9bd6793829ea21a636f30fc2b3f5d2e7418ba86d96e41dd1f460"
dependencies = [ dependencies = [
"alloy-eips",
"alloy-primitives", "alloy-primitives",
"alloy-serde", "alloy-serde",
"alloy-trie",
"serde", "serde",
] ]
@@ -193,29 +210,31 @@ dependencies = [
[[package]] [[package]]
name = "alloy-json-rpc" name = "alloy-json-rpc"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af5979e0d5a7bf9c7eb79749121e8256e59021af611322aee56e77e20776b4b3" checksum = "2008bedb8159a255b46b7c8614516eda06679ea82f620913679afbd8031fea72"
dependencies = [ dependencies = [
"alloy-primitives", "alloy-primitives",
"alloy-sol-types", "alloy-sol-types",
"serde", "serde",
"serde_json", "serde_json",
"thiserror 1.0.69", "thiserror 2.0.11",
"tracing", "tracing",
] ]
[[package]] [[package]]
name = "alloy-network" name = "alloy-network"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "204237129086ce5dc17a58025e93739b01b45313841f98fa339eb1d780511e57" checksum = "4556f01fe41d0677495df10a648ddcf7ce118b0e8aa9642a0e2b6dd1fb7259de"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-consensus-any",
"alloy-eips", "alloy-eips",
"alloy-json-rpc", "alloy-json-rpc",
"alloy-network-primitives", "alloy-network-primitives",
"alloy-primitives", "alloy-primitives",
"alloy-rpc-types-any",
"alloy-rpc-types-eth", "alloy-rpc-types-eth",
"alloy-serde", "alloy-serde",
"alloy-signer", "alloy-signer",
@@ -223,14 +242,16 @@ dependencies = [
"async-trait", "async-trait",
"auto_impl", "auto_impl",
"futures-utils-wasm", "futures-utils-wasm",
"thiserror 1.0.69", "serde",
"serde_json",
"thiserror 2.0.11",
] ]
[[package]] [[package]]
name = "alloy-network-primitives" name = "alloy-network-primitives"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "514f70ee2a953db21631cd817b13a1571474ec77ddc03d47616d5e8203489fde" checksum = "f31c3c6b71340a1d076831823f09cb6e02de01de5c6630a9631bdb36f947ff80"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-eips", "alloy-eips",
@@ -268,9 +289,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-provider" name = "alloy-provider"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4814d141ede360bb6cd1b4b064f1aab9de391e7c4d0d4d50ac89ea4bc1e25fbd" checksum = "5a22c4441b3ebe2d77fa9cf629ba68c3f713eb91779cff84275393db97eddd82"
dependencies = [ dependencies = [
"alloy-chains", "alloy-chains",
"alloy-consensus", "alloy-consensus",
@@ -296,7 +317,7 @@ dependencies = [
"schnellru", "schnellru",
"serde", "serde",
"serde_json", "serde_json",
"thiserror 1.0.69", "thiserror 2.0.11",
"tokio", "tokio",
"tracing", "tracing",
"url", "url",
@@ -327,9 +348,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-rpc-client" name = "alloy-rpc-client"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fc2bd1e7403463a5f2c61e955bcc9d3072b63aa177442b0f9aa6a6d22a941e3" checksum = "d06a292b37e182e514903ede6e623b9de96420e8109ce300da288a96d88b7e4b"
dependencies = [ dependencies = [
"alloy-json-rpc", "alloy-json-rpc",
"alloy-primitives", "alloy-primitives",
@@ -350,9 +371,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-rpc-types" name = "alloy-rpc-types"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eea9bf1abdd506f985a53533f5ac01296bcd6102c5e139bbc5d40bc468d2c916" checksum = "9383845dd924939e7ab0298bbfe231505e20928907d7905aa3bf112287305e06"
dependencies = [ dependencies = [
"alloy-primitives", "alloy-primitives",
"alloy-rpc-types-eth", "alloy-rpc-types-eth",
@@ -361,29 +382,41 @@ dependencies = [
] ]
[[package]] [[package]]
name = "alloy-rpc-types-eth" name = "alloy-rpc-types-any"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b034779a4850b4b03f5be5ea674a1cf7d746b2da762b34d1860ab45e48ca27" checksum = "ca445cef0eb6c2cf51cfb4e214fbf1ebd00893ae2e6f3b944c8101b07990f988"
dependencies = [
"alloy-consensus-any",
"alloy-rpc-types-eth",
"alloy-serde",
]
[[package]]
name = "alloy-rpc-types-eth"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0938bc615c02421bd86c1733ca7205cc3d99a122d9f9bff05726bd604b76a5c2"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-consensus-any",
"alloy-eips", "alloy-eips",
"alloy-network-primitives", "alloy-network-primitives",
"alloy-primitives", "alloy-primitives",
"alloy-rlp", "alloy-rlp",
"alloy-serde", "alloy-serde",
"alloy-sol-types", "alloy-sol-types",
"derive_more",
"itertools 0.13.0", "itertools 0.13.0",
"serde", "serde",
"serde_json", "serde_json",
"thiserror 2.0.11",
] ]
[[package]] [[package]]
name = "alloy-serde" name = "alloy-serde"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "028e72eaa9703e4882344983cfe7636ce06d8cce104a78ea62fd19b46659efc4" checksum = "ae0465c71d4dced7525f408d84873aeebb71faf807d22d74c4a426430ccd9b55"
dependencies = [ dependencies = [
"alloy-primitives", "alloy-primitives",
"serde", "serde",
@@ -392,9 +425,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-signer" name = "alloy-signer"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "592c185d7100258c041afac51877660c7bf6213447999787197db4842f0e938e" checksum = "9bfa395ad5cc952c82358d31e4c68b27bf4a89a5456d9b27e226e77dac50e4ff"
dependencies = [ dependencies = [
"alloy-dyn-abi", "alloy-dyn-abi",
"alloy-primitives", "alloy-primitives",
@@ -403,14 +436,14 @@ dependencies = [
"auto_impl", "auto_impl",
"elliptic-curve", "elliptic-curve",
"k256", "k256",
"thiserror 1.0.69", "thiserror 2.0.11",
] ]
[[package]] [[package]]
name = "alloy-signer-aws" name = "alloy-signer-aws"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a406102908a4e51834f32c4e5c1b29aa2c407b3fd23a5cad129c28b56d85e1b8" checksum = "0eb06810c34427d499863817eb506acf57cb9ded9224b374116cae4e22dbd4e9"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-network", "alloy-network",
@@ -420,15 +453,15 @@ dependencies = [
"aws-sdk-kms", "aws-sdk-kms",
"k256", "k256",
"spki", "spki",
"thiserror 1.0.69", "thiserror 2.0.11",
"tracing", "tracing",
] ]
[[package]] [[package]]
name = "alloy-signer-gcp" name = "alloy-signer-gcp"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8d363e12280cb43747d3b62a1e6f00d595bc1a56464bb20200c6b6ca5d68185" checksum = "d629e63fec8802ad53706d46e8eceeeae2b135c6648d0de41669a523bf17df4a"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-network", "alloy-network",
@@ -438,15 +471,15 @@ dependencies = [
"gcloud-sdk", "gcloud-sdk",
"k256", "k256",
"spki", "spki",
"thiserror 1.0.69", "thiserror 2.0.11",
"tracing", "tracing",
] ]
[[package]] [[package]]
name = "alloy-signer-ledger" name = "alloy-signer-ledger"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59a642c9f66ac73ae0d5398ce7ce3ce5bdfad5658d549abd48ea48962e585dca" checksum = "b426789566a19252cb46b757d91543a6f8e70330c72f312b86c5878595d092ef"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-dyn-abi", "alloy-dyn-abi",
@@ -458,15 +491,15 @@ dependencies = [
"coins-ledger", "coins-ledger",
"futures-util", "futures-util",
"semver 1.0.24", "semver 1.0.24",
"thiserror 1.0.69", "thiserror 2.0.11",
"tracing", "tracing",
] ]
[[package]] [[package]]
name = "alloy-signer-local" name = "alloy-signer-local"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6614f02fc1d5b079b2a4a5320018317b506fd0a6d67c1fd5542a71201724986c" checksum = "fbdc63ce9eda1283fcbaca66ba4a414b841c0e3edbeef9c86a71242fc9e84ccc"
dependencies = [ dependencies = [
"alloy-consensus", "alloy-consensus",
"alloy-network", "alloy-network",
@@ -475,7 +508,7 @@ dependencies = [
"async-trait", "async-trait",
"k256", "k256",
"rand", "rand",
"thiserror 1.0.69", "thiserror 2.0.11",
] ]
[[package]] [[package]]
@@ -550,9 +583,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-transport" name = "alloy-transport"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be77579633ebbc1266ae6fd7694f75c408beb1aeb6865d0b18f22893c265a061" checksum = "d17722a198f33bbd25337660787aea8b8f57814febb7c746bc30407bdfc39448"
dependencies = [ dependencies = [
"alloy-json-rpc", "alloy-json-rpc",
"base64 0.22.1", "base64 0.22.1",
@@ -560,7 +593,7 @@ dependencies = [
"futures-utils-wasm", "futures-utils-wasm",
"serde", "serde",
"serde_json", "serde_json",
"thiserror 1.0.69", "thiserror 2.0.11",
"tokio", "tokio",
"tower 0.5.2", "tower 0.5.2",
"tracing", "tracing",
@@ -570,9 +603,9 @@ dependencies = [
[[package]] [[package]]
name = "alloy-transport-http" name = "alloy-transport-http"
version = "0.5.4" version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91fd1a5d0827939847983b46f2f79510361f901dc82f8e3c38ac7397af142c6e" checksum = "6e1509599021330a31c4a6816b655e34bf67acb1cc03c564e09fd8754ff6c5de"
dependencies = [ dependencies = [
"alloy-json-rpc", "alloy-json-rpc",
"alloy-transport", "alloy-transport",
@@ -583,6 +616,22 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "alloy-trie"
version = "0.7.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6917c79e837aa7b77b7a6dae9f89cbe15313ac161c4d3cfaf8909ef21f3d22d8"
dependencies = [
"alloy-primitives",
"alloy-rlp",
"arrayvec",
"derive_more",
"nybbles",
"serde",
"smallvec",
"tracing",
]
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@@ -733,6 +782,9 @@ name = "arrayvec"
version = "0.7.6" version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "async-compression" name = "async-compression"
@@ -2590,6 +2642,19 @@ dependencies = [
"syn 2.0.96", "syn 2.0.96",
] ]
[[package]]
name = "nybbles"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8983bb634df7248924ee0c4c3a749609b5abcb082c28fffe3254b3eb3602b307"
dependencies = [
"alloy-rlp",
"const-hex",
"proptest",
"serde",
"smallvec",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.36.7" version = "0.36.7"
@@ -3644,6 +3709,9 @@ name = "smallvec"
version = "1.13.2" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "socket2" name = "socket2"
@@ -4163,7 +4231,7 @@ dependencies = [
[[package]] [[package]]
name = "tycho-execution" name = "tycho-execution"
version = "0.29.1" version = "0.30.1"
dependencies = [ dependencies = [
"alloy", "alloy",
"alloy-primitives", "alloy-primitives",
@@ -4470,9 +4538,9 @@ dependencies = [
[[package]] [[package]]
name = "wasmtimer" name = "wasmtimer"
version = "0.2.1" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7ed9d8b15c7fb594d72bfb4b5a276f3d2029333cd93a932f376f5937f6f80ee" checksum = "0048ad49a55b9deb3953841fa1fc5858f0efbcb7a18868c899a360269fac1b23"
dependencies = [ dependencies = [
"futures", "futures",
"js-sys", "js-sys",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "tycho-execution" name = "tycho-execution"
version = "0.29.1" version = "0.30.1"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
@@ -15,7 +15,7 @@ thiserror = "1.0.69"
tokio = { version = "1.38.0", features = ["full"] } tokio = { version = "1.38.0", features = ["full"] }
chrono = "0.4.39" chrono = "0.4.39"
alloy = { version = "0.5.4", features = ["providers", "rpc-types-eth", "eip712", "signer-local"], optional = true } alloy = { version = "0.9.2", features = ["providers", "rpc-types-eth", "eip712", "signer-local"], optional = true }
alloy-sol-types = { version = "0.8.14", optional = true } alloy-sol-types = { version = "0.8.14", optional = true }
alloy-primitives = { version = "0.8.9", optional = true } alloy-primitives = { version = "0.8.9", optional = true }
tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.46.0" } tycho-core = { git = "https://github.com/propeller-heads/tycho-indexer.git", package = "tycho-core", tag = "0.46.0" }

View File

@@ -4,9 +4,11 @@ use num_bigint::BigUint;
use tycho_core::{dto::ProtocolComponent, models::Chain, Bytes}; use tycho_core::{dto::ProtocolComponent, models::Chain, Bytes};
use tycho_execution::encoding::{ use tycho_execution::encoding::{
evm::{ evm::{
strategy_encoder::strategy_selector::EVMStrategySelector, tycho_encoder::EVMTychoEncoder, strategy_encoder::strategy_encoder_registry::EVMStrategyEncoderRegistry,
tycho_encoder::EVMTychoEncoder,
}, },
models::{Solution, Swap}, models::{Solution, Swap},
strategy_encoder::StrategyEncoderRegistry,
tycho_encoder::TychoEncoder, tycho_encoder::TychoEncoder,
}; };
@@ -17,11 +19,14 @@ fn main() {
Some("0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string()); Some("0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234".to_string());
let user_address = Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2") let user_address = Bytes::from_str("0xcd09f75E2BF2A4d11F3AB23f1389FcC1621c0cc2")
.expect("Failed to create user address"); .expect("Failed to create user address");
let executors_file_path = "src/encoding/config/executor_addresses.json";
// Initialize the encoder // Initialize the encoder
let encoder = let strategy_encoder_registry =
EVMTychoEncoder::new(EVMStrategySelector, router_address, signer_pk, Chain::Ethereum) EVMStrategyEncoderRegistry::new(Chain::Ethereum, executors_file_path, signer_pk.clone())
.expect("Failed to create encoder"); .expect("Failed to create strategy encoder registry");
let encoder = EVMTychoEncoder::new(strategy_encoder_registry, router_address)
.expect("Failed to create encoder");
// ------------------- Encode a simple swap ------------------- // ------------------- Encode a simple swap -------------------

View File

@@ -1,8 +1,6 @@
{ {
"executors": { "ethereum": {
"ethereum": { "uniswap_v2": "0x5C2F5a71f67c01775180ADc06909288B4C329308",
"uniswap_v2": "0x5C2F5a71f67c01775180ADc06909288B4C329308", "vm:balancer_v2": "0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"
"vm:balancer_v2": "0x543778987b293C7E8Cf0722BB2e935ba6f4068D4"
}
} }
} }

View File

@@ -7,9 +7,7 @@ use alloy::{
signers::{local::PrivateKeySigner, SignerSync}, signers::{local::PrivateKeySigner, SignerSync},
transports::BoxTransport, transports::BoxTransport,
}; };
#[allow(deprecated)] use alloy_primitives::{PrimitiveSignature as Signature, B256};
use alloy_primitives::Signature;
use alloy_primitives::B256;
use alloy_sol_types::{eip712_domain, sol, SolStruct, SolValue}; use alloy_sol_types::{eip712_domain, sol, SolStruct, SolValue};
use chrono::Utc; use chrono::Utc;
use num_bigint::BigUint; use num_bigint::BigUint;
@@ -117,7 +115,6 @@ impl Permit2 {
} }
} }
/// Creates permit single and signature /// Creates permit single and signature
#[allow(deprecated)]
pub fn get_permit( pub fn get_permit(
&self, &self,
spender: &Bytes, spender: &Bytes,

View File

@@ -1,2 +1,2 @@
pub mod strategy_encoder_registry;
mod strategy_encoders; mod strategy_encoders;
pub mod strategy_selector;

View File

@@ -0,0 +1,53 @@
use std::collections::HashMap;
use tycho_core::models::Chain;
use crate::encoding::{
errors::EncodingError,
evm::{
strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
},
models::Solution,
strategy_encoder::{StrategyEncoder, StrategyEncoderRegistry},
};
pub struct EVMStrategyEncoderRegistry {
strategies: HashMap<String, Box<dyn StrategyEncoder>>,
}
impl StrategyEncoderRegistry for EVMStrategyEncoderRegistry {
fn new(
chain: Chain,
executors_file_path: &str,
signer_pk: Option<String>,
) -> Result<Self, EncodingError> {
let swap_encoder_registry = SwapEncoderRegistry::new(executors_file_path, Chain::Ethereum)?;
let mut strategies: HashMap<String, Box<dyn StrategyEncoder>> = HashMap::new();
strategies.insert(
"executor".to_string(),
Box::new(ExecutorStrategyEncoder::new(swap_encoder_registry.clone())),
);
if let Some(signer) = signer_pk {
strategies.insert(
"split_swap".to_string(),
Box::new(
SplitSwapStrategyEncoder::new(signer, chain, swap_encoder_registry).unwrap(),
),
);
}
Ok(Self { strategies })
}
fn get_encoder(&self, solution: &Solution) -> Result<&Box<dyn StrategyEncoder>, EncodingError> {
if solution.direct_execution {
self.strategies
.get("executor")
.ok_or(EncodingError::FatalError("Executor strategy not found".to_string()))
} else {
self.strategies
.get("split_swap")
.ok_or(EncodingError::FatalError("Split swap strategy not found. Please pass the signer private key to the StrategySelector constructor".to_string()))
}
}
}

View File

@@ -14,11 +14,12 @@ use crate::encoding::{
evm::{ evm::{
approvals::permit2::Permit2, approvals::permit2::Permit2,
constants::{NATIVE_ADDRESS, WETH_ADDRESS}, constants::{NATIVE_ADDRESS, WETH_ADDRESS},
swap_encoder::SWAP_ENCODER_REGISTRY, swap_encoder::swap_encoder_registry::SwapEncoderRegistry,
utils::{biguint_to_u256, bytes_to_address, encode_input, percentage_to_uint24}, utils::{biguint_to_u256, bytes_to_address, encode_input, percentage_to_uint24},
}, },
models::{EncodingContext, NativeAction, Solution, Swap}, models::{EncodingContext, NativeAction, Solution, Swap},
strategy_encoder::StrategyEncoder, strategy_encoder::StrategyEncoder,
swap_encoder::SwapEncoder,
}; };
pub trait EVMStrategyEncoder: StrategyEncoder { pub trait EVMStrategyEncoder: StrategyEncoder {
@@ -58,14 +59,19 @@ pub trait EVMStrategyEncoder: StrategyEncoder {
} }
pub struct SplitSwapStrategyEncoder { pub struct SplitSwapStrategyEncoder {
swap_encoder_registry: SwapEncoderRegistry,
permit2: Permit2, permit2: Permit2,
selector: String, selector: String,
} }
impl SplitSwapStrategyEncoder { impl SplitSwapStrategyEncoder {
pub fn new(signer_pk: String, chain: Chain) -> Result<Self, EncodingError> { pub fn new(
signer_pk: String,
chain: Chain,
swap_encoder_registry: SwapEncoderRegistry,
) -> Result<Self, EncodingError> {
let selector = "swap(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string(); let selector = "swap(uint256,address,address,uint256,bool,bool,uint256,address,((address,uint160,uint48,uint48),address,uint256),bytes,bytes)".to_string();
Ok(Self { permit2: Permit2::new(signer_pk, chain)?, selector }) Ok(Self { permit2: Permit2::new(signer_pk, chain)?, selector, swap_encoder_registry })
} }
fn validate_split_percentages(&self, swaps: &[Swap]) -> Result<(), EncodingError> { fn validate_split_percentages(&self, swaps: &[Swap]) -> Result<(), EncodingError> {
@@ -286,16 +292,14 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
let mut swaps = vec![]; let mut swaps = vec![];
for swap in solution.swaps.iter() { for swap in solution.swaps.iter() {
let registry = SWAP_ENCODER_REGISTRY let swap_encoder = self
.read() .get_swap_encoder(&swap.component.protocol_system)
.map_err(|_| { .ok_or_else(|| {
EncodingError::FatalError( EncodingError::InvalidInput(format!(
"Failed to read the swap encoder registry".to_string(), "Swap encoder not found for protocol: {}",
) swap.component.protocol_system
))
})?; })?;
let swap_encoder = registry
.get_encoder(&swap.component.protocol_system)
.expect("Swap encoder not found");
let encoding_context = EncodingContext { let encoding_context = EncodingContext {
receiver: router_address.clone(), receiver: router_address.clone(),
@@ -353,11 +357,24 @@ impl StrategyEncoder for SplitSwapStrategyEncoder {
let contract_interaction = encode_input(&self.selector, method_calldata); let contract_interaction = encode_input(&self.selector, method_calldata);
Ok((contract_interaction, router_address)) Ok((contract_interaction, router_address))
} }
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
self.swap_encoder_registry
.get_encoder(protocol_system)
}
} }
/// This strategy encoder is used for solutions that are sent directly to the pool. /// This strategy encoder is used for solutions that are sent directly to the pool.
/// Only 1 solution with 1 swap is supported. /// Only 1 solution with 1 swap is supported.
pub struct ExecutorStrategyEncoder {} pub struct ExecutorStrategyEncoder {
swap_encoder_registry: SwapEncoderRegistry,
}
impl ExecutorStrategyEncoder {
pub fn new(swap_encoder_registry: SwapEncoderRegistry) -> Self {
Self { swap_encoder_registry }
}
}
impl EVMStrategyEncoder for ExecutorStrategyEncoder {} impl EVMStrategyEncoder for ExecutorStrategyEncoder {}
impl StrategyEncoder for ExecutorStrategyEncoder { impl StrategyEncoder for ExecutorStrategyEncoder {
fn encode_strategy( fn encode_strategy(
@@ -375,13 +392,9 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
.swaps .swaps
.first() .first()
.ok_or_else(|| EncodingError::InvalidInput("No swaps found in solution".to_string()))?; .ok_or_else(|| EncodingError::InvalidInput("No swaps found in solution".to_string()))?;
let registry = SWAP_ENCODER_REGISTRY
.read() let swap_encoder = self
.map_err(|_| { .get_swap_encoder(&swap.component.protocol_system)
EncodingError::FatalError("Failed to read the swap encoder registry".to_string())
})?;
let swap_encoder = registry
.get_encoder(&swap.component.protocol_system)
.ok_or_else(|| { .ok_or_else(|| {
EncodingError::InvalidInput(format!( EncodingError::InvalidInput(format!(
"Swap encoder not found for protocol: {}", "Swap encoder not found for protocol: {}",
@@ -400,6 +413,10 @@ impl StrategyEncoder for ExecutorStrategyEncoder {
.map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?; .map_err(|_| EncodingError::FatalError("Invalid executor address".to_string()))?;
Ok((protocol_data, executor_address)) Ok((protocol_data, executor_address))
} }
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
self.swap_encoder_registry
.get_encoder(protocol_system)
}
} }
#[cfg(test)] #[cfg(test)]
@@ -417,9 +434,15 @@ mod tests {
models::Swap, models::Swap,
}; };
fn get_swap_encoder_registry() -> SwapEncoderRegistry {
SwapEncoderRegistry::new("src/encoding/config/executor_addresses.json", Chain::Ethereum)
.unwrap()
}
#[test] #[test]
fn test_executor_strategy_encode() { fn test_executor_strategy_encode() {
let encoder = ExecutorStrategyEncoder {}; let swap_encoder_registry = get_swap_encoder_registry();
let encoder = ExecutorStrategyEncoder::new(swap_encoder_registry);
let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"); let token_in = Bytes::from("0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2");
let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f"); let token_out = Bytes::from("0x6b175474e89094c44da98b954eedeac495271d0f");
@@ -525,8 +548,10 @@ mod tests {
token_out: dai.clone(), token_out: dai.clone(),
split: 0f64, split: 0f64,
}; };
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum).unwrap(); let encoder =
SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry)
.unwrap();
let solution = Solution { let solution = Solution {
exact_out: false, exact_out: false,
given_token: weth, given_token: weth,
@@ -625,8 +650,10 @@ mod tests {
token_out: dai.clone(), token_out: dai.clone(),
split: 0f64, split: 0f64,
}; };
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum).unwrap(); let encoder =
SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry)
.unwrap();
let solution = Solution { let solution = Solution {
exact_out: false, exact_out: false,
given_token: NATIVE_ADDRESS.clone(), given_token: NATIVE_ADDRESS.clone(),
@@ -672,8 +699,10 @@ mod tests {
token_out: WETH_ADDRESS.clone(), token_out: WETH_ADDRESS.clone(),
split: 0f64, split: 0f64,
}; };
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum).unwrap(); let encoder =
SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry)
.unwrap();
let solution = Solution { let solution = Solution {
exact_out: false, exact_out: false,
given_token: dai, given_token: dai,
@@ -760,8 +789,10 @@ mod tests {
token_out: usdc.clone(), token_out: usdc.clone(),
split: 0f64, split: 0f64,
}; };
let swap_encoder_registry = get_swap_encoder_registry();
let encoder = SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum).unwrap(); let encoder =
SplitSwapStrategyEncoder::new(private_key, Chain::Ethereum, swap_encoder_registry)
.unwrap();
let solution = Solution { let solution = Solution {
exact_out: false, exact_out: false,
given_token: weth, given_token: weth,

View File

@@ -1,30 +0,0 @@
use tycho_core::models::Chain;
use crate::encoding::{
errors::EncodingError,
evm::strategy_encoder::strategy_encoders::{ExecutorStrategyEncoder, SplitSwapStrategyEncoder},
models::Solution,
strategy_encoder::{StrategyEncoder, StrategySelector},
};
pub struct EVMStrategySelector;
impl StrategySelector for EVMStrategySelector {
fn select_strategy(
&self,
solution: &Solution,
signer: Option<String>,
chain: Chain,
) -> Result<Box<dyn StrategyEncoder>, EncodingError> {
if solution.direct_execution {
Ok(Box::new(ExecutorStrategyEncoder {}))
} else {
let signer_pk = signer.ok_or_else(|| {
EncodingError::FatalError(
"Signer is required for SplitSwapStrategyEncoder".to_string(),
)
})?;
Ok(Box::new(SplitSwapStrategyEncoder::new(signer_pk, chain)?))
}
}
}

View File

@@ -1,4 +1,5 @@
use crate::encoding::{ use crate::encoding::{
errors::EncodingError,
evm::swap_encoder::swap_encoders::{BalancerV2SwapEncoder, UniswapV2SwapEncoder}, evm::swap_encoder::swap_encoders::{BalancerV2SwapEncoder, UniswapV2SwapEncoder},
swap_encoder::SwapEncoder, swap_encoder::SwapEncoder,
}; };
@@ -16,11 +17,14 @@ impl SwapEncoderBuilder {
} }
} }
pub fn build(self) -> Result<Box<dyn SwapEncoder>, String> { pub fn build(self) -> Result<Box<dyn SwapEncoder>, EncodingError> {
match self.protocol_system.as_str() { match self.protocol_system.as_str() {
"uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))), "uniswap_v2" => Ok(Box::new(UniswapV2SwapEncoder::new(self.executor_address))),
"vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))), "vm:balancer_v2" => Ok(Box::new(BalancerV2SwapEncoder::new(self.executor_address))),
_ => Err(format!("Unknown protocol system: {}", self.protocol_system)), _ => Err(EncodingError::FatalError(format!(
"Unknown protocol system: {}",
self.protocol_system
))),
} }
} }
} }

View File

@@ -1,19 +1,3 @@
mod builder; mod builder;
mod registry; pub mod swap_encoder_registry;
mod swap_encoders; mod swap_encoders;
use std::sync::RwLock;
use lazy_static::lazy_static;
use tycho_core::dto::Chain;
use crate::encoding::evm::swap_encoder::registry::{Config, SwapEncoderRegistry};
// TODO: init this at the higher level at some point
lazy_static! {
pub static ref SWAP_ENCODER_REGISTRY: RwLock<SwapEncoderRegistry> = {
let config = Config::from_file("src/encoding/config/executor_addresses.json")
.expect("Failed to load configuration file");
RwLock::new(SwapEncoderRegistry::new(config, Chain::Ethereum))
};
}

View File

@@ -1,51 +0,0 @@
use std::{collections::HashMap, fs};
use serde::Deserialize;
use tycho_core::dto::Chain;
use crate::encoding::{
errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder,
swap_encoder::SwapEncoder,
};
pub struct SwapEncoderRegistry {
encoders: HashMap<String, Box<dyn SwapEncoder>>,
}
impl SwapEncoderRegistry {
pub fn new(config: Config, blockchain: Chain) -> Self {
let mut encoders = HashMap::new();
let executors = config
.executors
.get(&blockchain)
.unwrap_or_else(|| panic!("No executors found for blockchain: {}", blockchain));
for (protocol, executor_address) in executors {
let builder = SwapEncoderBuilder::new(protocol, executor_address);
let encoder = builder.build().unwrap_or_else(|_| {
panic!("Failed to build swap encoder for protocol: {}", protocol)
});
encoders.insert(protocol.to_string(), encoder);
}
Self { encoders }
}
#[allow(clippy::borrowed_box)]
pub fn get_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
self.encoders.get(protocol_system)
}
}
#[derive(Deserialize)]
pub struct Config {
pub executors: HashMap<Chain, HashMap<String, String>>, /* Blockchain -> {Protocol ->
* Executor address} mapping */
}
impl Config {
pub fn from_file(path: &str) -> Result<Self, EncodingError> {
let config_str = fs::read_to_string(path)?;
let config: Config = serde_json::from_str(&config_str)?;
Ok(config)
}
}

View File

@@ -0,0 +1,36 @@
use std::{collections::HashMap, fs};
use tycho_core::models::Chain;
use crate::encoding::{
errors::EncodingError, evm::swap_encoder::builder::SwapEncoderBuilder,
swap_encoder::SwapEncoder,
};
#[derive(Clone)]
pub struct SwapEncoderRegistry {
encoders: HashMap<String, Box<dyn SwapEncoder>>,
}
impl SwapEncoderRegistry {
pub fn new(executors_file_path: &str, blockchain: Chain) -> Result<Self, EncodingError> {
let config_str = fs::read_to_string(executors_file_path)?;
let config: HashMap<Chain, HashMap<String, String>> = serde_json::from_str(&config_str)?;
let mut encoders = HashMap::new();
let executors = config
.get(&blockchain)
.ok_or(EncodingError::FatalError("No executors found for blockchain".to_string()))?;
for (protocol, executor_address) in executors {
let builder = SwapEncoderBuilder::new(protocol, executor_address);
let encoder = builder.build()?;
encoders.insert(protocol.to_string(), encoder);
}
Ok(Self { encoders })
}
#[allow(clippy::borrowed_box)]
pub fn get_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
self.encoders.get(protocol_system)
}
}

View File

@@ -12,6 +12,7 @@ use crate::encoding::{
swap_encoder::SwapEncoder, swap_encoder::SwapEncoder,
}; };
#[derive(Clone)]
pub struct UniswapV2SwapEncoder { pub struct UniswapV2SwapEncoder {
executor_address: String, executor_address: String,
executor_selector: String, executor_selector: String,
@@ -59,8 +60,13 @@ impl SwapEncoder for UniswapV2SwapEncoder {
fn executor_selector(&self) -> &str { fn executor_selector(&self) -> &str {
&self.executor_selector &self.executor_selector
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> {
Box::new(self.clone())
}
} }
#[derive(Clone)]
pub struct UniswapV3SwapEncoder { pub struct UniswapV3SwapEncoder {
executor_address: String, executor_address: String,
executor_selector: String, executor_selector: String,
@@ -129,8 +135,12 @@ impl SwapEncoder for UniswapV3SwapEncoder {
fn executor_selector(&self) -> &str { fn executor_selector(&self) -> &str {
&self.executor_selector &self.executor_selector
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> {
Box::new(self.clone())
}
} }
#[derive(Clone)]
pub struct BalancerV2SwapEncoder { pub struct BalancerV2SwapEncoder {
executor_address: String, executor_address: String,
executor_selector: String, executor_selector: String,
@@ -179,6 +189,9 @@ impl SwapEncoder for BalancerV2SwapEncoder {
fn executor_selector(&self) -> &str { fn executor_selector(&self) -> &str {
&self.executor_selector &self.executor_selector
} }
fn clone_box(&self) -> Box<dyn SwapEncoder> {
Box::new(self.clone())
}
} }
#[cfg(test)] #[cfg(test)]

View File

@@ -1,37 +1,30 @@
use std::str::FromStr; use std::str::FromStr;
use num_bigint::BigUint; use num_bigint::BigUint;
use tycho_core::{models::Chain, Bytes}; use tycho_core::Bytes;
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
evm::constants::{NATIVE_ADDRESS, WETH_ADDRESS}, evm::constants::{NATIVE_ADDRESS, WETH_ADDRESS},
models::{NativeAction, Solution, Transaction}, models::{NativeAction, Solution, Transaction},
strategy_encoder::StrategySelector, strategy_encoder::StrategyEncoderRegistry,
tycho_encoder::TychoEncoder, tycho_encoder::TychoEncoder,
}; };
pub struct EVMTychoEncoder<S: StrategySelector> { pub struct EVMTychoEncoder<S: StrategyEncoderRegistry> {
strategy_selector: S, strategy_selector: S,
signer_pk: Option<String>,
chain: Chain,
router_address: Bytes, router_address: Bytes,
} }
impl<S: StrategySelector> EVMTychoEncoder<S> { impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
pub fn new( pub fn new(strategy_selector: S, router_address: String) -> Result<Self, EncodingError> {
strategy_selector: S,
router_address: String,
signer_pk: Option<String>,
chain: Chain,
) -> Result<Self, EncodingError> {
let router_address = Bytes::from_str(&router_address) let router_address = Bytes::from_str(&router_address)
.map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?; .map_err(|_| EncodingError::FatalError("Invalid router address".to_string()))?;
Ok(EVMTychoEncoder { strategy_selector, signer_pk, chain, router_address }) Ok(EVMTychoEncoder { strategy_selector, router_address })
} }
} }
impl<S: StrategySelector> EVMTychoEncoder<S> { impl<S: StrategyEncoderRegistry> EVMTychoEncoder<S> {
fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> { fn validate_solution(&self, solution: &Solution) -> Result<(), EncodingError> {
if solution.exact_out { if solution.exact_out {
return Err(EncodingError::FatalError( return Err(EncodingError::FatalError(
@@ -74,7 +67,7 @@ impl<S: StrategySelector> EVMTychoEncoder<S> {
} }
} }
impl<S: StrategySelector> TychoEncoder<S> for EVMTychoEncoder<S> { impl<S: StrategyEncoderRegistry> TychoEncoder<S> for EVMTychoEncoder<S> {
fn encode_router_calldata( fn encode_router_calldata(
&self, &self,
solutions: Vec<Solution>, solutions: Vec<Solution>,
@@ -88,11 +81,9 @@ impl<S: StrategySelector> TychoEncoder<S> for EVMTychoEncoder<S> {
.clone() .clone()
.unwrap_or(self.router_address.clone()); .unwrap_or(self.router_address.clone());
let strategy = self.strategy_selector.select_strategy( let strategy = self
solution, .strategy_selector
self.signer_pk.clone(), .get_encoder(solution)?;
self.chain,
)?;
let (contract_interaction, target_address) = let (contract_interaction, target_address) =
strategy.encode_strategy(solution.clone(), router_address)?; strategy.encode_strategy(solution.clone(), router_address)?;
@@ -113,25 +104,35 @@ impl<S: StrategySelector> TychoEncoder<S> for EVMTychoEncoder<S> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use tycho_core::dto::ProtocolComponent; use tycho_core::{dto::ProtocolComponent, models::Chain};
use super::*; use super::*;
use crate::encoding::{models::Swap, strategy_encoder::StrategyEncoder}; use crate::encoding::{
models::Swap, strategy_encoder::StrategyEncoder, swap_encoder::SwapEncoder,
};
struct MockStrategySelector; struct MockStrategyRegistry {
strategy: Box<dyn StrategyEncoder>,
}
fn dai() -> Bytes { fn dai() -> Bytes {
Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap() Bytes::from_str("0x6b175474e89094c44da98b954eedeac495271d0f").unwrap()
} }
impl StrategySelector for MockStrategySelector { impl StrategyEncoderRegistry for MockStrategyRegistry {
fn select_strategy( fn new(
_chain: Chain,
_executors_file_path: &str,
_signer_pk: Option<String>,
) -> Result<MockStrategyRegistry, EncodingError> {
Ok(Self { strategy: Box::new(MockStrategy) })
}
fn get_encoder(
&self, &self,
_solution: &Solution, _solution: &Solution,
_signer: Option<String>, ) -> Result<&Box<dyn StrategyEncoder>, EncodingError> {
_chain: Chain, Ok(&self.strategy)
) -> Result<Box<dyn StrategyEncoder>, EncodingError> {
Ok(Box::new(MockStrategy))
} }
} }
@@ -150,15 +151,17 @@ mod tests {
Bytes::from_str("0xabcd").unwrap(), Bytes::from_str("0xabcd").unwrap(),
)) ))
} }
fn get_swap_encoder(&self, _protocol_system: &str) -> Option<&Box<dyn SwapEncoder>> {
None
}
} }
fn get_mocked_tycho_encoder() -> EVMTychoEncoder<MockStrategySelector> { fn get_mocked_tycho_encoder() -> EVMTychoEncoder<MockStrategyRegistry> {
let strategy_selector = MockStrategySelector; let strategy_selector = MockStrategyRegistry::new(Chain::Ethereum, "", None).unwrap();
EVMTychoEncoder::new( EVMTychoEncoder::new(
strategy_selector, strategy_selector,
"0x1234567890abcdef1234567890abcdef12345678".to_string(), "0x1234567890abcdef1234567890abcdef12345678".to_string(),
Some("0xabcdef".to_string()),
Chain::Ethereum,
) )
.unwrap() .unwrap()
} }

View File

@@ -2,6 +2,6 @@ mod errors;
#[cfg(feature = "evm")] #[cfg(feature = "evm")]
pub mod evm; pub mod evm;
pub mod models; pub mod models;
mod strategy_encoder; pub mod strategy_encoder;
mod swap_encoder; mod swap_encoder;
pub mod tycho_encoder; pub mod tycho_encoder;

View File

@@ -1,6 +1,6 @@
use tycho_core::{models::Chain, Bytes}; use tycho_core::{models::Chain, Bytes};
use crate::encoding::{errors::EncodingError, models::Solution}; use crate::encoding::{errors::EncodingError, models::Solution, swap_encoder::SwapEncoder};
pub trait StrategyEncoder { pub trait StrategyEncoder {
fn encode_strategy( fn encode_strategy(
@@ -8,13 +8,19 @@ pub trait StrategyEncoder {
to_encode: Solution, to_encode: Solution,
router_address: Bytes, router_address: Bytes,
) -> Result<(Vec<u8>, Bytes), EncodingError>; ) -> Result<(Vec<u8>, Bytes), EncodingError>;
#[allow(clippy::borrowed_box)]
fn get_swap_encoder(&self, protocol_system: &str) -> Option<&Box<dyn SwapEncoder>>;
} }
pub trait StrategySelector { pub trait StrategyEncoderRegistry {
fn select_strategy( fn new(
&self, chain: Chain,
solution: &Solution, executors_file_path: &str,
signer_pk: Option<String>, signer_pk: Option<String>,
chain_id: Chain, ) -> Result<Self, EncodingError>
) -> Result<Box<dyn StrategyEncoder>, EncodingError>; where
Self: Sized;
#[allow(clippy::borrowed_box)]
fn get_encoder(&self, solution: &Solution) -> Result<&Box<dyn StrategyEncoder>, EncodingError>;
} }

View File

@@ -2,7 +2,6 @@ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
models::{EncodingContext, Swap}, models::{EncodingContext, Swap},
}; };
#[allow(dead_code)]
pub trait SwapEncoder: Sync + Send { pub trait SwapEncoder: Sync + Send {
fn new(executor_address: String) -> Self fn new(executor_address: String) -> Self
where where
@@ -14,4 +13,14 @@ pub trait SwapEncoder: Sync + Send {
) -> Result<Vec<u8>, EncodingError>; ) -> Result<Vec<u8>, EncodingError>;
fn executor_address(&self) -> &str; fn executor_address(&self) -> &str;
fn executor_selector(&self) -> &str; fn executor_selector(&self) -> &str;
/// Clones the swap encoder as a trait object.
/// This allows the encoder to be cloned when it is being used as a `Box<dyn SwapEncoder>`.
fn clone_box(&self) -> Box<dyn SwapEncoder>;
}
impl Clone for Box<dyn SwapEncoder> {
fn clone(&self) -> Box<dyn SwapEncoder> {
self.clone_box()
}
} }

View File

@@ -1,10 +1,10 @@
use crate::encoding::{ use crate::encoding::{
errors::EncodingError, errors::EncodingError,
models::{Solution, Transaction}, models::{Solution, Transaction},
strategy_encoder::StrategySelector, strategy_encoder::StrategyEncoderRegistry,
}; };
pub trait TychoEncoder<S: StrategySelector> { pub trait TychoEncoder<S: StrategyEncoderRegistry> {
fn encode_router_calldata( fn encode_router_calldata(
&self, &self,
solutions: Vec<Solution>, solutions: Vec<Solution>,