Skip to content

Commit

Permalink
correct market errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bt-cryptomancer committed Jul 7, 2024
1 parent bb3a519 commit aa27fe3
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 7 deletions.
120 changes: 114 additions & 6 deletions contracts/market.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* eslint-disable no-await-in-loop */
/* eslint-disable quote-props */
/* eslint-disable max-len */
/* eslint-disable object-curly-newline */
/* global actions, api */
const HIVE_PEGGED_SYMBOL = 'SWAP.HIVE';
const HIVE_PEGGED_SYMBOL_PRESICION = 8;
const CONTRACT_NAME = 'market';

// trading by these accounts is blocked
const ACCOUNT_BLACKLIST = {
'sunsetjesus': 1,
};

const getMetric = async (symbol) => {
let metric = await api.db.findOne('metrics', { symbol });

Expand Down Expand Up @@ -171,6 +177,90 @@ const updateTradesHistory = async (type, buyer, seller, symbol, quantity, price,

const countDecimals = value => api.BigNumber(value).dp();

const removeBadOrders = async () => {
let nbOrdersToDelete = 0;
let nbOrdersDeleted = 0;
let ordersToDelete = await api.db.find(
'buyBook',
{
tokensLocked: {
$in: ['0.00000000'],
},
},
1000,
0,
[{ index: '_id', descending: false }],
);

nbOrdersToDelete = ordersToDelete.length;
while (nbOrdersToDelete > 0 && nbOrdersDeleted < 5000) {
for (let index = 0; index < nbOrdersToDelete; index += 1) {
nbOrdersDeleted += 1;
const order = ordersToDelete[index];

await api.db.remove('buyBook', order);
await updateBidMetric(order.symbol);
}

ordersToDelete = await api.db.find(
'buyBook',
{
tokensLocked: {
$in: ['0.00000000'],
},
},
1000,
0,
[{ index: '_id', descending: false }],
);

nbOrdersToDelete = ordersToDelete.length;
}
};

const removeBlacklistedOrders = async (type, targetAccount) => {
const table = type === 'buy' ? 'buyBook' : 'sellBook';
let nbOrdersToDelete = 0;
let nbOrdersDeleted = 0;
let ordersToDelete = await api.db.find(
table,
{
account: targetAccount,
},
1000,
0,
[{ index: '_id', descending: false }],
);

nbOrdersToDelete = ordersToDelete.length;
while (nbOrdersToDelete > 0 && nbOrdersDeleted < 5000) {
for (let index = 0; index < nbOrdersToDelete; index += 1) {
nbOrdersDeleted += 1;
const order = ordersToDelete[index];

await api.db.remove(table, order);

if (type === 'sell') {
await updateAskMetric(order.symbol);
} else {
await updateBidMetric(order.symbol);
}
}

ordersToDelete = await api.db.find(
table,
{
account: targetAccount,
},
1000,
0,
[{ index: '_id', descending: false }],
);

nbOrdersToDelete = ordersToDelete.length;
}
};

const removeExpiredOrders = async (table) => {
const timestampSec = api.BigNumber(new Date(`${api.hiveBlockTimestamp}.000Z`).getTime())
.dividedBy(1000)
Expand Down Expand Up @@ -242,12 +332,10 @@ actions.createSSC = async () => {
await api.db.createTable('tradesHistory', ['symbol']);
await api.db.createTable('metrics', ['symbol']);
} else {
// remove stuck 0 quantity orders
let order = await api.db.findOne('buyBook', { txId: 'MM-P-33520024-80518873-3-9-0' });
if (order) {
await api.db.remove('buyBook', order);
await updateBidMetric(order.symbol);
}
// remove stuck 0 quantity orders and any that have been blacklisted
await removeBadOrders();
await removeBlacklistedOrders('buy', 'sunsetjesus');
await removeBlacklistedOrders('sell', 'sunsetjesus');
}
};

Expand Down Expand Up @@ -724,6 +812,11 @@ actions.buy = async (payload) => {
const finalAccount = (account === undefined || api.sender !== 'null') ? api.sender : account;
const finalTxId = (txId === undefined || api.sender !== 'null') ? api.transactionId : txId;

// ignore any actions coming from blacklisted accounts
if (ACCOUNT_BLACKLIST[finalAccount] === 1) {
return;
}

// buy (quantity) of (symbol) at (price)(HIVE_PEGGED_SYMBOL) per (symbol)
if (api.assert(isSignedWithActiveKey === true || api.sender === 'null', 'you must use a custom_json signed with your active key')
&& api.assert(price && typeof price === 'string' && !api.BigNumber(price).isNaN()
Expand Down Expand Up @@ -794,6 +887,11 @@ actions.sell = async (payload) => {
const finalAccount = (account === undefined || api.sender !== 'null') ? api.sender : account;
const finalTxId = (txId === undefined || api.sender !== 'null') ? api.transactionId : txId;

// ignore any actions coming from blacklisted accounts
if (ACCOUNT_BLACKLIST[finalAccount] === 1) {
return;
}

// sell (quantity) of (symbol) at (price)(HIVE_PEGGED_SYMBOL) per (symbol)
if (api.assert(isSignedWithActiveKey === true || api.sender === 'null', 'you must use a custom_json signed with your active key')
&& api.assert(price && typeof price === 'string' && !api.BigNumber(price).isNaN()
Expand Down Expand Up @@ -857,6 +955,11 @@ actions.marketBuy = async (payload) => {

const finalAccount = (account === undefined || api.sender !== 'null') ? api.sender : account;

// ignore any actions coming from blacklisted accounts
if (ACCOUNT_BLACKLIST[finalAccount] === 1) {
return;
}

if (api.assert(isSignedWithActiveKey === true || api.sender === 'null', 'you must use a custom_json signed with your active key')
&& symbol && typeof symbol === 'string' && symbol !== HIVE_PEGGED_SYMBOL
&& quantity && typeof quantity === 'string' && !api.BigNumber(quantity).isNaN() && api.BigNumber(quantity).gt(0)) {
Expand Down Expand Up @@ -1043,6 +1146,11 @@ actions.marketSell = async (payload) => {

const finalAccount = (account === undefined || api.sender !== 'null') ? api.sender : account;

// ignore any actions coming from blacklisted accounts
if (ACCOUNT_BLACKLIST[finalAccount] === 1) {
return;
}

if (api.assert(isSignedWithActiveKey === true || api.sender === 'null', 'you must use a custom_json signed with your active key')
&& symbol && typeof symbol === 'string' && symbol !== HIVE_PEGGED_SYMBOL
&& quantity && typeof quantity === 'string' && !api.BigNumber(quantity).isNaN() && api.BigNumber(quantity).gt(0)) {
Expand Down
19 changes: 18 additions & 1 deletion test/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,9 +872,11 @@ describe('Market', function() {
transactions.push(new Transaction(refBlockNumber, fixture.getNextTxId(), CONSTANTS.HIVE_ENGINE_ACCOUNT, 'tokens', 'transfer', `{ "symbol": "${CONSTANTS.UTILITY_TOKEN_SYMBOL}", "to": "harpagon", "quantity": "1000", "isSignedWithActiveKey": true }`));
transactions.push(new Transaction(refBlockNumber, fixture.getNextTxId(), CONSTANTS.HIVE_ENGINE_ACCOUNT, 'tokens', 'create', '{ "isSignedWithActiveKey": true, "name": "token", "url": "https://token.com", "symbol": "TKN.TEST", "precision": 5, "maxSupply": "1000", "isSignedWithActiveKey": true }'));
transactions.push(new Transaction(refBlockNumber, fixture.getNextTxId(), CONSTANTS.HIVE_PEGGED_ACCOUNT, 'tokens', 'transfer', '{ "symbol": "SWAP.HIVE", "to": "satoshi", "quantity": "123.456", "isSignedWithActiveKey": true }'));
transactions.push(new Transaction(refBlockNumber, fixture.getNextTxId(), CONSTANTS.HIVE_PEGGED_ACCOUNT, 'tokens', 'transfer', '{ "symbol": "SWAP.HIVE", "to": "sunsetjesus", "quantity": "123.456", "isSignedWithActiveKey": true }'));
transactions.push(new Transaction(refBlockNumber, 'TXID1235', 'satoshi', 'market', 'buy', '{ "symbol": "TKN.TEST", "quantity": "1", "price": "0.00000001", "expiration": 2592000, "isSignedWithActiveKey": true }'));
transactions.push(new Transaction(refBlockNumber, 'TXID1236', 'satoshi', 'market', 'buy', '{ "symbol": "TKN.TEST", "quantity": "2", "price": "0.00000001", "expiration": 10, "isSignedWithActiveKey": true }'));
transactions.push(new Transaction(refBlockNumber, 'TXID1237', 'satoshi', 'market', 'buy', '{ "symbol": "TKN.TEST", "quantity": "3", "price": "0.00000001", "expiration": 30000000, "isSignedWithActiveKey": true }'));
transactions.push(new Transaction(refBlockNumber, 'TXID1238', 'sunsetjesus', 'market', 'buy', '{ "symbol": "TKN.TEST", "quantity": "4", "price": "0.00000001", "expiration": 30000000, "isSignedWithActiveKey": true }'));

let block = {
refHiveBlockNumber: refBlockNumber,
Expand All @@ -890,7 +892,6 @@ describe('Market', function() {
contract: 'market',
table: 'buyBook',
query: {
account: 'satoshi',
symbol: 'TKN.TEST'
}
});
Expand Down Expand Up @@ -919,6 +920,22 @@ describe('Market', function() {
assert.equal(buyOrders[2].timestamp, 1527811200);
assert.equal(buyOrders[2].expiration, 1527811200 + 2592000);

// the order from sunsetjesus should be ignored as this account is on the blacklist
assert.equal(buyOrders.length, 3);

// no tokens should have left the blacklisted account
const accountBalances = await fixture.database.find({
contract: 'tokens',
table: 'balances',
query: {
account: 'sunsetjesus',
symbol: 'SWAP.HIVE'
}
});

assert.equal(accountBalances.length, 1);
assert.equal(accountBalances[0].balance, '123.456');

resolve();
})
.then(() => {
Expand Down

0 comments on commit aa27fe3

Please sign in to comment.