-
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 23 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 |
---|---|---|
@@ -1,5 +1,68 @@ | ||
use alloy::primitives::utils::{format_units, parse_units, ParseUnits, Unit, UnitsError}; | ||
|
||
mod order_id; | ||
mod slice_list; | ||
|
||
pub use order_id::*; | ||
pub use slice_list::*; | ||
|
||
pub const DAY: u64 = 60 * 60 * 24; | ||
pub const YEAR: u64 = DAY * 365; | ||
|
||
/// 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(&YEAR.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() { | ||
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
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,51 @@ | ||
<script lang="ts"> | ||
import ButtonTab from '$lib/components/ButtonTab.svelte'; | ||
import { nowTimestamp, TIME_DELTA_1_YEAR, TIME_DELTA_30_DAYS } from '$lib/services/time'; | ||
import { ButtonGroup } from 'flowbite-svelte'; | ||
|
||
let now = nowTimestamp(); | ||
let timeDelta: number | undefined; | ||
|
||
function setNow() { | ||
now = nowTimestamp(); | ||
} | ||
|
||
export let startTimestamp: number | undefined; | ||
export let endTimestamp: number | undefined; | ||
</script> | ||
|
||
<ButtonGroup class="bg-gray-800" data-testid="lightweightChartYearButtons"> | ||
<ButtonTab | ||
on:click={() => { | ||
setNow(); | ||
timeDelta = undefined; | ||
startTimestamp = undefined; | ||
endTimestamp = undefined; | ||
}} | ||
active={timeDelta === undefined} | ||
size="xs" | ||
class="px-2 py-1">All Time</ButtonTab | ||
> | ||
<ButtonTab | ||
on:click={() => { | ||
setNow(); | ||
timeDelta = TIME_DELTA_1_YEAR; | ||
startTimestamp = now - TIME_DELTA_1_YEAR; | ||
endTimestamp = now; | ||
}} | ||
active={timeDelta === TIME_DELTA_1_YEAR} | ||
size="xs" | ||
class="px-2 py-1">Last Year</ButtonTab | ||
> | ||
<ButtonTab | ||
on:click={() => { | ||
setNow(); | ||
timeDelta = TIME_DELTA_30_DAYS; | ||
startTimestamp = now - TIME_DELTA_30_DAYS; | ||
endTimestamp = now; | ||
}} | ||
active={timeDelta === TIME_DELTA_30_DAYS} | ||
size="xs" | ||
class="px-2 py-1">Last Month</ButtonTab | ||
> | ||
</ButtonGroup> |
13 changes: 7 additions & 6 deletions
13
tauri-app/src/lib/components/charts/ChartTimeFilters.svelte
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
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,54 @@ | ||
<script lang="ts"> | ||
import { createInfiniteQuery } from '@tanstack/svelte-query'; | ||
import TanstackAppTable from './TanstackAppTable.svelte'; | ||
import { QKEY_ORDER_APY } from '$lib/queries/keys'; | ||
import { getOrderApy } from '$lib/queries/orderTradesList'; | ||
import { subgraphUrl } from '$lib/stores/settings'; | ||
import { TableBodyCell, TableHeadCell } from 'flowbite-svelte'; | ||
import ApyTimeFilters from '../charts/APYTimeFilters.svelte'; | ||
import { formatUnits } from 'viem'; | ||
|
||
export let id: string; | ||
|
||
let startTimestamp: number | undefined; | ||
let endTimestamp: number | undefined; | ||
|
||
$: orderApy = createInfiniteQuery({ | ||
queryKey: [id, QKEY_ORDER_APY + id], | ||
queryFn: () => getOrderApy(id, $subgraphUrl || '', startTimestamp, endTimestamp), | ||
initialPageParam: 0, | ||
getNextPageParam: () => undefined, | ||
enabled: !!$subgraphUrl, | ||
}); | ||
|
||
function formatApyToPercentage(value: string): string { | ||
let valueString = formatUnits(BigInt(value) * 100n, 18); | ||
const index = valueString.indexOf('.'); | ||
if (index > -1) { | ||
// 5 point decimals to show on UI | ||
valueString = valueString.substring(0, index + 6); | ||
} | ||
return valueString; | ||
} | ||
</script> | ||
|
||
<TanstackAppTable query={orderApy} emptyMessage="APY Unavailable" rowHoverable={false}> | ||
<svelte:fragment slot="timeFilter"> | ||
<ApyTimeFilters bind:startTimestamp bind:endTimestamp /> | ||
</svelte:fragment> | ||
<svelte:fragment slot="head"> | ||
<TableHeadCell padding="p-4">APY</TableHeadCell> | ||
</svelte:fragment> | ||
|
||
<svelte:fragment slot="bodyRow" let:item> | ||
<TableBodyCell tdClass="break-all px-4 py-2" data-testid="apy-field"> | ||
{item.denominatedApy | ||
? formatApyToPercentage(item.denominatedApy.apy) + | ||
'% in ' + | ||
(item.denominatedApy.token.symbol ?? | ||
item.denominatedApy.token.name ?? | ||
item.denominatedApy.token.address) | ||
: 'Unavailable APY'} | ||
</TableBodyCell> | ||
</svelte:fragment> | ||
</TanstackAppTable> |
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.
using u64 for times is kinda dangerous because it's really easy for someone to mistake whether it is seconds, millis, nanos, etc.
why not just use the time functionality in rust?
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.
used rust
chrono::TimeDelta