client-aftermath-ts-sdk
Version:
Client Aftermath TypeScript SDK
150 lines (149 loc) • 6.63 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Router = void 0;
const caller_1 = require("../../general/utils/caller");
const transactions_1 = require("@mysten/sui/transactions");
const transactions_2 = require("@mysten/sui.js/transactions");
/**
* @class Router Provider
*
* @example
* ```
* // Create provider
* const router = (new Aftermath("MAINNET")).Router();
* // Call sdk
* const supportedCoins = await router.getSupportedCoins();
* ```
*/
class Router extends caller_1.Caller {
// =========================================================================
// Constructor
// =========================================================================
/**
* Creates `Router` provider to call api.
*
* @param network - The Sui network to interact with
* @returns New `Router` instance
*/
constructor(config) {
super(config, "router");
// =========================================================================
// Public Methods
// =========================================================================
// =========================================================================
// Inspections
// =========================================================================
/**
* Retrieves the total volume in the last 24 hours.
* @returns A Promise that resolves to a number representing the total volume.
*/
this.getVolume24hrs = () => __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("volume-24hrs");
});
}
getSupportedCoins() {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("supported-coins");
});
}
searchSupportedCoins(inputs, abortSignal) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi(`supported-coins/${inputs.filter}`, undefined, abortSignal);
});
}
/**
* Creates route across multiple pools and protocols for best trade execution price
*
* @param inputs - Details for router to construct trade route
* @param abortSignal - Optional signal to abort passed to fetch call
* @returns Routes, paths, and amounts of each smaller trade within complete trade
*/
getCompleteTradeRouteGivenAmountIn(inputs, abortSignal) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("trade-route", inputs, abortSignal);
});
}
/**
* Creates route across multiple pools and protocols for best trade execution price
*
* @param inputs - Details for router to construct trade route
* @param abortSignal - Optional signal to abort passed to fetch call
* @returns Routes, paths, and amounts of each smaller trade within complete trade
*/
getCompleteTradeRouteGivenAmountOut(inputs, abortSignal) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApi("trade-route", inputs, abortSignal);
});
}
// =========================================================================
// Transactions
// =========================================================================
getTransactionForCompleteTradeRoute(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiTransaction("transactions/trade", inputs);
});
}
getTransactionForCompleteTradeRouteV0(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiTransactionV0("transactions/trade-v0", inputs);
});
}
addTransactionForCompleteTradeRoute(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
const { tx: newTx, coinOutId } = yield this.fetchApi("transactions/add-trade", Object.assign(Object.assign({}, otherInputs), { serializedTx: tx.serialize() }));
return {
tx: transactions_1.Transaction.from(newTx),
coinOutId,
};
});
}
addTransactionForCompleteTradeRouteV0(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const { tx } = inputs, otherInputs = __rest(inputs, ["tx"]);
const { tx: newTx, coinOutId } = yield this.fetchApi("transactions/add-trade-v0", Object.assign(Object.assign({}, otherInputs), { serializedTx: tx.serialize() }));
return {
tx: transactions_2.TransactionBlock.from(newTx),
coinOutId,
};
});
}
// =========================================================================
// Events
// =========================================================================
getInteractionEvents(inputs) {
return __awaiter(this, void 0, void 0, function* () {
return this.fetchApiIndexerEvents("events-by-user", inputs);
});
}
}
exports.Router = Router;
// =========================================================================
// Constants
// =========================================================================
Router.constants = {
/**
* Max fee percentage that third parties can charge on router trades
*/
maxExternalFeePercentage: 0.5, // 50%
};