Skip to content

Commit

Permalink
chore: show error about FAILED_LIMITS_EXCEEDED
Browse files Browse the repository at this point in the history
  • Loading branch information
topether21 committed Dec 8, 2023
1 parent 9381cec commit 720d898
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
42 changes: 40 additions & 2 deletions __tests__/utils.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -164,4 +164,42 @@ describe('get_included_tags', () => {
const result = get_included_tags({ fee_rate: 15 })
expect(result).toEqual([['special_name']])
})
})
})

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);
});
});
13 changes: 12 additions & 1 deletion deezy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
23 changes: 19 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}`
)
}
Expand Down Expand Up @@ -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 [email protected] 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
Expand Down
5 changes: 4 additions & 1 deletion utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 720d898

Please sign in to comment.