diff --git a/app/utils/btc-conversions.js b/app/utils/btc-conversions.js new file mode 100644 index 0000000..48cc9f9 --- /dev/null +++ b/app/utils/btc-conversions.js @@ -0,0 +1,11 @@ +export function floatToBtc(number) { + return Number(number.toFixed(8)) +} + +export function btcToSats(btc) { + return Math.round(btc * 100_000_000); +} + +export function satsToBtc(sats) { + return Math.round((sats / 100_000_000) * 100_000_000) / 100_000_000; +} diff --git a/tests/unit/utils/btc-conversions-test.js b/tests/unit/utils/btc-conversions-test.js new file mode 100644 index 0000000..c86673d --- /dev/null +++ b/tests/unit/utils/btc-conversions-test.js @@ -0,0 +1,16 @@ +import { floatToBtc, btcToSats, satsToBtc } from 'kredits-web/utils/btc-conversions'; +import { module, test } from 'qunit'; + +module('Unit | Utility | btc-conversions', function() { + test('floatToBtc', function(assert) { + assert.equal(floatToBtc(0.001429007), 0.00142901); + }); + + test('btcToSats', function(assert) { + assert.equal(btcToSats(0.001429007), 142901); + }); + + test('satsToBtc', function(assert) { + assert.equal(satsToBtc(142901), 0.00142901); + }); +});