@astonic-io/astonic-sdk
Version:
Official SDK for interacting with the Astonic Protocol
131 lines (130 loc) • 5.69 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLimitId = exports.getLimits = exports.getLimitsState = exports.getLimitsConfig = void 0;
const assert_1 = require("assert");
const ethers_1 = require("ethers");
/**
* Returns the limit configuration in the broker for the given exchange and asset
* @param broker an instance of the broker
* @param exchangeId the id of the exchange
* @param asset the address of the limited asset
* @returns the limit configuration
*/
function getLimitsConfig(broker, exchangeId, asset) {
return __awaiter(this, void 0, void 0, function* () {
const limitId = getLimitId(exchangeId, asset);
const cfg = yield broker.tradingLimitsConfig(limitId);
return {
asset,
timestep0: cfg['timestep0'],
timestep1: cfg['timestep1'],
limit0: cfg['limit0'],
limit1: cfg['limit1'],
limitGlobal: cfg['limitGlobal'],
flags: cfg['flags'],
};
});
}
exports.getLimitsConfig = getLimitsConfig;
/**
* Returns the limit state in the broker for the given exchange and asset
* @param broker an instance of the broker
* @param exchangeId the id of the exchange
* @param asset the address of the limited asset
* @returns the limit state
*/
function getLimitsState(broker, exchangeId, asset) {
return __awaiter(this, void 0, void 0, function* () {
const limitId = getLimitId(exchangeId, asset);
const [cfg, state] = yield Promise.all([
getLimitsConfig(broker, exchangeId, asset),
broker.tradingLimitsState(limitId),
]);
const isL0Enabled = cfg.timestep0 > 0;
const isL1Enabled = cfg.timestep1 > 0;
const nowEpoch = Math.floor(Date.now() / 1000);
const isL0Outdated = isL0Enabled && nowEpoch > state['lastUpdated0'] + cfg.timestep0;
const isL1Outdated = isL1Enabled && nowEpoch > state['lastUpdated1'] + cfg.timestep1;
return {
asset,
lastUpdated0: isL0Outdated ? nowEpoch : state['lastUpdated0'],
lastUpdated1: isL1Outdated ? nowEpoch : state['lastUpdated1'],
netflow0: isL0Outdated ? 0 : state['netflow0'],
netflow1: isL1Outdated ? 0 : state['netflow1'],
netflowGlobal: state['netflowGlobal'],
};
});
}
exports.getLimitsState = getLimitsState;
/**
* Returns a human-friendly representation of the limits for the given exchange and asset
* @param broker an instance of the broker
* @param exchangeId the id of the exchange
* @param asset the address of the asset with the limit
* @returns a list of TradingLimit objects
*/
function getLimits(broker, exchangeId, asset) {
return __awaiter(this, void 0, void 0, function* () {
const [cfg, state] = yield Promise.all([
getLimitsConfig(broker, exchangeId, asset),
getLimitsState(broker, exchangeId, asset),
]);
const limits = [];
if (cfg.limit0 > 0) {
limits.push({
asset: asset,
maxIn: cfg.limit0 - state.netflow0,
maxOut: cfg.limit0 + state.netflow0,
until: state.lastUpdated0 + cfg.timestep0,
});
}
if (cfg.limit1 > 0) {
limits.push({
asset: asset,
maxIn: cfg.limit1 - state.netflow1,
maxOut: cfg.limit1 + state.netflow1,
until: state.lastUpdated1 + cfg.timestep1,
});
}
if (cfg.limitGlobal > 0) {
const timestampIn2030 = 1893456000; // a far away timestamp
limits.push({
asset: asset,
maxIn: cfg.limitGlobal - state.netflowGlobal,
maxOut: cfg.limitGlobal + state.netflowGlobal,
until: timestampIn2030,
});
}
// Limits with a smaller timeframe are restricted by the ones with a larger one
// e.g: if maxIn is 0 in LG, it should also be 0 in L1 and L0
for (let i = limits.length - 1; i > 0; i--) {
limits[i - 1].maxIn = Math.min(limits[i - 1].maxIn, limits[i].maxIn);
limits[i - 1].maxOut = Math.min(limits[i - 1].maxOut, limits[i].maxOut);
}
return limits;
});
}
exports.getLimits = getLimits;
/**
* Returns the limit id for the given exchange and asset
* @param exchangeId the id of the exchange
* @param asset the address of the asset with the limit
* @returns the limit id
*/
function getLimitId(exchangeId, asset) {
const assetBytes32 = ethers_1.utils.zeroPad(asset, 32);
const exchangeIdBytes = ethers_1.utils.arrayify(exchangeId);
const assetBytes = ethers_1.utils.arrayify(assetBytes32);
(0, assert_1.strict)(exchangeIdBytes.length === assetBytes.length, 'exchangeId and asset0 must be the same length');
return ethers_1.utils.hexlify(exchangeIdBytes.map((b, i) => b ^ assetBytes[i]));
}
exports.getLimitId = getLimitId;