Skip to content

Commit

Permalink
Merge pull request #922 from rainlanguage/2024-10-05-net-vol
Browse files Browse the repository at this point in the history
net volume
  • Loading branch information
rouzwelt authored Oct 16, 2024
2 parents 7db075d + 9e78d2d commit dbab21d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
18 changes: 17 additions & 1 deletion crates/subgraph/src/vol.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::types::common::{Erc20, Trade};
use alloy::primitives::{ruint::ParseError, U256};
use alloy::primitives::{ruint::ParseError, I256, U256};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use typeshare::typeshare;
Expand All @@ -16,6 +16,8 @@ pub struct VaultVolume {
total_out: U256,
#[typeshare(typescript(type = "string"))]
total_vol: U256,
#[typeshare(typescript(type = "string"))]
net_vol: I256,
}

/// Get the vaults volume from array of trades
Expand All @@ -30,30 +32,36 @@ pub fn get_vaults_vol(trades: &[Trade]) -> Result<Vec<VaultVolume>, ParseError>
let amount = U256::from_str(&trade.input_vault_balance_change.amount.0[1..])?;
vault_vol.total_out += amount;
vault_vol.total_vol += amount;
vault_vol.net_vol -= I256::from_raw(amount);
} else {
let amount = U256::from_str(&trade.input_vault_balance_change.amount.0)?;
vault_vol.total_in += amount;
vault_vol.total_vol += amount;
vault_vol.net_vol += I256::from_raw(amount);
}
} else {
let mut total_in = U256::ZERO;
let mut total_out = U256::ZERO;
let mut total_vol = U256::ZERO;
let mut net_vol = I256::ZERO;
if trade.input_vault_balance_change.amount.0.starts_with('-') {
let amount = U256::from_str(&trade.input_vault_balance_change.amount.0[1..])?;
total_out += amount;
total_vol += amount;
net_vol -= I256::from_raw(amount);
} else {
let amount = U256::from_str(&trade.input_vault_balance_change.amount.0)?;
total_in += amount;
total_vol += amount;
net_vol += I256::from_raw(amount);
}
vaults_vol.push(VaultVolume {
id: trade.input_vault_balance_change.vault.vault_id.0.clone(),
token: trade.input_vault_balance_change.vault.token.clone(),
total_in,
total_out,
total_vol,
net_vol,
})
}
if let Some(vault_vol) = vaults_vol.iter_mut().find(|v| {
Expand All @@ -64,30 +72,36 @@ pub fn get_vaults_vol(trades: &[Trade]) -> Result<Vec<VaultVolume>, ParseError>
let amount = U256::from_str(&trade.output_vault_balance_change.amount.0[1..])?;
vault_vol.total_out += amount;
vault_vol.total_vol += amount;
vault_vol.net_vol -= I256::from_raw(amount);
} else {
let amount = U256::from_str(&trade.output_vault_balance_change.amount.0)?;
vault_vol.total_in += amount;
vault_vol.total_vol += amount;
vault_vol.net_vol += I256::from_raw(amount);
}
} else {
let mut total_in = U256::ZERO;
let mut total_out = U256::ZERO;
let mut total_vol = U256::ZERO;
let mut net_vol = I256::ZERO;
if trade.output_vault_balance_change.amount.0.starts_with('-') {
let amount = U256::from_str(&trade.output_vault_balance_change.amount.0[1..])?;
total_out += amount;
total_vol += amount;
net_vol -= I256::from_raw(amount);
} else {
let amount = U256::from_str(&trade.output_vault_balance_change.amount.0)?;
total_in += amount;
total_vol += amount;
net_vol += I256::from_raw(amount);
}
vaults_vol.push(VaultVolume {
id: trade.output_vault_balance_change.vault.vault_id.0.clone(),
token: trade.output_vault_balance_change.vault.token.clone(),
total_in,
total_out,
total_vol,
net_vol,
})
}
}
Expand Down Expand Up @@ -250,13 +264,15 @@ mod test {
total_in: U256::from(5),
total_out: U256::from(7),
total_vol: U256::from(12),
net_vol: I256::from_str("-2").unwrap(),
},
VaultVolume {
id: vault_id1.to_string(),
token: token1,
total_in: U256::from(3),
total_out: U256::from(2),
total_vol: U256::from(5),
net_vol: I256::from_str("1").unwrap(),
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<TableHeadCell padding="p-0">Token</TableHeadCell>
<TableHeadCell padding="p-0">In Volume</TableHeadCell>
<TableHeadCell padding="p-0">Out Volume</TableHeadCell>
<TableHeadCell padding="p-0">Net Volume</TableHeadCell>
<TableHeadCell padding="p-0">Total Volume</TableHeadCell>
</svelte:fragment>

Expand All @@ -53,6 +54,9 @@
<TableBodyCell tdClass="break-all py-2" data-testid="total-out">
{formatUnits(BigInt(item.totalOut), Number(item.token.decimals ?? 0))}
</TableBodyCell>
<TableBodyCell tdClass="break-all py-2" data-testid="net-vol">
{formatUnits(BigInt(item.netVol), Number(item.token.decimals ?? 0))}
</TableBodyCell>
<TableBodyCell tdClass="break-all py-2" data-testid="total-vol">
{formatUnits(BigInt(item.totalVol), Number(item.token.decimals ?? 0))}
</TableBodyCell>
Expand Down
16 changes: 16 additions & 0 deletions tauri-app/src/lib/components/tables/OrderVaultsVolTable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const mockVaultsVol: VaultVolume[] = [
totalIn: '1',
totalOut: '2',
totalVol: '3',
netVol: '-1',
},
{
id: '2',
Expand All @@ -58,6 +59,7 @@ const mockVaultsVol: VaultVolume[] = [
totalIn: '2',
totalOut: '5',
totalVol: '7',
netVol: '-3',
},
];

Expand Down Expand Up @@ -103,6 +105,20 @@ test('renders table with correct data', async () => {
}
});

await waitFor(async () => {
// get net vols
const rows = screen.getAllByTestId('net-vol');

// checking the net vols
for (let i = 0; i < mockVaultsVol.length; i++) {
const display = formatUnits(
BigInt(mockVaultsVol[i].netVol),
Number(mockVaultsVol[i].token.decimals),
);
expect(rows[i]).toHaveTextContent(display.toString());
}
});

await waitFor(async () => {
// get total vols
const rows = screen.getAllByTestId('total-vol');
Expand Down

0 comments on commit dbab21d

Please sign in to comment.