import * as aptos from 'aptos'; import { Table } from '../src/common'; export class TableClient { cli: aptos.AptosClient; tb: Table; keyTypeName: string; valueTypeName: string; constructor(aptosClient: aptos.AptosClient, tb: Table, keyTypeName: string, valueTypeName: string) { this.cli = aptosClient; this.tb = tb; this.keyTypeName = keyTypeName; this.valueTypeName = valueTypeName; } private formatKey(k: K): any { if (typeof k === 'bigint' || typeof k === 'number') { return k.toString(); } return k; } public async get(key: K, ledgerVersion?: bigint): Promise { try { const v: V = await this.cli.getTableItem( this.tb.handle, { key_type: this.keyTypeName, value_type: this.valueTypeName, key: this.formatKey(key), }, ledgerVersion ? { ledgerVersion: ledgerVersion, } : undefined, ); return v; } catch (e) { if (e instanceof aptos.ApiError && e.status == 404) { return null; } else { console.error('Table Client failed to get value', e); throw e; } } } }