|
20 | 20 | */ |
21 | 21 |
|
22 | 22 | const RosettaSDK = require('../../../..'); |
| 23 | +const Types = RosettaSDK.Client; |
23 | 24 |
|
24 | 25 | /* Data API: Block */ |
25 | 26 |
|
| 27 | +/** |
| 28 | +* Get a Block |
| 29 | +* Get a block by its Block Identifier. If transactions are returned in the same call to the node as fetching the block, the response should include these transactions in the Block object. If not, an array of Transaction Identifiers should be returned so /block/transaction fetches can be done to get all transaction information. |
| 30 | +* |
| 31 | +* blockRequest BlockRequest |
| 32 | +* returns BlockResponse |
| 33 | +* */ |
26 | 34 | const block = async (params) => { |
27 | 35 | const { blockRequest } = params; |
28 | | - return {}; |
| 36 | + |
| 37 | + if (blockRequest.block_identifier.index != 1000) { |
| 38 | + const previousBlockIndex = Math.max(0, blockRequest.block_identifier.index - 1); |
| 39 | + |
| 40 | + const blockIdentifier = new Types.BlockIdentifier( |
| 41 | + blockRequest.block_identifier.index, |
| 42 | + `block ${blockRequest.block_identifier.index}`, |
| 43 | + ); |
| 44 | + |
| 45 | + const parentBlockIdentifier = new Types.BlockIdentifier( |
| 46 | + previousBlockIndex, |
| 47 | + `block ${previousBlockIndex}`, |
| 48 | + ); |
| 49 | + |
| 50 | + const timestamp = Date.now() - 500000; |
| 51 | + const transactions = []; |
| 52 | + |
| 53 | + const block = new Types.Block( |
| 54 | + blockIdentifier, |
| 55 | + parentBlockIdentifier, |
| 56 | + timestamp, |
| 57 | + transactions, |
| 58 | + ); |
| 59 | + |
| 60 | + return new Types.BlockResponse(block); |
| 61 | + } |
| 62 | + |
| 63 | + const previousBlockIndex = Math.max(0, blockRequest.block_identifier.index - 1); |
| 64 | + |
| 65 | + const blockIdentifier = new Types.BlockIdentifier( |
| 66 | + 1000, |
| 67 | + 'block 1000', |
| 68 | + ); |
| 69 | + |
| 70 | + const parentBlockIdentifier = new Types.BlockIdentifier( |
| 71 | + 999, |
| 72 | + 'block 999', |
| 73 | + ); |
| 74 | + |
| 75 | + const timestamp = 1586483189000; |
| 76 | + const transactionIdentifier = new Types.TransactionIdentifier('transaction 0'); |
| 77 | + const operations = [ |
| 78 | + Types.Operation.constructFromObject({ |
| 79 | + 'operation_identifier': new Types.OperationIdentifier(0), |
| 80 | + 'type': 'Transfer', |
| 81 | + 'status': 'Success', |
| 82 | + 'account': new Types.AccountIdentifier('account 0'), |
| 83 | + 'amount': new Types.Amount( |
| 84 | + '-1000', |
| 85 | + new Types.Currency('ROS', 2) |
| 86 | + ), |
| 87 | + }), |
| 88 | + |
| 89 | + Types.Operation.constructFromObject({ |
| 90 | + 'operation_identifier': new Types.OperationIdentifier(1), |
| 91 | + 'related_operations': new Types.OperationIdentifier(0), |
| 92 | + 'type': 'Transfer', |
| 93 | + 'status': 'Reverted', |
| 94 | + 'account': new Types.AccountIdentifier('account 1'), |
| 95 | + 'amount': new Types.Amount( |
| 96 | + '1000', |
| 97 | + new Types.Currency('ROS', 2) |
| 98 | + ), |
| 99 | + }), |
| 100 | + ]; |
| 101 | + |
| 102 | + const transactions = [ |
| 103 | + new Types.Transaction(transactionIdentifier, operations), |
| 104 | + ]; |
| 105 | + |
| 106 | + const block = new Types.Block( |
| 107 | + blockIdentifier, |
| 108 | + parentBlockIdentifier, |
| 109 | + timestamp, |
| 110 | + transactions, |
| 111 | + ); |
| 112 | + |
| 113 | + const otherTransactions = [ |
| 114 | + new TransactionIdentifier('transaction 1'), |
| 115 | + ]; |
| 116 | + |
| 117 | + return new Types.BlockResponse( |
| 118 | + block, |
| 119 | + otherTransactions, |
| 120 | + ); |
29 | 121 | }; |
30 | 122 |
|
| 123 | +/** |
| 124 | +* Get a Block Transaction |
| 125 | +* Get a transaction in a block by its Transaction Identifier. This endpoint should only be used when querying a node for a block does not return all transactions contained within it. All transactions returned by this endpoint must be appended to any transactions returned by the /block method by consumers of this data. Fetching a transaction by hash is considered an Explorer Method (which is classified under the Future Work section). Calling this endpoint requires reference to a BlockIdentifier because transaction parsing can change depending on which block contains the transaction. For example, in Bitcoin it is necessary to know which block contains a transaction to determine the destination of fee payments. Without specifying a block identifier, the node would have to infer which block to use (which could change during a re-org). Implementations that require fetching previous transactions to populate the response (ex: Previous UTXOs in Bitcoin) may find it useful to run a cache within the Rosetta server in the /data directory (on a path that does not conflict with the node). |
| 126 | +* |
| 127 | +* blockTransactionRequest BlockTransactionRequest |
| 128 | +* returns BlockTransactionResponse |
| 129 | +* */ |
31 | 130 | const blockTransaction = async (params) => { |
32 | 131 | const { blockTransactionRequest } = params; |
33 | | - return {}; |
| 132 | + |
| 133 | + const transactionIdentifier = new Types.TransactionIdentifier('transaction 1'); |
| 134 | + const operations = [ |
| 135 | + Types.Operation.constructFromObject({ |
| 136 | + 'operation_identifier': new Types.OperationIdentifier(0), |
| 137 | + 'type': 'Reward', |
| 138 | + 'status': 'Success', |
| 139 | + 'account': new Types.AccountIdentifier('account 2'), |
| 140 | + 'amount': new Types.Amount( |
| 141 | + '1000', |
| 142 | + new Types.Currency('ROS', 2), |
| 143 | + ), |
| 144 | + }), |
| 145 | + ]; |
| 146 | + |
| 147 | + return new Types.Transaction(transactionIdentifier, operations); |
34 | 148 | }; |
35 | 149 |
|
36 | 150 | module.exports = { |
|
0 commit comments