-
Notifications
You must be signed in to change notification settings - Fork 2
robust broadcasting config with multiple failovers #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sirdeggen
wants to merge
2
commits into
master
Choose a base branch
from
dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import { | ||
| BroadcastResponse, | ||
| BroadcastFailure, | ||
| Broadcaster, | ||
| HttpClient, | ||
| defaultHttpClient, | ||
| Transaction, | ||
| } from '@bsv/sdk' | ||
|
|
||
| /** | ||
| * Represents an WhatsOnChain transaction broadcaster. | ||
| */ | ||
| export default class BitailsBroadcaster implements Broadcaster { | ||
| readonly network: string | ||
| private readonly URL: string | ||
| private readonly httpClient: HttpClient | ||
|
|
||
| /** | ||
| * Constructs an instance of the WhatsOnChain broadcaster. | ||
| * | ||
| * @param {'main' | 'test' | 'stn'} network - The BSV network to use when calling the WhatsOnChain API. | ||
|
sirdeggen marked this conversation as resolved.
Outdated
|
||
| * @param {HttpClient} httpClient - The HTTP client used to make requests to the API. | ||
| */ | ||
|
sirdeggen marked this conversation as resolved.
|
||
| constructor( | ||
| httpClient: HttpClient = defaultHttpClient() | ||
| ) { | ||
| this.URL = `https://api.bitails.io/tx/broadcast` | ||
| this.httpClient = httpClient | ||
| } | ||
|
|
||
| /** | ||
| * Broadcasts a transaction via WhatsOnChain. | ||
| * | ||
| * @param {Transaction} tx - The transaction to be broadcasted. | ||
| * @returns {Promise<BroadcastResponse | BroadcastFailure>} A promise that resolves to either a success or failure response. | ||
| */ | ||
|
sirdeggen marked this conversation as resolved.
|
||
| async broadcast( | ||
| tx: Transaction | ||
| ): Promise<BroadcastResponse | BroadcastFailure> { | ||
| const rawTx = tx.toHex() | ||
|
|
||
| const requestOptions = { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| Accept: 'application/json' | ||
| }, | ||
| data: { raw: rawTx } | ||
| } | ||
|
|
||
| try { | ||
| const response = await this.httpClient.request<{ txid: string }>( | ||
| this.URL, | ||
| requestOptions | ||
| ) | ||
| if (response.ok) { | ||
| const txid = response.data.txid | ||
| return { | ||
| status: 'success', | ||
| txid, | ||
| message: 'broadcast successful' | ||
| } | ||
| } else { | ||
| return { | ||
| status: 'error', | ||
| code: response.status.toString() ?? 'ERR_UNKNOWN', | ||
| description: response.data ?? 'Unknown error' | ||
| } | ||
| } | ||
| } catch (error) { | ||
| return { | ||
| status: 'error', | ||
| code: '500', | ||
| description: | ||
| typeof error.message === 'string' | ||
| ? error.message | ||
| : 'Internal Server Error' | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,39 +1,72 @@ | ||
| /** | ||
| * Configuration for TAAL's ARC (Authenticated Resource Calls) service. | ||
| * This module sets up and exports an ARC client instance for blockchain data management. | ||
| * | ||
| * The ARC service provides reliable access to the Bitcoin SV blockchain with features like: | ||
| * - Transaction broadcasting | ||
| * - Merkle proof verification | ||
| * - Callback notifications for transaction confirmations | ||
| */ | ||
|
|
||
| import { ARC } from "@bsv/sdk" | ||
| import { ARC, WhatsOnChainBroadcaster, Broadcaster, Transaction, BroadcastResponse, BroadcastFailure } from "@bsv/sdk" | ||
| import BitailsBroadcaster from "./BitailsBroadcaster" | ||
| import dotenv from 'dotenv' | ||
| dotenv.config() | ||
|
|
||
| // Environment variables for ARC configuration | ||
| export const { NETWORK, DOMAIN, CALLBACK_TOKEN, ARC_API_KEY, TEST_ARC_API_KEY } = process.env | ||
|
|
||
| /** | ||
| * ARC client configuration options | ||
| * @property {string} callbackUrl - Webhook URL where ARC will send transaction notifications | ||
| * @property {string} callbackToken - Authentication token for securing webhook endpoints | ||
| */ | ||
| const options = { | ||
| callbackUrl: 'https://' + DOMAIN + '/callback', | ||
| callbackToken: CALLBACK_TOKEN, | ||
| apiKey: NETWORK === 'main' ? ARC_API_KEY : TEST_ARC_API_KEY, | ||
| export const ARC_URL = NETWORK !== 'main' ? 'https://arc-test.taal.com' : 'https://arc.taal.com' | ||
|
|
||
| class SuperArc implements Broadcaster { | ||
| private broadcasters: Broadcaster[] | ||
|
|
||
| constructor() { | ||
| const taal = new ARC('https://arc.taal.com', { | ||
| callbackUrl: 'https://' + DOMAIN + '/callback', | ||
| callbackToken: CALLBACK_TOKEN, | ||
| apiKey: ARC_API_KEY, | ||
| }) | ||
| const gorillaPool = new ARC('https://arc.gorillapool.io', { | ||
| callbackUrl: 'https://' + DOMAIN + '/callback', | ||
| callbackToken: CALLBACK_TOKEN | ||
| }) | ||
| const bsva = new ARC('https://arc-mainnet-staging-eu-1.bsvb.tech', { | ||
|
sirdeggen marked this conversation as resolved.
|
||
| callbackUrl: 'https://' + DOMAIN + '/callback', | ||
| callbackToken: CALLBACK_TOKEN | ||
| }) | ||
| const WoC = new WhatsOnChainBroadcaster('main') | ||
| const bitails = new BitailsBroadcaster() | ||
| this.broadcasters = [taal, gorillaPool, bsva, WoC, bitails] | ||
| } | ||
|
|
||
| // this function tries each of the available broadcaster options in order, returning on first success. | ||
| // This is done sequentially such that if ARC TAAL works, no other options are used, but if ARC TAAL fails, then we try other options. | ||
| async broadcast(tx: Transaction): Promise<BroadcastResponse | BroadcastFailure> { | ||
| for (const broadcaster of this.broadcasters) { | ||
| const response = await broadcaster.broadcast(tx) | ||
| if (response.status === 'success') { | ||
| return response | ||
| } | ||
| } | ||
| return { | ||
| status: 'error', | ||
| code: 'ERR_UNKNOWN', | ||
| description: 'Failed to broadcast transaction to any of the configured broadcasters' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export const ARC_URL = (NETWORK === 'main') | ||
| ? 'https://arc.taal.com' | ||
| : 'https://arc-test.taal.com' | ||
| function createBroadcaster() { | ||
| // if we're on testnet just use TAAL | ||
| if (NETWORK !== 'main') { | ||
| return new ARC('https://arc-test.taal.com', { | ||
| callbackUrl: 'https://' + DOMAIN + '/callback', | ||
| callbackToken: CALLBACK_TOKEN, | ||
| apiKey: TEST_ARC_API_KEY, | ||
| }) | ||
| } | ||
| // otherwise set up a failover which tries a bunch of ways to broadcast the tx | ||
| return new SuperArc() | ||
| } | ||
|
|
||
|
|
||
| const broadcaster = createBroadcaster() | ||
|
|
||
| /** | ||
| * Initialize ARC client based on network environment | ||
| * Uses production endpoint for 'main' network, test endpoint otherwise | ||
| */ | ||
| const Arc = new ARC(ARC_URL, options) | ||
| export const ArcTaal = new ARC(ARC_URL, { | ||
| callbackUrl: 'https://' + DOMAIN + '/callback', | ||
| callbackToken: CALLBACK_TOKEN, | ||
| apiKey: ARC_API_KEY, | ||
| }) | ||
|
|
||
| export default Arc | ||
| export default broadcaster | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.