-
Notifications
You must be signed in to change notification settings - Fork 39
Add Yielder crate #816
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
Draft
0xh3rman
wants to merge
6
commits into
main
Choose a base branch
from
yielder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Yielder crate #816
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
db3dc5c
add yielder
0xh3rman 37d5a4c
add apy
0xh3rman 5b4482f
Merge branch 'main' into yielder
0xh3rman 01dffbd
Merge branch 'main' into yielder
0xh3rman 3931134
code improvement
0xh3rman b049bd2
Merge branch 'main' into yielder
0xh3rman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| [package] | ||
| name = "yielder" | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| license.workspace = true | ||
| homepage.workspace = true | ||
| description.workspace = true | ||
| repository.workspace = true | ||
| documentation.workspace = true | ||
|
|
||
| [features] | ||
| default = [] | ||
| yield_integration_tests = ["gem_jsonrpc/reqwest", "gem_client/reqwest", "tokio/rt-multi-thread"] | ||
|
|
||
| [dependencies] | ||
| alloy-primitives = { workspace = true } | ||
| alloy-sol-types = { workspace = true } | ||
| gem_client = { path = "../gem_client" } | ||
| gem_evm = { path = "../gem_evm", features = ["rpc"] } | ||
| primitives = { path = "../primitives" } | ||
| async-trait = { workspace = true } | ||
| num-traits = { workspace = true } | ||
| serde_json = { workspace = true } | ||
| tokio = { workspace = true, features = ["macros"] } | ||
|
|
||
| [dev-dependencies] | ||
| gem_client = { path = "../gem_client", features = ["reqwest"] } | ||
| gem_jsonrpc = { path = "../gem_jsonrpc", features = ["reqwest"] } | ||
| reqwest = { workspace = true } | ||
| tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| mod provider; | ||
| pub mod yo; | ||
|
|
||
| pub use provider::{Yield, YieldDetailsRequest, YieldPosition, YieldProvider, YieldProviderClient, YieldTransaction, Yielder}; | ||
| pub use yo::{IYoGateway, YO_GATEWAY_BASE_MAINNET, YO_PARTNER_ID_GEM, YO_USD, YieldError, YoGatewayClient, YoProvider, YoVault, YoYieldProvider, vaults}; | ||
|
|
||
| #[cfg(all(test, feature = "yield_integration_tests"))] | ||
| mod yield_integration_tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| use std::{fmt, str::FromStr, sync::Arc}; | ||
|
|
||
| use alloy_primitives::Address; | ||
| use async_trait::async_trait; | ||
| use primitives::{AssetId, Chain}; | ||
|
|
||
| use crate::yo::YieldError; | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum YieldProvider { | ||
| Yo, | ||
| } | ||
|
|
||
| impl YieldProvider { | ||
| pub fn name(&self) -> &'static str { | ||
| match self { | ||
| YieldProvider::Yo => "yo", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for YieldProvider { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| f.write_str(self.name()) | ||
| } | ||
| } | ||
|
|
||
| impl FromStr for YieldProvider { | ||
| type Err = YieldError; | ||
|
|
||
| fn from_str(value: &str) -> Result<Self, Self::Err> { | ||
| match value.to_ascii_lowercase().as_str() { | ||
| "yo" => Ok(YieldProvider::Yo), | ||
| other => Err(YieldError::new(format!("unknown yield provider {other}"))), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct Yield { | ||
| pub name: String, | ||
| pub asset_id: AssetId, | ||
| pub provider: YieldProvider, | ||
| pub apy: Option<f64>, | ||
| } | ||
|
|
||
| impl Yield { | ||
| pub fn new(name: impl Into<String>, asset_id: AssetId, provider: YieldProvider, apy: Option<f64>) -> Self { | ||
| Self { | ||
| name: name.into(), | ||
| asset_id, | ||
| provider, | ||
| apy, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct YieldTransaction { | ||
| pub chain: Chain, | ||
| pub from: String, | ||
| pub to: String, | ||
| pub data: String, | ||
| pub value: Option<String>, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct YieldDetailsRequest { | ||
| pub asset_id: AssetId, | ||
| pub wallet_address: String, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub struct YieldPosition { | ||
| pub asset_id: AssetId, | ||
| pub provider: YieldProvider, | ||
| pub vault_token_address: String, | ||
| pub asset_token_address: String, | ||
| pub vault_balance_value: Option<String>, | ||
| pub asset_balance_value: Option<String>, | ||
| pub apy: Option<f64>, | ||
| pub rewards: Option<String>, | ||
| } | ||
|
|
||
| impl YieldPosition { | ||
| pub fn new(asset_id: AssetId, provider: YieldProvider, share_token: Address, asset_token: Address) -> Self { | ||
| Self { | ||
| asset_id, | ||
| provider, | ||
| vault_token_address: share_token.to_string(), | ||
| asset_token_address: asset_token.to_string(), | ||
| vault_balance_value: None, | ||
| asset_balance_value: None, | ||
| apy: None, | ||
| rewards: None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| pub trait YieldProviderClient: Send + Sync { | ||
| fn provider(&self) -> YieldProvider; | ||
| fn yields(&self, asset_id: &AssetId) -> Vec<Yield>; | ||
| async fn deposit(&self, asset_id: &AssetId, wallet_address: &str, value: &str) -> Result<YieldTransaction, YieldError>; | ||
| async fn withdraw(&self, asset_id: &AssetId, wallet_address: &str, value: &str) -> Result<YieldTransaction, YieldError>; | ||
| async fn positions(&self, request: &YieldDetailsRequest) -> Result<YieldPosition, YieldError>; | ||
| async fn yields_with_apy(&self, asset_id: &AssetId) -> Result<Vec<Yield>, YieldError> { | ||
| Ok(self.yields(asset_id)) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| pub struct Yielder { | ||
| providers: Vec<Arc<dyn YieldProviderClient>>, | ||
| } | ||
|
|
||
| impl Yielder { | ||
| pub fn new() -> Self { | ||
| Self { providers: Vec::new() } | ||
| } | ||
|
|
||
| pub fn with_providers(providers: Vec<Arc<dyn YieldProviderClient>>) -> Self { | ||
| Self { providers } | ||
| } | ||
|
|
||
| pub fn add_provider<P>(&mut self, provider: P) | ||
| where | ||
| P: YieldProviderClient + 'static, | ||
| { | ||
| self.providers.push(Arc::new(provider)); | ||
| } | ||
|
|
||
| pub fn add_provider_arc(&mut self, provider: Arc<dyn YieldProviderClient>) { | ||
| self.providers.push(provider); | ||
| } | ||
|
|
||
| pub fn yields_for_asset(&self, asset_id: &AssetId) -> Vec<Yield> { | ||
| self.providers.iter().flat_map(|provider| provider.yields(asset_id)).collect() | ||
| } | ||
|
|
||
| pub async fn yields_for_asset_with_apy(&self, asset_id: &AssetId) -> Result<Vec<Yield>, YieldError> { | ||
| let mut yields = Vec::new(); | ||
| for provider in &self.providers { | ||
| let mut provider_yields = provider.yields_with_apy(asset_id).await?; | ||
| yields.append(&mut provider_yields); | ||
| } | ||
| Ok(yields) | ||
| } | ||
|
|
||
| pub async fn deposit(&self, provider: YieldProvider, asset_id: &AssetId, wallet_address: &str, value: &str) -> Result<YieldTransaction, YieldError> { | ||
| let provider = self.provider(provider)?; | ||
| provider.deposit(asset_id, wallet_address, value).await | ||
| } | ||
|
|
||
| pub async fn withdraw(&self, provider: YieldProvider, asset_id: &AssetId, wallet_address: &str, value: &str) -> Result<YieldTransaction, YieldError> { | ||
| let provider = self.provider(provider)?; | ||
| provider.withdraw(asset_id, wallet_address, value).await | ||
| } | ||
|
|
||
| pub async fn positions(&self, provider: YieldProvider, request: &YieldDetailsRequest) -> Result<YieldPosition, YieldError> { | ||
| let provider = self.provider(provider)?; | ||
| provider.positions(request).await | ||
| } | ||
|
|
||
| fn provider(&self, provider: YieldProvider) -> Result<Arc<dyn YieldProviderClient>, YieldError> { | ||
| self.providers | ||
| .iter() | ||
| .find(|candidate| candidate.provider() == provider) | ||
| .cloned() | ||
| .ok_or_else(|| YieldError::new(format!("provider {provider} not found"))) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #![cfg(all(test, feature = "yield_integration_tests"))] | ||
|
|
||
| use std::sync::Arc; | ||
|
|
||
| use gem_evm::rpc::EthereumClient; | ||
| use gem_jsonrpc::client::JsonRpcClient; | ||
| use primitives::EVMChain; | ||
|
|
||
| use crate::{YO_GATEWAY_BASE_MAINNET, YO_USD, YieldDetailsRequest, YieldProvider, YieldProviderClient, Yielder, YoGatewayClient, YoYieldProvider}; | ||
|
|
||
| #[tokio::test] | ||
| async fn yield_integration_test_fetches_performance_apy() -> Result<(), Box<dyn std::error::Error + Send + Sync>> { | ||
| let rpc_url = std::env::var("BASE_RPC_URL").unwrap_or_else(|_| "https://mainnet.base.org".to_string()); | ||
| let jsonrpc_client = JsonRpcClient::new_reqwest(rpc_url); | ||
| let ethereum_client = EthereumClient::new(jsonrpc_client, EVMChain::Base); | ||
| let gateway_client = YoGatewayClient::new(ethereum_client, YO_GATEWAY_BASE_MAINNET); | ||
| let provider: Arc<dyn YieldProviderClient> = Arc::new(YoYieldProvider::new(Arc::new(gateway_client))); | ||
| let yielder = Yielder::with_providers(vec![provider]); | ||
|
|
||
| let apy_yields = yielder.yields_for_asset_with_apy(&YO_USD.asset_id()).await?; | ||
| assert!(!apy_yields.is_empty(), "expected at least one Yo vault for asset"); | ||
| let apy = apy_yields[0].apy.expect("apy should be computed"); | ||
| assert!(apy.is_finite(), "apy should be finite"); | ||
| assert!(apy > -1.0, "apy should be > -100%"); | ||
|
|
||
| let details = yielder | ||
| .positions( | ||
| YieldProvider::Yo, | ||
| &YieldDetailsRequest { | ||
| asset_id: YO_USD.asset_id(), | ||
| wallet_address: "0x0000000000000000000000000000000000000000".to_string(), | ||
| }, | ||
| ) | ||
| .await?; | ||
|
|
||
| assert!(details.apy.is_some(), "apy should be present in details"); | ||
|
|
||
| Ok(()) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.