Skip to content

Commit

Permalink
fix: versions format and other PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulDremanovich committed Feb 26, 2024
1 parent 8792ad1 commit da8359c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 45 deletions.
21 changes: 10 additions & 11 deletions src/lib/nodes/btc/BtcNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createBtcLikeClient } from '../utils/createBtcLikeClient'
import type { AxiosInstance } from 'axios'
import { Node } from '@/lib/nodes/abstract.node'
import { NODE_LABELS } from '@/lib/nodes/constants'
import { formatBtcVersion } from '@/lib/nodes/utils/formatBtcVersion.ts'
import { formatBtcVersion } from '@/lib/nodes/utils/nodeVersionFormatters.ts'

type FetchBtcNodeInfoResult = {
error: string
Expand Down Expand Up @@ -43,16 +43,15 @@ export class BtcNode extends Node {
}

private async fetchNodeVersion(): Promise<void> {
try {
const { data } = await this.client.post<FetchBtcNodeInfoResult>('/bitcoind', {"jsonrpc":"1.0","id":"adm","method":"getnetworkinfo","params":[]})
const v = data?.result?.version
if (v) {
this.version = 'v' + formatBtcVersion(v)
} else {
console.error(data.error)
}
} catch (e) {
console.error(e)
const { data } = await this.client.post<FetchBtcNodeInfoResult>('/bitcoind', {
jsonrpc: '1.0',
id: 'adm',
method: 'getnetworkinfo',
params: []
})
const version = data.result.version
if (version) {
this.version = formatBtcVersion(version)
}
}
}
14 changes: 5 additions & 9 deletions src/lib/nodes/dash/DashNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ export class DashNode extends Node {
}

private async fetchNodeVersion(): Promise<void> {
try {
const { data } = await this.client.post<FetchNodeVersionResponse>('/', {method:'getnetworkinfo'})
if (data?.result.buildversion) {
this.version = data.result.buildversion
} else {
console.error(data.error)
}
} catch (e) {
console.error(e)
const { data } = await this.client.post<FetchNodeVersionResponse>('/', {
method: 'getnetworkinfo'
})
if (data.result.buildversion) {
this.version = data.result.buildversion
}
}
}
13 changes: 4 additions & 9 deletions src/lib/nodes/doge/DogeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createBtcLikeClient } from '../utils/createBtcLikeClient'
import type { AxiosInstance } from 'axios'
import { Node } from '@/lib/nodes/abstract.node'
import { NODE_LABELS } from '@/lib/nodes/constants'
import { formatBtcVersion } from '@/lib/nodes/utils/formatBtcVersion.ts'
import { formatDogeVersion } from '@/lib/nodes/utils/nodeVersionFormatters.ts'

type FetchBtcNodeInfoResult = {
info: {
Expand Down Expand Up @@ -42,14 +42,9 @@ export class DogeNode extends Node {
}

private async fetchNodeVersion(): Promise<void> {
try {
const response = await this.client.get<FetchBtcNodeInfoResult>('/api/status')
console.log(response)
if (response?.data?.info.version) {
this.version = 'v' + formatBtcVersion(response?.data?.info.version)
}
} catch (e) {
console.error(e)
const { data } = await this.client.get<FetchBtcNodeInfoResult>('/api/status')
if (data.info.version) {
this.version = formatDogeVersion(data.info.version)
}
}
}
11 changes: 2 additions & 9 deletions src/lib/nodes/eth/EthNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Web3Eth from 'web3-eth'
import { HttpProvider } from 'web3-providers-http'
import { Node } from '@/lib/nodes/abstract.node'
import { NODE_LABELS } from '@/lib/nodes/constants'
import { formatEthVersion } from '@/lib/nodes/utils/nodeVersionFormatters.ts'

/**
* Encapsulates a node. Provides methods to send API-requests
Expand Down Expand Up @@ -38,14 +39,6 @@ export class EthNode extends Node {
}

private async fetchNodeVersion(): Promise<void> {
try {
const parts = (await this.client.getNodeInfo()).split('/')
const clientName = parts.length > 0 ? parts[0] : ''
const fullVersion = parts.length > 1 ? parts[1]: ''
const simplifiedVersion = fullVersion.split('-')[0]
this.version = `${clientName}/${simplifiedVersion}`
} catch (e) {
console.error(e)
}
this.version = formatEthVersion(await this.client.getNodeInfo())
}
}
7 changes: 0 additions & 7 deletions src/lib/nodes/utils/formatBtcVersion.ts

This file was deleted.

30 changes: 30 additions & 0 deletions src/lib/nodes/utils/nodeVersionFormatters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const formatBtcVersion = (version: number) => {
const parts = getBtcVersionParts(version)
if (parts.length < 3) return ''
if (parts[0] === 0) {
return `v${parts[0]}.${parts[1]}.${parts[2]}`
} else {
return `v${parts[0]}.${parts[1]}`
}
}

export const formatDogeVersion = (version: number) => {
const parts = getBtcVersionParts(version)
if (parts.length < 3) return ''
return `v${parts[0]}.${parts[1]}.${parts[2]}`
}

export const formatEthVersion = (version: string) => {
const parts = version.split('/')
const clientName = parts.length > 0 ? parts[0] : ''
const fullVersion = parts.length > 1 ? parts[1] : ''
const simplifiedVersion = fullVersion.split('-')[0]
return `${clientName}/${simplifiedVersion}`
}

export const getBtcVersionParts = (version: number) =>
version
.toString()
.padStart(8, '0')
.match(/.{1,2}/g)
?.map((item) => +item) || []

0 comments on commit da8359c

Please sign in to comment.