Skip to content

Commit

Permalink
chore(sdk): better filter decode api for list of events/resources (st…
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Oct 24, 2022
1 parent 5fbbe66 commit 93e7413
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 38 deletions.
14 changes: 11 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ jobs:
command: ./scripts/build-all.sh
name: Build All
- run:
command: yarn gen && yarn test
name: Test Template
working_directory: sdk/template
command: yarn install && yarn gen && yarn test
name: Test Template - Aptos
working_directory: sdk/templates/aptos
- run:
command: yarn install && yarn gen && yarn test
name: Test Template - EVM
working_directory: sdk/templates/evm
- run:
command: yarn install && yarn test
name: Test Template - RAW
working_directory: sdk/templates/raw
# - persist_to_workspace:
# root: ~/project
# paths:
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ jobs:
- name: Build
run: yarn build
working-directory: sdk
- name: Clean template
run: rm -rf node_modules
working-directory: sdk/template
- name: Clean templates node mdules
run: "rm -rf */node_modules"
working-directory: sdk/templates
- name: Semantic Release
id: semantic
uses: cycjimmy/semantic-release-action@v3
Expand Down
10 changes: 4 additions & 6 deletions examples/aptos/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ SouffleChefCampaign.bind({ network: aptos.AptosNetwork.TEST_NET, startVersion: 6
ctx.meter.Counter('burned').add(1)
})
.onTransaction((txn, ctx) => {
if (txn.events) {
for (const event of txn.events) {
if (event && event.type === '0x3::token::DepositEvent') {
ctx.meter.Counter('deposit_token_count').add(Number(event.data.amount))
}
}
const events = aptos.TYPE_REGISTRY.filterAndDecodeEvents<token.DepositEvent>('0x3::token::DepositEvent', txn.events)
for (const event of events) {
// const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance
ctx.meter.Counter('deposit_token_count').add(event.data_typed.amount)
}
})

Expand Down
8 changes: 4 additions & 4 deletions sdk/src/aptos/aptos-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '.'

import Long from 'long'
import { EventInstance, DEFAULT_TYPE_REGISTRY } from './types'
import { EventInstance, TYPE_REGISTRY } from './types'
import { getChainId } from './network'

type IndexConfigure = {
Expand Down Expand Up @@ -59,7 +59,7 @@ export class AptosBaseProcessor {
this.moduleName = moduleName
this.configure(options)
global.PROCESSOR_STATE.aptosProcessors.push(this)
this.loadTypes(DEFAULT_TYPE_REGISTRY)
this.loadTypes(TYPE_REGISTRY)
}

// getABI(): MoveModule | undefined {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class AptosBaseProcessor {
txn.events = []
for (const evt of events) {
const eventInstance = evt as EventInstance
const decoded = DEFAULT_TYPE_REGISTRY.decodeEvent<any>(eventInstance)
const decoded = TYPE_REGISTRY.decodeEvent<any>(eventInstance)
await handler(decoded || eventInstance, ctx)
}
}
Expand Down Expand Up @@ -163,7 +163,7 @@ export class AptosBaseProcessor {
)
if (tx) {
const payload = tx.payload as TransactionPayload_EntryFunctionPayload
const decoded = DEFAULT_TYPE_REGISTRY.decodeFunctionPayload(payload)
const decoded = TYPE_REGISTRY.decodeFunctionPayload(payload)
await handler(decoded, ctx)
}
return ctx.getProcessResult()
Expand Down
3 changes: 2 additions & 1 deletion sdk/src/aptos/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type { Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from 'aptos-sdk/src/generated'
export type { EventInstance, TypedEventInstance, TypeRegistry, TypedEntryFunctionPayload } from './types'
export type { EventInstance, TypedEventInstance, TypedEntryFunctionPayload } from './types'
export { TYPE_REGISTRY, TypeRegistry } from './types'
export type { FunctionNameAndCallFilter, EventFilter, CallFilter, ArgumentsFilter } from './aptos-processor'
export { AptosBaseProcessor } from './aptos-processor'
export { AptosContext } from './context'
Expand Down
32 changes: 27 additions & 5 deletions sdk/src/aptos/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,35 @@ export class TypeRegistry {
decodeEvent<T>(event: Event): TypedEventInstance<T> | undefined {
return this.decodedInternal<T>(event) as TypedEventInstance<T>
}
filterAndDecodeEvents<T>(typePrefix: string, resources: Event[]): TypedEventInstance<T>[] {
return this.filterAndDecodeInternal(typePrefix, resources) as TypedEventInstance<T>[]
}
decodeResource<T>(res: MoveResource): TypedMoveResource<T> | undefined {
return this.decodedInternal<T>(res)
}
filterAndDecodeResources<T>(typePrefix: string, resources: MoveResource[]): TypedMoveResource<T>[] {
return this.filterAndDecodeInternal(typePrefix, resources)
}

decodeResource<T>(event: MoveResource): TypedMoveResource<T> | undefined {
return this.decodedInternal<T>(event)
private filterAndDecodeInternal<T>(typePrefix: string, structs: StructWithTag[]): StructWithType<T>[] {
if (!structs) {
return []
}
const results: StructWithType<T>[] = []
for (const resource of structs) {
if (!resource.type.startsWith(typePrefix)) {
continue
}
const result = this.decodedInternal(resource)
if (result) {
results.push(result as StructWithType<T>)
}
}
return results
}

private decodedInternal<T>(typeStruct: StructWithTag): StructWithType<T> | undefined {
const registry = DEFAULT_TYPE_REGISTRY
const registry = TYPE_REGISTRY
// this.loadTypes(registry)
// TODO check if module is not loaded

Expand All @@ -249,7 +271,7 @@ export class TypeRegistry {
}

decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {
const registry = DEFAULT_TYPE_REGISTRY
const registry = TYPE_REGISTRY
// this.loadTypes(registry)
const argumentsTyped: any[] = []

Expand All @@ -269,4 +291,4 @@ export class TypeRegistry {
}
}

export const DEFAULT_TYPE_REGISTRY = new TypeRegistry()
export const TYPE_REGISTRY = new TypeRegistry()
17 changes: 6 additions & 11 deletions sdk/src/tests/souffl3.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { SouffleChefCampaign, CandyMachine } from './types/aptos/souffle'
import { token } from '../builtin/aptos/0x3'
import { coin, voting } from '../builtin/aptos/0x1'
import { DEFAULT_TYPE_REGISTRY } from '../aptos/types'
import DepositEventInstance = coin.DepositEventInstance
import { voting } from '../builtin/aptos/0x1'
import { TYPE_REGISTRY } from '../aptos/types'

SouffleChefCampaign.bind({ startVersion: 3212312 })
.onEntryPullTokenV2((call: SouffleChefCampaign.PullTokenV2Payload, ctx) => {
Expand All @@ -22,14 +21,10 @@ SouffleChefCampaign.bind({ startVersion: 3212312 })
}
)
.onTransaction((txn, ctx) => {
if (txn.events) {
for (const event of txn.events) {
if (event && event.type === '0x3::token::DepositEvent') {
// const typedEvent = this.dec
const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance
ctx.meter.Counter('deposit_token_count').add(depositEventInstance.data_typed.amount)
}
}
const events = TYPE_REGISTRY.filterAndDecodeEvents<token.DepositEvent>('0x3::token::DepositEvent', txn.events)
for (const event of events) {
// const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance
ctx.meter.Counter('deposit_token_count').add(event.data_typed.amount)
}
})

Expand Down
15 changes: 10 additions & 5 deletions sdk/templates/aptos/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
{
"name": "template-aptos",
"private": true,
"license": "Apache-2.0",
"version": "1.0.0",
"scripts": {
"compile": "tsc -p .",
"test": "jest",
"build": "sentio build"
"gen": "sentio gen",
"build": "sentio build",
"upload": "sentio upload"
},
"dependencies": {
"@sentio/sdk": "1.0.0-development"
"@sentio/sdk": "^1.0.0-development"
},
"devDependencies": {}
"devDependencies": {
"@types/jest": "^29.0.0",
"jest": "^29.0.0",
"ts-jest": "^29.0.0",
"typescript": "^4.7.2"
}
}

0 comments on commit 93e7413

Please sign in to comment.