Skip to content

Commit 93e7413

Browse files
authored
chore(sdk): better filter decode api for list of events/resources (stephenh#188)
1 parent 5fbbe66 commit 93e7413

File tree

8 files changed

+67
-38
lines changed

8 files changed

+67
-38
lines changed

.circleci/config.yml

+11-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ jobs:
2828
command: ./scripts/build-all.sh
2929
name: Build All
3030
- run:
31-
command: yarn gen && yarn test
32-
name: Test Template
33-
working_directory: sdk/template
31+
command: yarn install && yarn gen && yarn test
32+
name: Test Template - Aptos
33+
working_directory: sdk/templates/aptos
34+
- run:
35+
command: yarn install && yarn gen && yarn test
36+
name: Test Template - EVM
37+
working_directory: sdk/templates/evm
38+
- run:
39+
command: yarn install && yarn test
40+
name: Test Template - RAW
41+
working_directory: sdk/templates/raw
3442
# - persist_to_workspace:
3543
# root: ~/project
3644
# paths:

.github/workflows/release.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
- name: Build
2727
run: yarn build
2828
working-directory: sdk
29-
- name: Clean template
30-
run: rm -rf node_modules
31-
working-directory: sdk/template
29+
- name: Clean templates node mdules
30+
run: "rm -rf */node_modules"
31+
working-directory: sdk/templates
3232
- name: Semantic Release
3333
id: semantic
3434
uses: cycjimmy/semantic-release-action@v3

examples/aptos/src/processor.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ SouffleChefCampaign.bind({ network: aptos.AptosNetwork.TEST_NET, startVersion: 6
2323
ctx.meter.Counter('burned').add(1)
2424
})
2525
.onTransaction((txn, ctx) => {
26-
if (txn.events) {
27-
for (const event of txn.events) {
28-
if (event && event.type === '0x3::token::DepositEvent') {
29-
ctx.meter.Counter('deposit_token_count').add(Number(event.data.amount))
30-
}
31-
}
26+
const events = aptos.TYPE_REGISTRY.filterAndDecodeEvents<token.DepositEvent>('0x3::token::DepositEvent', txn.events)
27+
for (const event of events) {
28+
// const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance
29+
ctx.meter.Counter('deposit_token_count').add(event.data_typed.amount)
3230
}
3331
})
3432

sdk/src/aptos/aptos-processor.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
} from '.'
1010

1111
import Long from 'long'
12-
import { EventInstance, DEFAULT_TYPE_REGISTRY } from './types'
12+
import { EventInstance, TYPE_REGISTRY } from './types'
1313
import { getChainId } from './network'
1414

1515
type IndexConfigure = {
@@ -59,7 +59,7 @@ export class AptosBaseProcessor {
5959
this.moduleName = moduleName
6060
this.configure(options)
6161
global.PROCESSOR_STATE.aptosProcessors.push(this)
62-
this.loadTypes(DEFAULT_TYPE_REGISTRY)
62+
this.loadTypes(TYPE_REGISTRY)
6363
}
6464

6565
// getABI(): MoveModule | undefined {
@@ -124,7 +124,7 @@ export class AptosBaseProcessor {
124124
txn.events = []
125125
for (const evt of events) {
126126
const eventInstance = evt as EventInstance
127-
const decoded = DEFAULT_TYPE_REGISTRY.decodeEvent<any>(eventInstance)
127+
const decoded = TYPE_REGISTRY.decodeEvent<any>(eventInstance)
128128
await handler(decoded || eventInstance, ctx)
129129
}
130130
}
@@ -163,7 +163,7 @@ export class AptosBaseProcessor {
163163
)
164164
if (tx) {
165165
const payload = tx.payload as TransactionPayload_EntryFunctionPayload
166-
const decoded = DEFAULT_TYPE_REGISTRY.decodeFunctionPayload(payload)
166+
const decoded = TYPE_REGISTRY.decodeFunctionPayload(payload)
167167
await handler(decoded, ctx)
168168
}
169169
return ctx.getProcessResult()

sdk/src/aptos/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export type { Transaction_UserTransaction, TransactionPayload_EntryFunctionPayload } from 'aptos-sdk/src/generated'
2-
export type { EventInstance, TypedEventInstance, TypeRegistry, TypedEntryFunctionPayload } from './types'
2+
export type { EventInstance, TypedEventInstance, TypedEntryFunctionPayload } from './types'
3+
export { TYPE_REGISTRY, TypeRegistry } from './types'
34
export type { FunctionNameAndCallFilter, EventFilter, CallFilter, ArgumentsFilter } from './aptos-processor'
45
export { AptosBaseProcessor } from './aptos-processor'
56
export { AptosContext } from './context'

sdk/src/aptos/types.ts

+27-5
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,35 @@ export class TypeRegistry {
225225
decodeEvent<T>(event: Event): TypedEventInstance<T> | undefined {
226226
return this.decodedInternal<T>(event) as TypedEventInstance<T>
227227
}
228+
filterAndDecodeEvents<T>(typePrefix: string, resources: Event[]): TypedEventInstance<T>[] {
229+
return this.filterAndDecodeInternal(typePrefix, resources) as TypedEventInstance<T>[]
230+
}
231+
decodeResource<T>(res: MoveResource): TypedMoveResource<T> | undefined {
232+
return this.decodedInternal<T>(res)
233+
}
234+
filterAndDecodeResources<T>(typePrefix: string, resources: MoveResource[]): TypedMoveResource<T>[] {
235+
return this.filterAndDecodeInternal(typePrefix, resources)
236+
}
228237

229-
decodeResource<T>(event: MoveResource): TypedMoveResource<T> | undefined {
230-
return this.decodedInternal<T>(event)
238+
private filterAndDecodeInternal<T>(typePrefix: string, structs: StructWithTag[]): StructWithType<T>[] {
239+
if (!structs) {
240+
return []
241+
}
242+
const results: StructWithType<T>[] = []
243+
for (const resource of structs) {
244+
if (!resource.type.startsWith(typePrefix)) {
245+
continue
246+
}
247+
const result = this.decodedInternal(resource)
248+
if (result) {
249+
results.push(result as StructWithType<T>)
250+
}
251+
}
252+
return results
231253
}
232254

233255
private decodedInternal<T>(typeStruct: StructWithTag): StructWithType<T> | undefined {
234-
const registry = DEFAULT_TYPE_REGISTRY
256+
const registry = TYPE_REGISTRY
235257
// this.loadTypes(registry)
236258
// TODO check if module is not loaded
237259

@@ -249,7 +271,7 @@ export class TypeRegistry {
249271
}
250272

251273
decodeFunctionPayload(payload: TransactionPayload_EntryFunctionPayload): TransactionPayload_EntryFunctionPayload {
252-
const registry = DEFAULT_TYPE_REGISTRY
274+
const registry = TYPE_REGISTRY
253275
// this.loadTypes(registry)
254276
const argumentsTyped: any[] = []
255277

@@ -269,4 +291,4 @@ export class TypeRegistry {
269291
}
270292
}
271293

272-
export const DEFAULT_TYPE_REGISTRY = new TypeRegistry()
294+
export const TYPE_REGISTRY = new TypeRegistry()

sdk/src/tests/souffl3.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { SouffleChefCampaign, CandyMachine } from './types/aptos/souffle'
22
import { token } from '../builtin/aptos/0x3'
3-
import { coin, voting } from '../builtin/aptos/0x1'
4-
import { DEFAULT_TYPE_REGISTRY } from '../aptos/types'
5-
import DepositEventInstance = coin.DepositEventInstance
3+
import { voting } from '../builtin/aptos/0x1'
4+
import { TYPE_REGISTRY } from '../aptos/types'
65

76
SouffleChefCampaign.bind({ startVersion: 3212312 })
87
.onEntryPullTokenV2((call: SouffleChefCampaign.PullTokenV2Payload, ctx) => {
@@ -22,14 +21,10 @@ SouffleChefCampaign.bind({ startVersion: 3212312 })
2221
}
2322
)
2423
.onTransaction((txn, ctx) => {
25-
if (txn.events) {
26-
for (const event of txn.events) {
27-
if (event && event.type === '0x3::token::DepositEvent') {
28-
// const typedEvent = this.dec
29-
const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance
30-
ctx.meter.Counter('deposit_token_count').add(depositEventInstance.data_typed.amount)
31-
}
32-
}
24+
const events = TYPE_REGISTRY.filterAndDecodeEvents<token.DepositEvent>('0x3::token::DepositEvent', txn.events)
25+
for (const event of events) {
26+
// const depositEventInstance = DEFAULT_TYPE_REGISTRY.decodeEvent(event) as DepositEventInstance
27+
ctx.meter.Counter('deposit_token_count').add(event.data_typed.amount)
3328
}
3429
})
3530

sdk/templates/aptos/package.json

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
{
22
"name": "template-aptos",
33
"private": true,
4-
"license": "Apache-2.0",
54
"version": "1.0.0",
65
"scripts": {
7-
"compile": "tsc -p .",
86
"test": "jest",
9-
"build": "sentio build"
7+
"gen": "sentio gen",
8+
"build": "sentio build",
9+
"upload": "sentio upload"
1010
},
1111
"dependencies": {
12-
"@sentio/sdk": "1.0.0-development"
12+
"@sentio/sdk": "^1.0.0-development"
1313
},
14-
"devDependencies": {}
14+
"devDependencies": {
15+
"@types/jest": "^29.0.0",
16+
"jest": "^29.0.0",
17+
"ts-jest": "^29.0.0",
18+
"typescript": "^4.7.2"
19+
}
1520
}

0 commit comments

Comments
 (0)