Skip to content

Commit

Permalink
Update transaction format in blocks to be same as by hash
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Jul 2, 2024
1 parent ea54290 commit 0d543b9
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions clients/nodejs/modules/JsonRpcServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,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 Down Expand Up @@ -790,12 +792,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 @@ -804,25 +808,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 Down

0 comments on commit 0d543b9

Please sign in to comment.