forked from TrueCarry/JettonGramGpuMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
130 lines (130 loc) · 5.2 KB
/
client.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getTonapiClient = exports.getLiteClient = exports.getTonCenterClient = exports.getTon4ClientTonhub = exports.getTon4ClientOrbs = exports.getTon4Client = exports.intToIP = void 0;
const ton_1 = require("@ton/ton");
const axios_1 = __importDefault(require("axios"));
const ton_lite_client_1 = require("ton-lite-client");
const ton_access_1 = require("@orbs-network/ton-access");
const tonapi_sdk_js_1 = require("tonapi-sdk-js");
let lc4 = undefined;
let lc = undefined;
let lcOrbs = undefined;
let lcHub = undefined;
let lcToncenter = undefined;
let tonapiClient = undefined;
let createLiteClient;
function intToIP(int) {
const part1 = int & 255;
const part2 = (int >> 8) & 255;
const part3 = (int >> 16) & 255;
const part4 = (int >> 24) & 255;
return `${part4}.${part3}.${part2}.${part1}`;
}
exports.intToIP = intToIP;
function getTon4Client(_configUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (lc4) {
return lc4;
}
lc4 = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : yield (0, ton_access_1.getHttpV4Endpoint)() });
return lc4;
});
}
exports.getTon4Client = getTon4Client;
function getTon4ClientOrbs(_configUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (lcOrbs) {
return lcOrbs;
}
lcOrbs = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : yield (0, ton_access_1.getHttpV4Endpoint)() });
return lcOrbs;
});
}
exports.getTon4ClientOrbs = getTon4ClientOrbs;
function getTon4ClientTonhub(_configUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (lcHub) {
return lcHub;
}
lcHub = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://mainnet-v4.tonhubapi.com' });
return lcHub;
});
}
exports.getTon4ClientTonhub = getTon4ClientTonhub;
function getTonCenterClient(_configUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (lcToncenter) {
return lcToncenter;
}
lcToncenter = new ton_1.TonClient({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://toncenter.com/api/v2/jsonRPC' });
return lcToncenter;
});
}
exports.getTonCenterClient = getTonCenterClient;
function getLiteClient(_configUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (lc) {
return lc;
}
if (!createLiteClient) {
createLiteClient = (() => __awaiter(this, void 0, void 0, function* () {
const { data } = yield (0, axios_1.default)(_configUrl);
// const data = JSON.parse(fs.readFileSync('ton-global.config', {encoding: 'utf8'}))
const liteServers = data.liteservers;
const engines = [];
for (const server of liteServers) {
const ls = server;
engines.push(new ton_lite_client_1.LiteSingleEngine({
host: `tcp://${intToIP(ls.ip)}:${ls.port}`,
publicKey: Buffer.from(ls.id.key, 'base64'),
}));
}
const engine = new ton_lite_client_1.LiteRoundRobinEngine(engines);
const lc2 = new ton_lite_client_1.LiteClient({
engine,
batchSize: 1,
});
lc = lc2;
}))();
}
yield createLiteClient;
return lc;
});
}
exports.getLiteClient = getLiteClient;
function getTonapiClient() {
return __awaiter(this, void 0, void 0, function* () {
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 tonapi_sdk_js_1.HttpClient({
baseUrl: 'https://tonapi.io',
baseApiParams: {
headers,
}
});
// Initialize the API client
const client = new tonapi_sdk_js_1.Api(httpClient);
tonapiClient = client;
return client;
});
}
exports.getTonapiClient = getTonapiClient;