@bayswap/sdk
Version:
SDK for BaySwap smart contract
128 lines • 4.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryModule = void 0;
const sui_js_1 = require("@mysten/sui.js");
const utils_1 = require("../utils");
const allTokenModules = ['btc', 'eth', 'bnb', 'usdt', 'dai'];
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 = (0, sui_js_1.getObjectId)(resp);
const obj = (0, sui_js_1.getMoveObject)(resp);
if (!(obj?.fields && obj?.type)) {
throw new Error(`pool ${poolId} is not found`);
}
const fields = obj.fields;
const { coinXType, coinYType, curveType } = (0, utils_1.parsingPoolTypes)(obj.type);
return {
id: poolId,
name: (0, utils_1.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: (0, utils_1.getSymbolFromType)(c.coinType),
balance: BigInt(c.totalBalance),
};
}
}
return balances;
}
}
exports.QueryModule = QueryModule;
//# sourceMappingURL=query.js.map