/
context.ts
122 lines (110 loc) · 3.31 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { RecordMetaData } from '@sentio/protos'
import { type Labels, normalizeLabels } from '@sentio/sdk'
import { MoveCoder } from '@sentio/sdk/aptos'
import {
Aptos,
Event,
MoveResource,
UserTransactionResponse,
MoveModuleBytecode,
AptosConfig
} from '@aptos-labs/ts-sdk'
import { defaultMoveCoder } from './move-coder.js'
import { AptosNetwork } from './network.js'
import { Endpoints } from '@sentio/runtime'
import { ServerError, Status } from 'nice-grpc'
import { MoveAccountContext, MoveContext } from '../move/index.js'
export class AptosContext extends MoveContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
moduleName: string
version: bigint
transaction: UserTransactionResponse
eventIndex: number
coder: MoveCoder
constructor(
moduleName: string,
network: AptosNetwork,
address: string,
version: bigint,
transaction: UserTransactionResponse,
eventIndex: number,
baseLabels: Labels | undefined
) {
super(baseLabels)
this.address = address.toLowerCase()
this.network = network
this.moduleName = moduleName
this.version = version
this.eventIndex = eventIndex
this.coder = defaultMoveCoder(network)
if (transaction) {
this.transaction = transaction
}
}
getChainId() {
return this.network
}
getTimestamp(): number {
return parseInt(this.transaction.timestamp)
}
getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
return {
address: this.address,
contractName: this.moduleName,
blockNumber: this.version,
transactionIndex: 0,
transactionHash: this.transaction?.hash || '', // TODO
logIndex: this.eventIndex,
chainId: this.getChainId(),
name: name,
labels: normalizeLabels(labels)
}
}
getClient(): Aptos {
let chainServer = Endpoints.INSTANCE.chainServer.get(this.network)
if (!chainServer) {
throw new ServerError(Status.INTERNAL, 'RPC endpoint not provided')
}
chainServer = chainServer + '/v1'
return new Aptos(new AptosConfig({ fullnode: chainServer }))
}
}
export class AptosResourcesContext extends MoveAccountContext<AptosNetwork, MoveModuleBytecode, Event | MoveResource> {
version: bigint
timestampInMicros: number
coder: MoveCoder
constructor(network: AptosNetwork, address: string, version: bigint, timestampInMicros: number, baseLabels?: Labels) {
super(baseLabels)
this.address = address
this.network = network
this.version = version
this.timestampInMicros = timestampInMicros
this.coder = defaultMoveCoder(network)
}
getChainId() {
return this.network
}
getTimestamp(): number {
return this.timestampInMicros
}
getMetaDataInternal(name: string, labels: Labels): RecordMetaData {
return {
address: this.address,
contractName: 'resources',
blockNumber: this.version,
transactionIndex: 0,
transactionHash: '',
logIndex: 0,
chainId: this.getChainId(),
name: name,
labels: normalizeLabels(labels)
}
}
getClient(): Aptos {
let chainServer = Endpoints.INSTANCE.chainServer.get(this.network)
if (!chainServer) {
throw new ServerError(Status.INTERNAL, 'RPC endpoint not provided')
}
chainServer = chainServer + '/v1'
return new Aptos(new AptosConfig({ fullnode: chainServer }))
}
}