From ff36dfbcd01a33d42412574e23a7c7c8fe8517f6 Mon Sep 17 00:00:00 2001 From: jejolare Date: Tue, 27 Aug 2024 14:55:49 +0700 Subject: [PATCH] fix Transaction page --- server/schemes/Block.ts | 5 +- server/schemes/Transaction.ts | 7 ++ server/server.ts | 127 +++++++++++--------------- src/pages/Transaction/Transaction.tsx | 3 +- 4 files changed, 68 insertions(+), 74 deletions(-) diff --git a/server/schemes/Block.ts b/server/schemes/Block.ts index d6bdf9a..6e64212 100644 --- a/server/schemes/Block.ts +++ b/server/schemes/Block.ts @@ -1,5 +1,6 @@ import { Model, DataTypes } from "sequelize"; import sequelize from "../database/sequelize"; +import Transaction from "./Transaction"; class Block extends Model { declare readonly id: number; @@ -39,7 +40,7 @@ Block.init( { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, - height: { type: DataTypes.INTEGER, allowNull: false }, + height: { type: DataTypes.INTEGER, allowNull: false, unique: true }, actual_timestamp: { type: DataTypes.DATE, allowNull: true }, base_reward: { type: DataTypes.TEXT, allowNull: true }, blob: { type: DataTypes.STRING, allowNull: true }, @@ -72,4 +73,6 @@ Block.init( } ); +// Block.hasMany(Transaction, { foreignKey: 'keeper_block', sourceKey: 'height' }); + export default Block; diff --git a/server/schemes/Transaction.ts b/server/schemes/Transaction.ts index 3620e93..171c7d0 100644 --- a/server/schemes/Transaction.ts +++ b/server/schemes/Transaction.ts @@ -1,5 +1,6 @@ import { Model, DataTypes } from "sequelize"; import sequelize from "../database/sequelize"; +import Block from "./Block"; class Transaction extends Model { declare readonly id: number; @@ -45,4 +46,10 @@ Transaction.init( } ); +Transaction.belongsTo(Block, { + foreignKey: "keeper_block", + targetKey: "height", + as: "block" +}); + export default Transaction; diff --git a/server/server.ts b/server/server.ts index 77c65bd..550d0d7 100644 --- a/server/server.ts +++ b/server/server.ts @@ -449,59 +449,58 @@ export const io = new Server(server, { transports: ['websocket', 'polling'] }); app.get( '/api/get_tx_details/:tx_hash', exceptionHandler(async (req, res) => { - const tx_hash = req.params.tx_hash.toLowerCase(); + try { + const tx_hash = req.params.tx_hash.toLowerCase(); - if (tx_hash) { - // Fetching transaction details with associated block information using Sequelize - const transaction = await Transaction.findOne({ - where: { id: tx_hash }, - include: [ - { - model: Block, - attributes: ['id', 'timestamp'], - required: false, - }, - ], - }); - - - const transactionBlock = await Block.findOne({ - where: { tx_id: transaction?.keeper_block }, - }).catch(() => null); - - if (transaction && transactionBlock) { - const response = { - ...transaction.toJSON(), - block_hash: transactionBlock?.tx_id, - block_timestamp: transactionBlock?.timestamp, - last_block: lastBlock.height, - }; - - res.json(response); - } else { - const response = await get_tx_details(tx_hash); - const data = response.data; - - if (data?.result?.tx_info) { - if (data.result.tx_info.ins && typeof data.result.tx_info.ins === 'object') { - data.result.tx_info.ins = JSON.stringify(data.result.tx_info.ins); - } - - if (data.result.tx_info.outs && typeof data.result.tx_info.outs === 'object') { - data.result.tx_info.outs = JSON.stringify(data.result.tx_info.outs); - } - - res.json(data.result.tx_info); + if (tx_hash) { + // Fetching transaction details with associated block information using Sequelize + const transaction = await Transaction.findOne({ + where: { tx_id: tx_hash }, + }); + + + const transactionBlock = await Block.findOne({ + where: { height: transaction?.keeper_block }, + }).catch(() => null); + + if (transaction && transactionBlock) { + const response = { + ...transaction.toJSON(), + block_hash: transactionBlock?.tx_id, + block_timestamp: transactionBlock?.timestamp, + last_block: lastBlock.height, + }; + + res.json(response); } else { - res.status(500).json({ - message: `/get_tx_details/:tx_hash ${JSON.stringify(req.params)}`, - }); + const response = await get_tx_details(tx_hash); + const data = response.data; + + if (data?.result?.tx_info) { + if (data.result.tx_info.ins && typeof data.result.tx_info.ins === 'object') { + data.result.tx_info.ins = JSON.stringify(data.result.tx_info.ins); + } + + if (data.result.tx_info.outs && typeof data.result.tx_info.outs === 'object') { + data.result.tx_info.outs = JSON.stringify(data.result.tx_info.outs); + } + + res.json(data.result.tx_info); + } else { + res.status(500).json({ + message: `/get_tx_details/:tx_hash ${JSON.stringify(req.params)}`, + }); + } } + } else { + res.status(500).json({ + message: `/get_tx_details/:tx_hash ${JSON.stringify(req.params)}`, + }); } - } else { - res.status(500).json({ - message: `/get_tx_details/:tx_hash ${JSON.stringify(req.params)}`, - }); + } catch (error) { + console.log(error); + + res.status(500).json({ error: error.message }); } }) ); @@ -858,23 +857,6 @@ export const io = new Server(server, { transports: ['websocket', 'polling'] }); }) ) - app.get( - '/api/get_tx_details/:tx_hash', - exceptionHandler(async (req, res) => { - let tx_hash = req.params.tx_hash - const response = await axios({ - method: 'get', - url: config.api, - data: { - method: 'get_tx_details', - params: { tx_hash: tx_hash } - } - }) - res.json(response.data) - }) - ); - - app.get('/api/price', exceptionHandler(async (req, res) => { if (req.query.asset_id) { if (req.query.asset_id === ZANO_ASSET_ID) { @@ -1149,6 +1131,13 @@ export const io = new Server(server, { transports: ['websocket', 'polling'] }); await sequelize.transaction(async (transaction) => { try { + + if (blockInserts.length > 0) { + await Block.bulkCreate(blockInserts, { + transaction, + }); + } + if (transactionInserts.length > 0) { await Transaction.bulkCreate(transactionInserts, { ignoreDuplicates: true, @@ -1162,12 +1151,6 @@ export const io = new Server(server, { transports: ['websocket', 'polling'] }); }); } - if (blockInserts.length > 0) { - await Block.bulkCreate(blockInserts, { - transaction, - }); - } - // const elementOne = state.block_array[0]; const newLastBlock = state.block_array.pop(); setLastBlock({ diff --git a/src/pages/Transaction/Transaction.tsx b/src/pages/Transaction/Transaction.tsx index c789ada..0acae68 100644 --- a/src/pages/Transaction/Transaction.tsx +++ b/src/pages/Transaction/Transaction.tsx @@ -40,8 +40,9 @@ function Transaction() { const result = await Fetch.getTransaction(hash); if (result.success === false) return; if (!(typeof result === "object")) return; + const newTransactionInfo: TransactionInfo = { - hash: result.hash || "", + hash: result.tx_id || "", amount: Utils.toShiftedNumber(result.amount || "0", 12), fee: Utils.toShiftedNumber(result.fee || "0", 12), size: result.blob_size || "0",