Skip to content

Commit

Permalink
Merge #2364
Browse files Browse the repository at this point in the history
2364: Do not take into account tokens with 0 ticker price r=Deniallugo a=Deniallugo



Co-authored-by: Danil <[email protected]>
  • Loading branch information
bors-matterlabs-dev[bot] and Deniallugo authored Apr 6, 2023
2 parents da61e60 + 28d55c6 commit f173366
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 55 deletions.
110 changes: 55 additions & 55 deletions core/lib/storage/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3161,61 +3161,6 @@
},
"query": "DELETE FROM mempool_reverted_txs_meta WHERE block_number = $1"
},
"55f394e48eca655ba989d46093cbb36c40398446fa6d7aa776a4f57a3ecac300": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Int4"
},
{
"name": "address",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "decimals",
"ordinal": 2,
"type_info": "Int2"
},
{
"name": "kind: _",
"ordinal": 3,
"type_info": {
"Custom": {
"kind": {
"Enum": [
"ERC20",
"NFT",
"None"
]
},
"name": "token_kind"
}
}
},
{
"name": "symbol",
"ordinal": 4,
"type_info": "Text"
}
],
"nullable": [
false,
false,
false,
false,
false
],
"parameters": {
"Left": [
"Numeric"
]
}
},
"query": "\n SELECT id, address, decimals, kind as \"kind: _\", symbol\n FROM tokens\n INNER JOIN ticker_market_volume\n ON tokens.id = ticker_market_volume.token_id\n WHERE ticker_market_volume.market_volume >= $1\n AND kind = 'ERC20'::token_kind\n ORDER BY id ASC\n "
},
"565dbc924bff0126aa6635daec86f2753d49a8de200a5e6207139c657b7169e6": {
"describe": {
"columns": [
Expand Down Expand Up @@ -3952,6 +3897,61 @@
},
"query": "\n INSERT INTO no_2fa_pub_key_hash VALUES ( $1, $2 )\n ON CONFLICT (account_id) DO UPDATE SET pub_key_hash = $2\n "
},
"70cfe7c346c2ff2c8789163d8c9bb42dd460744f98b6f0fb65ce1a09b86a1b11": {
"describe": {
"columns": [
{
"name": "id",
"ordinal": 0,
"type_info": "Int4"
},
{
"name": "address",
"ordinal": 1,
"type_info": "Text"
},
{
"name": "decimals",
"ordinal": 2,
"type_info": "Int2"
},
{
"name": "kind: _",
"ordinal": 3,
"type_info": {
"Custom": {
"kind": {
"Enum": [
"ERC20",
"NFT",
"None"
]
},
"name": "token_kind"
}
}
},
{
"name": "symbol",
"ordinal": 4,
"type_info": "Text"
}
],
"nullable": [
false,
false,
false,
false,
false
],
"parameters": {
"Left": [
"Numeric"
]
}
},
"query": "\n SELECT id, address, decimals, kind as \"kind: _\", symbol\n FROM tokens\n INNER JOIN ticker_market_volume\n ON tokens.id = ticker_market_volume.token_id\n INNER JOIN ticker_price \n ON tokens.id = ticker_price.token_id\n WHERE ticker_market_volume.market_volume >= $1\n AND ticker_price.usd_price > 0\n AND kind = 'ERC20'::token_kind\n ORDER BY id ASC\n "
},
"7102023319626d8894376477c6681184464f79c2b588bdb227d22cf032f3e8b7": {
"describe": {
"columns": [
Expand Down
33 changes: 33 additions & 0 deletions core/lib/storage/src/tests/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,41 @@ async fn test_ticker_price(mut storage: StorageProcessor<'_>) -> QueryResult<()>
#[db_test]
async fn test_market_volume(mut storage: StorageProcessor<'_>) -> QueryResult<()> {
const TOKEN_ID: TokenId = TokenId(0);
const WRONG_TOKEN_ID: TokenId = TokenId(1);

let market_volume = TokenMarketVolume {
market_volume: Ratio::new(BigUint::from(2u32), BigUint::from(5u32)),
last_updated: chrono::Utc::now(),
};

storage
.tokens_schema()
.store_or_update_token(Token {
id: WRONG_TOKEN_ID,
address: Address::random(),
..Default::default()
})
.await
.unwrap();
let price = TokenPrice {
usd_price: Ratio::new(BigUint::from(4u32), BigUint::from(9u32)),
last_updated: chrono::Utc::now(),
};
storage
.tokens_schema()
.update_historical_ticker_price(TOKEN_ID, price.clone())
.await?;

storage
.tokens_schema()
.update_token_market_volume(TOKEN_ID, market_volume.clone())
.await?;

storage
.tokens_schema()
.update_token_market_volume(WRONG_TOKEN_ID, market_volume.clone())
.await?;

let loaded = storage
.tokens_schema()
.get_token_market_volume(TOKEN_ID)
Expand All @@ -218,6 +242,15 @@ async fn test_market_volume(mut storage: StorageProcessor<'_>) -> QueryResult<()
.await
.expect("Load tokens by market volume query failed");
assert_eq!(tokens.len(), 1);
storage
.tokens_schema()
.update_historical_ticker_price(WRONG_TOKEN_ID, price)
.await?;
let tokens = TokensSchema(&mut storage)
.load_tokens_by_market_volume(Ratio::new(BigUint::from(2u32), BigUint::from(5u32)))
.await
.expect("Load tokens by market volume query failed");
assert_eq!(tokens.len(), 2);

Ok(())
}
Expand Down
4 changes: 4 additions & 0 deletions core/lib/storage/src/tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ impl<'a, 'c> TokensSchema<'a, 'c> {

/// Loads all the stored tokens, which have market_volume (ticker_market_volume table)
/// not less than parameter (min_market_volume)
/// And have not null price in ticker_price
pub async fn load_tokens_by_market_volume(
&mut self,
min_market_volume: Ratio<BigUint>,
Expand All @@ -244,7 +245,10 @@ impl<'a, 'c> TokensSchema<'a, 'c> {
FROM tokens
INNER JOIN ticker_market_volume
ON tokens.id = ticker_market_volume.token_id
INNER JOIN ticker_price
ON tokens.id = ticker_price.token_id
WHERE ticker_market_volume.market_volume >= $1
AND ticker_price.usd_price > 0
AND kind = 'ERC20'::token_kind
ORDER BY id ASC
"#,
Expand Down

0 comments on commit f173366

Please sign in to comment.