@logrix/sdk 0.3.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.
@@ -0,0 +1,269 @@
1
+ "use strict";
2
+ // Template generators for `logrix init` scaffolding.
3
+ // Each function returns the file content string for a given template.
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.ERC20_ABI = void 0;
6
+ exports.logrixYamlTemplate = logrixYamlTemplate;
7
+ exports.schemaGraphqlTemplate = schemaGraphqlTemplate;
8
+ exports.mappingTemplate = mappingTemplate;
9
+ exports.packageJsonTemplate = packageJsonTemplate;
10
+ exports.tsconfigTemplate = tsconfigTemplate;
11
+ exports.gitignoreTemplate = gitignoreTemplate;
12
+ exports.readmeTemplate = readmeTemplate;
13
+ function logrixYamlTemplate(a) {
14
+ return `specVersion: "0.1.0"
15
+
16
+ network:
17
+ name: "${a.networkName}"
18
+ chainId: ${a.chainId}
19
+ rpcUrl: "${a.rpcUrl}"
20
+
21
+ dataSources:
22
+ - kind: ethereum/contract
23
+ name: "${a.contractName}"
24
+ network: "${a.networkName}"
25
+ source:
26
+ address: "${a.contractAddress}"
27
+ abi: "${a.contractName}"
28
+ startBlock: ${a.startBlock}
29
+ mapping:
30
+ kind: wasm/assemblyscript
31
+ file: ./build/mapping.wasm
32
+ abis:
33
+ - name: "${a.contractName}"
34
+ file: ./abis/${a.contractName}.json
35
+ eventHandlers:
36
+ - event: "Transfer(address indexed,address indexed,uint256)"
37
+ handler: handleTransfer
38
+ - event: "Approval(address indexed,address indexed,uint256)"
39
+ handler: handleApproval
40
+ `;
41
+ }
42
+ function schemaGraphqlTemplate() {
43
+ return `type Transfer @entity {
44
+ id: ID!
45
+ blockNumber: BigInt!
46
+ fromAddress: String! @index
47
+ toAddress: String! @index
48
+ amount: BigInt!
49
+ transactionHash: String!
50
+ timestamp: BigInt!
51
+ }
52
+
53
+ type Approval @entity {
54
+ id: ID!
55
+ blockNumber: BigInt!
56
+ ownerAddress: String! @index
57
+ spenderAddress: String! @index
58
+ amount: BigInt!
59
+ transactionHash: String!
60
+ timestamp: BigInt!
61
+ }
62
+ `;
63
+ }
64
+ function mappingTemplate(contractName) {
65
+ return `import { EventLog, log, BigInt, Address, allocate } from "@logrix/sdk";
66
+ import { TransferEvent, ApprovalEvent } from "./generated/events";
67
+ import { Transfer, Approval } from "./generated/schema";
68
+
69
+ // Required export so the Logrix Rust engine can allocate linear memory
70
+ export { allocate } from "@logrix/sdk";
71
+
72
+ // ---------------------------------------------------------------------------
73
+ // Entry point called by the Logrix engine for every matched event log.
74
+ // ---------------------------------------------------------------------------
75
+ export function handle_event(ptr: i32, len: i32): i32 {
76
+ const bytes = new Uint8Array(len);
77
+ for (let i = 0; i < len; i++) {
78
+ bytes[i] = load<u8>(ptr + i);
79
+ }
80
+ const raw = String.UTF8.decode(bytes.buffer);
81
+
82
+ // Dispatch based on event signature topic
83
+ if (raw.includes('"Transfer"') || raw.includes("0xddf252ad")) {
84
+ return handleTransfer(raw);
85
+ }
86
+ if (raw.includes('"Approval"') || raw.includes("0x8c5be1e5")) {
87
+ return handleApproval(raw);
88
+ }
89
+
90
+ log.info("handle_event: unhandled event, skipping");
91
+ return 0;
92
+ }
93
+
94
+ function handleTransfer(raw: string): i32 {
95
+ const logObj = new EventLog();
96
+ logObj.transaction_hash = extractField(raw, "transaction_hash");
97
+ logObj.block_number = u64(parseInt(extractField(raw, "block_number")) as i32);
98
+ logObj.block_timestamp = u64(parseInt(extractField(raw, "block_timestamp")) as i32);
99
+
100
+ const event = new TransferEvent(logObj);
101
+ event.params.from = Address.fromString(extractField(raw, "from"));
102
+ event.params.to = Address.fromString(extractField(raw, "to"));
103
+ event.params.value = BigInt.fromString(extractField(raw, "value"));
104
+
105
+ const entityId = event.transactionHash + "-" + event.logIndex.toString();
106
+ let entity = Transfer.load(entityId);
107
+ if (!entity) {
108
+ entity = new Transfer(entityId);
109
+ }
110
+
111
+ entity.blockNumber = event.blockNumber;
112
+ entity.fromAddress = event.params.from.toHexString();
113
+ entity.toAddress = event.params.to.toHexString();
114
+ entity.amount = event.params.value;
115
+ entity.transactionHash = event.transactionHash;
116
+ entity.timestamp = event.blockTimestamp;
117
+ entity.save();
118
+
119
+ log.info("Indexed Transfer entity: " + entityId);
120
+ return 0;
121
+ }
122
+
123
+ function handleApproval(raw: string): i32 {
124
+ const logObj = new EventLog();
125
+ logObj.transaction_hash = extractField(raw, "transaction_hash");
126
+ logObj.block_number = u64(parseInt(extractField(raw, "block_number")) as i32);
127
+ logObj.block_timestamp = u64(parseInt(extractField(raw, "block_timestamp")) as i32);
128
+
129
+ const event = new ApprovalEvent(logObj);
130
+ event.params.owner = Address.fromString(extractField(raw, "owner"));
131
+ event.params.spender = Address.fromString(extractField(raw, "spender"));
132
+ event.params.value = BigInt.fromString(extractField(raw, "value"));
133
+
134
+ const entityId = event.transactionHash + "-" + event.logIndex.toString();
135
+ let entity = Approval.load(entityId);
136
+ if (!entity) {
137
+ entity = new Approval(entityId);
138
+ }
139
+
140
+ entity.blockNumber = event.blockNumber;
141
+ entity.ownerAddress = event.params.owner.toHexString();
142
+ entity.spenderAddress = event.params.spender.toHexString();
143
+ entity.amount = event.params.value;
144
+ entity.transactionHash = event.transactionHash;
145
+ entity.timestamp = event.blockTimestamp;
146
+ entity.save();
147
+
148
+ log.info("Indexed Approval entity: " + entityId);
149
+ return 0;
150
+ }
151
+
152
+ function extractField(json: string, key: string): string {
153
+ const needle = '"' + key + '":';
154
+ const idx = json.indexOf(needle);
155
+ if (idx === -1) return "";
156
+ let start = idx + needle.length;
157
+ while (start < json.length && json.charAt(start) === " ") start++;
158
+ if (start >= json.length) return "";
159
+ const ch = json.charAt(start);
160
+ if (ch === '"') {
161
+ start++;
162
+ const end = json.indexOf('"', start);
163
+ if (end === -1) return "";
164
+ return json.slice(start, end);
165
+ } else {
166
+ let end = start;
167
+ while (end < json.length) {
168
+ const c = json.charCodeAt(end);
169
+ if (c === 44 || c === 125 || c === 93 || c === 32 || c === 10 || c === 13) break;
170
+ end++;
171
+ }
172
+ return json.slice(start, end).trim();
173
+ }
174
+ }
175
+ `;
176
+ }
177
+ function packageJsonTemplate(projectName) {
178
+ return {
179
+ name: projectName,
180
+ version: "0.1.0",
181
+ private: true,
182
+ scripts: {
183
+ codegen: "logrix codegen",
184
+ build: "logrix build",
185
+ validate: "logrix validate",
186
+ "build:asc": "asc src/mapping.ts --config tsconfig.json -o build/mapping.wasm --optimize --exportRuntime",
187
+ },
188
+ dependencies: {
189
+ "@logrix/sdk": "^0.1.0",
190
+ },
191
+ devDependencies: {
192
+ assemblyscript: "^0.27.29",
193
+ },
194
+ };
195
+ }
196
+ function tsconfigTemplate() {
197
+ return {
198
+ extends: "assemblyscript/std/assembly.json",
199
+ include: ["src/**/*.ts"],
200
+ };
201
+ }
202
+ function gitignoreTemplate() {
203
+ return `node_modules/
204
+ build/
205
+ src/generated/
206
+ `;
207
+ }
208
+ function readmeTemplate(projectName) {
209
+ return `# ${projectName}
210
+
211
+ Blockchain indexer built with [Logrix](https://github.com/Vishal-770/Logrix).
212
+
213
+ ## Development Workflow
214
+
215
+ ### 1. Install dependencies
216
+ \`\`\`bash
217
+ npm install
218
+ \`\`\`
219
+
220
+ ### 2. Configure \`logrix.yaml\`
221
+ Set your chain, contract addresses, and event handlers.
222
+ To add additional contracts:
223
+ \`\`\`bash
224
+ npx logrix add contract AnotherContract --address 0x... --start-block 0
225
+ \`\`\`
226
+
227
+ ### 3. Generate typed bindings
228
+ \`\`\`bash
229
+ npm run codegen
230
+ \`\`\`
231
+ Generates \`src/generated/events.ts\` and \`src/generated/schema.ts\`.
232
+
233
+ ### 4. Validate configuration
234
+ \`\`\`bash
235
+ npm run validate
236
+ \`\`\`
237
+
238
+ ### 5. Write handler logic
239
+ Implement your event indexing logic in \`src/mapping.ts\`.
240
+
241
+ ### 6. Compile WebAssembly handler
242
+ \`\`\`bash
243
+ npm run build
244
+ \`\`\`
245
+ Produces \`build/mapping.wasm\`.
246
+ `;
247
+ }
248
+ exports.ERC20_ABI = [
249
+ {
250
+ anonymous: false,
251
+ inputs: [
252
+ { indexed: true, name: "from", type: "address" },
253
+ { indexed: true, name: "to", type: "address" },
254
+ { indexed: false, name: "value", type: "uint256" },
255
+ ],
256
+ name: "Transfer",
257
+ type: "event",
258
+ },
259
+ {
260
+ anonymous: false,
261
+ inputs: [
262
+ { indexed: true, name: "owner", type: "address" },
263
+ { indexed: true, name: "spender", type: "address" },
264
+ { indexed: false, name: "value", type: "uint256" },
265
+ ],
266
+ name: "Approval",
267
+ type: "event",
268
+ },
269
+ ];
package/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ // Root AssemblyScript entry point — re-exports the full runtime.
2
+ // asc resolves @logrix/sdk to this file when building user handler projects.
3
+ export * from "./src/runtime/index";
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@logrix/sdk",
3
+ "version": "0.3.0",
4
+ "description": "Official CLI and AssemblyScript runtime library for Logrix blockchain indexers",
5
+ "main": "index.ts",
6
+ "bin": {
7
+ "logrix": "dist/index.js"
8
+ },
9
+ "scripts": {
10
+ "build": "tsc -p tsconfig.json && node -e \"const fs=require('fs');const f='dist/index.js';const c=fs.readFileSync(f,'utf8');if(!c.startsWith('#!/'))fs.writeFileSync(f,'#!/usr/bin/env node\\n'+c);\"",
11
+ "prepublishOnly": "npm run build"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "src/runtime",
16
+ "index.ts",
17
+ "tsconfig.as.json",
18
+ "README.md"
19
+ ],
20
+ "dependencies": {
21
+ "@inquirer/prompts": "^5.3.8",
22
+ "commander": "^12.1.0",
23
+ "yaml": "^2.4.5"
24
+ },
25
+ "devDependencies": {
26
+ "@types/node": "^20.14.0",
27
+ "typescript": "^5.4.5"
28
+ },
29
+ "peerDependencies": {
30
+ "assemblyscript": "^0.27.0"
31
+ },
32
+ "license": "Apache-2.0",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "https://github.com/Vishal-770/Logrix.git",
36
+ "directory": "packages/logrix-sdk"
37
+ },
38
+ "keywords": [
39
+ "logrix",
40
+ "indexer",
41
+ "blockchain",
42
+ "ethereum",
43
+ "evm",
44
+ "assemblyscript",
45
+ "wasm",
46
+ "graphql"
47
+ ],
48
+ "publishConfig": {
49
+ "access": "public"
50
+ }
51
+ }