Skip to content

Commit d2258cf

Browse files
committed
Completed port of official example
1 parent 1a6d530 commit d2258cf

4 files changed

Lines changed: 220 additions & 5 deletions

File tree

examples/basic/server/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@
2222
const RosettaSDK = require('../../..');
2323

2424
const ServiceHandlers = require('./services');
25+
const networkIdentifier = require('./network');
26+
27+
const asserter = RosettaSDK.Asserter.NewServer(
28+
['Transfer', 'Reward'],
29+
false,
30+
[networkIdentifier],
31+
);
2532

2633
/* Create a server configuration */
2734
const Server = new RosettaSDK.Server({
2835
URL_PORT: 8080,
2936
});
3037

38+
// Register global asserter
39+
Server.useAsserter(asserter);
40+
3141
/* Data API: Network */
3242
Server.register('/network/list', ServiceHandlers.Network.networkList);
3343
Server.register('/network/options', ServiceHandlers.Network.networkOptions);

examples/basic/server/network.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright (c) 2020 DigiByte Foundation NZ Limited
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
* this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to
7+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8+
* the Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
const RosettaSDK = require('../../..');
23+
const networkIdentifier = new RosettaSDK.Client.NetworkIdentifier('Rosetta', 'Testnet');
24+
25+
module.exports = networkIdentifier;

examples/basic/server/services/BlockService.js

Lines changed: 116 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,131 @@
2020
*/
2121

2222
const RosettaSDK = require('../../../..');
23+
const Types = RosettaSDK.Client;
2324

2425
/* Data API: Block */
2526

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+
* */
2634
const block = async (params) => {
2735
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+
);
29121
};
30122

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+
* */
31130
const blockTransaction = async (params) => {
32131
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);
34148
};
35149

36150
module.exports = {

examples/basic/server/services/NetworkService.js

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,87 @@
2020
*/
2121

2222
const RosettaSDK = require('../../../..');
23+
const Types = RosettaSDK.Client;
24+
25+
const networkIdentifier = require('../network');
2326

2427
/* Data API: Network */
28+
29+
/**
30+
* Get List of Available Networks
31+
* This endpoint returns a list of NetworkIdentifiers that the Rosetta server can handle.
32+
*
33+
* metadataRequest MetadataRequest
34+
* returns NetworkListResponse
35+
* */
2536
const networkList = async (params) => {
2637
const { metadataRequest } = params;
27-
return {};
38+
39+
return new Types.NetworkListResponse(
40+
[ networkIdentifier ],
41+
);
2842
};
2943

44+
/**
45+
* Get Network Options
46+
* This endpoint returns the version information and allowed network-specific types for a NetworkIdentifier. Any NetworkIdentifier returned by /network/list should be accessible here. Because options are retrievable in the context of a NetworkIdentifier, it is possible to define unique options for each network.
47+
*
48+
* networkRequest NetworkRequest
49+
* returns NetworkOptionsResponse
50+
* */
3051
const networkOptions = async (params) => {
3152
const { networkRequest } = params;
32-
return {};
53+
54+
const rosettaVersion = '1.4.0';
55+
const nodeVersion = '0.0.1';
56+
57+
const operationStatuses = [
58+
new Types.OperationStatus('Success', true),
59+
new Types.OperationStatus('Reverted', false),
60+
];
61+
62+
const operationTypes = [
63+
'Transfer',
64+
'Reward',
65+
];
66+
67+
const errors = [
68+
new Types.Error(1, 'not implemented', false),
69+
];
70+
71+
return new Types.NetworkOptionsResponse(
72+
new Types.Version(rosettaVersion, nodeVersion),
73+
new Types.Allow(
74+
operationStatuses,
75+
operationTypes,
76+
errors,
77+
),
78+
);
3379
};
3480

81+
/**
82+
* Get Network Status
83+
* This endpoint returns the current status of the network requested. Any NetworkIdentifier returned by /network/list should be accessible here.
84+
*
85+
* networkRequest NetworkRequest
86+
* returns NetworkStatusResponse
87+
* */
3588
const networkStatus = async (params) => {
3689
const { networkRequest } = params;
37-
return {};
90+
91+
const currentBlockIdentifier = new Types.BlockIdentifier(1000, 'block 1000');
92+
const currentBlockTimestamp = 1586483189000;
93+
const genesisBlockIdentifier = new Types.BlockIdentifier(0, 'block 0');
94+
const peers = [
95+
new Types.Peer('peer 1'),
96+
];
97+
98+
return new Types.NetworkStatusResponse(
99+
currentBlockIdentifier,
100+
currentBlockTimestamp,
101+
genesisBlockIdentifier,
102+
peers,
103+
);
38104
};
39105

40106
module.exports = {

0 commit comments

Comments
 (0)