Skip to content

Commit

Permalink
Return same transaction format from all places
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Jul 4, 2024
1 parent c336fd3 commit 88c5592
Showing 1 changed file with 15 additions and 42 deletions.
57 changes: 15 additions & 42 deletions clients/nodejs/modules/JsonRpcServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,15 +325,17 @@ class JsonRpcServer {
async getTransactionByBlockHashAndIndex(blockHash, txIndex) {
const block = await this._client.getBlock(Nimiq.Hash.fromString(blockHash), true);
if (block && block.transactions.length > txIndex) {
return this._transactionToObj(block.transactions[txIndex], block, txIndex);
const head = await this._client.getHeadHeight()
return this._transactionToObj(block.transactions[txIndex], block, head);
}
return null;
}

async getTransactionByBlockNumberAndIndex(number, txIndex) {
const block = await this._getBlockByNumber(number);
if (block && block.transactions.length > txIndex) {
return this._transactionToObj(block.transactions[txIndex], block, txIndex);
const head = await this._client.getHeadHeight()
return this._transactionToObj(block.transactions[txIndex], block, head);
}
return null;
}
Expand All @@ -350,18 +352,6 @@ class JsonRpcServer {
return null;
}

async getTransactionByHash2(hash) {
return this._getTransactionByHash2(Nimiq.Hash.fromString(hash));
}

async _getTransactionByHash2(hash, blockHash, blockHeight) {
const tx = await this._client.getTransaction(hash, blockHash, blockHeight);
if (tx) {
return this._transactionDetailsToObj2(tx);
}
return null;
}

async getTransactionReceipt(hash) {
const receipt = await this._client.getTransactionReceipt(hash);
if (!receipt) return null;
Expand Down Expand Up @@ -791,12 +781,14 @@ class JsonRpcServer {
confirmations: (await this._client.getHeadHeight()) - block.height + 1
};
if (block.isFull()) {
const head = await this._client.getHeadHeight()

obj.miner = block.minerAddr.toHex();
obj.minerAddress = block.minerAddr.toUserFriendlyAddress();
obj.extraData = Nimiq.BufferUtils.toHex(block.body.extraData);
obj.size = block.serializedSize;
obj.transactions = includeTransactions
? await Promise.all(block.transactions.map((tx, i) => this._transactionToObj(tx, block, i)))
? await Promise.all(block.transactions.map((tx) => this._transactionToObj(tx, block, head)))
: block.transactions.map((tx) => tx.hash().toHex());
}
return obj;
Expand All @@ -805,25 +797,29 @@ class JsonRpcServer {
/**
* @param {Transaction} tx
* @param {Block} [block]
* @param {number} [i]
* @param {number} [head]
* @private
*/
async _transactionToObj(tx, block, i) {
async _transactionToObj(tx, block, head) {
return {
hash: tx.hash().toHex(),
blockHash: block ? block.hash().toHex() : undefined,
blockNumber: block ? block.height : undefined,
timestamp: block ? block.timestamp : undefined,
confirmations: block ? (await this._client.getHeadHeight()) - block.height + 1 : 0,
transactionIndex: i,
confirmations: block && head ? head - block.height + 1 : 0,
from: tx.sender.toHex(),
fromAddress: tx.sender.toUserFriendlyAddress(),
fromType: tx.senderType,
to: tx.recipient.toHex(),
toAddress: tx.recipient.toUserFriendlyAddress(),
toType: tx.recipientType,
value: tx.value,
fee: tx.fee,
data: Nimiq.BufferUtils.toHex(tx.data) || null,
proof: Nimiq.BufferUtils.toHex(tx.proof) || null,
flags: tx.flags,
validityStartHeight: tx.validityStartHeight,
networkId: tx.networkId,
};
}

Expand All @@ -832,29 +828,6 @@ class JsonRpcServer {
* @private
*/
_transactionDetailsToObj(tx) {
return {
hash: tx.transactionHash.toHex(),
blockHash: tx.blockHash ? tx.blockHash.toHex() : undefined,
blockNumber: tx.blockHeight,
timestamp: tx.timestamp,
confirmations: tx.confirmations,
from: tx.sender.toHex(),
fromAddress: tx.sender.toUserFriendlyAddress(),
to: tx.recipient.toHex(),
toAddress: tx.recipient.toUserFriendlyAddress(),
value: tx.value,
fee: tx.fee,
data: Nimiq.BufferUtils.toHex(tx.data.raw) || null,
proof: Nimiq.BufferUtils.toHex(tx.proof.raw) || null,
flags: tx.flags,
};
}

/**
* @param {Client.TransactionDetails} tx
* @private
*/
_transactionDetailsToObj2(tx) {
return {
hash: tx.transactionHash.toHex(),
blockHash: tx.blockHash ? tx.blockHash.toHex() : undefined,
Expand Down

0 comments on commit 88c5592

Please sign in to comment.