client.ts 3.2 KB

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