diff --git a/js/controllers/send.js b/js/controllers/send.js index 0dea1743b..7fd8bebd8 100644 --- a/js/controllers/send.js +++ b/js/controllers/send.js @@ -2,7 +2,7 @@ var bitcore = require('bitcore'); 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.loading = false; var satToUnit = 1 / config.unitToSatoshi; @@ -12,41 +12,56 @@ angular.module('copayApp.controllers').controller('SendController', $scope.unitToBtc = config.unitToSatoshi / bitcore.util.COIN; $scope.minAmount = config.limits.minAmountSatoshi * satToUnit; $scope.minAlternativeAmount = config.limits.minAmountSatoshi * satToAlternative; +// Mockup $rootScope.alternativeName = 'Dollars'; - $rootScope.alternativeShort = 'USD'; + $rootScope.alternativeIsoCode = 'USD'; + config.unitDecimals = 2; + this.rateService = rateService; $scope._amount = 0; $scope._alternative = 0; -// Mockup - var alternativeToUnit = function(val) { - return val * configAlternativeToSatoshi * satToUnit; - }; - var unitToAlternative = function(val) { - return val * config.unitToSatoshi * satToAlternative; + this.amountFilter = function(val) { + if (val) { + return val.toFixed(config.unitDecimals); + } }; + this.fiatFilter = function(val) { + if (val) { + return val.toFixed(2); + } + } + Object.defineProperty($scope, - "alternative", { - get: function () { - return this._alternative; - }, - set: function (newValue) { - this._alternative = newValue; - this._amount = alternativeToUnit(this._alternative); - }, - enumerable: true, - configurable: true + "alternative", { + get: function () { + return this._alternative; + }, + set: function (newValue) { + this._alternative = newValue; + if (typeof(newValue) === 'number') { + this._amount = -(-( + rateService.fromFiat(newValue, $rootScope.alternativeIsoCode) * satToUnit + ).toFixed(config.unitDecimals)); + } + }, + enumerable: true, + configurable: true }); Object.defineProperty($scope, - "amount", { - get: function () { - return this._amount; - }, - set: function (newValue) { - this._amount = newValue; - this._alternative = unitToAlternative(this._amount); - }, - enumerable: true, - configurable: true + "amount", { + get: function () { + return this._amount; + }, + set: function (newValue) { + this._amount = newValue; + if (newValue) { + this._alternative = -(-( + rateService.toFiat(newValue * config.unitToSatoshi, $rootScope.alternativeIsoCode) + ).toFixed(2)); + } + }, + enumerable: true, + configurable: true }); $scope.loadTxs = function() { diff --git a/js/services/rate.js b/js/services/rate.js new file mode 100644 index 000000000..84809cc30 --- /dev/null +++ b/js/services/rate.js @@ -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); diff --git a/views/send.html b/views/send.html index 7ce6aa6d2..c58a71683 100644 --- a/views/send.html +++ b/views/send.html @@ -1,4 +1,4 @@ -
+

Send Proposals

@@ -88,14 +88,14 @@
- {{$root.alternativeShort}} + {{$root.alternativeIsoCode}}