datai-sdk 1.1.1 → 1.1.3

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.
@@ -0,0 +1,156 @@
1
+ // Side-effect to evaluate eagerly the offset of stub AS runtime
2
+ import './common/eager_offset';
3
+ import { ByteArray, Bytes, Entity } from './common/collections';
4
+ import { Value } from './common/value';
5
+
6
+ // Arweave support
7
+ export * from './chain/arweave';
8
+ // Ethereum support
9
+ export * from './chain/ethereum';
10
+ // NEAR support
11
+ export * from './chain/near';
12
+ // Cosmos support
13
+ export * from './chain/cosmos';
14
+ // Starknet support
15
+ export * from './chain/starknet';
16
+ // Regular re-exports
17
+ export * from './common/collections';
18
+ export * from './common/conversion';
19
+ export * from './common/datasource';
20
+ export * from './common/json';
21
+ export * from './common/numbers';
22
+ export * from './common/value';
23
+ import * as API from '../../../API'
24
+ export { store, crypto } from '../../../API';
25
+
26
+
27
+ // /** Host IPFS interface */
28
+ // export declare namespace ipfs {
29
+ // function cat(hash: string): Bytes | null;
30
+ // function map(hash: string, callback: string, userData: Value, flags: string[]): void;
31
+ // }
32
+
33
+ // export namespace ipfs {
34
+ // export function mapJSON(hash: string, callback: string, userData: Value): void {
35
+ // ipfs.map(hash, callback, userData, ['json']);
36
+ // }
37
+ // }
38
+
39
+ // /**
40
+ // * Special function for ENS name lookups, not meant for general purpose use.
41
+ // * This function will only be useful if the graph-node instance has additional
42
+ // * data loaded **
43
+ // */
44
+ // export declare namespace ens {
45
+ // function nameByHash(hash: string): string | null;
46
+ // }
47
+
48
+ function format(fmt: string, args: string[]): string {
49
+ let out = '';
50
+ let argIndex = 0;
51
+ for (let i: i32 = 0, len: i32 = fmt.length; i < len; i++) {
52
+ if (
53
+ i < len - 1 &&
54
+ fmt.charCodeAt(i) == 0x7b /* '{' */ &&
55
+ fmt.charCodeAt(i + 1) == 0x7d /* '}' */
56
+ ) {
57
+ if (argIndex >= args.length) {
58
+ throw new Error('Too few arguments for format string: ' + fmt);
59
+ } else {
60
+ out += args[argIndex++];
61
+ i++;
62
+ }
63
+ } else {
64
+ out += fmt.charAt(i);
65
+ }
66
+ }
67
+ return out;
68
+ }
69
+
70
+ export namespace log {
71
+ export const log = API.log.log;
72
+
73
+ export enum Level {
74
+ CRITICAL = 0,
75
+ ERROR = 1,
76
+ WARNING = 2,
77
+ INFO = 3,
78
+ DEBUG = 4,
79
+ }
80
+
81
+ /**
82
+ * Logs a critical message that terminates the subgraph.
83
+ *
84
+ * @param msg Format string a la "Value = {}, other = {}".
85
+ * @param args Format string arguments.
86
+ */
87
+ export function critical(msg: string, args: Array<string>): void {
88
+ log(Level.CRITICAL, format(msg, args));
89
+ }
90
+
91
+ /**
92
+ * Logs an error message.
93
+ *
94
+ * @param msg Format string a la "Value = {}, other = {}".
95
+ * @param args Format string arguments.
96
+ */
97
+ export function error(msg: string, args: Array<string>): void {
98
+ log(Level.ERROR, format(msg, args));
99
+ }
100
+
101
+ /** Logs a warning message.
102
+ *
103
+ * @param msg Format string a la "Value = {}, other = {}".
104
+ * @param args Format string arguments.
105
+ */
106
+ export function warning(msg: string, args: Array<string>): void {
107
+ log(Level.WARNING, format(msg, args));
108
+ }
109
+
110
+ /** Logs an info message.
111
+ *
112
+ * @param msg Format string a la "Value = {}, other = {}".
113
+ * @param args Format string arguments.
114
+ */
115
+ export function info(msg: string, args: Array<string>): void {
116
+ log(Level.INFO, format(msg, args));
117
+ }
118
+
119
+ /** Logs a debug message.
120
+ *
121
+ * @param msg Format string a la "Value = {}, other = {}".
122
+ * @param args Format string arguments.
123
+ */
124
+ export function debug(msg: string, args: Array<string>): void {
125
+ log(Level.DEBUG, format(msg, args));
126
+ }
127
+ }
128
+
129
+ /**
130
+ * Helper functions for Ethereum.
131
+ */
132
+ export namespace EthereumUtils {
133
+ /**
134
+ * Returns the contract address that would result from the given CREATE2 call.
135
+ * @param from The Ethereum address of the account that is initiating the contract creation.
136
+ * @param salt A 32-byte value that is used to create a deterministic address for the contract. This can be any arbitrary value, but it should be unique to the contract being created.
137
+ * @param initCodeHash he compiled code that will be executed when the contract is created. This should be a hex-encoded string that represents the compiled bytecode.
138
+ * @returns Address of the contract that would be created.
139
+ */
140
+ export function getCreate2Address(from: Bytes, salt: Bytes, initCodeHash: Bytes): Bytes {
141
+ return Bytes.fromHexString(
142
+ Bytes.fromByteArray(
143
+ API.crypto.keccak256(
144
+ Bytes.fromHexString(
145
+ '0xff' +
146
+ from.toHexString().slice(2) +
147
+ salt.toHexString().slice(2) +
148
+ initCodeHash.toHexString().slice(2),
149
+ ),
150
+ ),
151
+ )
152
+ .toHexString()
153
+ .slice(26),
154
+ );
155
+ }
156
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "ascMain": "index.ts"
3
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../../../../node_modules/assemblyscript/std/assembly.json",
3
+ "include": ["index.ts", "helper-functions.ts", "global/global.ts", "test"]
4
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "assemblyscript/std/assembly"
3
+ }
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { Writer, Reader } from "as-proto/assembly";
7
7
  import { TokenBalancePb } from "./TokenBalancePb";
8
- import { Timestamp } from "../google/protobuf/Timestamp";
8
+ import { Timestamp } from "../../google/protobuf/Timestamp";
9
9
  import { NftItemPb } from "./NftItemPb";
10
10
  import { PerpPositionPb } from "./PerpPositionPb";
11
11
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "datai-sdk",
3
3
  "main": "index.ts",
4
- "version": "1.1.1",
4
+ "version": "1.1.3",
5
5
  "description": "Datai SDK which has useful libraries to help building projections",
6
6
  "scripts": {
7
7
  "postinstall": "node postinstall-script.js"
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Patch @graphprotocol/graph-ts to fix AssemblyScript type inference issue
3
+ *
4
+ * The issue: `assert(this !== null, ...)` in BigInt/BigDecimal methods causes
5
+ * AssemblyScript to infer `this` as `T | null`, which breaks type checking.
6
+ *
7
+ * This script removes the problematic assertions since:
8
+ * 1. In AssemblyScript, `this` in a class method can never be null when called normally
9
+ * 2. The assertions were added for defensive purposes but break compilation
10
+ *
11
+ * Run this script after `npm install` in the consuming project.
12
+ */
13
+
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+
17
+ // Find the graph-ts installation
18
+ const possiblePaths = [
19
+ path.resolve(__dirname, 'node_modules/@graphprotocol/graph-ts/common/numbers.ts'),
20
+ path.resolve(__dirname, '../node_modules/@graphprotocol/graph-ts/common/numbers.ts'),
21
+ path.resolve(process.cwd(), 'node_modules/@graphprotocol/graph-ts/common/numbers.ts'),
22
+ ];
23
+
24
+ let numbersPath = null;
25
+ for (const p of possiblePaths) {
26
+ if (fs.existsSync(p)) {
27
+ numbersPath = p;
28
+ break;
29
+ }
30
+ }
31
+
32
+ if (!numbersPath) {
33
+ console.log('graph-ts numbers.ts not found. Skipping patch (may already be patched or graph-ts not installed).');
34
+ process.exit(0);
35
+ }
36
+
37
+ console.log(`Patching ${numbersPath}...`);
38
+
39
+ let content = fs.readFileSync(numbersPath, 'utf8');
40
+
41
+ // Pattern to match assertions like:
42
+ // assert(this !== null, "Failed to ... because left hand side is 'null'");
43
+ // or "... value of it is 'null'"
44
+ // We match the entire line including leading whitespace and newline
45
+ // Handles optional whitespace after the semicolon
46
+ const assertPattern = /^\s*assert\(this !== null, "Failed to [^"]+"\);\s*\n/gm;
47
+
48
+ // Count matches
49
+ const matches = content.match(assertPattern);
50
+ if (!matches || matches.length === 0) {
51
+ console.log('File appears to already be patched or has different format. Skipping.');
52
+ process.exit(0);
53
+ }
54
+
55
+ console.log(`Found ${matches.length} assertions to remove.`);
56
+
57
+ // Remove the problematic assertions (replace with empty string to preserve next line's indentation)
58
+ const patched = content.replace(assertPattern, '');
59
+
60
+ // Backup original
61
+ const backupPath = numbersPath + '.bak';
62
+ if (!fs.existsSync(backupPath)) {
63
+ fs.writeFileSync(backupPath, content);
64
+ console.log(`Backup created at ${backupPath}`);
65
+ }
66
+
67
+ // Write patched version
68
+ fs.writeFileSync(numbersPath, patched);
69
+ console.log('Patch applied successfully!');
70
+ console.log('Removed assert(this !== null, ...) statements from BigInt and BigDecimal methods.');
@@ -9,28 +9,34 @@ const destinationFolder = path.resolve(
9
9
  );
10
10
 
11
11
  // Check if the source folder exists
12
- // If it doesn't exist, the script was likely already run or this is an npm install
13
- // In either case, we can safely exit without error
14
- if (!fs.existsSync(sourceFolder)) {
15
- console.log(`Source folder "${sourceFolder}" does not exist. Skipping postinstall (already configured or installed from npm).`);
16
- process.exit(0);
17
- }
12
+ // The @graphprotocol folder contains a custom fork of graph-ts that integrates
13
+ // with the SDK's host functions (bigInt, bigDecimal, ethereum.call, etc.)
14
+ if (fs.existsSync(sourceFolder)) {
15
+ // Create node_modules folder if it doesn't exist
16
+ const nodeModulesPath = path.resolve(__dirname, "node_modules");
17
+ if (!fs.existsSync(nodeModulesPath)) {
18
+ fs.mkdirSync(nodeModulesPath);
19
+ }
18
20
 
19
- // Create node_modules folder if it doesn't exist
20
- const nodeModulesPath = path.resolve(__dirname, "node_modules");
21
- if (!fs.existsSync(nodeModulesPath)) {
22
- fs.mkdirSync(nodeModulesPath);
23
- }
21
+ // Check if the destination folder already exists
22
+ if (fs.existsSync(destinationFolder)) {
23
+ console.log(
24
+ `Destination folder "${destinationFolder}" already exists. Removing it...`
25
+ );
26
+ fs.rmSync(destinationFolder, { recursive: true, force: true });
27
+ }
24
28
 
25
- // Check if the destination folder already exists
26
- if (fs.existsSync(destinationFolder)) {
27
- console.log(
28
- `Destination folder "${destinationFolder}" already exists. Removing it...`
29
- );
30
- fs.rmSync(destinationFolder, { recursive: true, force: true });
31
- }
29
+ // Move the source folder to node_modules
30
+ // This makes the custom graph-ts available to watchers that use the SDK
31
+ fs.renameSync(sourceFolder, destinationFolder);
32
32
 
33
- // Move the source folder to node_modules
34
- fs.renameSync(sourceFolder, destinationFolder);
35
-
36
- console.log(`Folder "${sourceFolder}" moved to "${destinationFolder}".`);
33
+ console.log(`Folder "${sourceFolder}" moved to "${destinationFolder}".`);
34
+ console.log("Custom graph-ts (integrated with SDK host functions) is now available.");
35
+ } else if (fs.existsSync(destinationFolder)) {
36
+ // Folder already moved in a previous install - this is fine
37
+ console.log("Custom graph-ts already installed in node_modules.");
38
+ } else {
39
+ // This shouldn't happen in normal usage, but warn instead of error
40
+ console.warn(`Warning: Source folder "${sourceFolder}" does not exist.`);
41
+ console.warn("If this is an npm install, the @graphprotocol folder should be included in the package.");
42
+ }
@@ -1,48 +0,0 @@
1
- // Code generated by protoc-gen-as. DO NOT EDIT.
2
- // Versions:
3
- // protoc-gen-as v1.3.0
4
- // protoc v6.33.4
5
-
6
- import { Writer, Reader } from "as-proto/assembly";
7
-
8
- export class Timestamp {
9
- static encode(message: Timestamp, writer: Writer): void {
10
- writer.uint32(8);
11
- writer.int64(message.seconds);
12
-
13
- writer.uint32(16);
14
- writer.int32(message.nanos);
15
- }
16
-
17
- static decode(reader: Reader, length: i32): Timestamp {
18
- const end: usize = length < 0 ? reader.end : reader.ptr + length;
19
- const message = new Timestamp();
20
-
21
- while (reader.ptr < end) {
22
- const tag = reader.uint32();
23
- switch (tag >>> 3) {
24
- case 1:
25
- message.seconds = reader.int64();
26
- break;
27
-
28
- case 2:
29
- message.nanos = reader.int32();
30
- break;
31
-
32
- default:
33
- reader.skipType(tag & 7);
34
- break;
35
- }
36
- }
37
-
38
- return message;
39
- }
40
-
41
- seconds: i64;
42
- nanos: i32;
43
-
44
- constructor(seconds: i64 = 0, nanos: i32 = 0) {
45
- this.seconds = seconds;
46
- this.nanos = nanos;
47
- }
48
- }