Skip to content

Commit

Permalink
add toJSON method for tx receipt and tx record (#1859)
Browse files Browse the repository at this point in the history
Signed-off-by: Petar Tonev <[email protected]>
  • Loading branch information
petreze authored Sep 8, 2023
1 parent 8e32e67 commit daf009c
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 2 deletions.
27 changes: 27 additions & 0 deletions src/ExchangeRate.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@

import Long from "long";

/**
* @typedef {object} ExchangeRateJSON
* @property {number} hbars
* @property {number} cents
* @property {string} expirationTime
* @property {number} exchangeRateInCents
*/

export default class ExchangeRate {
/**
* @private
Expand Down Expand Up @@ -100,4 +108,23 @@ export default class ExchangeRate {
},
};
}

/**
* @returns {ExchangeRateJSON}
*/
toJSON() {
return {
hbars: this.hbars,
cents: this.cents,
expirationTime: this.expirationTime.toString(),
exchangeRateInCents: this.exchangeRateInCents,
};
}

/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}
25 changes: 25 additions & 0 deletions src/Transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
import AccountId from "./account/AccountId.js";
import Hbar from "./Hbar.js";

/**
* @typedef {object} TransferJSON
* @property {string} accountId
* @property {number | string | Long | BigNumber | Hbar} amount
* @property {boolean} isApproved
*/

/**
* @namespace proto
* @typedef {import("@hashgraph/proto").proto.IAccountAmount} HashgraphProto.proto.IAccountAmount
Expand Down Expand Up @@ -103,4 +110,22 @@ export default class Transfer {
isApproval: this.isApproved,
};
}

/**
* @returns {TransferJSON}
*/
toJSON() {
return {
accountId: this.accountId.toString(),
amount: this.amount,
isApproved: this.isApproved,
};
}

/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}
2 changes: 1 addition & 1 deletion src/client/addressbooks/mainnet.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/client/addressbooks/testnet.js

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions src/token/TokenTransfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ import TokenId from "./TokenId.js";
* @typedef {import("bignumber.js").default} BigNumber
*/

/**
* @typedef {object} TokenTransferJSON
* @property {string} tokenId
* @property {string} accountId
* @property {number | null} expectedDecimals
* @property {Long | number} amount
* @property {boolean} isApproved
*/

/**
* An account, and the amount that it sends or receives during a cryptocurrency tokentransfer.
*/
Expand Down Expand Up @@ -130,4 +139,24 @@ export default class TokenTransfer {
isApproval: this.isApproved,
};
}

/**
* @returns {TokenTransferJSON}
*/
toJSON() {
return {
tokenId: this.tokenId.toString(),
accountId: this.accountId.toString(),
expectedDecimals: this.expectedDecimals,
amount: this.amount,
isApproved: this.isApproved,
};
}

/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}
64 changes: 64 additions & 0 deletions src/transaction/TransactionReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,29 @@ import Long from "long";
import * as HashgraphProto from "@hashgraph/proto";
import TransactionId from "../transaction/TransactionId.js";

/**
* @typedef {import("../ExchangeRate.js").ExchangeRateJSON} ExchangeRateJSON
*/

/**
* @typedef {object} TransactionReceiptJSON
* @property {string} status
* @property {?string} accountId
* @property {?string} filedId
* @property {?string} contractId
* @property {?string} topicId
* @property {?string} tokenId
* @property {?string} scheduleId
* @property {?ExchangeRateJSON} exchangeRate
* @property {?Long} topicSequenceNumber
* @property {?Uint8Array} topicRunningHash
* @property {?Long} totalSupply
* @property {?string} scheduledTransactionId
* @property {Long[]} serials
* @property {TransactionReceipt[]} duplicates
* @property {TransactionReceipt[]} children
*/

/**
* The consensus result for a transaction, which might not be currently known,
* or may succeed or fail.
Expand Down Expand Up @@ -340,4 +363,45 @@ export default class TransactionReceipt {
this._toProtobuf()
).finish();
}

/**
* @returns {TransactionReceiptJSON}
*/
toJSON() {
return {
status: this.status.toString(),
accountId:
this.accountId != null ? this.accountId.toString() : null,
filedId: this.fileId != null ? this.fileId.toString() : null,
contractId:
this.contractId != null ? this.contractId.toString() : null,
topicId: this.topicId != null ? this.topicId.toString() : null,
tokenId: this.tokenId != null ? this.tokenId.toString() : null,
scheduleId:
this.scheduleId != null ? this.scheduleId.toString() : null,
exchangeRate:
this.exchangeRate != null ? this.exchangeRate.toJSON() : null,
topicSequenceNumber:
this.topicSequenceNumber != null
? this.topicSequenceNumber
: null,
topicRunningHash:
this.topicRunningHash != null ? this.topicRunningHash : null,
totalSupply: this.totalSupply != null ? this.totalSupply : null,
scheduledTransactionId:
this.scheduledTransactionId != null
? this.scheduledTransactionId.toString()
: null,
serials: this.serials,
duplicates: this.duplicates,
children: this.children,
};
}

/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}
74 changes: 74 additions & 0 deletions src/transaction/TransactionRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,41 @@ import EvmAddress from "../EvmAddress.js";

/**
* @typedef {import("../token/TokenId.js").default} TokenId
* @typedef {import("../token/TokenTransfer.js").TokenTransferJSON} TokenTransferJSON
* @typedef {import("../account/HbarAllowance.js").default} HbarAllowance
* @typedef {import("../account/TokenAllowance.js").default} TokenAllowance
* @typedef {import("../account/TokenNftAllowance.js").default} TokenNftAllowance
* @typedef {import("./TransactionReceipt.js").TransactionReceiptJSON} TransactionReceiptJSON
* @typedef {import("../Transfer.js").TransferJSON} TransferJSON
*/

/**
* @typedef {object} TransactionRecordJSON
* @property {TransactionReceiptJSON} receipt
* @property {string} transactionHash
* @property {Timestamp} consensusTimestamp
* @property {string} transactionId
* @property {string} transactionMemo
* @property {Hbar} transactionFee
* @property {TransferJSON[]} transfers
* @property {TokenTransferMap} tokenTransfers
* @property {TokenTransferJSON[]} tokenTransfersList
* @property {?ScheduleId} scheduleRef
* @property {AssessedCustomFee[]} assessedCustomFees
* @property {TokenNftTransferMap} nftTransfers
* @property {TokenAssocation[]} automaticTokenAssociations
* @property {Timestamp | null} parentConsensusTimestamp
* @property {PublicKey | null} aliasKey
* @property {TransactionRecord[]} duplicates
* @property {TransactionRecord[]} children
* @property {HbarAllowance[]} hbarAllowanceAdjustments
* @property {TokenAllowance[]} tokenAllowanceAdjustments
* @property {TokenNftAllowance[]} nftAllowanceAdjustments
* @property {string} ethereumHash
* @property {Transfer[]} paidStakingRewards
* @property {?Uint8Array} prngBytes
* @property {?number} prngNumber
* @property {string} evmAddress
*/

/**
Expand Down Expand Up @@ -559,4 +591,46 @@ export default class TransactionRecord {
this._toProtobuf()
).finish();
}

/**
* @returns {TransactionRecordJSON}
*/
toJSON() {
return {
receipt: this.receipt.toJSON(),
transactionHash: JSON.stringify(this.transactionHash),
consensusTimestamp: this.consensusTimestamp,
transactionId: this.transactionId.toString(),
transactionMemo: this.transactionMemo,
transactionFee: this.transactionFee,
transfers: this.transfers.map((transfer) => transfer.toJSON()),
tokenTransfers: this.tokenTransfers,
tokenTransfersList: this.tokenTransfersList.map((transfer) =>
transfer.toJSON()
),
scheduleRef: this.scheduleRef,
assessedCustomFees: this.assessedCustomFees,
nftTransfers: this.nftTransfers,
automaticTokenAssociations: this.automaticTokenAssociations,
parentConsensusTimestamp: this.parentConsensusTimestamp,
aliasKey: this.aliasKey,
duplicates: this.duplicates,
children: this.children,
hbarAllowanceAdjustments: [],
tokenAllowanceAdjustments: [],
nftAllowanceAdjustments: [],
ethereumHash: JSON.stringify(this.ethereumHash),
paidStakingRewards: this.paidStakingRewards,
prngBytes: this.prngBytes,
prngNumber: this.prngNumber,
evmAddress: JSON.stringify(this.evmAddress),
};
}

/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}

0 comments on commit daf009c

Please sign in to comment.