UNPKG

@bayswap/sdk

Version:

SDK for BaySwap smart contract

124 lines 4.34 kB
import { getMoveObject, getObjectId } from '@mysten/sui.js'; import { buildPoolName, getSymbolFromType, parsingPoolTypes } from '../utils'; const allTokenModules = ['btc', 'eth', 'bnb', 'usdt', 'dai']; export class QueryModule { constructor(provider, registry) { this._provider = provider; this._registry = registry; } async getAllPools() { const res = []; const events = await this._provider.queryEvents({ query: { MoveModule: { package: this._registry.packageID, module: 'entry', }, }, order: 'descending', }); for (const eventData of events.data) { if (!eventData.type.includes('EventRegisteredPool')) { continue; } try { const fields = eventData.parsedJson; if (fields) { res.push({ coinX: `0x${fields.coin_x}`, coinY: `0x${fields.coin_y}`, curve: `0x${fields.curve}`, creator: fields.creator, poolID: fields.pool_id, }); } } catch (e) { console.log(e); throw e; } } return res; } async getAllTestTokens() { const res = []; await Promise.all(allTokenModules.map(async (coin) => { const eventQuery = { MoveEventType: `${this._registry.testPackage}::${coin}::EventCreatedCoin`, }; const events = await this._provider.queryEvents({ query: eventQuery, order: 'descending', }); for (const eventData of events.data) { const fields = eventData.parsedJson; if (fields) { res.push({ coinSymbol: fields.coin_symbol, coinType: `0x${fields.coin_type}`, treasuryCapID: fields.treasury_cap_id, }); } } })); return res; } parsePool(resp) { const poolId = getObjectId(resp); const obj = getMoveObject(resp); if (!(obj?.fields && obj?.type)) { throw new Error(`pool ${poolId} is not found`); } const fields = obj.fields; const { coinXType, coinYType, curveType } = parsingPoolTypes(obj.type); return { id: poolId, name: buildPoolName(coinXType, coinYType), fullType: obj.type, coinX: coinXType, coinXReserve: BigInt(fields.coin_x_reserve), coinY: coinYType, coinYReserve: BigInt(fields.coin_y_reserve), curve: curveType, feePercent: BigInt(fields.fee_percent), xScale: BigInt(fields.x_scale), yScale: BigInt(fields.y_scale), isLocked: fields.is_locked, lpTokenSupply: { value: BigInt(fields.lp_token_supply.fields.value), type: fields.lp_token_supply.type, }, }; } async getPool(poolId) { const pool = await this._provider.getObject({ id: poolId, options: { showContent: true, showDisplay: true }, }); return this.parsePool(pool); } async getPoolBatch(poolIds) { const pools = await this._provider.multiGetObjects({ ids: poolIds, options: { showContent: true, showDisplay: true }, }); return pools.map((pool) => this.parsePool(pool)); } async getAllBalances(owner) { const balances = {}; const coins = await this._provider.getAllBalances({ owner }); for (const c of coins) { if (balances[c.coinType] != undefined) { balances[c.coinType].balance += BigInt(c.totalBalance); } else { balances[c.coinType] = { symbol: getSymbolFromType(c.coinType), balance: BigInt(c.totalBalance), }; } } return balances; } } //# sourceMappingURL=query.js.map