@firefly-exchange/library-sui
Version:
Sui library housing helper methods, classes to interact with Bluefin protocol(s) deployed on Sui
294 lines (293 loc) • 10.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BCSUtils = exports.OverwriteUserAssets = exports.OverwriteUserPosition = exports.BatchTrades = exports.ClosePosition = exports.GasPool = exports.GasFee = exports.AccountType = exports.FeeTier = exports.PruneTable = exports.ApplyFundingRate = exports.MarketFundingRate = exports.SetFundingRate = exports.SyncOperator = exports.AdjustLeverage = exports.AdjustMargin = exports.Deleverage = exports.Liquidate = exports.TradeData = exports.Order = exports.authorizeLiquidator = exports.AuthorizeUser = exports.Withdrawal = exports.Signature = exports.PerpetualUpdate = exports.FundingRate = exports.SignedNumber = exports.AssetSupported = exports.Deposit = exports.bcs = void 0;
const bcs_1 = require("@mysten/sui/bcs");
Object.defineProperty(exports, "bcs", { enumerable: true, get: function () { return bcs_1.bcs; } });
const blv_1 = require("../../blv");
/// Deposit request type
exports.Deposit = bcs_1.bcs.struct("Deposit", {
eds_id: bcs_1.bcs.Address,
asset: bcs_1.bcs.string(),
from: bcs_1.bcs.Address,
to: bcs_1.bcs.Address,
amount: bcs_1.bcs.u64(),
nonce: bcs_1.bcs.u128(),
tainted: bcs_1.bcs.bool()
});
exports.AssetSupported = bcs_1.bcs.struct("AssetSupported", {
symbol: bcs_1.bcs.string(),
type: bcs_1.bcs.string(),
decimals: bcs_1.bcs.u8(),
weight: bcs_1.bcs.u64(),
price: bcs_1.bcs.u64(),
collateral: bcs_1.bcs.bool(),
min_deposit: bcs_1.bcs.u64(),
max_deposit: bcs_1.bcs.u64()
});
/// Signed Number
exports.SignedNumber = bcs_1.bcs.struct("SignedNumber", {
value: bcs_1.bcs.u64(),
sign: bcs_1.bcs.bool()
});
/// Signed Number
exports.FundingRate = bcs_1.bcs.struct("FundingRate", {
timestamp: bcs_1.bcs.u64(),
rate: exports.SignedNumber
});
exports.PerpetualUpdate = bcs_1.bcs.struct("PerpetualUpdate", {
id: bcs_1.bcs.Address,
symbol: bcs_1.bcs.string(),
imr: bcs_1.bcs.u64(),
mmr: bcs_1.bcs.u64(),
stepSize: bcs_1.bcs.u64(),
tickSize: bcs_1.bcs.u64(),
minTradeQty: bcs_1.bcs.u64(),
maxTradeQty: bcs_1.bcs.u64(),
minTradePrice: bcs_1.bcs.u64(),
maxTradePrice: bcs_1.bcs.u64(),
maxNotionalAtOpen: bcs_1.bcs.vector(bcs_1.bcs.u64()),
mtbLong: bcs_1.bcs.u64(),
mtbShort: bcs_1.bcs.u64(),
makerFee: bcs_1.bcs.u64(),
takerFee: bcs_1.bcs.u64(),
maxFundingRate: bcs_1.bcs.u64(),
insurancePoolRatio: bcs_1.bcs.u64(),
insurancePool: bcs_1.bcs.Address,
feePool: bcs_1.bcs.Address,
tradingStatus: bcs_1.bcs.bool(),
tradingStartTime: bcs_1.bcs.u64(),
delist: bcs_1.bcs.bool(),
delistingPrice: bcs_1.bcs.u64(),
isolatedOnly: bcs_1.bcs.bool(),
// off-chain config
baseAssetSymbol: bcs_1.bcs.string(),
baseAssetName: bcs_1.bcs.string(),
baseAssetDecimals: bcs_1.bcs.u64(),
maxLimitOrderQuantity: bcs_1.bcs.u64(),
maxMarketOrderQuantity: bcs_1.bcs.u64(),
defaultLeverage: bcs_1.bcs.u64(),
// @dev this will be zero when doing bcs serialization
oraclePrice: bcs_1.bcs.u64(),
// @dev This will be set as { timestamp: 0, value:0, sign: 0}
funding: exports.FundingRate
});
/// Signature type
exports.Signature = bcs_1.bcs.struct("Signature", {
sig: bcs_1.bcs.vector(bcs_1.bcs.u8()),
pk: bcs_1.bcs.vector(bcs_1.bcs.u8()),
type: bcs_1.bcs.u8()
});
/// Withdraw request type
exports.Withdrawal = bcs_1.bcs.struct("Withdrawal", {
eds: bcs_1.bcs.Address,
assetSymbol: bcs_1.bcs.string(),
account: bcs_1.bcs.Address,
amount: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
/// Authorize request type
exports.AuthorizeUser = bcs_1.bcs.struct("AuthorizeUser", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
user: bcs_1.bcs.Address,
status: bcs_1.bcs.bool(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
/// Authorize bankrupt liquidator type
exports.authorizeLiquidator = bcs_1.bcs.struct("authorizeLiquidator", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
authorized: bcs_1.bcs.bool(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
/// Order type
exports.Order = bcs_1.bcs.struct("Order", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
market: bcs_1.bcs.string(),
price: bcs_1.bcs.u64(),
quantity: bcs_1.bcs.u64(),
leverage: bcs_1.bcs.u64(),
side: bcs_1.bcs.string(),
positionType: bcs_1.bcs.string(),
expiration: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.TradeData = bcs_1.bcs.struct("TradeData", {
makerSignature: bcs_1.bcs.vector(bcs_1.bcs.u8()),
takerSignature: bcs_1.bcs.vector(bcs_1.bcs.u8()),
quantity: bcs_1.bcs.u64(),
timestamp: bcs_1.bcs.u64()
});
exports.Liquidate = bcs_1.bcs.struct("Liquidate", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
liquidatee: bcs_1.bcs.Address,
liquidator: bcs_1.bcs.Address,
market: bcs_1.bcs.string(),
quantity: bcs_1.bcs.u64(),
isolated: bcs_1.bcs.bool(),
assumeAsCross: bcs_1.bcs.bool(),
allOrNothing: bcs_1.bcs.bool(),
leverage: bcs_1.bcs.u64(),
expiry: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.Deleverage = bcs_1.bcs.struct("Deleverage", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
maker: bcs_1.bcs.Address,
taker: bcs_1.bcs.Address,
makerIsolated: bcs_1.bcs.bool(),
takerIsolated: bcs_1.bcs.bool(),
market: bcs_1.bcs.string(),
quantity: bcs_1.bcs.u64(),
expiry: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.AdjustMargin = bcs_1.bcs.struct("AdjustMargin", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
market: bcs_1.bcs.string(),
add: bcs_1.bcs.bool(),
amount: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.AdjustLeverage = bcs_1.bcs.struct("AdjustLeverage", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
market: bcs_1.bcs.string(),
leverage: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.SyncOperator = bcs_1.bcs.struct("SyncOperator", {
operatorType: bcs_1.bcs.string(),
previousOperator: bcs_1.bcs.Address,
newOperator: bcs_1.bcs.Address
});
exports.SetFundingRate = bcs_1.bcs.struct("SetFundingRate", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
timestamp: bcs_1.bcs.u64(),
marketFundingRates: bcs_1.bcs.vector(bcs_1.bcs.vector(bcs_1.bcs.u8())),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.MarketFundingRate = bcs_1.bcs.struct("MarketFundingRate", {
market: bcs_1.bcs.string(),
value: bcs_1.bcs.u64(),
sign: bcs_1.bcs.bool(),
oraclePrice: bcs_1.bcs.u64()
});
exports.ApplyFundingRate = bcs_1.bcs.struct("ApplyFundingRate", {
// the address of the Internal Data Store is to make the funding rate payload unique
// for local/dev/test and mainnet.
ids: bcs_1.bcs.Address,
timestamp: bcs_1.bcs.u64(),
accounts: bcs_1.bcs.vector(bcs_1.bcs.Address),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64(),
// market is an optional param, pass it as empty string "" when FR is to be applied for all markets
market: bcs_1.bcs.string()
});
exports.PruneTable = bcs_1.bcs.struct("PruneTable", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
hashes: bcs_1.bcs.vector(bcs_1.bcs.vector(bcs_1.bcs.u8())),
table_index: bcs_1.bcs.u8(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.FeeTier = bcs_1.bcs.struct("FeeTier", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
makerFee: bcs_1.bcs.u64(),
takerFee: bcs_1.bcs.u64(),
applied: bcs_1.bcs.bool(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.AccountType = bcs_1.bcs.struct("AccountType", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
isInstitution: bcs_1.bcs.bool(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.GasFee = bcs_1.bcs.struct("GasFee", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
amount: bcs_1.bcs.u64(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.GasPool = bcs_1.bcs.struct("GasPool", {
type: bcs_1.bcs.string(),
ids: bcs_1.bcs.Address,
pool: bcs_1.bcs.Address,
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.ClosePosition = bcs_1.bcs.struct("ClosePosition", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
market: bcs_1.bcs.string(),
isolated: bcs_1.bcs.bool(),
salt: bcs_1.bcs.u64(),
signedAt: bcs_1.bcs.u64()
});
exports.BatchTrades = bcs_1.bcs.struct("BatchTrades", {
makerOrders: bcs_1.bcs.vector(bcs_1.bcs.vector(bcs_1.bcs.u8())),
makerSignatures: bcs_1.bcs.vector(bcs_1.bcs.vector(bcs_1.bcs.u8())),
takerOrders: bcs_1.bcs.vector(bcs_1.bcs.vector(bcs_1.bcs.u8())),
takerSignatures: bcs_1.bcs.vector(bcs_1.bcs.vector(bcs_1.bcs.u8())),
fills: bcs_1.bcs.vector(bcs_1.bcs.u64())
});
exports.OverwriteUserPosition = bcs_1.bcs.struct("OverwriteUserPosition", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
perpetual: bcs_1.bcs.string(),
size: bcs_1.bcs.u64(),
average_entry_price: bcs_1.bcs.u64(),
leverage: bcs_1.bcs.u64(),
margin: bcs_1.bcs.u64(),
is_long: bcs_1.bcs.bool(),
is_isolated: bcs_1.bcs.bool(),
pending_funding_payment: bcs_1.bcs.u64(),
last_funding_timestamp: bcs_1.bcs.u64(),
last_funding_rate_value: bcs_1.bcs.u64(),
last_funding_rate_sign: bcs_1.bcs.bool(),
timestamp: bcs_1.bcs.u64()
});
exports.OverwriteUserAssets = bcs_1.bcs.struct("OverwriteUserAssets", {
ids: bcs_1.bcs.Address,
account: bcs_1.bcs.Address,
amount: bcs_1.bcs.u64(),
timestamp: bcs_1.bcs.u64()
});
class BCSUtils {
/// Returns serialized data hex string
static getSerializedDataBytes(data, dataType) {
return dataType.serialize(data).toBytes();
}
/// Returns serialized data hex string
static getSerializedDataHex(data, dataType) {
return dataType.serialize(data).toHex();
}
/// Deserializes a serialized data string (hex)
static deserializeData(data, dataType) {
return dataType.parse((0, blv_1.hexToUint8Array)(data));
}
}
exports.BCSUtils = BCSUtils;