UNPKG

ton3-liteclient

Version:
250 lines (209 loc) 7.09 kB
import EventEmitter from 'events' import { ADNLClient } from 'adnl' import Debug from 'debug' import { StreamReader, StreamWriter, uintToBytes } from '../../tl/stream' import * as dataTypes from '../../dataTypes' import { AccountId, AccountState, AllShardsInfo, BlockData, BlockHeader, BlockTransactions, MasterchainInfo, RunMethodResult, SendMsgStatus, TransactionId3, TransactionList, TransactionInfo, Version, } from '../../dataTypes/liteServer'; import * as functions from '../../functions'; import { BlockIdExt } from '../../dataTypes/tonNode'; import { LookupBlockInput } from '../../functions/liteServer'; import { Utils } from 'ton3-core'; const debug = Debug('tonkite:lite-api:client'); let counter = 0; const nextQueryId = () => { const buffer = Utils.Helpers.hexToBytes('0000000095d6fecb497dfd0aa5f031e7d412986b5ce720496db512052e8f2d10') buffer.set(uintToBytes(counter++, 4).reverse(), 0) //counter += 1 return buffer } const createADNLQuery = (queryId: Uint8Array, query: Uint8Array) => { const queryPadding = 4 - (((query.length + 1) % 4) || 4); const buffer = new Uint8Array(4 + 32 + 1 + query.length + queryPadding); // crc32('adnl.message.query query_id:int256 query:bytes = adnl.Message') // Write 4 bytes number in LE order buffer.set(uintToBytes(0xb48bf97a, 4).reverse(), 0) // query_id:int256 buffer.set(queryId, 4) // query:bytes buffer.set([ query.length ], 4 + 32); buffer.set(query, 4 + 32 + 1); return buffer; }; class LiteApiError extends Error { constructor(message: string, readonly code: number) { super(message); } } export class LiteApi { #events = new EventEmitter(); constructor(private readonly adnlClient: ADNLClient) { adnlClient.on('data', (data: Buffer) => { const dataReader = new StreamReader(data); const adnlAnswer = dataTypes.adnl.answerMessage.read(dataReader); const [tag, answer] = this.parseLiteServerAnswer(adnlAnswer.answer); this.#events.emit(Utils.Helpers.bytesToHex(adnlAnswer.queryId), { tag, answer, }); }); } /** * Parses an answer with a parser (chosen according to a tag) * @param dataReader * @private */ private parseLiteServerAnswer(dataReader: StreamReader) { const tag = dataReader.readInt32LE(); const codecs = Object.values(dataTypes.liteServer) as any[]; const codec = codecs.find((codec) => codec.tag === tag); if (!codec) { throw new Error(`Data type 0x${tag.toString(16).padStart(8, '0')} is not supported.`); } return [tag, codec.read(dataReader)]; } protected query<T>(dataWriterFn: (dataWriter: StreamWriter) => void): Promise<T> { const dataWriter = new StreamWriter(); dataWriterFn(dataWriter); const queryWriter = new StreamWriter(); dataTypes.liteServer.query.write(queryWriter, dataWriter.buffer); const queryId = nextQueryId(); this.adnlClient.write(createADNLQuery(queryId, queryWriter.buffer)); return new Promise<T>((resolve, reject) => { this.#events.once(Utils.Helpers.bytesToHex(queryId), ({ tag, answer }) => { if (tag === dataTypes.liteServer.error.tag) { reject(new LiteApiError(answer.message, answer.code)); return; } resolve(answer); }); }); } getMasterchainInfo() { debug('query getMasterchainInfo()'); return this.query<MasterchainInfo>((dataWriter) => { functions.liteServer.getMasterchainInfo(dataWriter); }); } getTime() { debug('query getTime()'); return this.query<string>((dataWriter) => { functions.liteServer.getTime(dataWriter); }); } getVersion() { debug('query getVersion()'); return this.query<Version>((dataWriter) => { functions.liteServer.getVersion(dataWriter); }); } getBlock(blockId: BlockIdExt) { debug('query getBlock()'); return this.query<BlockData>((dataWriter) => { functions.liteServer.getBlock(dataWriter, blockId); }); } getAllShardsInfo(blockId: BlockIdExt) { debug('query getAllShardsInfo()'); return this.query<AllShardsInfo>((dataWriter) => { functions.liteServer.getAllShardsInfo(dataWriter, blockId); }); } getState(blockId: BlockIdExt) { debug('query getState()'); return this.query<unknown>((dataWriter) => { functions.liteServer.getState(dataWriter, blockId); }); } getBlockHeader(blockId: BlockIdExt, mode: number) { debug('query getBlockHeader()'); return this.query<BlockHeader>((dataWriter) => { functions.liteServer.getBlockHeader(dataWriter, blockId, mode); }); } lookupBlock(input: LookupBlockInput) { debug('query lookupBlock()'); return this.query<BlockHeader>((dataWriter) => { functions.liteServer.lookupBlock(dataWriter, input); }); } getAccountState(blockId: BlockIdExt, account: AccountId) { debug( 'query getAccountState(%d, %d, %s)', blockId.workchain, blockId.seqno, `${account.workchain}:${Utils.Helpers.bytesToHex(account.id)}`, ); return this.query<AccountState>((dataWriter) => { functions.liteServer.getAccountState(dataWriter, blockId, account); }); } listBlockTransactions( blockId: BlockIdExt, count: number = 20, after: TransactionId3 | null = null, options: { reverseOrder?: boolean; wantProof?: boolean; } = {}, ) { return this.query<BlockTransactions>((dataWriter) => { debug('query listBlockTransactions()'); functions.liteServer.listBlockTransactions( dataWriter, blockId, count, after, !!options.reverseOrder, !!options.wantProof, ); }); } getTransactions(account: AccountId, lt: bigint, hash: Uint8Array, count: number = 20) { debug( 'query getTransactions(%s, %s, %s)', `${account.workchain}:${Utils.Helpers.bytesToHex(account.id)}`, lt.toString(10), Utils.Helpers.bytesToHex(hash), ); return this.query<TransactionList>((dataWriter) => { functions.liteServer.getTransactions(dataWriter, count, account, lt, hash); }); } getOneTransaction(id: BlockIdExt, account: AccountId, lt: bigint) { debug( 'query getOneTransaction(%s, %s, %s)', `${id.workchain},${id.shard.toString(16)},${id.seqno}`, `${account.workchain}:${Utils.Helpers.bytesToHex(account.id)}`, lt.toString(10), ); return this.query<TransactionInfo>((dataWriter) => { functions.liteServer.getOneTransaction(dataWriter, id, account, lt); }); } runSmcMethod(id: BlockIdExt, account: AccountId, methodId: bigint, params: Uint8Array) { debug('query runSmcMethod()'); return this.query<RunMethodResult>((dataWriter) => { functions.liteServer.runSmcMethod(dataWriter, id, account, methodId, params); }); } sendMessage(body: Uint8Array) { debug('query sendMessage()'); return this.query<SendMsgStatus>((dataWriter) => { functions.liteServer.sendMessage(dataWriter, body); }); } }