Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/order/structure/getMakerTakerOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ async function getMakerTakerOrders(api, order) {
* Remainder of the current order
* @type {Number}
*/
const remainder = orders.reduce((result, o) => result -= fromBaseUnits(o.contract.amount, order.asset.decimals), order.amount);
const remainder = orders[0]?.restrictedOrder ? 0 : orders.reduce((result, o) => result -= fromBaseUnits(o.contract.amount, order.asset.decimals), order.amount);

const assetPrecision = 1/(10 ** order.asset.decimals);

if (orders.length > 0 && orders[0].type === 'sell') {
if (orders.length > 0 ) {
if (remainder * order.price < 0.500001) {
return orders; // This is to prevent placing a maker buy order with a total amount less than 0.5 algo
}
Expand All @@ -107,7 +107,7 @@ async function getMakerTakerOrders(api, order) {
price: originalPrice,
execution: 'maker',
amount: remainder,
total: remainder * order.price,
total: remainder * originalPrice + (order.type === 'buy'? 0.001 : 0), // for sell orders the fee is taken out of the 0.5 algo supplied
appId: await api.getAppId(order),
indexer: api.indexer,
})),
Expand Down
38 changes: 35 additions & 3 deletions lib/order/structure/taker/getCutTakerOrders.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,6 @@ function updateTakerBalance(queuedOrder, takerOrderBalance, isAsaEscrow) {
}


takerOrderBalance['algoBalance'] -= executionFees;
takerOrderBalance['algoBalance'] -= algoTradeAmount;
takerOrderBalance['walletAlgoBalance'] -= executionFees;
takerOrderBalance['walletAlgoBalance'] -= algoTradeAmount;
Expand All @@ -380,15 +379,14 @@ function updateTakerBalance(queuedOrder, takerOrderBalance, isAsaEscrow) {

return takerOrderBalance;
} else {
const {algoAmountReceiving, asaAmountSending, txnFee} =
const {algoAmountReceiving, asaAmountSending} =
getExecuteAlgoOrderTakerTxnAmounts(queuedOrder, takerOrderBalance);

if (algoAmountReceiving === 0) {
console.debug('algoAmountReceiving is 0, nothing to do, returning early');
return 'escrowEmpty';
}

takerOrderBalance['algoBalance'] -= txnFee;
takerOrderBalance['algoBalance'] += algoAmountReceiving;
takerOrderBalance['asaBalance'] -= asaAmountSending;
takerOrderBalance['asaAmountSending'] = asaAmountSending;
Expand Down Expand Up @@ -584,6 +582,28 @@ function getSplitTimesByIter(queueOrder, loopIndex) {
* @property {string} orderEntry
* @ignore
*/


/**
*
* @param {*} takerOrderBalance
* @param {*} escrowOrderBalance
* @return {Boolean}
* @ignore
*/
function shouldRestrictOrder(takerOrderBalance, escrowOrderBalance) {
const queuedOrderAlgoBalance =
escrowOrderBalance.asaBalance * escrowOrderBalance.price;
const takerOrderTotalAlgoBalance =
( takerOrderBalance?.limitPrice + 0.0000009999999) * takerOrderBalance?.asaBalance;

// escrowOrderBalance.isASAEscrow
return escrowOrderBalance.isASAEscrow ?
takerOrderBalance.algoBalance < queuedOrderAlgoBalance && // If the taker isn't closing out the account
queuedOrderAlgoBalance - takerOrderBalance.algoBalance <= 500000 : // and the remainder of the partial order is less than 0.5 algo
takerOrderTotalAlgoBalance < escrowOrderBalance.algoBalance &&
escrowOrderBalance.algoBalance - takerOrderTotalAlgoBalance <= 500000;
}
/**
* ## ✉ getCutTakerOrders
* Composes taker orders and returns an Object with relevant splitTaker Txns
Expand Down Expand Up @@ -633,6 +653,9 @@ async function getCutTakerOrders(api, order, queuedOrders, takerOrderBalance) {
break;
}

const restrictOrder = shouldRestrictOrder(takerOrderBalance, queuedOrders[i]);


const {cutOrder, splitTimes} = getSplitTimesByIter(queuedOrders[i], i);

let runningBalance = getRunningBalance(queuedOrders[i]);
Expand All @@ -649,6 +672,15 @@ async function getCutTakerOrders(api, order, queuedOrders, takerOrderBalance) {
queuedOrders[i].isASAEscrow,
);

// TO get the original price we either add or subtract the amount that we padded the price (We padded the price to execute on escrow orders that have more than six decimal places)
const restrictIteration = (runningBalance - cutOrder.cutOrderAmount) * (order.type === 'buy' ? order.price - 0.0000009999999 : order.price + 0.0000009999999) <= 500000;

if (restrictOrder && restrictIteration) {
console.log('Returning early to keep escrow open with an amount greater than 0.5 algo');
allTransList[0].restrictedOrder = true;
return allTransList;
}

const cutQueuedOrderObject = {
queuedOrder: queuedOrders[i],
cutOrder: cutOrder,
Expand Down