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

apy #945

Merged
merged 67 commits into from
Dec 19, 2024
Merged

apy #945

Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
79f5f40
init
rouzwelt Oct 16, 2024
22c3e03
Revert "init"
rouzwelt Oct 17, 2024
4fdb5a6
apy logic
rouzwelt Oct 18, 2024
c61d43b
ui
rouzwelt Oct 18, 2024
6edab3b
update
rouzwelt Oct 18, 2024
7968399
apply requested changes
rouzwelt Oct 19, 2024
6b27e3e
Update apy.rs
rouzwelt Oct 20, 2024
04155e8
Update apy.rs
rouzwelt Oct 20, 2024
621a180
Update apy.rs
rouzwelt Oct 20, 2024
c2804e4
Update apy.rs
rouzwelt Oct 20, 2024
4c69002
Update apy.rs
rouzwelt Oct 20, 2024
b88b0c2
Update apy.rs
rouzwelt Oct 20, 2024
59a2a39
Update apy.rs
rouzwelt Oct 20, 2024
f509d86
Update apy.rs
rouzwelt Oct 20, 2024
06cd19a
Update apy.rs
rouzwelt Oct 20, 2024
8fe210d
Update apy.rs
rouzwelt Oct 20, 2024
e5cae7d
Update apy.rs
rouzwelt Oct 20, 2024
931bf38
Update apy.rs
rouzwelt Oct 20, 2024
78abfc7
Update apy.rs
rouzwelt Oct 21, 2024
36a34f7
fix pick denomination
rouzwelt Oct 21, 2024
d4883d7
update [skip ci]
rouzwelt Oct 21, 2024
b659f5b
update
rouzwelt Oct 22, 2024
cdadbf5
Merge branch 'main' into 2024-10-16-apy
rouzwelt Oct 22, 2024
31f54d7
apply requested changes
rouzwelt Oct 23, 2024
54d21b3
update
rouzwelt Oct 23, 2024
5b65481
Update number.ts
rouzwelt Oct 23, 2024
51b5f86
Update number.ts
rouzwelt Oct 23, 2024
6aa1bcc
update
rouzwelt Oct 23, 2024
ad43d24
Merge branch 'main' into 2024-10-16-apy
thedavidmeister Oct 23, 2024
c5adc1c
apply requested changes
rouzwelt Oct 27, 2024
b32e441
Merge branch 'main' into 2024-10-16-apy
rouzwelt Oct 27, 2024
249f21f
Merge branch '2024-10-30-bigint-math-18' into 2024-10-16-apy
rouzwelt Nov 14, 2024
9eeb517
Delete error.rs
rouzwelt Nov 14, 2024
32df562
update
rouzwelt Nov 18, 2024
2588b55
Merge branch 'main' into 2024-10-16-apy
rouzwelt Nov 18, 2024
dccd4ea
update
rouzwelt Nov 18, 2024
89932d5
Update vol.rs
rouzwelt Nov 18, 2024
f9cad84
Update impls.rs
rouzwelt Nov 18, 2024
0f1e192
Update OrderVaultsVolTable.svelte
rouzwelt Nov 18, 2024
b5c585b
Update OrderVaultsVolTable.test.ts
rouzwelt Nov 18, 2024
7a8659a
Update OrderVaultsVolTable.test.ts
rouzwelt Nov 18, 2024
2f7c4bd
Merge branch 'main' into 2024-10-16-apy
rouzwelt Nov 21, 2024
b3f1193
update
rouzwelt Nov 21, 2024
b1b7d6f
Merge branch 'main' into 2024-10-16-apy
rouzwelt Nov 21, 2024
f64c175
update
rouzwelt Nov 21, 2024
27a1b97
fix minor issues
rouzwelt Nov 21, 2024
0b8ae38
Update order_performance.rs
rouzwelt Nov 22, 2024
c285f5d
Merge branch 'main' into 2024-10-16-apy
rouzwelt Nov 22, 2024
a3b5ed7
update
rouzwelt Nov 22, 2024
e2b0c92
Merge branch 'main' into 2024-10-16-apy
thedavidmeister Nov 22, 2024
6f9eb78
apply requested changes
rouzwelt Nov 22, 2024
5a17414
Merge branch 'main' into 2024-10-16-apy
rouzwelt Nov 22, 2024
050ea35
fix
rouzwelt Nov 22, 2024
2bd4b59
lint
rouzwelt Nov 22, 2024
77cb6fc
Merge branch 'main' into 2024-10-16-apy
rouzwelt Nov 29, 2024
c808a93
Merge branch 'main' into 2024-10-16-apy
rouzwelt Dec 12, 2024
190b3cf
lint
rouzwelt Dec 12, 2024
a00cb3a
Update APYTimeFilters.svelte
rouzwelt Dec 12, 2024
972e638
Merge branch 'main' into 2024-10-16-apy
rouzwelt Dec 13, 2024
9afc855
Merge branch 'main' into 2024-10-16-apy
rouzwelt Dec 17, 2024
53ae997
Merge branch 'main' into 2024-10-16-apy
rouzwelt Dec 18, 2024
2a8162c
update
rouzwelt Dec 18, 2024
0fce59b
fix
rouzwelt Dec 19, 2024
481adf2
fix
rouzwelt Dec 19, 2024
fad05ca
Merge branch 'main' into 2024-10-16-apy
rouzwelt Dec 19, 2024
9fe1d05
fix div by zero of trade ratios
rouzwelt Dec 19, 2024
3e14914
Merge branch 'main' into 2024-10-16-apy
rouzwelt Dec 19, 2024
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
832 changes: 832 additions & 0 deletions crates/subgraph/src/apy.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions crates/subgraph/src/lib.rs
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;
Expand Down
6 changes: 6 additions & 0 deletions crates/subgraph/src/orderbook_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub enum OrderbookSubgraphClientError {
PaginationClientError(#[from] PaginationClientError),
#[error(transparent)]
ParseError(#[from] alloy::primitives::ruint::ParseError),
#[error(transparent)]
ParseBigIntConversionError(#[from] alloy::primitives::BigIntConversionError),
#[error(transparent)]
ParseFloatError(#[from] std::num::ParseFloatError),
#[error(transparent)]
ParseIntError(#[from] std::num::ParseIntError),
}

pub struct OrderbookSubgraphClient {
Expand Down
6 changes: 3 additions & 3 deletions crates/subgraph/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ pub struct OrderStructPartialTrade {
pub id: Bytes,
}

#[derive(cynic::QueryFragment, Debug, Serialize, Clone, PartialEq)]
#[derive(cynic::QueryFragment, Debug, Serialize, Clone, PartialEq, Eq, Hash)]
#[cynic(graphql_type = "ERC20")]
#[typeshare]
pub struct Erc20 {
Expand All @@ -305,11 +305,11 @@ pub struct AddOrder {
pub transaction: Transaction,
}

#[derive(cynic::Scalar, Debug, Clone, PartialEq)]
#[derive(cynic::Scalar, Debug, Clone, PartialEq, Eq, Hash)]
#[typeshare]
pub struct BigInt(pub String);

#[derive(cynic::Scalar, Debug, Clone, PartialEq)]
#[derive(cynic::Scalar, Debug, Clone, PartialEq, Eq, Hash)]
#[typeshare]
pub struct Bytes(pub String);

Expand Down
14 changes: 7 additions & 7 deletions crates/subgraph/src/vol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ use serde::{Deserialize, Serialize};
use std::str::FromStr;
use typeshare::typeshare;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[serde(rename_all = "camelCase")]
#[typeshare]
pub struct VaultVolume {
id: String,
token: Erc20,
pub id: String,
pub token: Erc20,
#[typeshare(typescript(type = "string"))]
total_in: U256,
pub total_in: U256,
#[typeshare(typescript(type = "string"))]
total_out: U256,
pub total_out: U256,
#[typeshare(typescript(type = "string"))]
total_vol: U256,
pub total_vol: U256,
#[typeshare(typescript(type = "string"))]
net_vol: I256,
pub net_vol: I256,
}

/// Get the vaults volume from array of trades
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
cargo install --git https://github.com/tomjw64/typeshare --rev 556b44aafd5304eedf17206800f69834e3820b7c
export PATH=$PATH:$CARGO_HOME/bin

typeshare crates/subgraph/src/types/common.rs crates/subgraph/src/types/order.rs crates/subgraph/src/types/vault.rs crates/subgraph/src/types/order_trade.rs crates/common/src/types/order_detail_extended.rs crates/subgraph/src/vol.rs --lang=typescript --output-file=tauri-app/src/lib/typeshare/subgraphTypes.ts;
typeshare crates/subgraph/src/types/common.rs crates/subgraph/src/types/order.rs crates/subgraph/src/types/vault.rs crates/subgraph/src/types/order_trade.rs crates/common/src/types/order_detail_extended.rs crates/subgraph/src/vol.rs crates/subgraph/src/apy.rs --lang=typescript --output-file=tauri-app/src/lib/typeshare/subgraphTypes.ts;

typeshare crates/settings/src/parse.rs --lang=typescript --output-file=tauri-app/src/lib/typeshare/appSettings.ts;
typeshare lib/rain.interpreter/crates/eval/src/trace.rs crates/common/src/fuzz/mod.rs crates/settings/src/config_source.rs crates/settings/src/config.rs crates/settings/src/plot_source.rs crates/settings/src/chart.rs crates/settings/src/deployer.rs crates/settings/src/network.rs crates/settings/src/order.rs crates/settings/src/orderbook.rs crates/settings/src/scenario.rs crates/settings/src/blocks.rs crates/settings/src/token.rs crates/settings/src/deployment.rs --lang=typescript --output-file=tauri-app/src/lib/typeshare/config.ts;
Expand Down
21 changes: 21 additions & 0 deletions tauri-app/src-tauri/src/commands/order_take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::CommandResult;
use rain_orderbook_common::{
csv::TryIntoCsv, subgraph::SubgraphArgs, types::FlattenError, types::OrderTakeFlattened,
};
use rain_orderbook_subgraph_client::apy::{get_order_apy, OrderAPY};
use rain_orderbook_subgraph_client::vol::VaultVolume;
use rain_orderbook_subgraph_client::{types::common::*, PaginationArgs};
use std::fs;
Expand Down Expand Up @@ -79,3 +80,23 @@ pub async fn order_trades_count(
.await?
.len())
}

#[tauri::command]
pub async fn order_apy(
order_id: String,
subgraph_args: SubgraphArgs,
start_timestamp: Option<u64>,
end_timestamp: Option<u64>,
) -> CommandResult<OrderAPY> {
let client = subgraph_args.to_subgraph_client().await?;
let order = client.order_detail(order_id.clone().into()).await?;
let trades = client
.order_trades_list_all(order_id.into(), start_timestamp, end_timestamp)
.await?;
Ok(get_order_apy(
&order,
&trades,
start_timestamp,
end_timestamp,
)?)
}
6 changes: 4 additions & 2 deletions tauri-app/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use commands::order::{
};
use commands::order_quote::{batch_order_quotes, debug_order_quote};
use commands::order_take::{
order_trades_count, order_trades_list, order_trades_list_write_csv, order_vaults_volume,
order_apy, order_trades_count, order_trades_list, order_trades_list_write_csv,
order_vaults_volume,
};
use commands::trade_debug::debug_trade;
use commands::vault::{
Expand Down Expand Up @@ -82,7 +83,8 @@ fn run_tauri_app() {
get_app_commit_sha,
validate_raindex_version,
order_vaults_volume,
order_trades_count
order_trades_count,
order_apy,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
51 changes: 51 additions & 0 deletions tauri-app/src/lib/components/charts/APYTimeFilters.svelte
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 tauri-app/src/lib/components/charts/ChartTimeFilters.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import ButtonTab from '$lib/components/ButtonTab.svelte';
import { ButtonGroup } from 'flowbite-svelte';

const TIME_DELTA_24_HOURS = 60 * 60 * 24;
const TIME_DELTA_7_DAYS = TIME_DELTA_24_HOURS * 7;
const TIME_DELTA_30_DAYS = TIME_DELTA_24_HOURS * 30;
const TIME_DELTA_1_YEAR = TIME_DELTA_24_HOURS * 365;
import ButtonTab from '$lib/components/ButtonTab.svelte';
import {
TIME_DELTA_1_YEAR,
TIME_DELTA_24_HOURS,
TIME_DELTA_30_DAYS,
TIME_DELTA_7_DAYS,
} from '$lib/services/time';

export let timeDelta = TIME_DELTA_1_YEAR;
</script>
Expand Down
11 changes: 6 additions & 5 deletions tauri-app/src/lib/components/charts/ChartTimeFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { render, fireEvent, screen } from '@testing-library/svelte';
import { get, writable } from 'svelte/store';
import { test, expect } from 'vitest';
import ChartTimeFiltersTest from './ChartTimeFilters.test.svelte';

const TIME_DELTA_24_HOURS = 60 * 60 * 24;
const TIME_DELTA_7_DAYS = TIME_DELTA_24_HOURS * 7;
const TIME_DELTA_30_DAYS = TIME_DELTA_24_HOURS * 30;
const TIME_DELTA_1_YEAR = TIME_DELTA_24_HOURS * 365;
import {
TIME_DELTA_1_YEAR,
TIME_DELTA_24_HOURS,
TIME_DELTA_30_DAYS,
TIME_DELTA_7_DAYS,
} from '$lib/services/time';

test('initial timeDelta is set to 1 year', async () => {
const timeDeltaStore = writable(TIME_DELTA_1_YEAR);
Expand Down
8 changes: 3 additions & 5 deletions tauri-app/src/lib/components/charts/TableTimeFilters.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<script lang="ts">
import ButtonTab from '$lib/components/ButtonTab.svelte';
import { nowTimestamp, TIME_DELTA_24_HOURS, TIME_DELTA_48_HOURS } from '$lib/services/time';
import { ButtonGroup } from 'flowbite-svelte';

const TIME_DELTA_24_HOURS = 60 * 60 * 24;
const TIME_DELTA_48_HOURS = TIME_DELTA_24_HOURS * 2;

let now = Math.floor(new Date().getTime() / 1000);
let now = nowTimestamp();
let timeDelta: number | undefined;

function setNow() {
now = Math.floor(new Date().getTime() / 1000);
now = nowTimestamp();
}

export let startTimestamp: number | undefined;
Expand Down
4 changes: 1 addition & 3 deletions tauri-app/src/lib/components/charts/TableTimeFilters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { render, fireEvent, screen } from '@testing-library/svelte';
import { get, writable } from 'svelte/store';
import { test, expect } from 'vitest';
import TableTimeFiltersTest from './TableTimeFilters.test.svelte';

const TIME_DELTA_24_HOURS = 60 * 60 * 24;
const TIME_DELTA_48_HOURS = TIME_DELTA_24_HOURS * 2;
import { TIME_DELTA_24_HOURS, TIME_DELTA_48_HOURS } from '$lib/services/time';

test('initial start/end time difference is set to all time', async () => {
const startTimeStore = writable<number | undefined>();
Expand Down
4 changes: 4 additions & 0 deletions tauri-app/src/lib/components/detail/OrderDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { onDestroy } from 'svelte';
import { queryClient } from '$lib/queries/queryClient';
import OrderVaultsVolTable from '../tables/OrderVaultsVolTable.svelte';
import OrderApy from '../tables/OrderAPY.svelte';

export let id: string;

Expand Down Expand Up @@ -143,6 +144,9 @@
<TabItem title="Volume">
<OrderVaultsVolTable {id} />
</TabItem>
<TabItem title="APY">
<OrderApy {id} />
</TabItem>
</Tabs>
</svelte:fragment>
</TanstackPageContentDetail>
54 changes: 54 additions & 0 deletions tauri-app/src/lib/components/tables/OrderAPY.svelte
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>
Loading
Loading