|
| 1 | +# Ethereum Classic Transaction Broadcast with Node.js |
| 2 | + |
| 3 | +This documentation provides a guide to broadcast Ethereum Classic transactions to the mainnet using a Node.js script and the CryptoAPIs service. |
| 4 | + |
| 5 | +## Requirements |
| 6 | + |
| 7 | +- Node.js installed on your machine |
| 8 | +- [Axios'](https://www.npmjs.com/package/axios) library for making HTTP requests |
| 9 | + |
| 10 | +## Setup |
| 11 | + |
| 12 | +1. Install the required package by running: |
| 13 | + |
| 14 | + ```bash |
| 15 | + npm install axios |
| 16 | + |
| 17 | +2. Obtain an API Key from CryptoAPIs: |
| 18 | + - Sign up for [cryptoapis.io](https://my.cryptoapis.io/login) |
| 19 | + - Create a new api-key [here](https://my.cryptoapis.io/api-keys) |
| 20 | + |
| 21 | +## Script |
| 22 | +Create a new file called `broadcastETCTransaction.js` |
| 23 | +and add the following script: |
| 24 | + |
| 25 | +```js |
| 26 | +const axios = require('axios'); |
| 27 | +
|
| 28 | +// Replace with your API key |
| 29 | +const apiKey = 'your-api-key-here'; |
| 30 | +
|
| 31 | +// Replace with your signed transaction hex |
| 32 | +const signedTransactionHex = 'your-signed-transaction-hex-here'; |
| 33 | +
|
| 34 | +const broadcastTransaction = async () => { |
| 35 | + try { |
| 36 | + const response = await axios.post( |
| 37 | + 'https://rest.cryptoapis.io/blockchain-tools/ethereum-classic/mainnet/transactions/broadcast?context=broadcastETC', |
| 38 | + { |
| 39 | + context: '', // Optional |
| 40 | + data: { |
| 41 | + item: { |
| 42 | + signedTransactionHex: signedTransactionHex |
| 43 | + } |
| 44 | + } |
| 45 | + }, |
| 46 | + { |
| 47 | + headers: { |
| 48 | + 'x-api-key': apiKey, |
| 49 | + 'Content-Type': 'application/json' |
| 50 | + } |
| 51 | + } |
| 52 | + ); |
| 53 | +
|
| 54 | + console.log('Transaction Broadcasted Successfully:', response.data); |
| 55 | + } catch (error) { |
| 56 | + console.error('Error broadcasting transaction:', error.response ? error.response.data : error.message); |
| 57 | + } |
| 58 | +}; |
| 59 | +
|
| 60 | +broadcastTransaction(); |
| 61 | +
|
| 62 | +``` |
| 63 | + |
| 64 | +## Running the script |
| 65 | +1. Replace the `apiKey` and `signedTransactionHex` variables with your actual API key and signed transaction hex. |
| 66 | +2. Run the script: |
| 67 | + ``` |
| 68 | + node broadcastETCTransaction.js |
| 69 | + ``` |
| 70 | +
|
| 71 | +## Response |
| 72 | +
|
| 73 | +If successful, the script will output a JSON object with the following structure: |
| 74 | +```json |
| 75 | +{ |
| 76 | + "apiVersion": "string", |
| 77 | + "requestId": "string", |
| 78 | + "context": "string", |
| 79 | + "data": { |
| 80 | + "item": { |
| 81 | + "transactionId": "string" |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | +The response will contain the following fields: |
| 87 | + |
| 88 | +- `apiVersion` (string): The version of the API. |
| 89 | + |
| 90 | +- `requestId` (string): The ID of the request. |
| 91 | + |
| 92 | +- `context` (string): The context of the response. |
| 93 | + |
| 94 | +- `data` (object): |
| 95 | + |
| 96 | + - `item` (object): |
| 97 | + |
| 98 | + - `transactionId` (string): The ID of the broadcasted transaction. |
0 commit comments