Skip to content

Commit

Permalink
chore(sui): move object change into object type processor (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
zfy0701 authored Dec 4, 2024
1 parent 3a76653 commit e4afb7f
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 34 deletions.
1 change: 0 additions & 1 deletion examples/sui/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,5 @@ SuiObjectTypeProcessor.bind({
},
60,
60 * 24 * 30,
undefined,
{ owned: false }
)
6 changes: 3 additions & 3 deletions packages/sdk/src/aptos/aptos-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
parseMoveType,
ResourceChangeHandler
} from '../move/index.js'
import { Labels, PromiseOrVoid } from '../core/index.js'
import { ALL_ADDRESS, Labels, PromiseOrVoid } from '../core/index.js'
import { TypeDescriptor } from '@typemove/move'
import { decodeResourceChange, ResourceChange } from '@typemove/aptos'

Expand Down Expand Up @@ -267,7 +267,7 @@ export class AptosBaseProcessor {

export class AptosModulesProcessor extends AptosBaseProcessor {
private constructor(options: AptosBindOptions) {
super('*', options)
super(ALL_ADDRESS, options)
}

static bind(options: AptosBindOptions): AptosModulesProcessor {
Expand Down Expand Up @@ -432,7 +432,7 @@ function configure(options: AptosBindOptions): IndexConfigure {

return {
startVersion: startVersion,
address: options.address === '*' ? '*' : accountTypeString(options.address), // aptos don't use address string in api, so only use type string
address: options.address === ALL_ADDRESS ? ALL_ADDRESS : accountTypeString(options.address), // aptos don't use address string in api, so only use type string
network: options.network || AptosNetwork.MAIN_NET,
baseLabels: options.baseLabels
}
Expand Down
14 changes: 7 additions & 7 deletions packages/sdk/src/aptos/codegen/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class AptosNetworkCodegen extends BaseAptosCodegen {
const MAINNET_CODEGEN = new AptosNetworkCodegen(AptosNetwork.MAIN_NET)
const TESTNET_CODEGEN = new AptosNetworkCodegen(AptosNetwork.TEST_NET)
const MOVEMENT_MAINNET_CODEGEN = new AptosNetworkCodegen(AptosNetwork.MOVEMENT_MAIN_NET)
const M2_TESTNET_CODEGEN = new AptosNetworkCodegen(AptosNetwork.MOVEMENT_TEST_NET)
// const M2_TESTNET_CODEGEN = new AptosNetworkCodegen(AptosNetwork.MOVEMENT_TEST_NET)
const MOVEMENT_PROTO_CODEGEN = new AptosNetworkCodegen(AptosNetwork.MOVEMENT_PORTO)

class AptosCodegen {
Expand All @@ -75,16 +75,16 @@ class AptosCodegen {
path.join(outputDir, 'movement-mainnet'),
builtin
)
const num4 = await M2_TESTNET_CODEGEN.generate(
path.join(srcDir, 'm2-testnet'),
path.join(outputDir, 'm2-testnet'),
builtin
)
// const num4 = await M2_TESTNET_CODEGEN.generate(
// path.join(srcDir, 'm2-testnet'),
// path.join(outputDir, 'm2-testnet'),
// builtin
// )
const num5 = await MOVEMENT_PROTO_CODEGEN.generate(
path.join(srcDir, 'movement-porto'),
path.join(outputDir, 'movement-porto'),
builtin
)
return num1 + num2 + num3 + num4 + num5
return num1 + num2 + num3 + num5
}
}
2 changes: 1 addition & 1 deletion packages/sdk/src/aptos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export {
} from './aptos-processor.js'
export { AptosResourceProcessorTemplate } from './aptos-resource-processor-template.js'
export { AptosContext, AptosResourcesContext } from './context.js'
export { AptosBindOptions, AptosNetwork } from './network.js'
export { AptosBindOptions, AptosNetwork, MovementNetwork } from './network.js'
export { type ResourceChange } from '@typemove/aptos'
export * from './api.js'

Expand Down
8 changes: 6 additions & 2 deletions packages/sdk/src/aptos/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ export const AptosNetwork = <const>{
TEST_NET: AptosChainId.APTOS_TESTNET,
// DEV_NET: AptosChainId.APTOS_DEVNET,
MOVEMENT_MAIN_NET: AptosChainId.APTOS_MOVEMENT_MAINNET,
MOVEMENT_TEST_NET: AptosChainId.APTOS_MOVEMENT_TESTNET,
MOVEMENT_PORTO: AptosChainId.APTOS_MOVEMENT_PORTO
}

export const MovementNetwork = <const>{
MAIN_NET: AptosChainId.APTOS_MOVEMENT_MAINNET,
PORTO: AptosChainId.APTOS_MOVEMENT_PORTO
}

export class AptosBindOptions {
address: string
network?: AptosNetwork = AptosNetwork.MAIN_NET
Expand All @@ -27,7 +31,7 @@ export function getRpcEndpoint(network: AptosNetwork): string {
return 'https://mainnet.aptoslabs.com/v1'
case AptosNetwork.TEST_NET:
return 'https://testnet.aptoslabs.com/v1'
case AptosNetwork.MOVEMENT_TEST_NET:
case AptosChainId.APTOS_MOVEMENT_TESTNET:
return 'https://aptos.testnet.suzuka.movementlabs.xyz/v1'
case AptosNetwork.MOVEMENT_MAIN_NET:
return 'https://mainnet.movementnetwork.xyz/v1'
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/core/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const ALL_ADDRESS = '*'
1 change: 1 addition & 0 deletions packages/sdk/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { type Numberish, toBigInteger, toMetricValue } from './numberish.js'

export { CorePlugin } from './core-plugin.js'
export { DatabaseSchema } from './database-schema.js'
export * from './constants.js'
7 changes: 4 additions & 3 deletions packages/sdk/src/eth/base-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import sha3 from 'js-sha3'
import { ListStateStorage, metricsStorage } from '@sentio/runtime'
import { EthChainId } from '@sentio/chain'
import { handlersProxy } from '../utils/metrics.js'
import { ALL_ADDRESS } from '../core/index.js'

export interface AddressOrTypeEventFilter extends DeferredTopicFilter {
addressType?: AddressType
Expand Down Expand Up @@ -85,7 +86,7 @@ export class GlobalProcessor {

constructor(config: Omit<BindOptions, 'address'>) {
this.config = {
address: '*',
address: ALL_ADDRESS,
name: config.name || 'Global',
network: config.network || EthChainId.ETHEREUM,
startBlock: 0n
Expand Down Expand Up @@ -180,7 +181,7 @@ export class GlobalProcessor {

const ctx = new GlobalContext(
chainId,
'*',
ALL_ADDRESS,
new Date(block.timestamp * 1000),
block,
undefined,
Expand All @@ -201,7 +202,7 @@ export class GlobalProcessor {

const ctx = new GlobalContext(
chainId,
'*',
ALL_ADDRESS,
new Date(block.timestamp * 1000),
block,
undefined,
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/eth/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { ContractContext } from './context.js'
import { getAddress } from 'ethers/address'
import { getBigInt, getNumber, hexlify } from 'ethers/utils'
import { EthCallContext, EthCallParam } from '@sentio/protos'
import { ALL_ADDRESS } from '../core/index.js'

export interface IResult {
/**
Expand Down Expand Up @@ -271,7 +272,7 @@ export function isNullAddress(address: string) {
}

export function validateAndNormalizeAddress(address: string): string {
if (address === '*') {
if (address === ALL_ADDRESS) {
return address
}
const normalizedAddress = getAddress(address)
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/eth/tests/erc20-star.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { expect } from 'chai'

import { firstCounterValue, TestProcessorServer } from '../../testing/index.js'
import { ERC20Processor, mockTransferLog } from '../builtin/erc20.js'
import { ALL_ADDRESS } from '@sentio/sdk'

describe('Test star Examples', () => {
const service = new TestProcessorServer(async () => {
ERC20Processor.bind({
address: '*',
address: ALL_ADDRESS,
startBlock: 14201940
}).onEventTransfer(async (evt, ctx) => {
ctx.meter.Counter('c1').add(1)
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/fuel/global-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { decodeFuelTransaction, DEFAULT_FUEL_FETCH_CONFIG, FuelFetchConfig } fro
import { FuelContext } from './context.js'
import { FuelProcessorConfig, getOptionsSignature } from './fuel-processor.js'
import { mergeProcessResults } from '@sentio/runtime'
import { ALL_ADDRESS } from '../core/index.js'

type GlobalFuelProcessorConfig = Omit<FuelProcessorConfig, 'address' | 'abi'>

Expand All @@ -21,7 +22,7 @@ export class FuelGlobalProcessor implements FuelBaseProcessor<GlobalFuelProcesso
'global_' +
getOptionsSignature({
...config,
address: '*'
address: ALL_ADDRESS
})
FuelProcessorState.INSTANCE.getOrSetValue(sig, processor)
return processor
Expand All @@ -48,7 +49,7 @@ export class FuelGlobalProcessor implements FuelBaseProcessor<GlobalFuelProcesso
}
const ctx = new FuelContext(
this.config.chainId,
'*',
ALL_ADDRESS,
this.config.name ?? '*',
call.timestamp || new Date(0),
tx,
Expand Down
86 changes: 79 additions & 7 deletions packages/sdk/src/sui/sui-object-processor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Data_SuiCall,
Data_SuiObject,
Data_SuiObjectChange,
HandleInterval,
MoveAccountFetchConfig,
MoveFetchConfig,
Expand All @@ -9,11 +10,11 @@ import {
} from '@sentio/protos'
import { ListStateStorage } from '@sentio/runtime'
import { SuiNetwork } from './network.js'
import { SuiAddressContext, SuiContext, SuiObjectContext } from './context.js'
import { SuiMoveObject, SuiTransactionBlockResponse } from '@mysten/sui/client'
import { PromiseOrVoid } from '../core/index.js'
import { SuiAddressContext, SuiContext, SuiObjectChangeContext, SuiObjectContext } from './context.js'
import { SuiMoveObject, SuiObjectChange, SuiTransactionBlockResponse } from '@mysten/sui/client'
import { ALL_ADDRESS, PromiseOrVoid } from '../core/index.js'
import { configure, DEFAULT_FETCH_CONFIG, IndexConfigure, SuiBindOptions } from './sui-processor.js'
import { CallHandler, TransactionFilter, accountTypeString } from '../move/index.js'
import { CallHandler, TransactionFilter, accountTypeString, ObjectChangeHandler } from '../move/index.js'
import { ServerError, Status } from 'nice-grpc'
import { TypeDescriptor } from '@typemove/move'
import { TypedSuiMoveObject } from './models.js'
Expand Down Expand Up @@ -58,6 +59,7 @@ export abstract class SuiBaseObjectOrAddressProcessor<HandlerType> {
templateId: number | undefined

objectHandlers: ObjectHandler[] = []
objectChangeHandlers: ObjectChangeHandler<Data_SuiObjectChange>[] = []

// static bind(options: SuiObjectsBindOptions): SuiBaseObjectsProcessor<any> {
// return new SuiBaseObjectsProcessor(options)
Expand Down Expand Up @@ -113,7 +115,11 @@ export abstract class SuiBaseObjectOrAddressProcessor<HandlerType> {
})
return this
}
}

abstract class SuiBaseObjectOrAddressProcessorInternal<
HandlerType
> extends SuiBaseObjectOrAddressProcessor<HandlerType> {
public onTimeInterval(
handler: HandlerType,
timeIntervalInMinutes = 60,
Expand Down Expand Up @@ -150,7 +156,7 @@ export abstract class SuiBaseObjectOrAddressProcessor<HandlerType> {
}
}

export class SuiAddressProcessor extends SuiBaseObjectOrAddressProcessor<
export class SuiAddressProcessor extends SuiBaseObjectOrAddressProcessorInternal<
(objects: SuiMoveObject[], ctx: SuiAddressContext) => PromiseOrVoid
> {
callHandlers: CallHandler<Data_SuiCall>[] = []
Expand Down Expand Up @@ -212,7 +218,7 @@ export class SuiAddressProcessor extends SuiBaseObjectOrAddressProcessor<
}
}

export class SuiObjectProcessor extends SuiBaseObjectOrAddressProcessor<
export class SuiObjectProcessor extends SuiBaseObjectOrAddressProcessorInternal<
(self: SuiMoveObject, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectContext) => PromiseOrVoid
> {
static bind(options: SuiObjectBindOptions): SuiObjectProcessor {
Expand Down Expand Up @@ -242,9 +248,10 @@ export class SuiObjectTypeProcessor<T> extends SuiBaseObjectOrAddressProcessor<
(self: TypedSuiMoveObject<T>, dynamicFieldObjects: SuiMoveObject[], ctx: SuiObjectContext) => PromiseOrVoid
> {
objectType: TypeDescriptor<T>

static bind<T>(options: SuiObjectTypeBindOptions<T>): SuiObjectTypeProcessor<T> {
const processor = new SuiObjectTypeProcessor<T>({
address: options.objectType.qname,
address: ALL_ADDRESS, // current only support on all address
network: options.network,
startCheckpoint: options.startCheckpoint,
ownerType: MoveOwnerType.TYPE,
Expand All @@ -270,6 +277,71 @@ export class SuiObjectTypeProcessor<T> extends SuiBaseObjectOrAddressProcessor<
const object = await ctx.coder.filterAndDecodeObjects(this.objectType, [data.self as SuiMoveObject])
return handler(object[0], data.objects as SuiMoveObject[], ctx)
}

public onObjectChange(handler: (changes: SuiObjectChange[], ctx: SuiObjectChangeContext) => void): this {
if (this.config.network === SuiNetwork.TEST_NET) {
throw new ServerError(Status.INVALID_ARGUMENT, 'object change not supported in testnet')
}
const processor = this
this.objectChangeHandlers.push({
handler: async function (data: Data_SuiObjectChange) {
const ctx = new SuiObjectChangeContext(
processor.config.network,
processor.config.address,
data.timestamp || new Date(0),
data.slot,
data.txDigest,
processor.config.baseLabels
)
await handler(data.changes as SuiObjectChange[], ctx)
return ctx.stopAndGetResult()
},
type: this.objectType.qname
})

return this
}

public onTimeInterval(
handler: (
self: TypedSuiMoveObject<T>,
dynamicFieldObjects: SuiMoveObject[],
ctx: SuiObjectContext
) => PromiseOrVoid,
timeIntervalInMinutes = 60,
backfillTimeIntervalInMinutes = 240,
fetchConfig?: Partial<MoveAccountFetchConfig>
): this {
return this.onInterval(
handler,
{
recentInterval: timeIntervalInMinutes,
backfillInterval: backfillTimeIntervalInMinutes
},
undefined,
this.objectType.qname,
fetchConfig
)
}

public onCheckpointInterval(
handler: (
self: TypedSuiMoveObject<T>,
dynamicFieldObjects: SuiMoveObject[],
ctx: SuiObjectContext
) => PromiseOrVoid,
checkpointInterval = 100000,
backfillCheckpointInterval = 400000,
fetchConfig?: Partial<MoveAccountFetchConfig>
): this {
return this.onInterval(
handler,
undefined,
{ recentInterval: checkpointInterval, backfillInterval: backfillCheckpointInterval },
this.objectType.qname,
fetchConfig
)
}
}

export class SuiWrappedObjectProcessor extends SuiBaseObjectOrAddressProcessor<
Expand Down
11 changes: 11 additions & 0 deletions packages/sdk/src/sui/sui-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export class SuiPlugin extends Plugin {
}
contractConfig.moveCallConfigs.push(functionHandlerConfig)
}
// deprecated, use objectType processor instead
for (const handler of suiProcessor.objectChangeHandlers) {
const handlerId = handlers.suiObjectChangeHandlers.push(handler.handler) - 1
const objectChangeHandler: MoveResourceChangeConfig = {
Expand All @@ -141,6 +142,16 @@ export class SuiPlugin extends Plugin {
})
for (const handler of processor.objectHandlers) {
const handlerId = handlers.suiObjectHandlers.push(handler.handler) - 1

for (const handler of processor.objectChangeHandlers) {
const handlerId = handlers.suiObjectChangeHandlers.push(handler.handler) - 1
const objectChangeHandler: MoveResourceChangeConfig = {
type: handler.type,
handlerId
}
accountConfig.moveResourceChangeConfigs.push(objectChangeHandler)
}

accountConfig.moveIntervalConfigs.push({
intervalConfig: {
handlerId: handlerId,
Expand Down
Loading

0 comments on commit e4afb7f

Please sign in to comment.