From 35837d8f030876ad2412662b024c4ffe4f9d43a2 Mon Sep 17 00:00:00 2001 From: artin Date: Mon, 17 Jul 2023 16:36:55 +0000 Subject: [PATCH] added catchup for transactions --- index.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/index.js b/index.js index e54dc9d..971869d 100644 --- a/index.js +++ b/index.js @@ -45,6 +45,39 @@ const accounts = { } } +async function getTransactionsAll(account,acct_id, api_key) { + for (const rg of [...Array(32).keys()]) { + try { + f = rg * 1000 + 1; + to = f + 1000; + const response = await axios.request({ + url: `${APIURL}/accounts/${acct_id}/transactions/idrange?from=${f}&to=${to}`, + method: 'get', + headers: { + 'Authorization': `Bearer ${api_key}` + } + }); + + for (const t of response.data.transactions){ + qty = 0; + tp = ""; + if(typeof(t['instrument']) !== undefined){ + tp = t['instrument'] + } + if(typeof(t['units']) !== undefined){ + qty = t['units'] + } + await client.query(`insert into orders (order_id, account_id, tpair, order_type, order_ref, order_reason, order_date, order_data, quantity) values ($1,$2,$3,$4,$5,$6,$7,$8,$9)`, [t.id, account, tp, t.type, t.batchID, t.reason, t.time, t, qty]); + + } + + + } catch (error) { + console.error(error); + } + +} +} async function getTransactions(account,acct_id, api_key) { // for (const rg of [...Array(32).keys()]) { try { @@ -337,6 +370,18 @@ console.log(error) }) +app.get('/transactionsAll/:account', async (req, res) => { + + let r = {} + let response = await getTransactions(account,accounts[account]['ACCT'], accounts[account]['APIKEY']) + try { + r = {}; +} catch(error) { +console.log(error) +} + res.header('Access-Control-Allow-Origin', '*') + res.json("ok") +}) app.get('/transactions', async (req, res) => { let r = {} @@ -351,6 +396,7 @@ console.log(error) res.header('Access-Control-Allow-Origin', '*') res.json("ok") }) + app.get('/trades', async (req, res) => { let r = {} @@ -365,6 +411,7 @@ console.log(error) res.header('Access-Control-Allow-Origin', '*') res.json(r) }) + app.get('/accounts/del/:user_id/:account_id', async(req,res) => { await client.query(`delete from accounts where user_id = ${req.params.user_id} and id = ${req.params.account_id}`); res.header('Access-Control-Allow-Origin', '*'); @@ -424,7 +471,9 @@ app.get('/order/:instrument/:quantity', async (req, res) => { } */ for (account of Object.keys(accounts)) { td = await getTradesByInstrument(accounts[account]['ACCT'], accounts[account]['APIKEY'], req.params.instrument); + if (td['state'] != "OPEN" || td['state'] != "PENDING") { response = await order(accounts[account]['ACCT'], accounts[account]['APIKEY'], req.params.instrument, req.params.quantity); + } }