Add RateService to get info about fiat currencies
This commit is contained in:
parent
decc9e9dba
commit
3da033cb06
3 changed files with 86 additions and 31 deletions
|
|
@ -2,7 +2,7 @@
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('SendController',
|
angular.module('copayApp.controllers').controller('SendController',
|
||||||
function($scope, $rootScope, $window, $timeout, $anchorScroll, $modal, isMobile, notification, controllerUtils) {
|
function($scope, $rootScope, $window, $timeout, $anchorScroll, $modal, isMobile, notification, controllerUtils, rateService) {
|
||||||
$scope.title = 'Send';
|
$scope.title = 'Send';
|
||||||
$scope.loading = false;
|
$scope.loading = false;
|
||||||
var satToUnit = 1 / config.unitToSatoshi;
|
var satToUnit = 1 / config.unitToSatoshi;
|
||||||
|
|
@ -12,41 +12,56 @@ angular.module('copayApp.controllers').controller('SendController',
|
||||||
$scope.unitToBtc = config.unitToSatoshi / bitcore.util.COIN;
|
$scope.unitToBtc = config.unitToSatoshi / bitcore.util.COIN;
|
||||||
$scope.minAmount = config.limits.minAmountSatoshi * satToUnit;
|
$scope.minAmount = config.limits.minAmountSatoshi * satToUnit;
|
||||||
$scope.minAlternativeAmount = config.limits.minAmountSatoshi * satToAlternative;
|
$scope.minAlternativeAmount = config.limits.minAmountSatoshi * satToAlternative;
|
||||||
|
// Mockup
|
||||||
$rootScope.alternativeName = 'Dollars';
|
$rootScope.alternativeName = 'Dollars';
|
||||||
$rootScope.alternativeShort = 'USD';
|
$rootScope.alternativeIsoCode = 'USD';
|
||||||
|
config.unitDecimals = 2;
|
||||||
|
this.rateService = rateService;
|
||||||
|
|
||||||
$scope._amount = 0;
|
$scope._amount = 0;
|
||||||
$scope._alternative = 0;
|
$scope._alternative = 0;
|
||||||
// Mockup
|
this.amountFilter = function(val) {
|
||||||
var alternativeToUnit = function(val) {
|
if (val) {
|
||||||
return val * configAlternativeToSatoshi * satToUnit;
|
return val.toFixed(config.unitDecimals);
|
||||||
};
|
}
|
||||||
var unitToAlternative = function(val) {
|
|
||||||
return val * config.unitToSatoshi * satToAlternative;
|
|
||||||
};
|
};
|
||||||
|
this.fiatFilter = function(val) {
|
||||||
|
if (val) {
|
||||||
|
return val.toFixed(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Object.defineProperty($scope,
|
Object.defineProperty($scope,
|
||||||
"alternative", {
|
"alternative", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._alternative;
|
return this._alternative;
|
||||||
},
|
},
|
||||||
set: function (newValue) {
|
set: function (newValue) {
|
||||||
this._alternative = newValue;
|
this._alternative = newValue;
|
||||||
this._amount = alternativeToUnit(this._alternative);
|
if (typeof(newValue) === 'number') {
|
||||||
},
|
this._amount = -(-(
|
||||||
enumerable: true,
|
rateService.fromFiat(newValue, $rootScope.alternativeIsoCode) * satToUnit
|
||||||
configurable: true
|
).toFixed(config.unitDecimals));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
});
|
});
|
||||||
Object.defineProperty($scope,
|
Object.defineProperty($scope,
|
||||||
"amount", {
|
"amount", {
|
||||||
get: function () {
|
get: function () {
|
||||||
return this._amount;
|
return this._amount;
|
||||||
},
|
},
|
||||||
set: function (newValue) {
|
set: function (newValue) {
|
||||||
this._amount = newValue;
|
this._amount = newValue;
|
||||||
this._alternative = unitToAlternative(this._amount);
|
if (newValue) {
|
||||||
},
|
this._alternative = -(-(
|
||||||
enumerable: true,
|
rateService.toFiat(newValue * config.unitToSatoshi, $rootScope.alternativeIsoCode)
|
||||||
configurable: true
|
).toFixed(2));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
enumerable: true,
|
||||||
|
configurable: true
|
||||||
});
|
});
|
||||||
|
|
||||||
$scope.loadTxs = function() {
|
$scope.loadTxs = function() {
|
||||||
|
|
|
||||||
40
js/services/rate.js
Normal file
40
js/services/rate.js
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var RateService = function($http) {
|
||||||
|
this.isAvailable = false;
|
||||||
|
this.SAT_TO_BTC = 1 / 1e8;
|
||||||
|
var that = this;
|
||||||
|
var backoff = 5;
|
||||||
|
var retrieve = function() {
|
||||||
|
$http({method: 'GET', url: 'https://bitpay.com/api/rates'}).
|
||||||
|
success(function(data, status, headers, config) {
|
||||||
|
var rates = {};
|
||||||
|
data.forEach(function(element) {
|
||||||
|
rates[element.code] = element.rate;
|
||||||
|
});
|
||||||
|
that.isAvailable = true;
|
||||||
|
that.rates = rates;
|
||||||
|
}).
|
||||||
|
error(function(data, status, headers, config) {
|
||||||
|
backoff *= 1.5;
|
||||||
|
setTimeout(retrieve, backoff * 1000);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
retrieve();
|
||||||
|
};
|
||||||
|
|
||||||
|
RateService.prototype.toFiat = function(satoshis, code) {
|
||||||
|
if (!this.isAvailable) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return satoshis * this.SAT_TO_BTC * this.rates[code];
|
||||||
|
};
|
||||||
|
|
||||||
|
RateService.prototype.fromFiat = function(amount, code) {
|
||||||
|
if (!this.isAvailable) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return amount / this.rates[code] / this.SAT_TO_BTC;
|
||||||
|
};
|
||||||
|
|
||||||
|
angular.module('copayApp.services').service('rateService', RateService);
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
<div class="send" data-ng-controller="SendController" data-ng-init="loadTxs()">
|
<div class="send" data-ng-controller="SendController as ctrl" data-ng-init="loadTxs()">
|
||||||
<div ng-show='$root.wallet.isReady()'>
|
<div ng-show='$root.wallet.isReady()'>
|
||||||
|
|
||||||
<h1 ng-show="txs.length != 0">Send Proposals</h1>
|
<h1 ng-show="txs.length != 0">Send Proposals</h1>
|
||||||
|
|
@ -88,14 +88,14 @@
|
||||||
<label for="alternative">Amount in {{ $root.alternativeName }} </label>
|
<label for="alternative">Amount in {{ $root.alternativeName }} </label>
|
||||||
<div class="small-9 columns">
|
<div class="small-9 columns">
|
||||||
<input type="number" id="alternative_amount"
|
<input type="number" id="alternative_amount"
|
||||||
ng-disabled="loading"
|
ng-disabled="loading || !ctrl.rateService.isAvailable "
|
||||||
name="alternative" placeholder="Amount" ng-model="alternative"
|
name="alternative" placeholder="Amount" ng-model="alternative"
|
||||||
min="{{minAlternativeAmount}}" max="10000000000" enough-amount required
|
min="{{minAlternativeAmount}}" max="10000000000" enough-amount required
|
||||||
autocomplete="off"
|
autocomplete="off"
|
||||||
>
|
>
|
||||||
</div>
|
</div>
|
||||||
<div class="small-3 columns">
|
<div class="small-3 columns">
|
||||||
<span class="postfix">{{$root.alternativeShort}}</span>
|
<span class="postfix">{{$root.alternativeIsoCode}}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue