UNPKG

whales-otc-pre-market-aptos-sdk

Version:

TypeScript SDK for Whales OTC Pre-Market trading on Aptos blockchain

295 lines (294 loc) 10.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.OTCPreMarketAptos = void 0; const ts_sdk_1 = require("@aptos-labs/ts-sdk"); const constants_1 = require("./constants"); const eventCrawler_1 = require("./eventCrawler"); const utils_1 = require("./utils"); class OTCPreMarketAptos { constructor(aptosConfig, graphQlConfig) { this.aptos = new ts_sdk_1.Aptos(new ts_sdk_1.AptosConfig({ network: aptosConfig?.network ?? ts_sdk_1.Network.MAINNET, fullnode: ts_sdk_1.NetworkToNodeAPI[aptosConfig?.network ?? ts_sdk_1.Network.MAINNET], indexer: ts_sdk_1.NetworkToIndexerAPI[aptosConfig?.network ?? ts_sdk_1.Network.MAINNET], faucet: ts_sdk_1.NetworkToFaucetAPI[aptosConfig?.network ?? ts_sdk_1.Network.MAINNET], pepper: ts_sdk_1.NetworkToPepperAPI[aptosConfig?.network ?? ts_sdk_1.Network.MAINNET], prover: ts_sdk_1.NetworkToProverAPI[aptosConfig?.network ?? ts_sdk_1.Network.MAINNET], ...aptosConfig, })); if (this.aptos.config.network === ts_sdk_1.Network.MAINNET) { this.packageId = constants_1.MAINNET.packageId; } else { this.packageId = constants_1.TESTNET.packageId; } this.eventCrawler = new eventCrawler_1.EventCrawler(this.aptos, this.packageId, graphQlConfig); } async getConfig() { const res = await this.aptos.view({ payload: { function: `${this.packageId}::otc_pre_market::get_config`, }, }); const configRaw = res; return { version: configRaw[0], owner: configRaw[1], feePercent: configRaw[2], feeWallet: configRaw[3], }; } async getOtcOffer(offerId) { const res = await this.aptos.view({ payload: { function: `${this.packageId}::otc_pre_market::get_otc_offer`, functionArguments: [offerId], }, }); const offerRaw = res; return { orderId: offerRaw[0], isBuyer: offerRaw[1], owner: offerRaw[2], exToken: offerRaw[3], value: offerRaw[4], deadline: offerRaw[5], status: offerRaw[6], }; } async getRole(user) { const res = await this.aptos.view({ payload: { function: `${this.packageId}::otc_pre_market::get_role`, functionArguments: [user], }, }); return res[0]; } async isOfferExpired(offerId) { const res = await this.aptos.view({ payload: { function: `${this.packageId}::otc_pre_market::is_offer_expired`, functionArguments: [offerId], }, }); return res[0]; } async buildUpdateConfig(owner, params) { const txData = { sender: owner, data: { function: `${this.packageId}::otc_pre_market::update_config`, functionArguments: [ new ts_sdk_1.MoveOption(params.feePercent ? new ts_sdk_1.U64(params.feePercent) : null), new ts_sdk_1.MoveOption(params.feeWallet ? ts_sdk_1.AccountAddress.from(params.feeWallet) : null), ], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildMigrate(owner) { const txData = { sender: owner, data: { function: `${this.packageId}::otc_pre_market::migrate`, functionArguments: [], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildAddRole(admin, user, role) { const txData = { sender: admin, data: { function: `${this.packageId}::otc_pre_market::add_role`, functionArguments: [user, role], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildRemoveRole(admin, user) { const txData = { sender: admin, data: { function: `${this.packageId}::otc_pre_market::remove_role`, functionArguments: [user], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildForceCancelOffer(operator, offerObj) { const txData = { sender: operator, data: { function: `${this.packageId}::otc_pre_market::force_cancel_offer`, functionArguments: [offerObj], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildForceCancelOffers(operator, offerObjs) { const txData = { sender: operator, data: { function: `${this.packageId}::otc_pre_market::force_cancel_offers`, functionArguments: [offerObjs.map((addr) => ts_sdk_1.AccountAddress.from(addr))], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildCreateOffer(user, params) { const txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::create_offer`, functionArguments: [ ts_sdk_1.AccountAddress.from(params.orderObj), params.isBuyer, ts_sdk_1.AccountAddress.from(params.exToken), new ts_sdk_1.U64(params.value), new ts_sdk_1.U64(params.deadline), ], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildBatchCreateOffer(user, params) { const txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::batch_create_offer`, functionArguments: [ params.orderObjs.map((addr) => ts_sdk_1.AccountAddress.from(addr)), params.isBuyers, params.exTokens.map((addr) => ts_sdk_1.AccountAddress.from(addr)), params.values.map((v) => new ts_sdk_1.U64(v)), params.deadlines.map((d) => new ts_sdk_1.U64(d)), ], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildUpdateOffer(user, params) { const txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::update_offer`, functionArguments: [ ts_sdk_1.AccountAddress.from(params.offerObj), new ts_sdk_1.U64(params.newValue), new ts_sdk_1.U64(params.newDeadline), ], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildFillOffer(user, offerObj) { const otcOfferData = await this.getOtcOffer(offerObj); const { useCoin, coinType } = await (0, utils_1.checkCoinToFa)(this.aptos, user, otcOfferData.exToken); let txData; if (!useCoin) { txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::fill_offer`, functionArguments: [ts_sdk_1.AccountAddress.from(offerObj)], }, }; } else { txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::fill_offer_with_coin`, functionArguments: [ts_sdk_1.AccountAddress.from(offerObj)], typeArguments: [coinType], }, }; } const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildCancelOffer(user, offerObj) { const txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::cancel_offer`, functionArguments: [ts_sdk_1.AccountAddress.from(offerObj)], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildCancelExpiredOffer(user, offerObj) { const txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::cancel_expired_offer`, functionArguments: [ts_sdk_1.AccountAddress.from(offerObj)], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } async buildCancelExpiredOffers(user, offerObjs) { const txData = { sender: user, data: { function: `${this.packageId}::otc_pre_market::cancel_expired_offers`, functionArguments: [offerObjs.map((addr) => ts_sdk_1.AccountAddress.from(addr))], }, }; const rawTx = await this.aptos.transaction.build.simple(txData); return { rawTx, txData: txData, }; } } exports.OTCPreMarketAptos = OTCPreMarketAptos;