diff --git a/server/server.ts b/server/server.ts index f3b9c0d..f8e9bfb 100644 --- a/server/server.ts +++ b/server/server.ts @@ -28,6 +28,7 @@ import bodyParser from 'body-parser'; import { syncHistoricalPrice, syncLatestPrice } from "./services/zanoPrice.service"; import ZanoPrice from "./schemes/ZanoPrice"; import cron from "node-cron"; +import syncService from "./services/sync.service.ts"; // @ts-ignore const __dirname = import.meta.dirname; @@ -1464,36 +1465,36 @@ async function waitForDb() { let tx_info = response.data.result.tx_info; - for (let item of tx_info.extra) { - if (item.type === 'alias_info') { - let arr = item.short_view.split('-->'); - let aliasName = arr[0]; - let aliasAddress = arr[1]; - let aliasComment = parseComment(item.datails_view || item.details_view); - let aliasTrackingKey = parseTrackingKey(item.datails_view || item.details_view); - let aliasBlock = bl.height; - let aliasTransaction = localTr.id; + // for (let item of tx_info.extra) { + // if (item.type === 'alias_info') { + // let arr = item.short_view.split('-->'); + // let aliasName = arr[0]; + // let aliasAddress = arr[1]; + // let aliasComment = parseComment(item.datails_view || item.details_view); + // let aliasTrackingKey = parseTrackingKey(item.datails_view || item.details_view); + // let aliasBlock = bl.height; + // let aliasTransaction = localTr.id; - await Alias.update( - { enabled: 0 }, - { where: { alias: aliasName } } - ); + // await Alias.update( + // { enabled: 0 }, + // { where: { alias: aliasName } } + // ); - try { - await Alias.upsert({ - alias: decodeString(aliasName), - address: aliasAddress, - comment: decodeString(aliasComment), - tracking_key: decodeString(aliasTrackingKey), - block: aliasBlock, - transact: aliasTransaction, - enabled: 1, - }); - } catch (error) { - log(`SyncTransactions() Insert into aliases ERROR: ${error}`); - } - } - } + // try { + // await Alias.upsert({ + // alias: decodeString(aliasName), + // address: aliasAddress, + // comment: decodeString(aliasComment), + // tracking_key: decodeString(aliasTrackingKey), + // block: aliasBlock, + // transact: aliasTransaction, + // enabled: 1, + // }); + // } catch (error) { + // log(`SyncTransactions() Insert into aliases ERROR: ${error}`); + // } + // } + // } for (let item of tx_info.ins) { if (item.global_indexes) { @@ -2112,6 +2113,11 @@ cron.schedule("0 */4 * * *", async () => { await waitForDb(); + + await syncService.syncAliases(); + + await new Promise(resolve => setTimeout(resolve, 5000000)); + if (process.env.RESYNC_ASSETS === "true") { console.log('Resyncing assets'); diff --git a/server/services/rpc.service.ts b/server/services/rpc.service.ts new file mode 100644 index 0000000..35129c8 --- /dev/null +++ b/server/services/rpc.service.ts @@ -0,0 +1,46 @@ +import { config } from "../utils/utils"; + +export interface AliasDetails { + address: string; + alias: string; + comment: string; + tracking_key: string; +} + +class RPCService { + + private async fetchDaemon(method: string, params: object) { + const result = await fetch(config.api, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + "id": 0, + "jsonrpc": "2.0", + "method": method, + "params": params + }) + }).then(res => res.json()).catch(err => { + console.log(err); + return null; + }); + + return result?.result; + } + + async getAllAliasesDetails() { + const allAliases = await this.fetchDaemon("get_all_alias_details", {}); + + if (!allAliases?.aliases || !Array.isArray(allAliases.aliases)) { + throw new Error("Failed to fetch aliases from daemon"); + } + + return allAliases.aliases as AliasDetails[]; + + } +} + +const rpcService = new RPCService(); + +export default rpcService; \ No newline at end of file diff --git a/server/services/sync.service.ts b/server/services/sync.service.ts new file mode 100644 index 0000000..6dd83b2 --- /dev/null +++ b/server/services/sync.service.ts @@ -0,0 +1,28 @@ +import Alias from "../schemes/Alias"; +import rpcService from "./rpc.service"; + +class SyncService { + async syncAliases() { + console.log('fetching all alias details from daemon...'); + const allAliases = await rpcService.getAllAliasesDetails(); + console.log(`Fetched ${allAliases.length} aliases from daemon.`); + + const preparedData = allAliases.map(e => ({ + alias: e.alias, + address: e.address, + comment: e.comment, + tracking_key: e.tracking_key, + block: null, + transaction: null, + enabled: 1 + })); + + Alias.bulkCreate(preparedData, { + ignoreDuplicates: true, + }) + } +} + +const syncService = new SyncService(); + +export default syncService; \ No newline at end of file