@triadxyz/triad-protocol
Version:
<div align="center"> <h1>Triad Protocol</h1> </div>
241 lines (240 loc) • 9.88 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOrderStatus = exports.getOrderSide = exports.getOrderType = exports.getOrderSideFromNumber = exports.getOrderDirectionFromNumber = exports.getOrderDirection = exports.getTokenProgram = exports.calculateStakeRewards = exports.formatCustomer = exports.formatBookOrder = exports.formatOrder = exports.formatUserTrade = exports.formatMarket = exports.formatPool = exports.formatUnstake = exports.formatStake = exports.formatStakeVault = exports.decodeString = exports.encodeString = void 0;
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const types_1 = require("../types");
const constants_1 = require("./constants");
const encodeString = (value, alloc = 32) => {
const buffer = Buffer.alloc(alloc, 32);
buffer.write(value);
return Array(...buffer);
};
exports.encodeString = encodeString;
const decodeString = (bytes) => {
return Buffer.from(bytes).toString('utf8').trim();
};
exports.decodeString = decodeString;
const formatStakeVault = (stakeVault) => {
return {
name: stakeVault.name,
collection: stakeVault.collection,
slots: stakeVault.slots.toNumber(),
amount: stakeVault.amount.toNumber(),
isLocked: stakeVault.isLocked,
tokenMint: stakeVault.tokenMint.toBase58(),
tokenDecimals: stakeVault.tokenDecimals,
amountPaid: stakeVault.amountPaid.toNumber(),
nftStaked: stakeVault.nftStaked.toNumber(),
week: stakeVault.week,
initTs: stakeVault.initTs.toNumber(),
endTs: stakeVault.endTs.toNumber(),
authority: stakeVault.authority.toBase58(),
tokenStaked: stakeVault.tokenStaked.toNumber() / Math.pow(10, stakeVault.tokenDecimals)
};
};
exports.formatStakeVault = formatStakeVault;
const formatStake = (stake) => {
return {
authority: stake.authority.toBase58(),
initTs: stake.initTs.toNumber(),
checkedTs: stake.checkedTs.toNumber(),
claimed: stake.claimed.toNumber() / Math.pow(10, 6),
available: stake.available.toNumber() / Math.pow(10, 6),
amount: stake.amount.toNumber() / Math.pow(10, 6)
};
};
exports.formatStake = formatStake;
const formatUnstake = (unstake, address) => {
return {
address: address.toBase58(),
authority: unstake.authority.toBase58(),
amount: unstake.amount.toNumber() / Math.pow(10, 6),
withdrawTs: unstake.withdrawTs.toNumber()
};
};
exports.formatUnstake = formatUnstake;
const formatPool = (account, address) => {
return {
address: address.toString(),
id: account.id.toNumber(),
question: Buffer.from(account.question).toString().replace(/\0+$/, ''),
authority: account.authority.toString(),
isFast: account.isFast,
isFastMarketActive: account.isFastMarketActive
};
};
exports.formatPool = formatPool;
const formatMarket = (account, address) => {
return {
bump: account.bump,
address: address.toString(),
authority: account.authority.toString(),
marketId: account.marketId.toString(),
hypePrice: account.hypePrice.toString(),
flopPrice: account.flopPrice.toString(),
hypeLiquidity: account.hypeLiquidity.toString(),
flopLiquidity: account.flopLiquidity.toString(),
hypeShares: account.hypeShares.toString(),
flopShares: account.flopShares.toString(),
volume: account.volume.toString(),
mint: account.mint.toString(),
updateTs: account.updateTs.toString(),
resolvedTs: account.resolvedTs.toString(),
nextOrderId: account.nextOrderId.toString(),
feeBps: account.feeBps,
isAllowedToPayout: account.isAllowedToPayout,
marketStart: account.marketStart.toString(),
marketEnd: account.marketEnd.toString(),
question: Buffer.from(account.question).toString().replace(/\0+$/, ''),
payoutFeeAvailable: account.payoutFeeAvailable.toString(),
payoutFeeClaimed: account.payoutFeeClaimed.toString(),
marketFeeAvailable: account.marketFeeAvailable.toString(),
marketFeeClaimed: account.marketFeeClaimed.toString(),
winningDirection: types_1.WinningDirection[Object.keys(account.winningDirection)[0].toUpperCase()],
marketLiquidityAtStart: account.marketLiquidityAtStart.toNumber() > 0
? account.marketLiquidityAtStart.toString()
: '0',
payoutFee: account.payoutFee,
customer: account.customerId === 0 ? 'Triad' : account.customerId.toString(),
poolId: account.poolId.toNumber(),
feeRecipient: account.feeRecipient.toString()
};
};
exports.formatMarket = formatMarket;
const formatUserTrade = (account, publicKey) => {
return {
user: publicKey.toString(),
totalDeposits: account.totalDeposits.toString(),
totalWithdraws: account.totalWithdraws.toString(),
orders: account.orders.map((order) => (0, exports.formatOrder)(order, account.authority.toString())),
nonce: account.nonce.toString(),
poseidon: account.poseidon ? account.poseidon.toString() : '',
isSubUser: account.isSubUser
};
};
exports.formatUserTrade = formatUserTrade;
const formatOrder = (order, authority) => {
return {
ts: order.ts.toString(),
authority: authority ? authority : '',
userNonce: order.userNonce.toString(),
createdAt: order.createdAt ? order.createdAt.toString() : '',
orderId: order.orderId.toString(),
marketId: order.marketId.toString(),
orderStatus: (0, exports.getOrderStatus)(order.orderStatus),
orderType: (0, exports.getOrderType)(order.orderType),
orderDirection: (0, exports.getOrderDirection)(order.orderDirection),
orderSide: (0, exports.getOrderSide)(order.orderSide),
price: order.price.toString(),
totalShares: order.totalShares.toString(),
filledShares: order.filledShares ? order.filledShares.toString() : '',
isTrdPayout: order.isTrdPayout
};
};
exports.formatOrder = formatOrder;
const formatBookOrder = (order) => {
return {
id: order.id.toString(),
price: order.price.toString(),
totalShares: order.totalShares.toString(),
authority: new web3_js_1.PublicKey(order.authority).toString(),
filledShares: order.filledShares.toString(),
orderDirection: (0, exports.getOrderDirectionFromNumber)(order.orderDirection),
orderSide: (0, exports.getOrderSideFromNumber)(order.orderSide),
userNonce: order.userNonce.toString(),
linkedBookOrderId: order.linkedBookOrderId.toString()
};
};
exports.formatBookOrder = formatBookOrder;
const formatCustomer = (account, publicKey) => {
return {
id: account.id,
authority: publicKey.toString(),
name: account.name,
feeRecipient: account.feeRecipient.toString(),
feeBps: account.feeBps,
isVerified: account.isVerified,
address: publicKey.toString()
};
};
exports.formatCustomer = formatCustomer;
const calculateStakeRewards = (stake) => {
const maxRank = 1633;
const rank = 369;
const rankWeight = (maxRank - rank + 1) / maxRank;
const userStakedAmount = stake.amount;
const formattedAmount = userStakedAmount;
const adjustedAmount = formattedAmount * rankWeight;
const lastClaim = stake.checkedTs;
const currentTime = Math.floor(Date.now() / 1000);
const secondsStaked = currentTime - lastClaim;
const userBaseRewards = ((adjustedAmount / 10000) * secondsStaked) / 86400;
const rewards = userBaseRewards;
return rewards + stake.available;
};
exports.calculateStakeRewards = calculateStakeRewards;
const getTokenProgram = (mint) => {
if (mint.toBase58() === constants_1.TRD_MINT.toBase58()) {
return spl_token_1.TOKEN_2022_PROGRAM_ID;
}
return spl_token_1.TOKEN_PROGRAM_ID;
};
exports.getTokenProgram = getTokenProgram;
const getOrderDirection = (orderDirection) => {
if (Object.keys(orderDirection)[0] === 'hype') {
return types_1.OrderDirection.HYPE;
}
return types_1.OrderDirection.FLOP;
};
exports.getOrderDirection = getOrderDirection;
const getOrderDirectionFromNumber = (orderDirection) => {
if (orderDirection === 0) {
return types_1.OrderDirection.HYPE;
}
if (orderDirection === 1) {
return types_1.OrderDirection.FLOP;
}
};
exports.getOrderDirectionFromNumber = getOrderDirectionFromNumber;
const getOrderSideFromNumber = (orderSide) => {
if (orderSide === 0) {
return types_1.OrderSide.BID;
}
return types_1.OrderSide.ASK;
};
exports.getOrderSideFromNumber = getOrderSideFromNumber;
const getOrderType = (orderType) => {
if (Object.keys(orderType)[0] === 'limit') {
return types_1.OrderType.LIMIT;
}
return types_1.OrderType.MARKET;
};
exports.getOrderType = getOrderType;
const getOrderSide = (orderSide) => {
if (Object.keys(orderSide)[0] === 'bid') {
return types_1.OrderSide.BID;
}
return types_1.OrderSide.ASK;
};
exports.getOrderSide = getOrderSide;
const getOrderStatus = (status) => {
let currentStatus = Object.keys(status)[0];
switch (currentStatus) {
case 'init':
return types_1.OrderStatus.INIT;
case 'open':
return types_1.OrderStatus.OPEN;
case 'closed':
return types_1.OrderStatus.CLOSED;
case 'claimed':
return types_1.OrderStatus.CLAIMED;
case 'liquidated':
return types_1.OrderStatus.LIQUIDATED;
case 'waiting':
return types_1.OrderStatus.WAITING;
default:
throw new Error('Invalid order status');
}
};
exports.getOrderStatus = getOrderStatus;