datai-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. package/@graphprotocol/graph-ts/README.md +13 -0
  2. package/@graphprotocol/graph-ts/chain/arweave.ts +82 -0
  3. package/@graphprotocol/graph-ts/chain/cosmos.ts +426 -0
  4. package/@graphprotocol/graph-ts/chain/ethereum.ts +727 -0
  5. package/@graphprotocol/graph-ts/chain/near.ts +420 -0
  6. package/@graphprotocol/graph-ts/chain/starknet.ts +39 -0
  7. package/@graphprotocol/graph-ts/common/collections.ts +495 -0
  8. package/@graphprotocol/graph-ts/common/conversion.ts +3 -0
  9. package/@graphprotocol/graph-ts/common/datasource.ts +41 -0
  10. package/@graphprotocol/graph-ts/common/eager_offset.ts +42 -0
  11. package/@graphprotocol/graph-ts/common/json.ts +28 -0
  12. package/@graphprotocol/graph-ts/common/numbers.ts +407 -0
  13. package/@graphprotocol/graph-ts/common/value.ts +585 -0
  14. package/@graphprotocol/graph-ts/global/global.ts +4 -0
  15. package/@graphprotocol/graph-ts/helper-functions.ts +79 -0
  16. package/@graphprotocol/graph-ts/index.ts +156 -0
  17. package/@graphprotocol/graph-ts/package.json +3 -0
  18. package/@graphprotocol/graph-ts/tsconfig.json +4 -0
  19. package/@graphprotocol/graph-ts/types/tsconfig.base.json +3 -0
  20. package/API/index.ts +1 -0
  21. package/API/v1/activePositions/ActivePositionsResult.ts +147 -0
  22. package/API/v1/activePositions/TokenBalance.ts +25 -0
  23. package/API/v1/activePositions/activePositions.ts +37 -0
  24. package/API/v1/bigDecimal/BigDecimalPb.ts +40 -0
  25. package/API/v1/bigDecimal/bigDecimal.ts +75 -0
  26. package/API/v1/bigInt/bigInt.ts +123 -0
  27. package/API/v1/crypto/crypto.ts +16 -0
  28. package/API/v1/ethereum/SmartContractCallPb.ts +84 -0
  29. package/API/v1/ethereum/ValuePb.ts +113 -0
  30. package/API/v1/ethereum/ValuesPb.ts +39 -0
  31. package/API/v1/ethereum/ethereum.ts +76 -0
  32. package/API/v1/index.ts +10 -0
  33. package/API/v1/log/log.ts +18 -0
  34. package/API/v1/proto/activePositions/ActivePositionsResultPb.ts +139 -0
  35. package/API/v1/proto/activePositions/TokenBalancePb.ts +69 -0
  36. package/API/v1/proto/bigDecimal/BigDecimalPb.ts +56 -0
  37. package/API/v1/proto/ethereum/SmartContractCallPb.ts +102 -0
  38. package/API/v1/proto/ethereum/ValueKindPb.ts +17 -0
  39. package/API/v1/proto/ethereum/ValuePb.ts +96 -0
  40. package/API/v1/proto/ethereum/ValuesPb.ts +53 -0
  41. package/API/v1/proto/google/protobuf/Any.ts +56 -0
  42. package/API/v1/proto/google/protobuf/Timestamp.ts +56 -0
  43. package/API/v1/proto/store/EntitiesPb.ts +53 -0
  44. package/API/v1/proto/store/EntityPb.ts +99 -0
  45. package/API/v1/proto/store/EntityWithMetaPb.ts +75 -0
  46. package/API/v1/proto/store/ValueKindPb.ts +17 -0
  47. package/API/v1/proto/store/ValuePb.ts +132 -0
  48. package/API/v1/proto/store/ValuesPb.ts +53 -0
  49. package/API/v1/proto/watcher/WatcherResultPb.ts +62 -0
  50. package/API/v1/store/EntitiesPb.ts +39 -0
  51. package/API/v1/store/EntityPb.ts +75 -0
  52. package/API/v1/store/ValuePb.ts +154 -0
  53. package/API/v1/store/ValuesPb.ts +39 -0
  54. package/API/v1/store/store.ts +78 -0
  55. package/API/v1/typeConversion/typeConversion.ts +57 -0
  56. package/API/v1/watcher/WatcherInput.ts +9 -0
  57. package/API/v1/watcher/watcher.ts +33 -0
  58. package/index.ts +5 -0
  59. package/package.json +11 -0
  60. package/utils/constants.ts +16 -0
  61. package/utils/mathUtils.ts +77 -0
  62. package/utils/pdk.ts +171 -0
  63. package/utils/subgraphUtils.ts +27 -0
@@ -0,0 +1,33 @@
1
+ import { Protobuf } from 'as-proto/assembly'
2
+ import { WatcherResultPb } from '../proto/watcher/WatcherResultPb'
3
+ import { Any } from '../proto/google/protobuf/Any'
4
+ import { Host } from '@extism/as-pdk'
5
+ import { WatcherInput } from './WatcherInput'
6
+ import { Entity } from '../../../@graphprotocol/graph-ts'
7
+ import { EntityPb } from '../store/EntityPb'
8
+
9
+ export namespace watcher {
10
+ export const SCHEDULE_DEFAULT = 'DEFAULT'
11
+ export const SCHEDULE_LOW_PRIORITY = 'LOW_PRIORITY'
12
+
13
+ export function input(): WatcherInput {
14
+ const position = Protobuf.decode<Entity>(Host.input(), EntityPb.decode)
15
+ return new WatcherInput(position)
16
+ }
17
+
18
+ export function output(
19
+ resultTypeUrl: string,
20
+ resultBytes: Uint8Array,
21
+ updateTrigger: string
22
+ ): void {
23
+ const watcherResult = new WatcherResultPb()
24
+ const payload = new Any()
25
+ payload.typeUrl = resultTypeUrl
26
+ payload.value = resultBytes
27
+ watcherResult.payload = payload
28
+ watcherResult.updateTrigger = updateTrigger
29
+
30
+ const resultMsg = Protobuf.encode(watcherResult, WatcherResultPb.encode)
31
+ Host.output(resultMsg)
32
+ }
33
+ }
package/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from './utils/constants'
2
+ export * from './utils/mathUtils'
3
+ export * from './utils/pdk'
4
+ export * from './utils/subgraphUtils'
5
+ export * from './API'
package/package.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "name": "datai-sdk",
3
+ "main": "index.ts",
4
+ "version": "1.0.0",
5
+ "description": "Datai SDK which has useful libraries to help building projections",
6
+ "dependencies": {
7
+ "@extism/as-pdk": "^1.0.0",
8
+ "as-proto": "^1.3.0",
9
+ "@graphprotocol/graph-ts": "^0.34.0"
10
+ }
11
+ }
@@ -0,0 +1,16 @@
1
+ import { BigDecimal, BigInt } from '../@graphprotocol/graph-ts'
2
+
3
+ // Zero Address
4
+ export const ADDRESS_ZERO = '0x0000000000000000000000000000000000000000'
5
+
6
+ // BigInt
7
+ export const BI_0 = BigInt.fromI32(0)
8
+ export const BI_1 = BigInt.fromI32(1)
9
+ export const BI_2 = BigInt.fromI32(2)
10
+ export const BI_7 = BigInt.fromI32(7)
11
+ export const BI_9 = BigInt.fromI32(9)
12
+ export const BI_18 = BigInt.fromI32(18)
13
+
14
+ // BigDecimal
15
+ export const BD_0 = new BigDecimal(BI_0)
16
+ export const BD_1 = new BigDecimal(BI_1)
@@ -0,0 +1,77 @@
1
+ import { BigDecimal, BigInt, Bytes, Address } from '../@graphprotocol/graph-ts'
2
+ import { BD_0, BD_1 } from './constants'
3
+
4
+ const MAX_DECIMALS = 77 // max decimals in decimal128 IEEE 754 standard (limitation of thegraph host)
5
+ const MIN_DECIMALS = -18
6
+
7
+ export function pow(value: BigDecimal, power: i32): BigDecimal {
8
+ if (power == 0) {
9
+ return BD_1
10
+ }
11
+ const negativePower = power < 0
12
+ let result = BD_0.plus(value)
13
+ const powerAbs = Math.abs(power)
14
+ for (let i = 1; i < powerAbs; i++) {
15
+ result = result.times(value) //TODO: move to host side
16
+ }
17
+
18
+ if (negativePower) {
19
+ result = safeDiv(BD_1, result)
20
+ }
21
+
22
+ return result
23
+ }
24
+
25
+ export function safeDiv(amount0: BigDecimal, amount1: BigDecimal): BigDecimal {
26
+ if (amount1.equals(BD_0)) {
27
+ return BD_0
28
+ } else {
29
+ return amount0.div(amount1)
30
+ }
31
+ }
32
+
33
+ export function exponentToBigInt(decimals: i32): BigInt {
34
+ assert(decimals <= MAX_DECIMALS && decimals >= 0, 'Decimals out of range')
35
+ return BigInt.fromI32(10).pow(u8(decimals))
36
+ }
37
+
38
+ export function exponentToBigDecimal(decimals: i32): BigDecimal {
39
+ assert(
40
+ decimals <= MAX_DECIMALS && decimals >= MIN_DECIMALS,
41
+ 'Decimals out of range'
42
+ )
43
+ if (decimals >= 0) {
44
+ return BigInt.fromI32(10).pow(u8(decimals)).toBigDecimal()
45
+ } else {
46
+ return BD_1.div(BigInt.fromI32(10).pow(u8(-decimals)).toBigDecimal())
47
+ }
48
+ }
49
+
50
+ export function floor(amount: BigDecimal, decimals: i32 = 0): BigInt {
51
+ assert(decimals <= MAX_DECIMALS && decimals >= 0, 'Decimals out of range')
52
+ const result = amount.truncate(decimals)
53
+ return result.digits.times(exponentToBigInt(decimals + result.exp.toI32()))
54
+ }
55
+
56
+ export function squareRoot(value: BigDecimal, precision: i32 = 9): BigDecimal {
57
+ assert(
58
+ precision <= MAX_DECIMALS / 2 && precision >= 0,
59
+ 'Precision out of range'
60
+ )
61
+ return floor(value, precision * 2)
62
+ .sqrt()
63
+ .toBigDecimal()
64
+ .div(exponentToBigDecimal(precision))
65
+ }
66
+
67
+ export function bytesToAddress(bytes: Bytes): Address {
68
+ if (bytes.length > 20) {
69
+ bytes = Bytes.fromUint8Array(bytes.slice(bytes.length - 20))
70
+ }
71
+
72
+ return Address.fromBytes(bytes)
73
+ }
74
+
75
+ export function addressToBytes(address: Address): Bytes {
76
+ return Bytes.fromHexString(address.toHexString())
77
+ }
package/utils/pdk.ts ADDED
@@ -0,0 +1,171 @@
1
+ import { Memory, Host, LogLevel, Var } from '@extism/as-pdk'
2
+ import { length } from '@extism/as-pdk/lib/env'
3
+ import { Protobuf, Writer, Reader } from 'as-proto/assembly'
4
+
5
+ export declare namespace pdk {
6
+ export function hostAbort(
7
+ w0: u64,
8
+ w1: u64,
9
+ w2: u64,
10
+ w3: u64,
11
+ w4: u64,
12
+ w5: u64,
13
+ w6: u64,
14
+ w7: u64,
15
+ w8: u64,
16
+ w9: u64
17
+ ): void
18
+ }
19
+
20
+ // eslint-disable-next-line unused-imports/no-unused-vars
21
+ function abortHandler(
22
+ message: string | null,
23
+ fileName: string | null,
24
+ lineNumber: u32,
25
+ columnNumber: u32
26
+ ): void {
27
+ message = `${fileName || ''} (${lineNumber}:${columnNumber}): ${message || '[Error]'}`
28
+ message = message.padEnd(10 * 8, '\u0000') // min 10 words of 64 bytes
29
+ const bytes = String.UTF8.encode(message)
30
+ const memory = Memory.allocate(bytes.byteLength)
31
+ if (memory.offset > 0) {
32
+ memory.store(Uint8Array.wrap(bytes))
33
+ Host.logMemory(LogLevel.Error, memory)
34
+ const abortFlag = Uint8Array.wrap(String.UTF8.encode('true'))
35
+ Var.set('isAbort', abortFlag)
36
+ } else {
37
+ // memory cannot be allocated, use stack-based fallback
38
+ const w = Uint64Array.wrap(bytes, 0, 10)
39
+ pdk.hostAbort(w[0], w[1], w[2], w[3], w[4], w[5], w[6], w[7], w[8], w[9])
40
+ }
41
+ }
42
+
43
+ // eslint-disable-next-line unused-imports/no-unused-vars
44
+ namespace consoleHandler {
45
+ export function assert<T>(assertion: T, message?: string): void {
46
+ if (!assertion) {
47
+ safeLog(LogLevel.Error, message || '')
48
+ }
49
+ }
50
+
51
+ export function log(message?: string): void {
52
+ safeLog(LogLevel.Info, message || '')
53
+ }
54
+
55
+ export function debug(message?: string): void {
56
+ safeLog(LogLevel.Debug, message || '')
57
+ }
58
+
59
+ export function info(message?: string): void {
60
+ safeLog(LogLevel.Info, message || '')
61
+ }
62
+
63
+ export function warn(message?: string): void {
64
+ safeLog(LogLevel.Error, message || '')
65
+ }
66
+
67
+ export function error(message?: string): void {
68
+ safeLog(LogLevel.Error, message || '')
69
+ }
70
+ }
71
+
72
+ export function storeUint8Array(data: Uint8Array): Memory {
73
+ const memory = Memory.allocate(data.length)
74
+ if (memory.offset == 0 && data.length > 0) {
75
+ throw new Error('Out of extism memory')
76
+ }
77
+ memory.store(data)
78
+ return memory
79
+ }
80
+
81
+ export function storeString(data: string): Memory {
82
+ const buffer = String.UTF8.encode(data)
83
+ const bytes = Uint8Array.wrap(buffer)
84
+
85
+ return storeUint8Array(bytes)
86
+ }
87
+
88
+ export function storeProtobuf<TMessage>(
89
+ message: TMessage,
90
+ encoder: (message: TMessage, writer: Writer) => void
91
+ ): Memory {
92
+ const msg = Protobuf.encode(message, encoder)
93
+ return storeUint8Array(msg)
94
+ }
95
+
96
+ export function safeLog(level: LogLevel, s: string): void {
97
+ const memory = storeString(s)
98
+ Host.logMemory(level, memory)
99
+ memory.free()
100
+ }
101
+
102
+ export function findMemory(
103
+ offset: u64,
104
+ allowNull: bool = false
105
+ ): Memory | null {
106
+ if (offset == 0) {
107
+ if (allowNull) return null
108
+ throw new Error('Empty memory offset')
109
+ }
110
+
111
+ const len = length(offset)
112
+ if (len == 0) {
113
+ if (allowNull) return null
114
+ throw new Error('Zero memory length')
115
+ }
116
+
117
+ return new Memory(offset, len)
118
+ }
119
+
120
+ export function findUint8Array(offset: u64): Uint8Array {
121
+ return findUint8ArrayOrNull(offset, false)!
122
+ }
123
+
124
+ export function findUint8ArrayOrNull(
125
+ offset: u64,
126
+ allowNull: bool = true
127
+ ): Uint8Array | null {
128
+ const memory = findMemory(offset, allowNull)
129
+ if (memory === null) {
130
+ return null
131
+ }
132
+ const result = memory.toUint8Array()
133
+ memory.free()
134
+ return result
135
+ }
136
+
137
+ export function findString(offset: u64): string {
138
+ return findStringOrNull(offset, false)!
139
+ }
140
+
141
+ export function findStringOrNull(
142
+ offset: u64,
143
+ allowNull: bool = true
144
+ ): string | null {
145
+ const memory = findMemory(offset, allowNull)
146
+ if (memory === null) {
147
+ return null
148
+ }
149
+ const result = memory.toString()
150
+ memory.free()
151
+ return result
152
+ }
153
+
154
+ export function findProtobuOrNull<TMessage>(
155
+ offset: u64,
156
+ decoder: (reader: Reader, length: i32) => TMessage,
157
+ allowNull: bool = true
158
+ ): TMessage | null {
159
+ const data = findUint8ArrayOrNull(offset, allowNull)
160
+ if (data == null) {
161
+ return null
162
+ }
163
+ return Protobuf.decode<TMessage>(data, decoder)
164
+ }
165
+
166
+ export function findProtobuf<TMessage>(
167
+ offset: u64,
168
+ decoder: (reader: Reader, length: i32) => TMessage
169
+ ): TMessage {
170
+ return findProtobuOrNull<TMessage>(offset, decoder)!
171
+ }
@@ -0,0 +1,27 @@
1
+ import { Entity, store } from '@graphprotocol/graph-ts'
2
+ import { watcher } from '../API'
3
+
4
+ export function registerUser(userId: string): void {
5
+ let user = store.get('User', userId)
6
+
7
+ if (user == null) {
8
+ user = new Entity()
9
+ user.setString('id', userId)
10
+ store.set('User', userId, user)
11
+ }
12
+ }
13
+
14
+ export function ensureUpdateTrigger(
15
+ userPositionId: string,
16
+ updateTriggerId: string = watcher.SCHEDULE_DEFAULT
17
+ ): bool {
18
+ const updateTrigger = store.get('PositionUpdateTrigger', userPositionId)
19
+ if (updateTrigger == null) {
20
+ const updateTrigger = new Entity()
21
+ updateTrigger.setString('id', userPositionId)
22
+ updateTrigger.setString('updateTrigger', updateTriggerId)
23
+ store.set('PositionUpdateTrigger', userPositionId, updateTrigger)
24
+ return false
25
+ }
26
+ return true
27
+ }