Merge branch 'alias_fix'

This commit is contained in:
jejolare 2025-10-21 20:50:36 +07:00
commit 4eb60a5ebd
4 changed files with 122 additions and 30 deletions

View file

@ -18,8 +18,8 @@ class Alias extends Model {
Alias.init(
{
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
alias: { type: DataTypes.STRING, allowNull: true },
address: { type: DataTypes.STRING, allowNull: true },
alias: { type: DataTypes.STRING, allowNull: false, unique: true },
address: { type: DataTypes.STRING, allowNull: false },
comment: { type: DataTypes.STRING, allowNull: true },
tracking_key: { type: DataTypes.STRING, allowNull: true },
block: { type: DataTypes.STRING, allowNull: true },

View file

@ -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;
@ -1471,36 +1472,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) {
@ -2123,6 +2124,9 @@ cron.schedule("0 */4 * * *", async () => {
await waitForDb();
await syncService.startSyncDaemon();
if (process.env.RESYNC_ASSETS === "true") {
console.log('Resyncing assets');

View file

@ -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;

View file

@ -0,0 +1,42 @@
import sequelize from "../database/sequelize";
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
}));
await sequelize.transaction(async (t) => {
await Alias.destroy({ where: {}, transaction: t });
await Alias.bulkCreate(preparedData, { transaction: t });
});
}
async startSyncDaemon() {
while (true) {
try {
await this.syncAliases();
} catch (error) {
console.log(error);
}
await new Promise(resolve => setTimeout(resolve, 20000));
}
}
}
const syncService = new SyncService();
export default syncService;