From 720d8984335b596d565ccd35a9ef04a28c25e917 Mon Sep 17 00:00:00 2001 From: topether21 Date: Thu, 7 Dec 2023 22:59:13 -0600 Subject: [PATCH] chore: show error about FAILED_LIMITS_EXCEEDED --- __tests__/utils.js | 42 ++++++++++++++++++++++++++++++++++++++++-- deezy.js | 13 ++++++++++++- index.js | 23 +++++++++++++++++++---- utils.js | 5 ++++- 4 files changed, 75 insertions(+), 8 deletions(-) diff --git a/__tests__/utils.js b/__tests__/utils.js index 6cabb18..2ae74d3 100644 --- a/__tests__/utils.js +++ b/__tests__/utils.js @@ -1,4 +1,4 @@ -const { get_excluded_tags, get_min_tag_sizes, get_included_tags } = require('../utils') +const { get_excluded_tags, get_min_tag_sizes, get_included_tags, satoshi_to_BTC } = require('../utils') describe('get_excluded_tags', () => { test('should return correct format', () => { @@ -164,4 +164,42 @@ describe('get_included_tags', () => { const result = get_included_tags({ fee_rate: 15 }) expect(result).toEqual([['special_name']]) }) -}) \ No newline at end of file +}) + +describe('satoshi_to_BTC', () => { + test('should correctly convert satoshi to BTC', () => { + const satoshi = 123456789; + const result = satoshi_to_BTC(satoshi); + expect(result).toBe(1.23456789); + }); + + test('should correctly convert satoshi to BTC', () => { + const satoshi = 6789; + const result = satoshi_to_BTC(satoshi); + expect(result).toBe(0.00006789); + }); + + test('should remove trailing zeros', () => { + const satoshi = 100000000; + const result = satoshi_to_BTC(satoshi); + expect(result).toBe(1); + }); + + test('should remove trailing zeros', () => { + const satoshi = 100050000; + const result = satoshi_to_BTC(satoshi); + expect(result).toBe(1.0005); + }); + + test('should remove trailing zeros', () => { + const satoshi = 20050010; + const result = satoshi_to_BTC(satoshi); + expect(result).toBe(0.2005001); + }); + + test('should handle zero', () => { + const satoshi = 0; + const result = satoshi_to_BTC(satoshi); + expect(result).toBe(0); + }); +}); \ No newline at end of file diff --git a/deezy.js b/deezy.js index e37d36d..0481187 100644 --- a/deezy.js +++ b/deezy.js @@ -63,7 +63,18 @@ async function get_scan_request({ scan_request_id }) { return data } +async function get_user_limits() { + check_api_key() + const url = `${BASE_URL}/sat-hunting/user/limits` + const { data } = await axios.get(url, { headers: { 'x-api-token': process.env.DEEZY_API_KEY } }).catch(err => { + console.error(err) + return { data: {} } + }) + return data +} + module.exports = { post_scan_request, - get_scan_request + get_scan_request, + get_user_limits, } \ No newline at end of file diff --git a/index.js b/index.js index 968c595..c895216 100644 --- a/index.js +++ b/index.js @@ -13,10 +13,10 @@ const { fetch_most_recent_unconfirmed_send } = require('./wallet') const { get_fee_rate } = require('./fees') -const { post_scan_request, get_scan_request } = require('./deezy') +const { post_scan_request, get_scan_request, get_user_limits } = require('./deezy') const { generate_satributes_messages } = require('./satributes') const { sendNotifications, TELEGRAM_BOT_ENABLED, PUSHOVER_ENABLED } = require('./notifications.js') -const { get_excluded_tags, get_included_tags, get_min_tag_sizes } = require('./utils.js') +const { get_excluded_tags, get_included_tags, get_min_tag_sizes, satoshi_to_BTC } = require('./utils.js') const LOOP_SECONDS = process.env.LOOP_SECONDS ? parseInt(process.env.LOOP_SECONDS) : 10 const available_exchanges = Object.keys(exchanges) const FALLBACK_MAX_FEE_RATE = 200 @@ -103,8 +103,7 @@ async function decode_sign_and_send_psbt({ psbt, exchange_address, rare_sat_addr console.log(`Broadcasted transaction with txid: ${txid} and fee rate of ${final_fee_rate} sat/vbyte`) if (!process.env.ONLY_NOTIFY_ON_SATS) { await sendNotifications( - `Broadcasted ${ - is_replacement ? 'replacement ' : '' + `Broadcasted ${is_replacement ? 'replacement ' : '' }tx at ${final_fee_rate} sat/vbyte https://mempool.space/tx/${txid}` ) } @@ -225,6 +224,22 @@ async function run() { const scan_request_id = scan_request_ids[i] console.log(`Checking status of scan request with id: ${scan_request_id}`) const info = await get_scan_request({ scan_request_id }) + if (info.status === 'FAILED_LIMITS_EXCEEDED') { + const { + payment_address, + amount, + days, + one_time_cost, + } = await get_user_limits() + const allowed_volume = satoshi_to_BTC(amount) // We are using satoshis in the DB as default + const msg = ` + Sat Hunting limits exceeded. To purchase more scans, you can send BTC to the following address: ${payment_address}. + Your plan allows for ${allowed_volume} BTC every ${days} days, and allows purchasing additional volume at a rate of ${one_time_cost} satoshis per 1 BTC of scan volume. + Contact help@deezy.io for questions or to change your plan.` + console.log(`Scan request with id: ${scan_request_id} failed`) + console.log(msg) + continue + } if (info.status === 'FAILED') { console.log(`Scan request with id: ${scan_request_id} failed`) continue diff --git a/utils.js b/utils.js index ad86936..5c005e2 100644 --- a/utils.js +++ b/utils.js @@ -57,8 +57,11 @@ function get_included_tags({ fee_rate }) { .map((tag) => tag.split('/')) } +const satoshi_to_BTC = (satoshi) => parseFloat((satoshi / 100000000).toFixed(8)); + module.exports = { get_excluded_tags, get_min_tag_sizes, - get_included_tags + get_included_tags, + satoshi_to_BTC }