Skip to content

Commit

Permalink
adjust tron-usdt test project
Browse files Browse the repository at this point in the history
  • Loading branch information
tmcgroul committed Sep 24, 2024
1 parent 49b1e02 commit 9c7fb1d
Show file tree
Hide file tree
Showing 21 changed files with 634 additions and 478 deletions.
14 changes: 13 additions & 1 deletion common/config/rush/pnpm-lock.yaml

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

6 changes: 6 additions & 0 deletions rush.json
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,12 @@
"shouldPublish": true,
"versionPolicyName": "npm"
},
{
"packageName": "@subsquid/tron-objects",
"projectFolder": "tron/tron-objects",
"shouldPublish": true,
"versionPolicyName": "npm"
},
{
"packageName": "@subsquid/tron-stream",
"projectFolder": "tron/tron-stream",
Expand Down
1 change: 1 addition & 0 deletions test/tron-usdt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@subsquid/batch-processor": "^0.0.0",
"@subsquid/graphql-server": "^4.7.0",
"@subsquid/tron-stream": "^0.0.0",
"@subsquid/tron-objects": "^0.0.0",
"@subsquid/typeorm-migration": "^1.3.0",
"@subsquid/typeorm-store": "^1.5.1",
"dotenv": "^16.3.1",
Expand Down
47 changes: 41 additions & 6 deletions test/tron-usdt/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
import {run} from '@subsquid/batch-processor'
import {DataSourceBuilder, assertNotNull} from '@subsquid/tron-stream'
import {augmentBlock} from '@subsquid/tron-objects'
import {DataSourceBuilder} from '@subsquid/tron-stream'
import {TypeormDatabase} from '@subsquid/typeorm-store'
import * as erc20 from './abi/erc20'
import {Transfer} from './model'


const CONTRACT = 'a614f803b6fd780986a42c78ec9c7f77e6ded13c'
const TOPIC0 = 'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'


const dataSource = new DataSourceBuilder()
.setHttpApi({
url: assertNotNull(process.env.TRON_HTTP_API)
.setGateway('https://v2.archive.subsquid.io/network/tron-mainnet')
.setBlockRange({from: 11322942, to: 11323358})
.addLog({
where: {
address: [CONTRACT],
topic0: [TOPIC0]
},
include: {
transaction: true
}
})
.includeAllBlocks()
.build()


const database = new TypeormDatabase()


run(dataSource, database, async ctx => {
for (let block of ctx.blocks) {
console.log(block)
let transfers: Transfer[] = []

let blocks = ctx.blocks.map(augmentBlock)

for (let block of blocks) {
for (let log of block.logs) {
if (log.address == CONTRACT && log.topics[0] === TOPIC0) {
log.topics = log.topics.map(t => '0x' + t)
log.data = '0x' + log.data
let {from, to, value} = erc20.events.Transfer.decode(log)
let tx = log.getTransaction()

transfers.push(new Transfer({
id: log.id,
blockNumber: block.header.height,
timestamp: new Date(block.header.timestamp),
tx: tx.hash,
from,
to,
amount: value
}))
}
}
}

await ctx.store.insert(transfers)
})
2 changes: 1 addition & 1 deletion tron/tron-data/src/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class HttpDataSource {
this.headPollInterval = options.headPollInterval ?? 1000
this.strideSize = options.strideSize || 10
this.strideConcurrency = options.strideConcurrency || 2
this.finalityConfirmation = 0
this.finalityConfirmation = 20
}

getBlockHeader(height: number) {
Expand Down
14 changes: 11 additions & 3 deletions tron/tron-normalization/src/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ export interface Transaction {
netFee?: number
originEnergyUsage?: number
energyPenaltyTotal?: number
_transferContractOwner?: string
_transferContractTo?: string
_transferAssetContractOwner?: string
_transferAssetContractTo?: string
_transferAssetContractAsset?: string
_triggerSmartContractOwner?: string
_triggerSmartContractContract?: string
_triggerSmartContractSighash?: string
}


Expand All @@ -78,7 +86,7 @@ export interface InternalTransaction {

export interface Block {
header: BlockHeader,
logs?: Log[]
transactions?: Transaction[]
internalTransactions?: InternalTransaction[]
logs: Log[]
transactions: Transaction[]
internalTransactions: InternalTransaction[]
}
27 changes: 26 additions & 1 deletion tron/tron-normalization/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import * as raw from '@subsquid/tron-data'
import {assertNotNull} from '@subsquid/util-internal'
import assert from 'assert'
import {Block, BlockHeader, InternalTransaction, Log, Transaction} from './data'


function toSighash(val?: string) {
if (val && val.length >= 8) {
return val.slice(0, 8)
} else {
return undefined
}
}


function mapBlockHeader(src: raw.Block): BlockHeader {
return {
hash: src.blockID,
Expand All @@ -21,7 +31,7 @@ function mapTransaction(src: raw.Transaction, transactionIndex: number, info?: r
assert(src.raw_data.contract.length == 1)
if (info) assert(info.contractResult.length == 1)
let contract = src.raw_data.contract[0]
return {
let tx: Transaction = {
hash: src.txID,
transactionIndex,
ret: src.ret,
Expand Down Expand Up @@ -52,6 +62,21 @@ function mapTransaction(src: raw.Transaction, transactionIndex: number, info?: r
originEnergyUsage: info?.receipt.origin_energy_usage,
energyPenaltyTotal: info?.receipt.energy_penalty_total,
}

if (tx.type == 'TransferContract') {
tx._transferContractTo = assertNotNull(tx.parameter.value.to_address)
tx._transferContractOwner = assertNotNull(tx.parameter.value.owner_address)
} else if (tx.type == 'TransferAssetContract') {
tx._transferAssetContractAsset = assertNotNull(tx.parameter.value.asset_name)
tx._transferAssetContractOwner = assertNotNull(tx.parameter.value.owner_address)
tx._transferAssetContractTo = assertNotNull(tx.parameter.value.to_address)
} else if (tx.type == 'TriggerSmartContract') {
tx._triggerSmartContractContract = assertNotNull(tx.parameter.value.contract_address)
tx._triggerSmartContractOwner = assertNotNull(tx.parameter.value.owner_address)
tx._triggerSmartContractSighash = toSighash(tx.parameter.value.data)
}

return tx
}


Expand Down
29 changes: 29 additions & 0 deletions tron/tron-objects/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@subsquid/tron-objects",
"version": "0.0.0",
"description": "Augmented Tron data model",
"license": "GPL-3.0-or-later",
"repository": "[email protected]:subsquid/squid.git",
"publishConfig": {
"access": "public"
},
"main": "lib/index.js",
"files": [
"lib",
"src"
],
"scripts": {
"build": "rm -rf lib && tsc"
},
"dependencies": {
"@subsquid/util-internal": "^3.2.0"
},
"peerDependencies": {
"@subsquid/tron-stream": "^0.0.0"
},
"devDependencies": {
"@subsquid/tron-stream": "^0.0.0",
"@types/node": "^18.18.14",
"typescript": "~5.3.2"
}
}
11 changes: 11 additions & 0 deletions tron/tron-objects/src/augment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as base from '@subsquid/tron-stream'
import {setUpRelations} from './relations'
import * as types from './types'
import {Block} from './items'


export function augmentBlock<F extends base.FieldSelection>(src: base.Block<F>): types.Block<F> {
let block = Block.fromPartial(src)
setUpRelations(block)
return block as unknown as types.Block<F>
}
2 changes: 2 additions & 0 deletions tron/tron-objects/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './types'
export * from './augment'
Loading

0 comments on commit 9c7fb1d

Please sign in to comment.