client.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __importDefault = (this && this.__importDefault) || function (mod) {
  12. return (mod && mod.__esModule) ? mod : { "default": mod };
  13. };
  14. Object.defineProperty(exports, "__esModule", { value: true });
  15. exports.getTonapiClient = exports.getLiteClient = exports.getTonCenterClient = exports.getTon4ClientTonhub = exports.getTon4Client = exports.intToIP = void 0;
  16. const ton_1 = require("@ton/ton");
  17. const axios_1 = __importDefault(require("axios"));
  18. const ton_lite_client_1 = require("ton-lite-client");
  19. // import { getHttpEndpoint, getHttpV4Endpoint } from "@orbs-network/ton-access";
  20. const tonapi_sdk_js_1 = require("tonapi-sdk-js");
  21. let lc4 = undefined;
  22. let lc = undefined;
  23. let lcOrbs = undefined;
  24. let lcHub = undefined;
  25. let lcToncenter = undefined;
  26. let tonapiClient = undefined;
  27. let createLiteClient;
  28. function intToIP(int) {
  29. const part1 = int & 255;
  30. const part2 = (int >> 8) & 255;
  31. const part3 = (int >> 16) & 255;
  32. const part4 = (int >> 24) & 255;
  33. return `${part4}.${part3}.${part2}.${part1}`;
  34. }
  35. exports.intToIP = intToIP;
  36. function getTon4Client(_configUrl) {
  37. return __awaiter(this, void 0, void 0, function* () {
  38. if (lc4) {
  39. return lc4;
  40. }
  41. lc4 = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://mainnet-v4.tonhubapi.com' });
  42. return lc4;
  43. });
  44. }
  45. exports.getTon4Client = getTon4Client;
  46. function getTon4ClientTonhub(_configUrl) {
  47. return __awaiter(this, void 0, void 0, function* () {
  48. if (lcHub) {
  49. return lcHub;
  50. }
  51. lcHub = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://mainnet-v4.tonhubapi.com' });
  52. return lcHub;
  53. });
  54. }
  55. exports.getTon4ClientTonhub = getTon4ClientTonhub;
  56. function getTonCenterClient(_configUrl) {
  57. return __awaiter(this, void 0, void 0, function* () {
  58. if (lcToncenter) {
  59. return lcToncenter;
  60. }
  61. lcToncenter = new ton_1.TonClient({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://toncenter.com/api/v2/jsonRPC' });
  62. return lcToncenter;
  63. });
  64. }
  65. exports.getTonCenterClient = getTonCenterClient;
  66. function getLiteClient(_configUrl) {
  67. return __awaiter(this, void 0, void 0, function* () {
  68. if (lc) {
  69. return lc;
  70. }
  71. if (!createLiteClient) {
  72. createLiteClient = (() => __awaiter(this, void 0, void 0, function* () {
  73. const { data } = yield (0, axios_1.default)(_configUrl);
  74. // const data = JSON.parse(fs.readFileSync('ton-global.config', {encoding: 'utf8'}))
  75. const liteServers = data.liteservers;
  76. const engines = [];
  77. for (const server of liteServers) {
  78. const ls = server;
  79. engines.push(new ton_lite_client_1.LiteSingleEngine({
  80. host: `tcp://${intToIP(ls.ip)}:${ls.port}`,
  81. publicKey: Buffer.from(ls.id.key, 'base64'),
  82. }));
  83. }
  84. const engine = new ton_lite_client_1.LiteRoundRobinEngine(engines);
  85. const lc2 = new ton_lite_client_1.LiteClient({
  86. engine,
  87. batchSize: 1,
  88. });
  89. lc = lc2;
  90. }))();
  91. }
  92. yield createLiteClient;
  93. return lc;
  94. });
  95. }
  96. exports.getLiteClient = getLiteClient;
  97. function getTonapiClient() {
  98. return __awaiter(this, void 0, void 0, function* () {
  99. if (tonapiClient) {
  100. return tonapiClient;
  101. }
  102. const headers = {
  103. 'Content-type': 'application/json'
  104. };
  105. if (process.env.TONAPI_TOKEN) {
  106. headers['Authorization'] = `Bearer ${process.env.TONAPI_TOKEN}`;
  107. }
  108. const httpClient = new tonapi_sdk_js_1.HttpClient({
  109. baseUrl: 'https://tonapi.io',
  110. baseApiParams: {
  111. headers,
  112. }
  113. });
  114. // Initialize the API client
  115. const client = new tonapi_sdk_js_1.Api(httpClient);
  116. tonapiClient = client;
  117. return client;
  118. });
  119. }
  120. exports.getTonapiClient = getTonapiClient;