client.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.getTon4ClientOrbs = 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. const ton_access_1 = require("@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 : yield (0, ton_access_1.getHttpV4Endpoint)() });
  42. return lc4;
  43. });
  44. }
  45. exports.getTon4Client = getTon4Client;
  46. function getTon4ClientOrbs(_configUrl) {
  47. return __awaiter(this, void 0, void 0, function* () {
  48. if (lcOrbs) {
  49. return lcOrbs;
  50. }
  51. lcOrbs = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : yield (0, ton_access_1.getHttpV4Endpoint)() });
  52. return lcOrbs;
  53. });
  54. }
  55. exports.getTon4ClientOrbs = getTon4ClientOrbs;
  56. function getTon4ClientTonhub(_configUrl) {
  57. return __awaiter(this, void 0, void 0, function* () {
  58. if (lcHub) {
  59. return lcHub;
  60. }
  61. lcHub = new ton_1.TonClient4({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://mainnet-v4.tonhubapi.com' });
  62. return lcHub;
  63. });
  64. }
  65. exports.getTon4ClientTonhub = getTon4ClientTonhub;
  66. function getTonCenterClient(_configUrl) {
  67. return __awaiter(this, void 0, void 0, function* () {
  68. if (lcToncenter) {
  69. return lcToncenter;
  70. }
  71. lcToncenter = new ton_1.TonClient({ endpoint: _configUrl !== null && _configUrl !== void 0 ? _configUrl : 'https://toncenter.com/api/v2/jsonRPC' });
  72. return lcToncenter;
  73. });
  74. }
  75. exports.getTonCenterClient = getTonCenterClient;
  76. function getLiteClient(_configUrl) {
  77. return __awaiter(this, void 0, void 0, function* () {
  78. if (lc) {
  79. return lc;
  80. }
  81. if (!createLiteClient) {
  82. createLiteClient = (() => __awaiter(this, void 0, void 0, function* () {
  83. const { data } = yield (0, axios_1.default)(_configUrl);
  84. // const data = JSON.parse(fs.readFileSync('ton-global.config', {encoding: 'utf8'}))
  85. const liteServers = data.liteservers;
  86. const engines = [];
  87. for (const server of liteServers) {
  88. const ls = server;
  89. engines.push(new ton_lite_client_1.LiteSingleEngine({
  90. host: `tcp://${intToIP(ls.ip)}:${ls.port}`,
  91. publicKey: Buffer.from(ls.id.key, 'base64'),
  92. }));
  93. }
  94. const engine = new ton_lite_client_1.LiteRoundRobinEngine(engines);
  95. const lc2 = new ton_lite_client_1.LiteClient({
  96. engine,
  97. batchSize: 1,
  98. });
  99. lc = lc2;
  100. }))();
  101. }
  102. yield createLiteClient;
  103. return lc;
  104. });
  105. }
  106. exports.getLiteClient = getLiteClient;
  107. function getTonapiClient() {
  108. return __awaiter(this, void 0, void 0, function* () {
  109. if (tonapiClient) {
  110. return tonapiClient;
  111. }
  112. const headers = {
  113. 'Content-type': 'application/json'
  114. };
  115. if (process.env.TONAPI_TOKEN) {
  116. headers['Authorization'] = `Bearer ${process.env.TONAPI_TOKEN}`;
  117. }
  118. const httpClient = new tonapi_sdk_js_1.HttpClient({
  119. baseUrl: 'https://tonapi.io',
  120. baseApiParams: {
  121. headers,
  122. }
  123. });
  124. // Initialize the API client
  125. const client = new tonapi_sdk_js_1.Api(httpClient);
  126. tonapiClient = client;
  127. return client;
  128. });
  129. }
  130. exports.getTonapiClient = getTonapiClient;