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

Add DEX tasks and endpoints #125

Merged
merged 41 commits into from Dec 9, 2022
Merged

Conversation

miksax
Copy link
Contributor

@miksax miksax commented Nov 17, 2022

Store swap from DEXes (WingRiders, MinSwap, SundaeSwap)
Store mean values from DEXes

All assets amounts are without decimal places

Add endpoints

mean price - show the mean price after each exchange.

It is only a ratio between asset amounts (without fees, etc..)

POST /dex/mean-price HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Content-Length: 411

{
    "assetPairs": [
        {
            "asset2": null,
            "asset1": {
                "policyId": "c0ee29a85b13209423b10447d3c2e6a50641a15c57770e27cb9d5073",
                "assetName": "57696e67526964657273"
            }
        }
    ],
    "dexes": ["WingRiders", "MinSwap", "SundaeSwap"],
    "limit": 1,
    "untilBlock": "a4432189e2e3a75c97480e65460b76d023bc0f2fb0ee961c5caf19779bc64e18"
}
{
    "meanPrices": [
        {
            "tx_hash": "483315a7dfc225d02f9f666b96a54e04c7a44efd7a0960905ddeec76b8c95255",
            "dex": "WingRiders",
            "asset1": null,
            "asset2": {
                "policyId": "c0ee29a85b13209423b10447d3c2e6a50641a15c57770e27cb9d5073",
                "assetName": "57696e67526964657273"
            },
            "amount1": "1929381124638",
            "amount2": "5766737594563"
        }
    ]
}

swap price - show all real swap prices

POST /dex/swap-price HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Content-Length: 386

{
    "assetPairs": [
        {
            "asset1": null,
            "asset2": {
                "policyId": "c0ee29a85b13209423b10447d3c2e6a50641a15c57770e27cb9d5073",
                "assetName": "57696e67526964657273"
            }
        }
    ],
    "dexes": ["WingRiders"],
    "limit": 3,
    "untilBlock": "a4432189e2e3a75c97480e65460b76d023bc0f2fb0ee961c5caf19779bc64e18"
}
{
    "swap": [
        {
            "tx_hash": "4eb8fed0bf9db2ca4b0ff42342ad8ce950c1cda7906cadaf3000d313af5c6ce5",
            "dex": "WingRiders",
            "asset1": null,
            "asset2": {
                "policyId": "c0ee29a85b13209423b10447d3c2e6a50641a15c57770e27cb9d5073",
                "assetName": "57696e67526964657273"
            },
            "amount1": "85504346",
            "amount2": "256473383",
            "direction": "sell"
        }
    ]
}

the last price - show the final price from all active DEXes

POST /dex/last-price HTTP/1.1
Host: localhost:3000
Content-Type: application/json
Content-Length: 268

{
    "assetPairs": [
        {
            "asset2": null,
            "asset1": {
                "policyId": "8e51398904a5d3fc129fbf4f1589701de23c7824d5c90fdb9490e15a",
                "assetName": "434841524c4933"
            }
        }
    ],
    "type": "buy"
}
{
    "lastPrice": [
        {
            "asset1": null,
            "asset2": {
                "policyId": "8e51398904a5d3fc129fbf4f1589701de23c7824d5c90fdb9490e15a",
                "assetName": "434841524c4933"
            },
            "amount1": "5000087662",
            "amount2": "8550230148",
            "dex": "SundaeSwap"
        },
        {
            "asset1": null,
            "asset2": {
                "policyId": "8e51398904a5d3fc129fbf4f1589701de23c7824d5c90fdb9490e15a",
                "assetName": "434841524c4933"
            },
            "amount1": "2197800000",
            "amount2": "3869564858",
            "dex": "Minswap"
        },
        {
            "asset1": null,
            "asset2": {
                "policyId": "8e51398904a5d3fc129fbf4f1589701de23c7824d5c90fdb9490e15a",
                "assetName": "434841524c4933"
            },
            "amount1": "61055009",
            "amount2": "106621898",
            "dex": "WingRiders"
        }
    ]
}

@miksax miksax changed the title Dm all dex tasks Add DEX tasks and endpoints Nov 17, 2022
@miksax miksax force-pushed the dm-all-dex-tasks branch 3 times, most recently from c030a86 to 445e88e Compare November 17, 2022 17:50
indexer/execution_plans/dex_prices.toml Outdated Show resolved Hide resolved
webserver/shared/models/DexSwap.ts Outdated Show resolved Hide resolved
webserver/shared/models/DexMeanPrice.ts Outdated Show resolved Hide resolved
webserver/shared/models/DexLastPrice.ts Outdated Show resolved Hide resolved
webserver/server/app/services/utils.ts Outdated Show resolved Hide resolved
webserver/server/app/services/utils.ts Outdated Show resolved Hide resolved
webserver/shared/models/common.ts Outdated Show resolved Hide resolved
Comment on lines 5 to 22
#[sea_orm(table_name = "DexMeanPrice")]
pub struct Model {
#[sea_orm(primary_key, column_type = "BigInteger")]
pub id: i64,
#[sea_orm(column_type = "BigInteger")]
pub tx_id: i64,
#[sea_orm(column_type = "BigInteger")]
pub address_id: i64,
pub dex: i32,
#[sea_orm(column_type = "BigInteger", nullable)]
pub asset1_id: Option<i64>,
#[sea_orm(column_type = "BigInteger", nullable)]
pub asset2_id: Option<i64>,
#[sea_orm(column_type = "BigUnsigned")]
pub amount1: u64,
#[sea_orm(column_type = "BigUnsigned")]
pub amount2: u64,
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the benefit of representing the mean price as a separate table with separate logic instead of a materialized view? Maybe there is a good reason for this, and if so we should write it down somewhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We split tables because data represents different approaches. The mean price is the theoretical price for an asset, swap is the real price.
But there is no problem with merging tables

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but why can't the theoretical price be a (maybe materialized) view instead merging tables or having separate tables? It'd be nice to outline the tradeoffs and why one is picked

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged tables and crate views for the webserver.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

view removed. Everything is routed from joined tables

indexer/tasks/src/multiera/dex/common.rs Outdated Show resolved Hide resolved
webserver/server/app/controllers/DexLastPriceController.ts Outdated Show resolved Hide resolved
return {
swap: swap.map(result => ({
tx_hash: result.tx_hash.toString('hex'),
dex: valueToDex(result.dex || '-1'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

be careful with using || because for example, 0 is falsy in javascript. If you're doing a null-check, it's better to use ??
I mention this here, but this applies to a few places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only because views return type or null value.
There is no option that the value will be really null.

@SebastienGllmt SebastienGllmt merged commit 4e2c189 into dcSpark:main Dec 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants