whales-pre-market-aptos-sdk
Version:
TypeScript SDK for Whales Pre-Market on Aptos blockchain
499 lines (498 loc) • 17.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PreMarketAptos = 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 PreMarketAptos {
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}::whales_pre_market::get_config`,
},
});
const configRaw = res;
return {
version: configRaw[0],
owner: configRaw[1],
discountVerifierPubkey: configRaw[2].bytes,
pledgeRate: configRaw[3],
feeSettle: configRaw[4],
feeRefund: configRaw[5],
feeWallet: configRaw[6],
currentTokenIndex: configRaw[7],
};
}
async getTokenConfig(tokenConfigId) {
const res = await this.aptos.view({
payload: {
function: `${this.packageId}::whales_pre_market::get_token_config`,
functionArguments: [tokenConfigId],
},
});
const tokenConfigRaw = res;
return {
index: tokenConfigRaw[0],
settleTime: tokenConfigRaw[1],
settleDuration: tokenConfigRaw[2],
status: tokenConfigRaw[3],
settleRate: tokenConfigRaw[4],
token: tokenConfigRaw[5]?.vec[0] || null,
};
}
async getOffer(offerId) {
const res = await this.aptos.view({
payload: {
function: `${this.packageId}::whales_pre_market::get_offer`,
functionArguments: [offerId],
},
});
const offerRaw = res;
return {
tokenId: offerRaw[0],
exToken: offerRaw[1],
exTokenStore: offerRaw[2],
offerType: offerRaw[3],
status: offerRaw[4],
amount: offerRaw[5],
value: offerRaw[6],
filledAmount: offerRaw[7],
collateral: offerRaw[8],
fullMatch: offerRaw[9],
owner: offerRaw[10],
};
}
async getOrder(orderId) {
const res = await this.aptos.view({
payload: {
function: `${this.packageId}::whales_pre_market::get_order`,
functionArguments: [orderId],
},
});
const orderRaw = res;
return {
tokenId: orderRaw[0],
offerId: orderRaw[1],
exToken: orderRaw[2],
exTokenStore: orderRaw[3],
status: orderRaw[4],
amount: orderRaw[5],
value: orderRaw[6],
collateral: orderRaw[7],
buyer: orderRaw[8],
seller: orderRaw[9],
};
}
async getRole(user) {
const res = await this.aptos.view({
payload: {
function: `${this.packageId}::whales_pre_market::get_role`,
functionArguments: [user],
},
});
return res[0];
}
async isTokenAccepted(token) {
const res = await this.aptos.view({
payload: {
function: `${this.packageId}::whales_pre_market::is_token_accepted`,
functionArguments: [token],
},
});
return res[0];
}
async buildUpdateConfig(owner, params) {
const txData = {
sender: owner,
data: {
function: `${this.packageId}::whales_pre_market::update_config`,
functionArguments: [
new ts_sdk_1.MoveOption(params.discountVerifierPubkey
? ts_sdk_1.MoveVector.U8(params.discountVerifierPubkey)
: null),
new ts_sdk_1.MoveOption(params.pledgeRate ? new ts_sdk_1.U64(params.pledgeRate) : null),
new ts_sdk_1.MoveOption(params.feeSettle ? new ts_sdk_1.U64(params.feeSettle) : null),
new ts_sdk_1.MoveOption(params.feeRefund ? new ts_sdk_1.U64(params.feeRefund) : 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}::whales_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}::whales_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}::whales_pre_market::remove_role`,
functionArguments: [user],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildAcceptExToken(operator, token) {
const txData = {
sender: operator,
data: {
function: `${this.packageId}::whales_pre_market::accept_ex_token`,
functionArguments: [token],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildRemoveExToken(operator, token) {
const txData = {
sender: operator,
data: {
function: `${this.packageId}::whales_pre_market::remove_ex_token`,
functionArguments: [token],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildCreateToken(admin, tokenIndex, settleDuration) {
const txData = {
sender: admin,
data: {
function: `${this.packageId}::whales_pre_market::create_token`,
functionArguments: [tokenIndex.toString(), settleDuration.toString()],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildTokenToSettlePhase(operator, tokenConfig, token, settleRate) {
const txData = {
sender: operator,
data: {
function: `${this.packageId}::whales_pre_market::token_to_settle_phase`,
functionArguments: [tokenConfig, token, settleRate.toString()],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildUpdateSettleDuration(operator, tokenConfig, newDuration) {
const txData = {
sender: operator,
data: {
function: `${this.packageId}::whales_pre_market::update_settle_duration`,
functionArguments: [tokenConfig, newDuration.toString()],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildTokenForceCancelSettlePhase(admin, tokenConfig) {
const txData = {
sender: admin,
data: {
function: `${this.packageId}::whales_pre_market::token_force_cancel_settle_phase`,
functionArguments: [tokenConfig],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildCancelToken(admin, tokenConfig) {
const txData = {
sender: admin,
data: {
function: `${this.packageId}::whales_pre_market::cancel_token`,
functionArguments: [tokenConfig],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildToggleTokenStatus(admin, tokenConfig) {
const txData = {
sender: admin,
data: {
function: `${this.packageId}::whales_pre_market::toggle_token_status`,
functionArguments: [tokenConfig],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildForceCancelOrder(operator, order) {
const txData = {
sender: operator,
data: {
function: `${this.packageId}::whales_pre_market::force_cancel_order`,
functionArguments: [order],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildForceCancelOrders(operator, orderList) {
const txData = {
sender: operator,
data: {
function: `${this.packageId}::whales_pre_market::force_cancel_orders`,
functionArguments: [orderList],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildCreateOffer(sender, tokenConfig, exToken, offerType, amount, value, fullMatch) {
const { useCoin, coinType } = await (0, utils_1.checkCoinToFa)(this.aptos, sender, exToken.toString());
let txData;
if (useCoin) {
txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::create_offer_with_coin`,
functionArguments: [
tokenConfig,
exToken,
offerType,
amount.toString(),
value.toString(),
fullMatch,
],
typeArguments: [coinType],
},
};
}
else {
txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::create_offer`,
functionArguments: [
tokenConfig,
exToken,
offerType,
amount.toString(),
value.toString(),
fullMatch,
],
},
};
}
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildFillOffer(sender, offer, amount) {
const offerData = await this.getOffer(offer);
const { useCoin, coinType } = await (0, utils_1.checkCoinToFa)(this.aptos, sender, offerData.exToken);
let txData;
if (useCoin) {
txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::fill_offer_with_coin`,
functionArguments: [offer, amount.toString()],
typeArguments: [coinType],
},
};
}
else {
txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::fill_offer`,
functionArguments: [offer, amount.toString()],
},
};
}
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildCancelOffer(sender, offer) {
const txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::cancel_offer`,
functionArguments: [offer],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildCancelOfferWithDiscount(sender, offer, discount) {
const txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::cancel_offer_with_discount`,
functionArguments: [
offer,
discount.buyerDiscount.toString(),
discount.sellerDiscount.toString(),
discount.signature,
],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildSettleFilled(sender, order) {
const txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::settle_filled`,
functionArguments: [order],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildSettleFilledWithDiscount(sender, order, discount) {
const txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::settle_filled_with_discount`,
functionArguments: [
order,
discount.buyerDiscount.toString(),
discount.sellerDiscount.toString(),
discount.signature,
],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildSettleCancelled(sender, order) {
const txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::settle_cancelled`,
functionArguments: [order],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
async buildSettleCancelledWithDiscount(sender, order, discount) {
const txData = {
sender: sender,
data: {
function: `${this.packageId}::whales_pre_market::settle_cancelled_with_discount`,
functionArguments: [
order,
discount.buyerDiscount.toString(),
discount.sellerDiscount.toString(),
discount.signature,
],
},
};
const rawTx = await this.aptos.transaction.build.simple(txData);
return {
rawTx,
txData: txData,
};
}
}
exports.PreMarketAptos = PreMarketAptos;