Giter Club home page Giter Club logo

Comments (7)

tmcgroul avatar tmcgroul commented on September 2, 2024

Hi @zefir01
Would you mind to show your addCall/addEvent/etc handlers?

from squid-sdk.

zefir01 avatar zefir01 commented on September 2, 2024

all the usual...

import {lookupArchive} from "@subsquid/archive-registry"
import * as ss58 from "@subsquid/ss58"
import {BatchContext, BatchProcessorItem, SubstrateBatchProcessor} from "@subsquid/substrate-processor"
import {Store, TypeormDatabase} from "@subsquid/typeorm-store"
import {In} from "typeorm"
import {Account, HistoricalBalance} from "./model"
import {BalancesTransferEvent} from "./types/events"
import {decodeAddress} from '@polkadot/util-crypto';
import {u8aToHex} from '@polkadot/util';


const processor = new SubstrateBatchProcessor()
    .setTypesBundle('polkadot')
    .setBatchSize(1000)
    //.setBlockRange({from: 11270865, to: 11270867})
    .setDataSource({
        // Lookup archive by the network name in the Subsquid registry
        //archive: lookupArchive("polkadot", { release: "FireSquid" })
        archive: 'https://blablabla.com/graphql',
        //chain: 'wss://blablabla.com'

        // Use archive created by archive/docker-compose.yml
        // archive: 'http://localhost:8888/graphql'
    })
    .setPrometheusPort(9090)
    .addEvent('Balances.Transfer', {
        data: {event: {args: true}}
    } as const)


type Item = BatchProcessorItem<typeof processor>
type Ctx = BatchContext<Store, Item>

const targetAccount = '1ENYhhP9K1ysJSoKrwVEiyD6XAY2w1eZANRJbRq597pkn3Q';

processor.run(new TypeormDatabase(), async ctx => {
    let transfers = getTransfers(ctx).filter(p=>p.from===targetAccount || p.to===targetAccount);

    let accountIds = new Set<string>();
    for (let t of transfers) {
        accountIds.add(t.from);
        accountIds.add(t.to);
    }

    let accounts = await ctx.store.findBy(Account, {id: In([...accountIds])}).then(accounts => {
        return new Map(accounts.map(a => [a.id, a]))
    })

    let history: HistoricalBalance[] = []

    for (let t of transfers) {
        let from = getAccount(accounts, t.from)
        let to = getAccount(accounts, t.to)

        from.balance -= t.amount
        to.balance += t.amount

        let balance=new HistoricalBalance({
            id: t.id + "-from",
            accountFrom: from,
            accountTo: to,
            balanceFrom: from.balance,
            balanceTo: to.balance,
            timestamp: t.timestamp
        });
        console.log(balance);

        history.push(balance);

        // history.push(new HistoricalBalance({
        //     id: t.id + "-to",
        //     account: to,
        //     balance: to.balance,
        //     timestamp: t.timestamp
        // }))
    }

    await ctx.store.save(Array.from(accounts.values()))
    await ctx.store.insert(history)
})


interface TransferEvent {
    id: string
    from: string
    to: string
    amount: bigint
    timestamp: bigint
}


function getTransfers(ctx: Ctx): TransferEvent[] {
    let transfers: TransferEvent[] = []
    for (let block of ctx.blocks) {
        for (let item of block.items) {
            if (item.name == "Balances.Transfer") {
                let e = new BalancesTransferEvent(ctx, item.event)
                let rec: {from: Uint8Array, to: Uint8Array, amount: bigint}
                if (e.isV1020) {
                    let [from, to, amount, ] = e.asV1020
                    rec = {from, to, amount}
                } else if (e.isV1050) {
                    let [from, to, amount] = e.asV1050
                    rec = {from, to, amount}
                } else {
                    rec = e.asV9130
                }
                transfers.push({
                    id: item.event.id,
                    from: ss58.codec('polkadot').encode(rec.from),
                    to: ss58.codec('polkadot').encode(rec.to),
                    amount: rec.amount,
                    timestamp: BigInt(block.header.timestamp)
                })
            }
        }
    }
    return transfers
}


function getAccount(m: Map<string, Account>, id: string): Account {
    let acc = m.get(id)
    if (acc == null) {
        acc = new Account()
        acc.id = id
        acc.balance = 0n
        m.set(id, acc)
    }
    return acc
}

from squid-sdk.

tmcgroul avatar tmcgroul commented on September 2, 2024

This query shouldn't invoke full scan. Try to ping api:

curl -g -X POST -H "Content-Type: application/json" \
-d '{"query":"query { status { head } }"}' \
https://blablabla.com/graphql

from squid-sdk.

zefir01 avatar zefir01 commented on September 2, 2024

from squid-sdk.

tmcgroul avatar tmcgroul commented on September 2, 2024

It will not fix the problem but how about use subsquid hosted archive? - https://polkadot.archive.subsquid.io/graphql

from squid-sdk.

zefir01 avatar zefir01 commented on September 2, 2024

It is not safe. we work with money.

from squid-sdk.

dzhelezov avatar dzhelezov commented on September 2, 2024

We use a cockroach DB cluster in production and postgres is probably not a good fit for 1M+ blocks as it doesn't scale.
Can you pls elaborate on your concerns about "it's not safe"?

from squid-sdk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.