forked from TrueCarry/JettonGramGpuMiner
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.ts
117 lines (92 loc) · 3.4 KB
/
client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { TonClient, TonClient4 } from "@ton/ton"
import axios from "axios"
import { LiteClient, LiteSingleEngine, LiteRoundRobinEngine } from "ton-lite-client"
// import { getHttpEndpoint, getHttpV4Endpoint } from "@orbs-network/ton-access";
import { HttpClient, Api } from 'tonapi-sdk-js';
import {IS_TESTNET} from "./config";
let lc4: TonClient4 | undefined = undefined
let lc: LiteClient | undefined = undefined
let lcOrbs: TonClient4 | undefined = undefined
let lcHub: TonClient4 | undefined = undefined
let lcToncenter: TonClient | undefined = undefined
let tonapiClient: Api<unknown> | undefined = undefined
let createLiteClient: Promise<void>
export function intToIP(int: number) {
const part1 = int & 255
const part2 = (int >> 8) & 255
const part3 = (int >> 16) & 255
const part4 = (int >> 24) & 255
return `${part4}.${part3}.${part2}.${part1}`
}
export async function getTon4Client(_configUrl?: string): Promise<TonClient4> {
if (lc4) {
return lc4
}
lc4 = new TonClient4({ endpoint: _configUrl ?? (IS_TESTNET ? 'https://testnet-v4.tonhubapi.com' : 'https://mainnet-v4.tonhubapi.com') })
return lc4 as TonClient4
}
export async function getTon4ClientTonhub(_configUrl?: string): Promise<TonClient4> {
if (lcHub) {
return lcHub
}
lcHub = new TonClient4({ endpoint: _configUrl ?? (IS_TESTNET ? 'https://testnet-v4.tonhubapi.com' : 'https://mainnet-v4.tonhubapi.com') })
return lcHub as TonClient4
}
export async function getTonCenterClient(_configUrl?: string): Promise<TonClient> {
if (lcToncenter) {
return lcToncenter
}
lcToncenter = new TonClient({ endpoint: _configUrl ?? (IS_TESTNET ? 'https://testnet.toncenter.com/api/v2/jsonRPC' : 'https://toncenter.com/api/v2/jsonRPC') })
return lcToncenter as TonClient
}
export async function getLiteClient(_configUrl): Promise<LiteClient> {
if (lc) {
return lc
}
if (!createLiteClient) {
createLiteClient = (async () => {
const { data } = await axios(_configUrl)
// const data = JSON.parse(fs.readFileSync('ton-global.config', {encoding: 'utf8'}))
const liteServers = data.liteservers
const engines: any[] = []
for (const server of liteServers) {
const ls = server
engines.push(
new LiteSingleEngine({
host: `tcp://${intToIP(ls.ip)}:${ls.port}`,
publicKey: Buffer.from(ls.id.key, 'base64'),
})
)
}
const engine = new LiteRoundRobinEngine(engines)
const lc2 = new LiteClient({
engine,
batchSize: 1,
})
lc = lc2
})()
}
await createLiteClient
return lc as any
}
export async function getTonapiClient(): Promise<Api<unknown>> {
if (tonapiClient) {
return tonapiClient
}
const headers = {
'Content-type': 'application/json'
}
if (process.env.TONAPI_TOKEN) {
headers['Authorization'] = `Bearer ${process.env.TONAPI_TOKEN}`
}
const httpClient = new HttpClient({
baseUrl: IS_TESTNET ? 'https://testnet.tonapi.io' : 'https://tonapi.io',
baseApiParams: {
headers,
}
});
// Initialize the API client
const client = new Api(httpClient);
tonapiClient = client
return client
}