@develit-services/blockchain 0.8.1 → 0.8.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.
Files changed (37) hide show
  1. package/dist/database/schema.cjs +5 -1
  2. package/dist/database/schema.d.cts +1 -1
  3. package/dist/database/schema.d.mts +1 -1
  4. package/dist/database/schema.d.ts +1 -1
  5. package/dist/database/schema.mjs +3 -1
  6. package/dist/export/worker.cjs +20 -103
  7. package/dist/export/worker.d.cts +2 -2
  8. package/dist/export/worker.d.mts +2 -2
  9. package/dist/export/worker.d.ts +2 -2
  10. package/dist/export/worker.mjs +20 -103
  11. package/dist/export/workflows.cjs +88 -82
  12. package/dist/export/workflows.mjs +83 -77
  13. package/dist/shared/{blockchain.Bq5JL2Nn.d.cts → blockchain.7cRY-udH.d.ts} +33 -5
  14. package/dist/shared/blockchain.BUbQBxqd.cjs +335 -0
  15. package/dist/shared/blockchain.BhDiNGPI.mjs +332 -0
  16. package/dist/shared/{blockchain.B_9YwgFd.mjs → blockchain.CKh8Fs7w.mjs} +42 -20
  17. package/dist/shared/{blockchain.BFesyGyk.d.mts → blockchain.CaPQzwns.d.cts} +33 -5
  18. package/dist/shared/blockchain.CbmnjjFl.cjs +70 -0
  19. package/dist/shared/{blockchain.C0wB1XUE.cjs → blockchain.Cjq9eH7Z.cjs} +44 -19
  20. package/dist/shared/{blockchain.C1uxxlk3.d.ts → blockchain.CvNI0YTF.d.mts} +33 -5
  21. package/dist/shared/blockchain.DUhjXgba.d.cts +1107 -0
  22. package/dist/shared/blockchain.DUhjXgba.d.mts +1107 -0
  23. package/dist/shared/blockchain.DUhjXgba.d.ts +1107 -0
  24. package/dist/shared/blockchain._nv3VxNA.mjs +64 -0
  25. package/dist/types.cjs +4 -1
  26. package/dist/types.d.cts +12 -4
  27. package/dist/types.d.mts +12 -4
  28. package/dist/types.d.ts +12 -4
  29. package/dist/types.mjs +1 -1
  30. package/package.json +1 -1
  31. package/dist/shared/blockchain.0tUJ62WT.mjs +0 -6
  32. package/dist/shared/blockchain.BBvwu2_7.cjs +0 -39
  33. package/dist/shared/blockchain.Cx60lJ0c.d.cts +0 -566
  34. package/dist/shared/blockchain.Cx60lJ0c.d.mts +0 -566
  35. package/dist/shared/blockchain.Cx60lJ0c.d.ts +0 -566
  36. package/dist/shared/blockchain.DN735AwB.cjs +0 -8
  37. package/dist/shared/blockchain._wwKu1qP.mjs +0 -35
@@ -1,10 +1,14 @@
1
1
  'use strict';
2
2
 
3
- const database_schema = require('../shared/blockchain.BBvwu2_7.cjs');
3
+ const database_schema = require('../shared/blockchain.CbmnjjFl.cjs');
4
4
  require('@develit-io/backend-sdk');
5
5
  require('drizzle-orm/sqlite-core');
6
+ require('../shared/blockchain.Cjq9eH7Z.cjs');
7
+ require('zod');
6
8
 
7
9
 
8
10
 
9
11
  exports.address = database_schema.address;
12
+ exports.addressCredentials = database_schema.addressCredentials;
13
+ exports.tokens = database_schema.tokens;
10
14
  exports.transaction = database_schema.transaction;
@@ -1,2 +1,2 @@
1
- export { a as address, t as transaction } from '../shared/blockchain.Cx60lJ0c.cjs';
1
+ export { c as address, d as addressCredentials, t as tokens, e as transaction } from '../shared/blockchain.DUhjXgba.cjs';
2
2
  import 'drizzle-orm/sqlite-core';
@@ -1,2 +1,2 @@
1
- export { a as address, t as transaction } from '../shared/blockchain.Cx60lJ0c.mjs';
1
+ export { c as address, d as addressCredentials, t as tokens, e as transaction } from '../shared/blockchain.DUhjXgba.mjs';
2
2
  import 'drizzle-orm/sqlite-core';
@@ -1,2 +1,2 @@
1
- export { a as address, t as transaction } from '../shared/blockchain.Cx60lJ0c.js';
1
+ export { c as address, d as addressCredentials, t as tokens, e as transaction } from '../shared/blockchain.DUhjXgba.js';
2
2
  import 'drizzle-orm/sqlite-core';
@@ -1,3 +1,5 @@
1
- export { a as address, t as transaction } from '../shared/blockchain._wwKu1qP.mjs';
1
+ export { a as address, b as addressCredentials, t as tokens, c as transaction } from '../shared/blockchain._nv3VxNA.mjs';
2
2
  import '@develit-io/backend-sdk';
3
3
  import 'drizzle-orm/sqlite-core';
4
+ import '../shared/blockchain.CKh8Fs7w.mjs';
5
+ import 'zod';
@@ -3,104 +3,17 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  const backendSdk = require('@develit-io/backend-sdk');
6
- const viem = require('viem');
7
- const drizzle = require('../shared/blockchain.DN735AwB.cjs');
8
- const syncAddress = require('../shared/blockchain.C0wB1XUE.cjs');
6
+ const blockchain = require('../shared/blockchain.BUbQBxqd.cjs');
7
+ const syncAddress = require('../shared/blockchain.Cjq9eH7Z.cjs');
9
8
  const cloudflare_workers = require('cloudflare:workers');
10
9
  const d1 = require('drizzle-orm/d1');
11
- require('../shared/blockchain.BBvwu2_7.cjs');
10
+ require('../shared/blockchain.CbmnjjFl.cjs');
12
11
  require('drizzle-orm/sqlite-core');
13
12
  require('drizzle-orm');
13
+ require('viem');
14
+ require('viem/accounts');
14
15
  require('zod');
15
16
 
16
- class IBlockchainConnector {
17
- }
18
-
19
- class AlchemyConnector extends IBlockchainConnector {
20
- constructor(config) {
21
- super();
22
- this.connectorKey = "ALCHEMY";
23
- this.client = null;
24
- this.rpcUrl = config.rpcUrl;
25
- }
26
- createClient() {
27
- if (!this.client) {
28
- this.client = viem.createPublicClient({
29
- transport: viem.http(this.rpcUrl)
30
- });
31
- }
32
- return this.client;
33
- }
34
- async getTransaction(txHash) {
35
- const client = this.createClient();
36
- const [tx, txError] = await backendSdk.useResult(
37
- client.getTransaction({ hash: txHash })
38
- );
39
- if (txError || !tx) {
40
- throw backendSdk.createInternalError(txError);
41
- }
42
- let status = "pending";
43
- let gasUsed = null;
44
- let timestamp = null;
45
- if (tx.blockNumber) {
46
- const [receipt, receiptError] = await backendSdk.useResult(
47
- client.getTransactionReceipt({ hash: txHash })
48
- );
49
- if (receiptError || !receipt) {
50
- throw backendSdk.createInternalError(receiptError);
51
- }
52
- const [block, blockError] = await backendSdk.useResult(
53
- client.getBlock({ blockNumber: tx.blockNumber })
54
- );
55
- if (blockError || !block) {
56
- throw backendSdk.createInternalError(blockError);
57
- }
58
- status = receipt.status === "success" ? "success" : "reverted";
59
- gasUsed = receipt.gasUsed.toString();
60
- timestamp = Number(block.timestamp) * 1e3;
61
- }
62
- return {
63
- hash: tx.hash,
64
- from: tx.from,
65
- to: tx.to,
66
- value: tx.value.toString(),
67
- blockNumber: tx.blockNumber?.toString() ?? null,
68
- blockHash: tx.blockHash ?? null,
69
- gasPrice: tx.gasPrice?.toString() ?? null,
70
- gas: tx.gas.toString(),
71
- nonce: tx.nonce,
72
- transactionIndex: tx.transactionIndex,
73
- status,
74
- gasUsed,
75
- timestamp
76
- };
77
- }
78
- async getTransactionReceipt(txHash) {
79
- const client = this.createClient();
80
- const [receipt, error] = await backendSdk.useResult(
81
- client.getTransactionReceipt({ hash: txHash })
82
- );
83
- if (error || !receipt) {
84
- throw backendSdk.createInternalError(error);
85
- }
86
- return receipt;
87
- }
88
- async getBlock({
89
- blockHash,
90
- blockNumber,
91
- blockTag
92
- }) {
93
- const client = this.createClient();
94
- const [block, error] = await backendSdk.useResult(
95
- blockHash !== void 0 ? client.getBlock({ blockHash }) : blockNumber !== void 0 ? client.getBlock({ blockNumber }) : client.getBlock({ blockTag: blockTag ?? "finalized" })
96
- );
97
- if (error || !block) {
98
- throw error;
99
- }
100
- return block;
101
- }
102
- }
103
-
104
17
  var __defProp = Object.defineProperty;
105
18
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
106
19
  var __decorateClass = (decorators, target, key, kind) => {
@@ -116,7 +29,7 @@ let BlockchainServiceBase = class extends backendSdk.develitWorker(
116
29
  ) {
117
30
  constructor(ctx, env) {
118
31
  super(ctx, env);
119
- this.db = d1.drizzle(this.env.BLOCKCHAIN_D1, { schema: drizzle.tables });
32
+ this.db = d1.drizzle(this.env.BLOCKCHAIN_D1, { schema: blockchain.tables });
120
33
  }
121
34
  async syncAddress(input) {
122
35
  return this.handleAction(
@@ -140,11 +53,13 @@ let BlockchainServiceBase = class extends backendSdk.develitWorker(
140
53
  return this.handleAction(
141
54
  { data: input, schema: syncAddress.getTransactionInputSchema },
142
55
  { successMessage: "Transaction retrieved successfully." },
143
- async ({ txHash }) => {
144
- const rpcUrl = (await this.env.SECRETS_STORE.get({
145
- secretName: "BLOCKCHAIN_SERVICE_RPC_URL"
146
- })).data?.secretValue || "";
147
- const connector = new AlchemyConnector({ rpcUrl });
56
+ async ({ chain, txHash }) => {
57
+ const connector = await blockchain.initiateConnector({
58
+ chain,
59
+ env: this.env,
60
+ tokens: [],
61
+ connectedAddresses: []
62
+ });
148
63
  return connector.getTransaction(txHash);
149
64
  }
150
65
  );
@@ -153,11 +68,13 @@ let BlockchainServiceBase = class extends backendSdk.develitWorker(
153
68
  return this.handleAction(
154
69
  { data: input, schema: syncAddress.getBlockInputSchema },
155
70
  { successMessage: "Block retrieved successfully." },
156
- async ({ blockHash, blockNumber, blockTag }) => {
157
- const rpcUrl = (await this.env.SECRETS_STORE.get({
158
- secretName: "BLOCKCHAIN_SERVICE_RPC_URL"
159
- })).data?.secretValue || "";
160
- const connector = new AlchemyConnector({ rpcUrl });
71
+ async ({ chain, blockHash, blockNumber, blockTag }) => {
72
+ const connector = await blockchain.initiateConnector({
73
+ chain,
74
+ env: this.env,
75
+ tokens: [],
76
+ connectedAddresses: []
77
+ });
161
78
  return connector.getBlock({
162
79
  blockHash,
163
80
  blockNumber,
@@ -1,10 +1,10 @@
1
1
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { IRPCResponse } from '@develit-io/backend-sdk';
3
- import { t as tables, S as SyncAddressInput, a as SyncAddressOutput, G as GetTransactionInput, b as GetTransactionOutput, c as GetBlockInput, d as GetBlockOutput } from '../shared/blockchain.Bq5JL2Nn.cjs';
3
+ import { t as tables, S as SyncAddressInput, a as SyncAddressOutput, G as GetTransactionInput, b as GetTransactionOutput, c as GetBlockInput, d as GetBlockOutput } from '../shared/blockchain.CaPQzwns.cjs';
4
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
5
5
  import { DrizzleD1Database } from 'drizzle-orm/d1';
6
6
  import 'zod';
7
- import '../shared/blockchain.Cx60lJ0c.cjs';
7
+ import '../shared/blockchain.DUhjXgba.cjs';
8
8
  import 'drizzle-orm/sqlite-core';
9
9
 
10
10
  declare const BlockchainServiceBase_base: (abstract new (ctx: ExecutionContext, env: BlockchainEnv) => WorkerEntrypoint<BlockchainEnv, {}>) & (abstract new (...args: any[]) => _develit_io_backend_sdk.DevelitWorkerMethods);
@@ -1,10 +1,10 @@
1
1
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { IRPCResponse } from '@develit-io/backend-sdk';
3
- import { t as tables, S as SyncAddressInput, a as SyncAddressOutput, G as GetTransactionInput, b as GetTransactionOutput, c as GetBlockInput, d as GetBlockOutput } from '../shared/blockchain.BFesyGyk.mjs';
3
+ import { t as tables, S as SyncAddressInput, a as SyncAddressOutput, G as GetTransactionInput, b as GetTransactionOutput, c as GetBlockInput, d as GetBlockOutput } from '../shared/blockchain.CvNI0YTF.mjs';
4
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
5
5
  import { DrizzleD1Database } from 'drizzle-orm/d1';
6
6
  import 'zod';
7
- import '../shared/blockchain.Cx60lJ0c.mjs';
7
+ import '../shared/blockchain.DUhjXgba.mjs';
8
8
  import 'drizzle-orm/sqlite-core';
9
9
 
10
10
  declare const BlockchainServiceBase_base: (abstract new (ctx: ExecutionContext, env: BlockchainEnv) => WorkerEntrypoint<BlockchainEnv, {}>) & (abstract new (...args: any[]) => _develit_io_backend_sdk.DevelitWorkerMethods);
@@ -1,10 +1,10 @@
1
1
  import * as _develit_io_backend_sdk from '@develit-io/backend-sdk';
2
2
  import { IRPCResponse } from '@develit-io/backend-sdk';
3
- import { t as tables, S as SyncAddressInput, a as SyncAddressOutput, G as GetTransactionInput, b as GetTransactionOutput, c as GetBlockInput, d as GetBlockOutput } from '../shared/blockchain.C1uxxlk3.js';
3
+ import { t as tables, S as SyncAddressInput, a as SyncAddressOutput, G as GetTransactionInput, b as GetTransactionOutput, c as GetBlockInput, d as GetBlockOutput } from '../shared/blockchain.7cRY-udH.js';
4
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
5
5
  import { DrizzleD1Database } from 'drizzle-orm/d1';
6
6
  import 'zod';
7
- import '../shared/blockchain.Cx60lJ0c.js';
7
+ import '../shared/blockchain.DUhjXgba.js';
8
8
  import 'drizzle-orm/sqlite-core';
9
9
 
10
10
  declare const BlockchainServiceBase_base: (abstract new (ctx: ExecutionContext, env: BlockchainEnv) => WorkerEntrypoint<BlockchainEnv, {}>) & (abstract new (...args: any[]) => _develit_io_backend_sdk.DevelitWorkerMethods);
@@ -1,102 +1,15 @@
1
- import { useResult, createInternalError, develitWorker, action, service } from '@develit-io/backend-sdk';
2
- import { createPublicClient, http } from 'viem';
3
- import { t as tables } from '../shared/blockchain.0tUJ62WT.mjs';
4
- import { s as syncAddressInputSchema, b as getTransactionInputSchema, g as getBlockInputSchema } from '../shared/blockchain.B_9YwgFd.mjs';
1
+ import { develitWorker, action, service } from '@develit-io/backend-sdk';
2
+ import { t as tables, i as initiateConnector } from '../shared/blockchain.BhDiNGPI.mjs';
3
+ import { s as syncAddressInputSchema, d as getTransactionInputSchema, g as getBlockInputSchema } from '../shared/blockchain.CKh8Fs7w.mjs';
5
4
  import { WorkerEntrypoint } from 'cloudflare:workers';
6
5
  import { drizzle } from 'drizzle-orm/d1';
7
- import '../shared/blockchain._wwKu1qP.mjs';
6
+ import '../shared/blockchain._nv3VxNA.mjs';
8
7
  import 'drizzle-orm/sqlite-core';
9
8
  import 'drizzle-orm';
9
+ import 'viem';
10
+ import 'viem/accounts';
10
11
  import 'zod';
11
12
 
12
- class IBlockchainConnector {
13
- }
14
-
15
- class AlchemyConnector extends IBlockchainConnector {
16
- constructor(config) {
17
- super();
18
- this.connectorKey = "ALCHEMY";
19
- this.client = null;
20
- this.rpcUrl = config.rpcUrl;
21
- }
22
- createClient() {
23
- if (!this.client) {
24
- this.client = createPublicClient({
25
- transport: http(this.rpcUrl)
26
- });
27
- }
28
- return this.client;
29
- }
30
- async getTransaction(txHash) {
31
- const client = this.createClient();
32
- const [tx, txError] = await useResult(
33
- client.getTransaction({ hash: txHash })
34
- );
35
- if (txError || !tx) {
36
- throw createInternalError(txError);
37
- }
38
- let status = "pending";
39
- let gasUsed = null;
40
- let timestamp = null;
41
- if (tx.blockNumber) {
42
- const [receipt, receiptError] = await useResult(
43
- client.getTransactionReceipt({ hash: txHash })
44
- );
45
- if (receiptError || !receipt) {
46
- throw createInternalError(receiptError);
47
- }
48
- const [block, blockError] = await useResult(
49
- client.getBlock({ blockNumber: tx.blockNumber })
50
- );
51
- if (blockError || !block) {
52
- throw createInternalError(blockError);
53
- }
54
- status = receipt.status === "success" ? "success" : "reverted";
55
- gasUsed = receipt.gasUsed.toString();
56
- timestamp = Number(block.timestamp) * 1e3;
57
- }
58
- return {
59
- hash: tx.hash,
60
- from: tx.from,
61
- to: tx.to,
62
- value: tx.value.toString(),
63
- blockNumber: tx.blockNumber?.toString() ?? null,
64
- blockHash: tx.blockHash ?? null,
65
- gasPrice: tx.gasPrice?.toString() ?? null,
66
- gas: tx.gas.toString(),
67
- nonce: tx.nonce,
68
- transactionIndex: tx.transactionIndex,
69
- status,
70
- gasUsed,
71
- timestamp
72
- };
73
- }
74
- async getTransactionReceipt(txHash) {
75
- const client = this.createClient();
76
- const [receipt, error] = await useResult(
77
- client.getTransactionReceipt({ hash: txHash })
78
- );
79
- if (error || !receipt) {
80
- throw createInternalError(error);
81
- }
82
- return receipt;
83
- }
84
- async getBlock({
85
- blockHash,
86
- blockNumber,
87
- blockTag
88
- }) {
89
- const client = this.createClient();
90
- const [block, error] = await useResult(
91
- blockHash !== void 0 ? client.getBlock({ blockHash }) : blockNumber !== void 0 ? client.getBlock({ blockNumber }) : client.getBlock({ blockTag: blockTag ?? "finalized" })
92
- );
93
- if (error || !block) {
94
- throw error;
95
- }
96
- return block;
97
- }
98
- }
99
-
100
13
  var __defProp = Object.defineProperty;
101
14
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
102
15
  var __decorateClass = (decorators, target, key, kind) => {
@@ -136,11 +49,13 @@ let BlockchainServiceBase = class extends develitWorker(
136
49
  return this.handleAction(
137
50
  { data: input, schema: getTransactionInputSchema },
138
51
  { successMessage: "Transaction retrieved successfully." },
139
- async ({ txHash }) => {
140
- const rpcUrl = (await this.env.SECRETS_STORE.get({
141
- secretName: "BLOCKCHAIN_SERVICE_RPC_URL"
142
- })).data?.secretValue || "";
143
- const connector = new AlchemyConnector({ rpcUrl });
52
+ async ({ chain, txHash }) => {
53
+ const connector = await initiateConnector({
54
+ chain,
55
+ env: this.env,
56
+ tokens: [],
57
+ connectedAddresses: []
58
+ });
144
59
  return connector.getTransaction(txHash);
145
60
  }
146
61
  );
@@ -149,11 +64,13 @@ let BlockchainServiceBase = class extends develitWorker(
149
64
  return this.handleAction(
150
65
  { data: input, schema: getBlockInputSchema },
151
66
  { successMessage: "Block retrieved successfully." },
152
- async ({ blockHash, blockNumber, blockTag }) => {
153
- const rpcUrl = (await this.env.SECRETS_STORE.get({
154
- secretName: "BLOCKCHAIN_SERVICE_RPC_URL"
155
- })).data?.secretValue || "";
156
- const connector = new AlchemyConnector({ rpcUrl });
67
+ async ({ chain, blockHash, blockNumber, blockTag }) => {
68
+ const connector = await initiateConnector({
69
+ chain,
70
+ env: this.env,
71
+ tokens: [],
72
+ connectedAddresses: []
73
+ });
157
74
  return connector.getBlock({
158
75
  blockHash,
159
76
  blockNumber,
@@ -1,38 +1,57 @@
1
1
  'use strict';
2
2
 
3
3
  const backendSdk = require('@develit-io/backend-sdk');
4
- const drizzle = require('../shared/blockchain.DN735AwB.cjs');
4
+ const blockchain = require('../shared/blockchain.BUbQBxqd.cjs');
5
5
  const drizzleOrm = require('drizzle-orm');
6
6
  const cloudflare_workers = require('cloudflare:workers');
7
7
  const cloudflare_workflows = require('cloudflare:workflows');
8
8
  const d1 = require('drizzle-orm/d1');
9
- const viem = require('viem');
10
- const chains = require('viem/chains');
11
- require('../shared/blockchain.BBvwu2_7.cjs');
9
+ require('../shared/blockchain.CbmnjjFl.cjs');
12
10
  require('drizzle-orm/sqlite-core');
11
+ require('../shared/blockchain.Cjq9eH7Z.cjs');
12
+ require('zod');
13
+ require('viem');
14
+ require('viem/accounts');
15
+
16
+ const updateAddressLastSyncBlockCommand = (db, {
17
+ lastSyncBlock,
18
+ addressId
19
+ }) => {
20
+ const command = db.update(blockchain.tables.address).set({
21
+ lastSyncBlock
22
+ }).where(drizzleOrm.eq(blockchain.tables.address.id, addressId)).returning();
23
+ return {
24
+ command
25
+ };
26
+ };
13
27
 
14
28
  const updateAddressLastSyncCommand = (db, {
15
29
  lastSyncAt,
16
30
  addressId,
17
31
  lastSyncMetadata
18
32
  }) => {
19
- const command = db.update(drizzle.tables.address).set({
33
+ const command = db.update(blockchain.tables.address).set({
20
34
  lastSyncAt,
21
35
  lastSyncMetadata
22
- }).where(drizzleOrm.eq(drizzle.tables.address.id, addressId)).returning();
36
+ }).where(drizzleOrm.eq(blockchain.tables.address.id, addressId)).returning();
23
37
  return {
24
38
  command
25
39
  };
26
40
  };
27
41
 
28
42
  const getAddressById = async (db, id) => {
29
- const address = await db.select().from(drizzle.tables.address).where(drizzleOrm.eq(drizzle.tables.address.id, id)).get();
43
+ const address = await db.select().from(blockchain.tables.address).where(drizzleOrm.eq(blockchain.tables.address.id, id)).get();
30
44
  return address;
31
45
  };
32
46
 
47
+ const getContractAddresses = async (db, { connector }) => {
48
+ const addresses = await db.select().from(blockchain.tables.tokens).where(drizzleOrm.eq(blockchain.tables.tokens.connector, connector)).all();
49
+ return addresses;
50
+ };
51
+
33
52
  const createTransactionCommand = (db, { transaction }) => {
34
53
  const id = backendSdk.uuidv4();
35
- const command = db.insert(drizzle.tables.transaction).values({ ...transaction, id }).returning();
54
+ const command = db.insert(blockchain.tables.transaction).values({ ...transaction, id }).returning();
36
55
  return { command };
37
56
  };
38
57
 
@@ -48,7 +67,7 @@ function pushToQueue(queue, message) {
48
67
  class BlockchainSyncAddressTransactions extends cloudflare_workers.WorkflowEntrypoint {
49
68
  async run(event, step) {
50
69
  const { addressId } = event.payload;
51
- const db = d1.drizzle(this.env.BLOCKCHAIN_D1, { schema: drizzle.tables });
70
+ const db = d1.drizzle(this.env.BLOCKCHAIN_D1, { schema: blockchain.tables });
52
71
  if (!addressId) {
53
72
  throw new cloudflare_workflows.NonRetryableError(`Haven't obtained addressId to load.`);
54
73
  }
@@ -64,7 +83,9 @@ class BlockchainSyncAddressTransactions extends cloudflare_workers.WorkflowEntry
64
83
  return address2;
65
84
  });
66
85
  if (!address.lastSyncAt) {
67
- throw new Error(`lastSyncedAt is not set for address: ${address}`);
86
+ throw new cloudflare_workflows.NonRetryableError(
87
+ `lastSyncedAt is not set for address: ${address}`
88
+ );
68
89
  }
69
90
  const transactions = await step.do(
70
91
  "fetch address transactions",
@@ -73,86 +94,63 @@ class BlockchainSyncAddressTransactions extends cloudflare_workers.WorkflowEntry
73
94
  timeout: "30 seconds"
74
95
  },
75
96
  async () => {
76
- const rpcUrl = (await this.env.SECRETS_STORE.get({
77
- secretName: "BLOCKCHAIN_SERVICE_RPC_URL"
78
- })).data?.secretValue || "";
79
- const client = viem.createPublicClient({
80
- chain: chains.gnosisChiado,
81
- transport: viem.http(rpcUrl)
97
+ const tokens = await getContractAddresses(db, {
98
+ connector: address.connector
82
99
  });
83
- const currentBlock = await client.getBlockNumber();
84
- const estimatedBlocksPerInterval = Math.ceil(
85
- address.syncIntervalS * 1.5 / 5
86
- );
87
- const blocksToScan = Math.min(estimatedBlocksPerInterval, 1e3);
88
- const startBlock = currentBlock - BigInt(blocksToScan);
89
- const endBlock = currentBlock;
90
- const lastSyncedTimestamp = address.lastSyncAt || 0;
91
- const allTransactions = [];
92
- for (let blockNum = startBlock; blockNum <= endBlock; blockNum++) {
93
- const block = await client.getBlock({
94
- blockNumber: blockNum,
95
- includeTransactions: true
96
- });
97
- const blockTimestamp = Number(block.timestamp);
98
- const lastSyncedTimestampNum = typeof lastSyncedTimestamp === "number" ? lastSyncedTimestamp : Number(lastSyncedTimestamp);
99
- if (blockTimestamp <= lastSyncedTimestampNum) {
100
- continue;
101
- }
102
- for (const tx of block.transactions) {
103
- if (typeof tx === "string" || !tx.to) continue;
104
- if (tx.from.toLowerCase() === address.number.toLowerCase()) {
105
- const receipt = await client.getTransactionReceipt({
106
- hash: tx.hash
107
- });
108
- allTransactions.push({
109
- hash: tx.hash,
110
- from: tx.from,
111
- to: tx.to,
112
- value: tx.value.toString(),
113
- blockNumber: block.number.toString(),
114
- blockHash: block.hash,
115
- gasUsed: receipt.gasUsed.toString(),
116
- gasPrice: tx.gasPrice?.toString() ?? "",
117
- timestamp: blockTimestamp,
118
- status: receipt.status
119
- });
120
- }
121
- }
100
+ if (!tokens || tokens.length === 0) {
101
+ throw new cloudflare_workflows.NonRetryableError(
102
+ `No contract addresses found for connector: ${address.connector}`
103
+ );
122
104
  }
123
- return allTransactions;
105
+ const connector = await blockchain.initiateConnector({
106
+ chain: address.connector,
107
+ env: this.env,
108
+ tokens,
109
+ connectedAddresses: [
110
+ {
111
+ id: address.id,
112
+ number: address.number,
113
+ connector: address.connector
114
+ }
115
+ ]
116
+ });
117
+ return await connector.getAllAddressTransactions({
118
+ address: address.number,
119
+ filter: { fromBlock: address.lastSyncBlock }
120
+ });
124
121
  }
125
122
  );
126
- if (transactions.length > 0) {
123
+ if (transactions.transactions.length > 0) {
127
124
  await step.do(
128
125
  "process new transactions and update lastSyncAt",
129
126
  async () => {
130
- const createCommands = transactions.map(
127
+ const createCommands = transactions.transactions.map(
131
128
  (tx) => createTransactionCommand(db, { transaction: tx }).command
132
129
  );
133
- const eventsToEmit = transactions.map(
134
- (tx) => ({
135
- eventType: "BLOCKCHAIN_TRANSACTION",
136
- blockchainTransaction: {
137
- address: address.number,
138
- hash: tx.hash,
139
- from: tx.from,
140
- to: tx.to,
141
- value: tx.value,
142
- blockNumber: tx.blockNumber,
143
- blockHash: tx.blockHash,
144
- gasUsed: tx.gasUsed,
145
- gasPrice: tx.gasPrice,
146
- timestamp: tx.timestamp,
147
- status: tx.status
148
- },
149
- metadata: {
150
- correlationId: backendSdk.uuidv4(),
151
- entityId: backendSdk.uuidv4(),
152
- timestamp: (/* @__PURE__ */ new Date()).toDateString()
153
- }
154
- })
155
- );
130
+ const eventsToEmit = transactions.transactions.map((tx) => ({
131
+ eventType: "BLOCKCHAIN_TRANSACTION",
132
+ blockchainTransaction: {
133
+ address: address.number,
134
+ hash: tx.hash,
135
+ from: tx.from,
136
+ to: tx.to,
137
+ value: tx.value,
138
+ blockNumber: tx.blockNumber,
139
+ blockHash: tx.blockHash,
140
+ gasUsed: tx.gasUsed,
141
+ gasPrice: tx.gasPrice,
142
+ timestamp: tx.timestamp,
143
+ status: tx.status,
144
+ decimals: tx.decimals,
145
+ connector: address.connector,
146
+ ticker: tx.ticker
147
+ },
148
+ metadata: {
149
+ correlationId: backendSdk.uuidv4(),
150
+ entityId: backendSdk.uuidv4(),
151
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
152
+ }
153
+ }));
156
154
  const lastSyncMetadata = {
157
155
  eventsEmitted: eventsToEmit.length
158
156
  };
@@ -161,9 +159,17 @@ class BlockchainSyncAddressTransactions extends cloudflare_workers.WorkflowEntry
161
159
  lastSyncAt: now,
162
160
  lastSyncMetadata
163
161
  }).command;
162
+ const updateLastSyncBlockCommand = updateAddressLastSyncBlockCommand(db, {
163
+ addressId: address.id,
164
+ lastSyncBlock: transactions.latestBlock
165
+ }).command;
164
166
  if (eventsToEmit.length) {
165
167
  await db.batch(
166
- backendSdk.asNonEmpty([updateLastSyncCommand, ...createCommands])
168
+ backendSdk.asNonEmpty([
169
+ updateLastSyncCommand,
170
+ updateLastSyncBlockCommand,
171
+ ...createCommands
172
+ ])
167
173
  );
168
174
  await pushToQueue(
169
175
  this.env.QUEUE_BUS_QUEUE,