Skip to content

Commit efd1699

Browse files
author
Arik Sosman
committed
add currency conversion method
Reviewers: luis Reviewed By: luis Subscribers: ben Differential Revision: https://phabricator.bitgo.com/D5952
1 parent 6745bea commit efd1699

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "bitgo",
3-
"version": "3.4.5",
3+
"version": "3.4.6",
44
"description": "BitGo Javascript SDK",
55
"main": "./src/index.js",
66
"keywords": [

src/v2/baseCoin.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var Keychains;
2+
var BigNumber = require('bignumber.js');
23
var PendingApprovals;
34
var Wallet;
45
var Wallets;
@@ -74,6 +75,16 @@ BaseCoin.prototype.initializeCoin = function(coin) {
7475
coinInstance.call(this);
7576
};
7677

78+
/**
79+
* Convert a currency amount represented in base units (satoshi, wei, atoms, drops) to big units (btc, eth, rmg, xrp)
80+
* @param baseUnits
81+
*/
82+
BaseCoin.prototype.baseUnitsToBigUnits = function(baseUnits) {
83+
const dividend = this.getBaseFactor();
84+
const bigNumber = new BigNumber(baseUnits).dividedBy(dividend);
85+
return bigNumber.toFormat();
86+
};
87+
7788
/**
7889
* If a coin needs to add additional parameters to the wallet generation, it does it in this method
7990
* @param walletParams

test/v2/baseCoin.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// Tests for Wallets
3+
//
4+
5+
var assert = require('assert');
6+
var should = require('should');
7+
8+
var common = require('../../src/common');
9+
var TestV2BitGo = require('../lib/test_bitgo');
10+
11+
describe('V2 Base Coin:', function() {
12+
var bitgo;
13+
var wallets;
14+
var keychains;
15+
var basecoin;
16+
17+
before(function() {
18+
// TODO: replace dev with test
19+
bitgo = new TestV2BitGo({ env: 'test' });
20+
bitgo.initializeTestVars();
21+
basecoin = bitgo.coin('teth');
22+
wallets = basecoin.wallets();
23+
keychains = basecoin.keychains();
24+
});
25+
26+
describe('Currenncy conversion', function() {
27+
it('should convert wei amounts to ETH', function() {
28+
// 1 wei
29+
basecoin.baseUnitsToBigUnits(1).should.equal('0.000000000000000001');
30+
// 100 wei
31+
basecoin.baseUnitsToBigUnits(100).should.equal('0.0000000000000001');
32+
// 1 ETH
33+
basecoin.baseUnitsToBigUnits('1000000000000000000').should.equal('1');
34+
// others
35+
basecoin.baseUnitsToBigUnits('1000000010000000000').should.equal('1.00000001');
36+
});
37+
});
38+
});

0 commit comments

Comments
 (0)