|
| 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 RosettaClient = RosettaSDK.Client; |
| 24 | + |
| 25 | +// Create an instance of APIClient and configure it. |
| 26 | +const APIClient = new RosettaClient.ApiClient(); |
| 27 | +APIClient.basePath = 'http://localhost:8080'; |
| 28 | +APIClient.agent = 'rosetta-sdk-node'; |
| 29 | +APIClient.timeout = 10 * 1000; // 10 seconds |
| 30 | + |
| 31 | +async function startClient() { |
| 32 | + // Step1: Create an instance of RosettaSDK Client |
| 33 | + const networkAPI = new RosettaClient.NetworkApi(APIClient).promises; |
| 34 | + |
| 35 | + // Step 2: Get all available networks |
| 36 | + const metadataRequest = new RosettaClient.MetadataRequest(); |
| 37 | + const networkList = await networkAPI.networkList(metadataRequest); |
| 38 | + if (networkList.network_identifiers.length == 0) { |
| 39 | + console.error('Server did not respond with any network identifier'); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + // Step 3: Print the primary network |
| 44 | + const primaryNetwork = networkList.network_identifiers[0]; |
| 45 | + console.log(`Primary Network: ${primaryNetwork}`); |
| 46 | + |
| 47 | + // Step 4: Fetch the network status |
| 48 | + const networkRequest = new RosettaClient.NetworkRequest(primaryNetwork); |
| 49 | + const networkStatus = await networkAPI.networkStatus(networkRequest); |
| 50 | + |
| 51 | + // Step 5: Print Network Status |
| 52 | + console.log(`Network Status: ${networkStatus}`); |
| 53 | + |
| 54 | + // Step 6: Asserter (ToDo?) |
| 55 | + // Response Assertions are already handled by the server. |
| 56 | + |
| 57 | + // Step 7: Fetch the Network Options |
| 58 | + const networkOptions = await networkAPI.networkOptions(networkRequest); |
| 59 | + |
| 60 | + // Step 8: Print Network Options |
| 61 | + console.log(`Network Options: ${networkOptions}`); |
| 62 | + |
| 63 | + // Step 9: Asserter (ToDo?), refer to step 6. |
| 64 | + // Step 10: ClientAsserter, refer to step 6. |
| 65 | + // ... |
| 66 | + |
| 67 | + // Step 11: Fetch current block |
| 68 | + const blockIdentifier = RosettaClient.PartialBlockIdentifier.constructFromObject({ |
| 69 | + hash: networkStatus.current_block_identifier, |
| 70 | + }); |
| 71 | + const blockRequest = new RosettaClient.BlockRequest(primaryNetwork, blockIdentifier); |
| 72 | + const block = await networkAPI.block(blockRequest); |
| 73 | + |
| 74 | + // Step 12: Print the block |
| 75 | + console.log(`Current Block: ${block}`); |
| 76 | + |
| 77 | + // Step 13: Assert the block response is valid, refer to step 6 |
| 78 | + |
| 79 | + // Step 14: Print transactions in that block |
| 80 | + block.OtherTransactions.forEach(tx => { |
| 81 | + console.log(` Transaction: ${tx}`); |
| 82 | + }); |
| 83 | +}; |
| 84 | + |
| 85 | +startClient() |
| 86 | + .catch(e => console.error(e)); |
0 commit comments