-
Notifications
You must be signed in to change notification settings - Fork 5
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
apy #945
Open
rouzwelt
wants to merge
31
commits into
main
Choose a base branch
from
2024-10-16-apy
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.
Open
apy #945
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
79f5f40
init
rouzwelt 22c3e03
Revert "init"
rouzwelt 4fdb5a6
apy logic
rouzwelt c61d43b
ui
rouzwelt 6edab3b
update
rouzwelt 7968399
apply requested changes
rouzwelt 6b27e3e
Update apy.rs
rouzwelt 04155e8
Update apy.rs
rouzwelt 621a180
Update apy.rs
rouzwelt c2804e4
Update apy.rs
rouzwelt 4c69002
Update apy.rs
rouzwelt b88b0c2
Update apy.rs
rouzwelt 59a2a39
Update apy.rs
rouzwelt f509d86
Update apy.rs
rouzwelt 06cd19a
Update apy.rs
rouzwelt 8fe210d
Update apy.rs
rouzwelt e5cae7d
Update apy.rs
rouzwelt 931bf38
Update apy.rs
rouzwelt 78abfc7
Update apy.rs
rouzwelt 36a34f7
fix pick denomination
rouzwelt d4883d7
update [skip ci]
rouzwelt b659f5b
update
rouzwelt cdadbf5
Merge branch 'main' into 2024-10-16-apy
rouzwelt 31f54d7
apply requested changes
rouzwelt 54d21b3
update
rouzwelt 5b65481
Update number.ts
rouzwelt 51b5f86
Update number.ts
rouzwelt 6aa1bcc
update
rouzwelt ad43d24
Merge branch 'main' into 2024-10-16-apy
thedavidmeister c5adc1c
apply requested changes
rouzwelt b32e441
Merge branch 'main' into 2024-10-16-apy
rouzwelt 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod apy; | ||
mod cynic_client; | ||
mod orderbook_client; | ||
mod pagination; | ||
|
This file contains 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 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 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,191 @@ | ||
use super::common::*; | ||
use crate::utils::{one_18, to_18_decimals}; | ||
use alloy::primitives::{ | ||
utils::{ParseUnits, UnitsError}, | ||
I256, U256, | ||
}; | ||
use std::str::FromStr; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum ParseUnitsError { | ||
#[error(transparent)] | ||
UnitsError(#[from] UnitsError), | ||
#[error(transparent)] | ||
ParseUnsignedError(#[from] alloy::primitives::ruint::ParseError), | ||
#[error(transparent)] | ||
ParseSignedError(#[from] alloy::primitives::ParseSignedError), | ||
#[error(transparent)] | ||
BigIntConversionError(#[from] alloy::primitives::BigIntConversionError), | ||
} | ||
|
||
impl Trade { | ||
/// Converts this trade's input to 18 point decimals in U256/I256 | ||
pub fn input_to_18_decimals(&self) -> Result<ParseUnits, ParseUnitsError> { | ||
Ok(to_18_decimals( | ||
ParseUnits::U256(U256::from_str(&self.input_vault_balance_change.amount.0)?), | ||
self.input_vault_balance_change | ||
.vault | ||
.token | ||
.decimals | ||
.as_ref() | ||
.map(|v| v.0.as_str()) | ||
.unwrap_or("18"), | ||
)?) | ||
} | ||
|
||
/// Converts this trade's output to 18 point decimals in U256/I256 | ||
pub fn output_to_18_decimals(&self) -> Result<ParseUnits, ParseUnitsError> { | ||
Ok(to_18_decimals( | ||
ParseUnits::I256(I256::from_str(&self.output_vault_balance_change.amount.0)?), | ||
self.output_vault_balance_change | ||
.vault | ||
.token | ||
.decimals | ||
.as_ref() | ||
.map(|v| v.0.as_str()) | ||
.unwrap_or("18"), | ||
)?) | ||
} | ||
|
||
/// Calculates the trade's I/O ratio | ||
pub fn ratio(&self) -> Result<U256, ParseUnitsError> { | ||
Ok(self | ||
.input_to_18_decimals()? | ||
.get_absolute() | ||
.saturating_mul(one_18().get_absolute()) | ||
.checked_div( | ||
self.output_to_18_decimals()? | ||
.get_signed() | ||
.saturating_neg() | ||
.try_into()?, | ||
) | ||
.unwrap_or(U256::MAX)) | ||
} | ||
|
||
/// Calculates the trade's O/I ratio (inverse) | ||
pub fn inverse_ratio(&self) -> Result<U256, ParseUnitsError> { | ||
Ok( | ||
TryInto::<U256>::try_into(self.output_to_18_decimals()?.get_signed().saturating_neg())? | ||
.saturating_mul(one_18().get_absolute()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as below |
||
.checked_div(self.input_to_18_decimals()?.get_absolute()) | ||
.unwrap_or(U256::MAX), | ||
) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use crate::types::common::{ | ||
BigInt, Bytes, Orderbook, TradeEvent, TradeStructPartialOrder, TradeVaultBalanceChange, | ||
Transaction, VaultBalanceChangeVault, | ||
}; | ||
use alloy::primitives::Address; | ||
|
||
#[test] | ||
fn test_input_to_18_decimals() { | ||
let result = get_trade().input_to_18_decimals().unwrap(); | ||
let expected = U256::from_str("3000000000000000000").unwrap(); | ||
assert_eq!(result.get_absolute(), expected); | ||
} | ||
|
||
#[test] | ||
fn test_output_to_18_decimals() { | ||
let result = get_trade().output_to_18_decimals().unwrap(); | ||
let expected = I256::from_str("-6000000000000000000").unwrap(); | ||
assert_eq!(result.get_signed(), expected); | ||
} | ||
|
||
#[test] | ||
fn test_ratio() { | ||
let result = get_trade().ratio().unwrap(); | ||
let expected = U256::from_str("500000000000000000").unwrap(); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
#[test] | ||
fn test_inverse_ratio() { | ||
let result = get_trade().inverse_ratio().unwrap(); | ||
let expected = U256::from_str("2000000000000000000").unwrap(); | ||
assert_eq!(result, expected); | ||
} | ||
|
||
// helper to get trade struct | ||
fn get_trade() -> Trade { | ||
let token_address = Address::from_slice(&[0x11u8; 20]); | ||
let token = Erc20 { | ||
id: Bytes(token_address.to_string()), | ||
address: Bytes(token_address.to_string()), | ||
name: Some("Token1".to_string()), | ||
symbol: Some("Token1".to_string()), | ||
decimals: Some(BigInt(6.to_string())), | ||
}; | ||
let input_trade_vault_balance_change = TradeVaultBalanceChange { | ||
id: Bytes("".to_string()), | ||
__typename: "".to_string(), | ||
amount: BigInt("3000000".to_string()), | ||
new_vault_balance: BigInt("".to_string()), | ||
old_vault_balance: BigInt("".to_string()), | ||
vault: VaultBalanceChangeVault { | ||
id: Bytes("".to_string()), | ||
vault_id: BigInt("".to_string()), | ||
token: token.clone(), | ||
}, | ||
timestamp: BigInt("".to_string()), | ||
transaction: Transaction { | ||
id: Bytes("".to_string()), | ||
from: Bytes("".to_string()), | ||
block_number: BigInt("".to_string()), | ||
timestamp: BigInt("".to_string()), | ||
}, | ||
orderbook: Orderbook { | ||
id: Bytes("".to_string()), | ||
}, | ||
}; | ||
let output_trade_vault_balance_change = TradeVaultBalanceChange { | ||
id: Bytes("".to_string()), | ||
__typename: "".to_string(), | ||
amount: BigInt("-6000000".to_string()), | ||
new_vault_balance: BigInt("".to_string()), | ||
old_vault_balance: BigInt("".to_string()), | ||
vault: VaultBalanceChangeVault { | ||
id: Bytes("".to_string()), | ||
vault_id: BigInt("".to_string()), | ||
token: token.clone(), | ||
}, | ||
timestamp: BigInt("".to_string()), | ||
transaction: Transaction { | ||
id: Bytes("".to_string()), | ||
from: Bytes("".to_string()), | ||
block_number: BigInt("".to_string()), | ||
timestamp: BigInt("".to_string()), | ||
}, | ||
orderbook: Orderbook { | ||
id: Bytes("".to_string()), | ||
}, | ||
}; | ||
Trade { | ||
id: Bytes("".to_string()), | ||
trade_event: TradeEvent { | ||
transaction: Transaction { | ||
id: Bytes("".to_string()), | ||
from: Bytes("".to_string()), | ||
block_number: BigInt("".to_string()), | ||
timestamp: BigInt("".to_string()), | ||
}, | ||
sender: Bytes("".to_string()), | ||
}, | ||
output_vault_balance_change: output_trade_vault_balance_change, | ||
input_vault_balance_change: input_trade_vault_balance_change, | ||
order: TradeStructPartialOrder { | ||
id: Bytes("".to_string()), | ||
order_hash: Bytes("".to_string()), | ||
}, | ||
timestamp: BigInt("".to_string()), | ||
orderbook: Orderbook { | ||
id: Bytes("".to_string()), | ||
}, | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod common; | ||
pub mod impls; | ||
pub mod order; | ||
pub mod order_detail_traits; | ||
pub mod order_trade; | ||
|
This file contains 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 |
---|---|---|
@@ -1,5 +1,67 @@ | ||
use alloy::primitives::utils::{format_units, parse_units, ParseUnits, Unit, UnitsError}; | ||
use chrono::TimeDelta; | ||
|
||
mod order_id; | ||
mod slice_list; | ||
|
||
pub use order_id::*; | ||
pub use slice_list::*; | ||
|
||
/// Returns 18 point decimals 1 as I256/U256 | ||
pub fn one_18() -> ParseUnits { | ||
rouzwelt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parse_units("1", 18).unwrap() | ||
} | ||
|
||
/// Returns YEAR as 18 point decimals as I256/U256 | ||
pub fn year_18() -> ParseUnits { | ||
parse_units(&TimeDelta::days(365).num_seconds().to_string(), 18).unwrap() | ||
} | ||
|
||
/// Converts a U256/I256 value to a 18 fixed point U256/I256 given the decimals point | ||
pub fn to_18_decimals<T: TryInto<Unit, Error = UnitsError>>( | ||
amount: ParseUnits, | ||
decimals: T, | ||
) -> Result<ParseUnits, UnitsError> { | ||
parse_units(&format_units(amount, decimals)?, 18) | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
use alloy::primitives::{I256, U256}; | ||
use std::str::FromStr; | ||
|
||
#[test] | ||
fn test_one() { | ||
let result = one_18(); | ||
let expected_signed = I256::from_str("1_000_000_000_000_000_000").unwrap(); | ||
let expected_absolute = U256::from_str("1_000_000_000_000_000_000").unwrap(); | ||
assert_eq!(result.get_signed(), expected_signed); | ||
assert_eq!(result.get_absolute(), expected_absolute); | ||
} | ||
|
||
#[test] | ||
fn test_year_18_decimals() { | ||
const YEAR: u64 = 60 * 60 * 24 * 365; | ||
let result = year_18(); | ||
let expected_signed = I256::try_from(YEAR) | ||
.unwrap() | ||
.saturating_mul(one_18().get_signed()); | ||
let expected_absolute = U256::from(YEAR).saturating_mul(one_18().get_absolute()); | ||
assert_eq!(result.get_signed(), expected_signed); | ||
assert_eq!(result.get_absolute(), expected_absolute); | ||
} | ||
|
||
#[test] | ||
fn test_to_18_decimals() { | ||
let value = ParseUnits::I256(I256::from_str("-123456789").unwrap()); | ||
let result = to_18_decimals(value, 5).unwrap(); | ||
let expected = ParseUnits::I256(I256::from_str("-1234567890000000000000").unwrap()); | ||
assert_eq!(result, expected); | ||
|
||
let value = ParseUnits::U256(U256::from_str("123456789").unwrap()); | ||
let result = to_18_decimals(value, 12).unwrap(); | ||
let expected = ParseUnits::U256(U256::from_str("123456789000000").unwrap()); | ||
assert_eq!(result, expected); | ||
} | ||
} |
This file contains 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 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 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as below