Skip to content

Commit

Permalink
Merge pull request #194 from jmicko/fix-duplicate-order-id-conflict
Browse files Browse the repository at this point in the history
Fix a bug that was preventing failed order from saving when fixed
  • Loading branch information
jmicko authored Oct 31, 2024
2 parents dd950c1 + 6ddcb6c commit cd720a1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/modules/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ class Messenger {
const messages = await getAllMessages(this.userID);
const botMessages = await getBotMessages(this.userID);
const chatMessages = await getChatMessages(this.userID);
console.log(messages, 'all messages from database', this.userID);
// console.log(messages, 'all messages from database', this.userID);
// console.log(messages, 'all messages from database', this.userID);
// clear the messages array and add the messages from the database
this.messages.length = 0;
Expand Down
40 changes: 39 additions & 1 deletion server/modules/databaseClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,45 @@ export const storeTrade = (newOrder, originalDetails, flipped_at) => {
"reject_message",
"cancel_message"
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37);`;
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37)
ON CONFLICT ("order_id") DO UPDATE SET
"order_id" = EXCLUDED."order_id",
"userID" = EXCLUDED."userID",
"original_buy_price" = EXCLUDED."original_buy_price",
"original_sell_price" = EXCLUDED."original_sell_price",
"trade_pair_ratio" = EXCLUDED."trade_pair_ratio",
"flipped_at" = EXCLUDED."flipped_at",
"reorder" = EXCLUDED."reorder",
"product_id" = EXCLUDED."product_id",
"coinbase_user_id" = EXCLUDED."coinbase_user_id",
"base_size" = EXCLUDED."base_size",
"limit_price" = EXCLUDED."limit_price",
"post_only" = EXCLUDED."post_only",
"side" = EXCLUDED."side",
"client_order_id" = EXCLUDED."client_order_id",
"next_client_order_id" = EXCLUDED."next_client_order_id",
"status" = EXCLUDED."status",
"time_in_force" = EXCLUDED."time_in_force",
"created_time" = EXCLUDED."created_time",
"completion_percentage" = EXCLUDED."completion_percentage",
"filled_size" = EXCLUDED."filled_size",
"average_filled_price" = EXCLUDED."average_filled_price",
"fee" = EXCLUDED."fee",
"number_of_fills" = EXCLUDED."number_of_fills",
"filled_value" = EXCLUDED."filled_value",
"pending_cancel" = EXCLUDED."pending_cancel",
"size_in_quote" = EXCLUDED."size_in_quote",
"total_fees" = EXCLUDED."total_fees",
"previous_total_fees" = EXCLUDED."previous_total_fees",
"size_inclusive_of_fees" = EXCLUDED."size_inclusive_of_fees",
"total_value_after_fees" = EXCLUDED."total_value_after_fees",
"trigger_status" = EXCLUDED."trigger_status",
"order_type" = EXCLUDED."order_type",
"reject_reason" = EXCLUDED."reject_reason",
"settled" = EXCLUDED."settled",
"product_type" = EXCLUDED."product_type",
"reject_message" = EXCLUDED."reject_message",
"cancel_message" = EXCLUDED."cancel_message";`;
try {
const results = await pool.query(sqlText, [
newOrder.order_id,
Expand Down
27 changes: 21 additions & 6 deletions server/modules/robot.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ async function initializeUserLoops(user) {
try {
// set up cache for user
await userStorage.createNewUser(user);
// update funds if the user is all of the above except for maintenance
user = userStorage.getUser(userID);
devLog(user, '<- user while init loops')
await sleep(10000);
if (user?.active && user?.approved && !botSettings.maintenance) {
await updateFunds(userID);
devLog('FUNDS INITED')
await sleep(5000);
}
// start syncing orders over the REST api
syncOrders(userID);
// start looking for orders to process
Expand Down Expand Up @@ -388,15 +397,18 @@ async function processOrders(userID) {
const willCancel = userStorage[userID].checkCancel(dbOrder.order_id);
if (!willCancel) {
// place the new trade on coinbase
// devLog(tradeDetails, '<- tradeDetails before placing the flipped order')
let cbOrder = await cbClients[userID].placeOrder(tradeDetails);
// let cbOrder = false
// if the new trade was successfully placed...
// devLog(cbOrder, 'cbOrder before checking success')
if (cbOrder.success) {
// ...get the new order from coinbase since not all details are returned when placing
const newOrder = await cbClients[userID].getOrder(cbOrder.order_id);
const newOrder = await cbClients[userID].getOrder(cbOrder.success_response.order_id);
// ...store the new trade
// take the time the new order was created, and use it as the flipped_at value
const flipped_at = newOrder.order.created_time
// devLog('storing trade', newOrder.order)
await databaseClient.storeTrade(newOrder.order, dbOrder, flipped_at);
// await databaseClient.storeTrade(cbOrder, dbOrder, cbOrder.created_at);
// ...mark the old trade as flipped
Expand All @@ -420,8 +432,8 @@ async function processOrders(userID) {
devLog('Timed out!!!!! from processOrders');
errorText = 'Coinbase timed out while flipping an order';
} else if (err.response?.status === 400) {
devLog(err.response, 'Insufficient funds! from processOrders');
errorText = 'Insufficient funds while trying to flip a trade!';
devLog(err.response, 'Bad Request! from processOrders');
errorText = 'Bad request while trying to flip a trade! After the flip function';
// todo - check funds to make sure there is enough for
// all of them to be replaced, and balance if needed
} else {
Expand Down Expand Up @@ -472,7 +484,7 @@ function flipTrade(dbOrder, user, allFlips, simulation) {

const avail = userStorage[userID].getAvailableFunds();
const prodFunds = avail[tradeDetails.product_id]
// devLog(dbOrder, 'dbOrder... needs to flip. Price is too many decimals?', prodFunds)
// devLog(dbOrder, '<- dbOrder... needs to flip. Price is too many decimals?', prodFunds, '<- prodfunds', avail, '<- avail')

// get decimals after .
const base_increment_decimals = prodFunds.base_increment.split('.')[1]?.split('').findIndex((char) => char !== '0') + 1;
Expand Down Expand Up @@ -647,8 +659,8 @@ async function updateMultipleOrders(userID, params) {
await reorder(orderToCheck);
} else {
// if not a reorder, look up the full details on CB
// devLog(orderToCheck, 'order to check BIG PROBLEM');
let updatedOrder = await cbClients[userID].getOrder(orderToCheck.order_id);
// devLog(orderToCheck, 'order to check');
// if it was cancelled, set it for reorder
if (updatedOrder.order.status === 'CANCELLED') {
devLog('was canceled but should not have been!')
Expand All @@ -662,6 +674,7 @@ async function updateMultipleOrders(userID, params) {
}
} catch (err) {
devLog(err, 'error in updateMultipleOrders loop');
await sleep(1000);
let errorText = `Error updating order details`
if (err?.error_response?.message) {
errorText = errorText + '. Reason: ' + err.error_response.message
Expand Down Expand Up @@ -706,7 +719,8 @@ async function reorder(orderToReorder) {
// send the new order with the trade details
let pendingTrade = await cbClients[userID].placeOrder(tradeDetails);
if (pendingTrade.success) {
let newTrade = await cbClients[userID].getOrder(pendingTrade.order_id)
// devLog(pendingTrade, '<- pendingTrade, should be a success and we need the id')
let newTrade = await cbClients[userID].getOrder(pendingTrade.success_response.order_id)
// because the storeDetails function will see the upToDateDbOrder as the "old order", need to store previous_total_fees as just total_fees
upToDateDbOrder.total_fees = upToDateDbOrder.previous_total_fees;
// store the new trade in the db. the trade details are also sent to store trade position prices
Expand All @@ -728,6 +742,7 @@ async function reorder(orderToReorder) {
}
} catch (err) {
devLog(err, 'error in reorder function in robot.js');
await sleep(1000);
reject(err)
}
resolve();
Expand Down

0 comments on commit cd720a1

Please sign in to comment.