UNPKG

hadeswap-sdk-public

Version:

HadeSwap SDK for interacting with protocol

132 lines (131 loc) 6.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.nftToOrderMapState = exports.ordersInCartByPairMapState = exports.pairsInActionMapState = exports.getRelevantPairFiltered = exports.getPairsInAction = exports.getCart = exports.removeFromCart = exports.addToCart = void 0; const __1 = require(".."); const types_1 = require("../types"); const addToCart = (order_type, nft, targetPair, state) => { if (state.ordersInCartByPairMapState[nft]) { throw Error('this nft is already in cart'); } if (!state.pairsInActionMapState[targetPair.publicKey]) { state.pairsInActionMapState[targetPair.publicKey] = Object.assign(Object.assign({}, targetPair), { takenMints: [] }); } const pairInAction = state.pairsInActionMapState[targetPair.publicKey]; if (order_type === types_1.OrderType.Buy && pairInAction.nftsCount === 0) { throw Error('no buy orders to add to the cart'); } if (order_type === types_1.OrderType.Sell && pairInAction.buyOrdersQuantity === 0) { throw Error('no sell orders to add to the cart'); } if (pairInAction.takenMints.includes(nft)) { throw Error('this nft is taken in this pair: ' + nft); } const price = order_type === types_1.OrderType.Sell ? pairInAction.spotPrice : __1.helpers.next_spot_price({ orderType: types_1.OrderType.Buy, spot_price: pairInAction.spotPrice, delta: pairInAction.bondingCurve.delta, bondingCurveType: pairInAction.bondingCurve.bondingType, }); pairInAction.spotPrice = __1.helpers.next_spot_price({ orderType: order_type, spot_price: pairInAction.spotPrice, delta: pairInAction.bondingCurve.delta, bondingCurveType: pairInAction.bondingCurve.bondingType, }); pairInAction.nftsCount = order_type === types_1.OrderType.Buy ? pairInAction.nftsCount - 1 : pairInAction.nftsCount; pairInAction.buyOrdersQuantity = order_type === types_1.OrderType.Sell ? pairInAction.buyOrdersQuantity - 1 : pairInAction.buyOrdersQuantity; if (order_type === types_1.OrderType.Buy) { pairInAction.takenMints = [...pairInAction.takenMints, nft]; } const newOrder = { type: order_type, nftMint: nft, targetPairPukey: targetPair.publicKey, price, }; state.ordersInCartByPairMapState[targetPair.publicKey] = [ ...(state.ordersInCartByPairMapState[targetPair.publicKey] || []), newOrder, ]; state.nftToOrderMapState[nft] = newOrder; }; exports.addToCart = addToCart; const removeFromCart = (orderNftMint, state) => { const orderInCart = state.nftToOrderMapState[orderNftMint]; if (!orderInCart) { throw Error('this order doesnt exist'); } const pairInAction = state.pairsInActionMapState[orderInCart.targetPairPukey]; pairInAction.spotPrice = __1.helpers.next_spot_price({ orderType: orderInCart.type === types_1.OrderType.Buy ? types_1.OrderType.Sell : types_1.OrderType.Buy, spot_price: pairInAction.spotPrice, delta: pairInAction.bondingCurve.delta, bondingCurveType: pairInAction.bondingCurve.bondingType, }); pairInAction.nftsCount = orderInCart.type === types_1.OrderType.Buy ? pairInAction.nftsCount + 1 : pairInAction.nftsCount; pairInAction.buyOrdersQuantity = orderInCart.type === types_1.OrderType.Sell ? pairInAction.buyOrdersQuantity + 1 : pairInAction.buyOrdersQuantity; pairInAction.takenMints = [...pairInAction.takenMints].filter((mint) => mint === orderInCart.nftMint); const affectedMarketOrdersRaw = Object.values(exports.nftToOrderMapState).filter((order) => exports.pairsInActionMapState[order.targetPairPukey].hadoMarket === pairInAction.hadoMarket); // .filter( // (order) => order.nftMint === orderInCart.nftMint, // ); // removed buy order price 2 SOL // affectedOrders = [1, 1,5, (2SOL deleted), 2.5, 3] // removed sell order price 2 SOL // affectedOrders = [1, 1,5, (2SOL deleted), 2.5, 3] // ex removed buy order price 2 SOL // affectedOrders = [1, 1,5, 2.0, 2.5, 3, ,3.5, 4] if (orderInCart.type === types_1.OrderType.Buy) { const affectedOrdersRaw = state.ordersInCartByPairMapState[orderInCart.targetPairPukey]; const affectedSameOrdersSortedByPrice = affectedOrdersRaw .filter((order) => order.type === orderInCart.type) .sort((orderA, orderB) => orderA.price - orderB.price); const indexToRemove = affectedSameOrdersSortedByPrice.findIndex((order) => order.nftMint === orderInCart.nftMint); // 0 const highestOrder = affectedSameOrdersSortedByPrice[affectedSameOrdersSortedByPrice.length - 1]; // 1200000000 highestOrder.price = orderInCart.price; affectedSameOrdersSortedByPrice[indexToRemove] = highestOrder; affectedSameOrdersSortedByPrice.pop(); const notTouchedOrders = affectedOrdersRaw.filter((order) => order.type !== orderInCart.type); state.ordersInCartByPairMapState[orderInCart.targetPairPukey] = [ ...notTouchedOrders, ...affectedSameOrdersSortedByPrice, ]; delete state.nftToOrderMapState[orderInCart.nftMint]; } else if (orderInCart.type === types_1.OrderType.Sell) { const affectedMarketOrdersRaw = Object.values(state.nftToOrderMapState).filter((order) => state.pairsInActionMapState[order.targetPairPukey].hadoMarket === pairInAction.hadoMarket); const affectedSameOrdersSortedByPrice = affectedMarketOrdersRaw .filter((order) => order.type === orderInCart.type) .sort((orderA, orderB) => orderA.price - orderB.price); const lowestOrder = affectedSameOrdersSortedByPrice[0]; const indexToRemoveLowest = state.ordersInCartByPairMapState[lowestOrder.targetPairPukey].findIndex((order) => order.nftMint === lowestOrder.nftMint); state.ordersInCartByPairMapState[lowestOrder.targetPairPukey].splice(indexToRemoveLowest, 1); // lowestOrder.price = orderInCart.price; // lowestOrder.targetPairPukey = orderInCart.targetPairPukey; exports.nftToOrderMapState[orderInCart.nftMint] = orderInCart; orderInCart.nftMint = lowestOrder.nftMint; } }; exports.removeFromCart = removeFromCart; const getCart = (state) => { return Object.values(state.nftToOrderMapState); }; exports.getCart = getCart; const getPairsInAction = (state) => { return Object.values(state.pairsInActionMapState); }; exports.getPairsInAction = getPairsInAction; const getRelevantPairFiltered = (state) => { }; exports.getRelevantPairFiltered = getRelevantPairFiltered; // ['pairPubkey']: 'PairInAction', exports.pairsInActionMapState = {}; // ['pairPubkey']: 'OrderInCart[]', exports.ordersInCartByPairMapState = {}; // ['nft']: 'OrderInCart', exports.nftToOrderMapState = {};