Skip to content

Commit

Permalink
adjust tron-usdt example
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgroul committed Oct 4, 2024
1 parent 74a2d53 commit 929b698
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 33 deletions.
34 changes: 34 additions & 0 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -967,11 +967,11 @@
"projectFolder": "test/astar-erc20",
"shouldPublish": false
},
// {
// "packageName": "tron-usdt",
// "projectFolder": "test/tron-usdt",
// "shouldPublish": false
// },
{
"packageName": "tron-usdt",
"projectFolder": "test/tron-usdt",
"shouldPublish": false
},
{
"packageName": "@subsquid/starknet-data",
"projectFolder": "starknet/starknet-data",
Expand Down
23 changes: 10 additions & 13 deletions test/tron-usdt/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {run} from '@subsquid/batch-processor'
import {augmentBlock} from '@subsquid/tron-objects'
import {DataSourceBuilder} from '@subsquid/tron-stream'
import {TronBatchProcessor} from '@subsquid/tron-processor'
import {TypeormDatabase} from '@subsquid/typeorm-store'
import assert from 'assert'
import * as erc20 from './abi/erc20'
Expand All @@ -11,9 +9,14 @@ const CONTRACT = 'a614f803b6fd780986a42c78ec9c7f77e6ded13c'
const TOPIC0 = 'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'


const dataSource = new DataSourceBuilder()
const processor = new TronBatchProcessor()
.setGateway('https://v2.archive.subsquid.io/network/tron-mainnet')
.setBlockRange({from: 11322942, to: 11323358})
.setHttpApi({
url: 'https://rpc.ankr.com/http/tron',
strideConcurrency: 1,
strideSize: 1,
})
.setBlockRange({from: 65797512})
.addLog({
where: {
address: [CONTRACT],
Expand All @@ -23,18 +26,12 @@ const dataSource = new DataSourceBuilder()
transaction: true
}
})
.build()


const database = new TypeormDatabase()


run(dataSource, database, async ctx => {
processor.run(new TypeormDatabase(), async ctx => {
let transfers: Transfer[] = []

let blocks = ctx.blocks.map(augmentBlock)

for (let block of blocks) {
for (let block of ctx.blocks) {
for (let log of block.logs) {
if (log.address == CONTRACT && log.topics?.[0] === TOPIC0) {
assert(log.data)
Expand Down
21 changes: 18 additions & 3 deletions tron/tron-processor/src/data/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,38 @@ export type Transaction<F extends FieldSelection = {}> = Item<
TransactionRequiredFields,
F,
'transaction'
>
> & {
id: string
block: BlockHeader<F>,
logs?: Log<F>[]
internalTransactions: InternalTransaction<F>[]
}


export type Log<F extends FieldSelection = {}> = Item<
data.Log,
LogRequiredFields,
F,
'log'
>
> & {
id: string
block: BlockHeader<F>,
transaction?: Transaction<F>
getTransaction(): Transaction<F>
}


export type InternalTransaction<F extends FieldSelection = {}> = Item<
data.InternalTransaction,
InternalTransactionRequiredFields,
F,
'internalTransaction'
>
> & {
id: string
block: BlockHeader<F>,
transaction?: Transaction<F>
getTransaction(): Transaction<F>
}


export interface Block<F extends FieldSelection = {}> {
Expand Down
13 changes: 11 additions & 2 deletions tron/tron-processor/src/http/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Batch, HotDatabaseState, HotDataSource, HotUpdate} from '@subsquid/util-
import {DataRequest} from '../data/data-request'
import {filterBlockBatch} from './filter'
import {Block} from '../mapping/entities'
import {setUpRelations} from '../mapping/relations'


export class HttpDataSource implements HotDataSource<Block, DataRequest> {
Expand All @@ -31,7 +32,11 @@ export class HttpDataSource implements HotDataSource<Block, DataRequest> {
filterBlockBatch(requests, blocks)
yield {
...batch,
blocks: blocks.map(b => Block.fromPartial(b))
blocks: blocks.map(b => {
let block = Block.fromPartial(b)
setUpRelations(block)
return block
})
}
}
}
Expand All @@ -47,7 +52,11 @@ export class HttpDataSource implements HotDataSource<Block, DataRequest> {
upd => {
let blocks = upd.blocks.map(b => mapBlock(b))
filterBlockBatch(requests, blocks)
return cb({...upd, blocks: blocks.map(b => Block.fromPartial(b))})
return cb({...upd, blocks: blocks.map(b => {
let block = Block.fromPartial(b)
setUpRelations(block)
return block
})})
}
)
}
Expand Down
21 changes: 11 additions & 10 deletions tron/tron-processor/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import assert from 'assert'
import {HttpDataSource} from './http/source'
import {TronGateway} from './gateway/source'
import {Block, FieldSelection} from './data/model'
import {getFields} from './data/fields'
import {
TransactionRequest,
TriggerSmartContractTransactionRequest,
Expand Down Expand Up @@ -299,17 +300,17 @@ export class TronBatchProcessor<F extends FieldSelection = {}> {
}
})

if (this.fields) {
requests = requests.map(({range, request}) => {
return {
range,
request: {
fields: this.fields,
...request
}
let fields = getFields(this.fields)

requests = requests.map(({range, request}) => {
return {
range,
request: {
fields,
...request
}
})
}
}
})

return applyRangeBound(requests, this.blockRange)
}
Expand Down

0 comments on commit 929b698

Please sign in to comment.