CronCats / query-indexer

ExpressJS server connecting to CronCat indexer Postgres database
0 stars 1 forks source link

Query by task hash, showing `total_deposit` #1

Closed mikedotexe closed 1 year ago

mikedotexe commented 1 year ago

So far, we have only one ExpressJS route, shown here: https://github.com/CronCats/query-indexer/blob/3d22bef979ccd5b580e1b8ac17ce6519ecd5b1e8/src/index.ts#L61

There is a lot more you can do with routes, especially when you give parameters: https://expressjs.com/en/guide/routing.html#route-parameters

The page above shows an example that looks like this:

app.get('/users/:userId/books/:bookId', (req, res) => {
  res.send(req.params)
})

We'll want to do something similar for this ticket, but instead of :userId and :bookId we will have one parameter :txHash.


Let's talk a bit about how this is working on the smart contract level, and then how that translates to what's stored in the database by the Indexer Sweeper. (this repo: https://github.com/CronCats/indexer-sweep)

Someone can query the CronCat Manager smart contract method get_task and give the hash as a parameter. That will eventually lead to a function that returns this struct:

pub struct TaskResponse {
    pub task_hash: String,

    pub owner_id: Addr,

    pub interval: Interval,
    pub boundary: Option<Boundary>,

    pub stop_on_fail: bool,
    pub total_deposit: Vec<Coin>, // ✨✨✨ THIS IS THE FIELD WE'RE CONCERNED WITH IN THIS TICKET ✨✨✨
    pub total_cw20_deposit: Vec<Cw20CoinVerified>,
    pub amount_for_one_task_native: Vec<Coin>,
    pub amount_for_one_task_cw20: Vec<Cw20CoinVerified>,

    pub actions: Vec<Action>,
    pub rules: Option<Vec<CroncatQuery>>,
}

When it comes to the Indexer Sweeper, this field is saved here:

https://github.com/CronCats/indexer-sweep/blob/39bb496e2d8818a8ccdc3d853a67f663fe31cfef/src/entities/tasks/tasks.ts#L105-L117

(Since it's not showing a preview, I'll paste it as well)

// Total deposits (total_deposit)
// NOTE: at the time of this writing, it seems like we're just covering native token
// but the database table task_deposits will be able to have other types
for (const totalDepositNative of task.total_deposit) {
    promises.push(
        db('js_task_deposits').insert({
            fk_task_id: taskFkId,
            type: 'native',
            denom: totalDepositNative.denom,
            amount: totalDepositNative.amount
        })
    )
}

So this is in the js_task_deposits table and looks like this:

Screen Shot 2022-12-05 at 2 04 06 PM

So the ExpressJS route will probably look like this, but I am open to you modifying after digging into this ticket, of course:

app.get('/task/:taskHash/deposits', (req, res) => {
  // …
})

That will lead to a query that uses similar logic to the existing one in this repo: https://github.com/CronCats/query-indexer/blob/main/src/queries/tx.ts#L8-L13

but will return all the deposits for that particular task.

NOTE! As I have been writing this ticket, I am seeing something odd, which you can see in the screenshot from Postico showing the Postgres database. It looks like fk_task_id is an incrementing number, when I would expect that the foreign key would not appear like that. I would expect Task 1 to have a foreign key of 1 in this table, and to see 1 multiple times as the total_deposit decreases from the agent calling. I will look into this.

I believe this is enough information, however, to get going on this task))