Skip to content

Commit

Permalink
Move block explorer urls to CLI client side and out of the SDK (#4510)
Browse files Browse the repository at this point in the history
  • Loading branch information
danield9tqh authored Jan 9, 2024
1 parent 86c04ea commit ae68eae
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 67 deletions.
6 changes: 2 additions & 4 deletions ironfish-cli/src/commands/miners/pools/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Flags } from '@oclif/core'
import dns from 'dns'
import { IronfishCommand } from '../../../command'
import { RemoteFlags } from '../../../flags'
import { getExplorer } from '../../../utils/explorer'

export class StartPool extends IronfishCommand {
static description = `Start a mining pool that connects to a node`
Expand Down Expand Up @@ -76,8 +77,6 @@ export class StartPool extends IronfishCommand {
new Discord({
webhook: discordWebhook,
logger: this.logger,
explorerBlocksUrl: this.sdk.config.get('explorerBlocksUrl'),
explorerTransactionsUrl: this.sdk.config.get('explorerTransactionsUrl'),
}),
)

Expand All @@ -90,8 +89,6 @@ export class StartPool extends IronfishCommand {
new Lark({
webhook: larkWebhook,
logger: this.logger,
explorerBlocksUrl: this.sdk.config.get('explorerBlocksUrl'),
explorerTransactionsUrl: this.sdk.config.get('explorerTransactionsUrl'),
}),
)

Expand Down Expand Up @@ -146,6 +143,7 @@ export class StartPool extends IronfishCommand {
banning: flags.banning,
tls: flags.tls,
tlsOptions: tlsOptions,
getExplorer,
})

await this.pool.start()
Expand Down
13 changes: 9 additions & 4 deletions ironfish-cli/src/commands/wallet/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { IronfishCommand } from '../../command'
import { IronFlag, RemoteFlags } from '../../flags'
import { selectAsset } from '../../utils/asset'
import { promptCurrency } from '../../utils/currency'
import { getExplorer } from '../../utils/explorer'
import { selectFee } from '../../utils/fees'
import { watchTransaction } from '../../utils/transaction'

Expand Down Expand Up @@ -220,12 +221,16 @@ export class Burn extends IronfishCommand {
this.log(`Amount: ${CurrencyUtils.renderIron(amount)}`)
this.log(`Hash: ${transaction.hash().toString('hex')}`)
this.log(`Fee: ${CurrencyUtils.renderIron(transaction.fee(), true)}`)
this.log(
`\nIf the transaction is mined, it will appear here https://explorer.ironfish.network/transaction/${transaction
.hash()
.toString('hex')}`,

const networkId = (await client.chain.getNetworkInfo()).content.networkId
const transactionUrl = getExplorer(networkId)?.getTransactionUrl(
transaction.hash().toString('hex'),
)

if (transactionUrl) {
this.log(`\nIf the transaction is mined, it will appear here: ${transactionUrl}`)
}

if (flags.watch) {
this.log('')

Expand Down
13 changes: 9 additions & 4 deletions ironfish-cli/src/commands/wallet/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IronfishCommand } from '../../command'
import { IronFlag, RemoteFlags } from '../../flags'
import { selectAsset } from '../../utils/asset'
import { promptCurrency } from '../../utils/currency'
import { getExplorer } from '../../utils/explorer'
import { selectFee } from '../../utils/fees'
import { watchTransaction } from '../../utils/transaction'

Expand Down Expand Up @@ -293,12 +294,16 @@ export class Mint extends IronfishCommand {
)
this.log(`Fee: ${CurrencyUtils.renderIron(transaction.fee(), true)}`)
this.log(`Hash: ${transaction.hash().toString('hex')}`)
this.log(
`\nIf the transaction is mined, it will appear here https://explorer.ironfish.network/transaction/${transaction
.hash()
.toString('hex')}`,

const networkId = (await client.chain.getNetworkInfo()).content.networkId
const transactionUrl = getExplorer(networkId)?.getTransactionUrl(
transaction.hash().toString('hex'),
)

if (transactionUrl) {
this.log(`\nIf the transaction is mined, it will appear here: ${transactionUrl}`)
}

if (flags.watch) {
this.log('')

Expand Down
13 changes: 9 additions & 4 deletions ironfish-cli/src/commands/wallet/notes/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import inquirer from 'inquirer'
import { IronfishCommand } from '../../../command'
import { IronFlag, RemoteFlags } from '../../../flags'
import { ProgressBar } from '../../../types'
import { getExplorer } from '../../../utils/explorer'
import { selectFee } from '../../../utils/fees'
import { displayTransactionSummary, watchTransaction } from '../../../utils/transaction'

Expand Down Expand Up @@ -549,12 +550,16 @@ export class CombineNotesCommand extends IronfishCommand {
await this.displayCombinedNoteHashes(client, from, transaction)

this.log(`Transaction hash: ${transaction.hash().toString('hex')}`)
this.log(
`If the transaction is mined, it will appear here https://explorer.ironfish.network/transaction/${transaction
.hash()
.toString('hex')}`,

const networkId = (await client.chain.getNetworkInfo()).content.networkId
const transactionUrl = getExplorer(networkId)?.getTransactionUrl(
transaction.hash().toString('hex'),
)

if (transactionUrl) {
this.log(`\nIf the transaction is mined, it will appear here: ${transactionUrl}`)
}

if (flags.watch) {
this.log('')

Expand Down
13 changes: 9 additions & 4 deletions ironfish-cli/src/commands/wallet/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IronfishCommand } from '../../command'
import { HexFlag, IronFlag, RemoteFlags } from '../../flags'
import { selectAsset } from '../../utils/asset'
import { promptCurrency } from '../../utils/currency'
import { getExplorer } from '../../utils/explorer'
import { selectFee } from '../../utils/fees'
import { displayTransactionSummary, watchTransaction } from '../../utils/transaction'

Expand Down Expand Up @@ -254,12 +255,16 @@ export class Send extends IronfishCommand {
this.log(`Hash: ${transaction.hash().toString('hex')}`)
this.log(`Fee: ${CurrencyUtils.renderIron(transaction.fee(), true)}`)
this.log(`Memo: ${memo}`)
this.log(
`\nIf the transaction is mined, it will appear here https://explorer.ironfish.network/transaction/${transaction
.hash()
.toString('hex')}`,

const networkId = (await client.chain.getNetworkInfo()).content.networkId
const transactionUrl = getExplorer(networkId)?.getTransactionUrl(
transaction.hash().toString('hex'),
)

if (transactionUrl) {
this.log(`\nIf the transaction is mined, it will appear here: ${transactionUrl}`)
}

if (flags.watch) {
this.log('')

Expand Down
27 changes: 27 additions & 0 deletions ironfish-cli/src/utils/explorer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

export const EXPLORER_URLS = {
0: 'https://testnet.explorer.ironfish.network',
1: 'https://explorer.ironfish.network',
}

type ValidNetworkId = keyof typeof EXPLORER_URLS

type Explorer = {
getBlockUrl: (hash: string) => string
getTransactionUrl: (hash: string) => string
}

export const getExplorer = (networkId: number): Explorer | null => {
if (!(networkId in EXPLORER_URLS)) {
return null
}

const url = EXPLORER_URLS[networkId as ValidNetworkId]
return {
getBlockUrl: (hash: string) => `${url}/blocks/${hash}`,
getTransactionUrl: (hash: string) => `${url}/transaction/${hash}`,
}
}
14 changes: 0 additions & 14 deletions ironfish/src/fileStores/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,6 @@ export type ConfigOptions = {
*/
jsonLogs: boolean

/**
* URL for viewing block information in a block explorer
*/
explorerBlocksUrl: string

/**
* URL for viewing transaction information in a block explorer
*/
explorerTransactionsUrl: string

/**
* How many blocks back from the head of the chain to use to calculate the fee
* estimate
Expand Down Expand Up @@ -373,8 +363,6 @@ export const ConfigOptionsSchema: yup.ObjectSchema<Partial<ConfigOptions>> = yup
poolMaxConnectionsPerIp: YupUtils.isPositiveInteger,
poolLarkWebhook: yup.string(),
jsonLogs: yup.boolean(),
explorerBlocksUrl: YupUtils.isUrl,
explorerTransactionsUrl: YupUtils.isUrl,
feeEstimatorMaxBlockHistory: YupUtils.isPositiveInteger,
feeEstimatorPercentileSlow: YupUtils.isPositiveInteger,
feeEstimatorPercentileAverage: YupUtils.isPositiveInteger,
Expand Down Expand Up @@ -473,8 +461,6 @@ export class Config extends KeyStore<ConfigOptions> {
poolMaxConnectionsPerIp: 0,
poolLarkWebhook: '',
jsonLogs: false,
explorerBlocksUrl: 'https://explorer.ironfish.network/blocks/',
explorerTransactionsUrl: 'https://explorer.ironfish.network/transaction/',
feeEstimatorMaxBlockHistory: DEFAULT_FEE_ESTIMATOR_MAX_BLOCK_HISTORY,
feeEstimatorPercentileSlow: DEFAULT_FEE_ESTIMATOR_PERCENTILE_SLOW,
feeEstimatorPercentileAverage: DEFAULT_FEE_ESTIMATOR_PERCENTILE_AVERAGE,
Expand Down
14 changes: 12 additions & 2 deletions ironfish/src/mining/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { MiningStatusMessage } from './stratum/messages'
import { StratumServer } from './stratum/stratumServer'
import { StratumServerClient } from './stratum/stratumServerClient'
import { mineableHeaderString } from './utils'
import { WebhookNotifier } from './webhooks'
import { Explorer, WebhookNotifier } from './webhooks'

const RECALCULATE_TARGET_TIMEOUT = 10000
const EVENT_LOOP_MS = 10 * 1000
Expand Down Expand Up @@ -59,13 +59,16 @@ export class MiningPool {
private recalculateTargetInterval: SetIntervalToken | null
private notifyStatusInterval: SetIntervalToken | null

private getExplorer: (networkId: number) => Explorer | null = () => null

private constructor(options: {
rpc: RpcSocketClient
shares: MiningPoolShares
config: Config
logger: Logger
webhooks?: WebhookNotifier[]
banning?: boolean
getExplorer?: (networkId: number) => Explorer | null
}) {
this.rpc = options.rpc
this.logger = options.logger
Expand Down Expand Up @@ -98,6 +101,8 @@ export class MiningPool {

this.recalculateTargetInterval = null
this.notifyStatusInterval = null

this.getExplorer = options.getExplorer ?? this.getExplorer
}

static async init(options: {
Expand All @@ -111,6 +116,7 @@ export class MiningPool {
banning?: boolean
tls?: boolean
tlsOptions?: tls.TlsOptions
getExplorer?: (networkId: number) => Explorer | null
}): Promise<MiningPool> {
const shares = await MiningPoolShares.init({
rpc: options.rpc,
Expand All @@ -127,6 +133,7 @@ export class MiningPool {
webhooks: options.webhooks,
shares,
banning: options.banning,
getExplorer: options.getExplorer,
})

if (options.tls) {
Expand Down Expand Up @@ -345,7 +352,10 @@ export class MiningPool {
}

if (connected) {
this.webhooks.map((w) => w.poolConnected())
const networkResponse = await this.rpc.chain.getNetworkInfo()
const explorer = this.getExplorer(networkResponse.content.networkId)

this.webhooks.map((w) => w.poolConnected(explorer ?? undefined))
}

this.connectWarned = false
Expand Down
2 changes: 1 addition & 1 deletion ironfish/src/mining/webhooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

export { Discord } from './discord'
export { Lark } from './lark'
export { WebhookNotifier } from './webhookNotifier'
export { Explorer, WebhookNotifier } from './webhookNotifier'
44 changes: 15 additions & 29 deletions ironfish/src/mining/webhooks/webhookNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ import { createRootLogger, Logger } from '../../logger'
import { CurrencyUtils, ErrorUtils } from '../../utils'
import { FileUtils } from '../../utils/file'

export type Explorer = {
getBlockUrl: (hash: string) => string
getTransactionUrl: (hash: string) => string
}

export abstract class WebhookNotifier {
protected readonly webhook: string | null = null
protected readonly client: AxiosInstance | null = null
protected readonly logger: Logger
protected readonly explorerBlocksUrl: string | null = null
protected readonly explorerTransactionsUrl: string | null = null
protected explorer: Explorer | null = null

constructor(options: {
webhook: string | null
logger?: Logger
explorerBlocksUrl?: string | null
explorerTransactionsUrl?: string | null
}) {
constructor(options: { webhook: string | null; logger?: Logger }) {
this.logger = options.logger ?? createRootLogger()
this.explorerBlocksUrl = options.explorerBlocksUrl ?? null
this.explorerTransactionsUrl = options.explorerTransactionsUrl ?? null

if (options.webhook) {
this.webhook = options.webhook
Expand All @@ -32,7 +29,8 @@ export abstract class WebhookNotifier {

abstract sendText(text: string): void

poolConnected(): void {
poolConnected(explorer?: Explorer): void {
this.explorer = explorer ?? this.explorer
this.sendText('Successfully connected to node')
}

Expand All @@ -42,12 +40,9 @@ export abstract class WebhookNotifier {

poolSubmittedBlock(hashedHeaderHex: string, hashRate: number, clients: number): void {
this.sendText(
`Block ${this.renderHashHex(
hashedHeaderHex,
this.explorerBlocksUrl,
)} submitted successfully! ${FileUtils.formatHashRate(
hashRate,
)}/s with ${clients} miners`,
`Block ${
this.explorer?.getBlockUrl(hashedHeaderHex) ?? `\`${hashedHeaderHex}\``
} submitted successfully! ${FileUtils.formatHashRate(hashRate)}/s with ${clients} miners`,
)
}

Expand All @@ -62,10 +57,9 @@ export abstract class WebhookNotifier {
this.sendText(
`Successfully created payout of ${shareCount} shares to ${
outputs.length
} users for ${CurrencyUtils.renderIron(total, true)} in transaction ${this.renderHashHex(
transactionHashHex,
this.explorerTransactionsUrl,
)}. Transaction pending (${payoutPeriodId})`,
} users for ${CurrencyUtils.renderIron(total, true)} in transaction ${
this.explorer?.getTransactionUrl(transactionHashHex) ?? `\`${transactionHashHex}\``
}. Transaction pending (${payoutPeriodId})`,
)
}

Expand Down Expand Up @@ -105,12 +99,4 @@ export abstract class WebhookNotifier {
}\n\tBans: ${status.bans}`,
)
}

private renderHashHex(hashHex: string, explorerUrl: string | null): string {
if (explorerUrl == null) {
return `\`${hashHex}\``
}

return `${explorerUrl + hashHex}`
}
}
2 changes: 1 addition & 1 deletion ironfish/src/rpc/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ export abstract class RpcClient {

getNetworkInfo: (
params?: GetNetworkInfoRequest,
): Promise<RpcResponse<GetNetworkInfoResponse>> => {
): Promise<RpcResponseEnded<GetNetworkInfoResponse>> => {
return this.request<GetNetworkInfoResponse>(
`${ApiNamespace.chain}/getNetworkInfo`,
params,
Expand Down

0 comments on commit ae68eae

Please sign in to comment.