client.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 ?? await getHttpV4Endpoint() })
  25. return lc4 as TonClient4
  26. }
  27. export async function getTon4ClientOrbs(_configUrl?: string): Promise<TonClient4> {
  28. if (lcOrbs) {
  29. return lcOrbs
  30. }
  31. lcOrbs = new TonClient4({ endpoint: _configUrl ?? await getHttpV4Endpoint() })
  32. return lcOrbs as TonClient4
  33. }
  34. export async function getTon4ClientTonhub(_configUrl?: string): Promise<TonClient4> {
  35. if (lcHub) {
  36. return lcHub
  37. }
  38. lcHub = new TonClient4({ endpoint: _configUrl ?? 'https://mainnet-v4.tonhubapi.com' })
  39. return lcHub as TonClient4
  40. }
  41. export async function getTonCenterClient(_configUrl?: string): Promise<TonClient> {
  42. if (lcToncenter) {
  43. return lcToncenter
  44. }
  45. lcToncenter = new TonClient({ endpoint: _configUrl ?? 'https://toncenter.com/api/v2/jsonRPC' })
  46. return lcToncenter as TonClient
  47. }
  48. export async function getLiteClient(_configUrl): Promise<LiteClient> {
  49. if (lc) {
  50. return lc
  51. }
  52. if (!createLiteClient) {
  53. createLiteClient = (async () => {
  54. const { data } = await axios(_configUrl)
  55. // const data = JSON.parse(fs.readFileSync('ton-global.config', {encoding: 'utf8'}))
  56. const liteServers = data.liteservers
  57. const engines: any[] = []
  58. for (const server of liteServers) {
  59. const ls = server
  60. engines.push(
  61. new LiteSingleEngine({
  62. host: `tcp://${intToIP(ls.ip)}:${ls.port}`,
  63. publicKey: Buffer.from(ls.id.key, 'base64'),
  64. })
  65. )
  66. }
  67. const engine = new LiteRoundRobinEngine(engines)
  68. const lc2 = new LiteClient({
  69. engine,
  70. batchSize: 1,
  71. })
  72. lc = lc2
  73. })()
  74. }
  75. await createLiteClient
  76. return lc as any
  77. }
  78. export async function getTonapiClient(): Promise<Api<unknown>> {
  79. if (tonapiClient) {
  80. return tonapiClient
  81. }
  82. const headers = {
  83. 'Content-type': 'application/json'
  84. }
  85. if (process.env.TONAPI_TOKEN) {
  86. headers['Authorization'] = `Bearer ${process.env.TONAPI_TOKEN}`
  87. }
  88. const httpClient = new HttpClient({
  89. baseUrl: 'https://tonapi.io',
  90. baseApiParams: {
  91. headers,
  92. }
  93. });
  94. // Initialize the API client
  95. const client = new Api(httpClient);
  96. tonapiClient = client
  97. return client
  98. }