mirror of https://github.com/BTCPrivate/copay.git
Feat: rate provider and alt currency implemented
This commit is contained in:
parent
d68da20300
commit
f6fe635189
|
@ -37,6 +37,8 @@ import { SettingsPage } from '../pages/settings/settings';
|
|||
/* Settings */
|
||||
import { AboutPage } from '../pages/settings/about/about';
|
||||
import { TermsOfUsePage } from '../pages/settings/about/terms-of-use/terms-of-use';
|
||||
import { AltCurrencyPage } from '../pages/settings/alt-currency/alt-currency';
|
||||
|
||||
/* Send */
|
||||
import { AmountPage } from '../pages/send/amount/amount';
|
||||
import { ConfirmPage } from '../pages/send/confirm/confirm';
|
||||
|
@ -54,6 +56,7 @@ import { LanguageProvider } from '../providers/language/language';
|
|||
import { ScanProvider } from '../providers/scan/scan';
|
||||
import { ProfileProvider } from '../providers/profile/profile';
|
||||
import { BwcProvider } from '../providers/bwc/bwc';
|
||||
import { RateProvider } from '../providers/rate/rate';
|
||||
|
||||
export function createTranslateLoader(http: Http) {
|
||||
return new TranslatePoHttpLoader(http, 'assets/i18n', '.po');
|
||||
|
@ -77,7 +80,8 @@ export function createTranslateLoader(http: Http) {
|
|||
TabsPage,
|
||||
AmountPage,
|
||||
ConfirmPage,
|
||||
CustomAmountPage
|
||||
CustomAmountPage,
|
||||
AltCurrencyPage
|
||||
],
|
||||
imports: [
|
||||
BrowserModule,
|
||||
|
@ -112,7 +116,8 @@ export function createTranslateLoader(http: Http) {
|
|||
TabsPage,
|
||||
AmountPage,
|
||||
ConfirmPage,
|
||||
CustomAmountPage
|
||||
CustomAmountPage,
|
||||
AltCurrencyPage
|
||||
],
|
||||
providers: [
|
||||
StatusBar,
|
||||
|
@ -144,7 +149,8 @@ export function createTranslateLoader(http: Http) {
|
|||
QRScanner,
|
||||
ScanProvider,
|
||||
ProfileProvider,
|
||||
BwcProvider
|
||||
BwcProvider,
|
||||
RateProvider
|
||||
]
|
||||
})
|
||||
export class AppModule { }
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
<ion-header>
|
||||
|
||||
<ion-navbar>
|
||||
<ion-buttons start>
|
||||
<button (click)="goBack()" ion-button icon-only>
|
||||
<ion-icon name="arrow-back"></ion-icon>
|
||||
</button>
|
||||
</ion-buttons>
|
||||
<ion-title>Alternative currency</ion-title>
|
||||
</ion-navbar>
|
||||
|
||||
</ion-header>
|
||||
|
||||
|
||||
<ion-content padding>
|
||||
<ion-input type="search" placeholder="Search your currency" [(ngModel)]="searchedAltCurrency" (ngModelChange)="findCurrency(searchedAltCurrency)"></ion-input>
|
||||
<ion-list>
|
||||
<button ion-item *ngFor="let alt of altCurrencyList" (click)="altSelected(alt)">
|
||||
{{ alt.name }} <span item-end>{{alt.isoCode}}</span>
|
||||
</button>
|
||||
</ion-list>
|
||||
</ion-content>
|
|
@ -0,0 +1,3 @@
|
|||
page-alt-currency {
|
||||
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { NavController, NavParams } from 'ionic-angular';
|
||||
import { RateProvider } from '../../../providers/rate/rate';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
@Component({
|
||||
selector: 'page-alt-currency',
|
||||
templateUrl: 'alt-currency.html',
|
||||
})
|
||||
export class AltCurrencyPage {
|
||||
|
||||
public completeAlternativeList: Array<any>;
|
||||
public searchedAltCurrency: string;
|
||||
public altCurrencyList: Array<any>;
|
||||
public loading: any;
|
||||
|
||||
constructor(public navCtrl: NavController, public navParams: NavParams, private rate: RateProvider) {
|
||||
this.completeAlternativeList = [];
|
||||
this.altCurrencyList = [];
|
||||
this.rate.updateRates().then((data) => {
|
||||
this.completeAlternativeList = this.rate.listAlternatives(true);
|
||||
this.altCurrencyList = this.completeAlternativeList;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("Error: ", error);
|
||||
});
|
||||
}
|
||||
|
||||
ionViewDidLoad() {
|
||||
console.log('ionViewDidLoad AltCurrencyPage');
|
||||
}
|
||||
|
||||
goBack() {
|
||||
this.navCtrl.pop();
|
||||
}
|
||||
|
||||
altSelected(alt: any) {
|
||||
console.log("Alt selected: ", alt);
|
||||
}
|
||||
|
||||
findCurrency(searchedAltCurrency: string) {
|
||||
this.altCurrencyList = _.filter(this.completeAlternativeList, (item) => {
|
||||
var val = item.name
|
||||
var val2 = item.isoCode;
|
||||
return _.includes(val.toLowerCase(), searchedAltCurrency.toLowerCase()) || _.includes(val2.toLowerCase(), searchedAltCurrency.toLowerCase());
|
||||
})
|
||||
}
|
||||
|
||||
}
|
|
@ -41,15 +41,11 @@
|
|||
<ion-option *ngFor="let lang of languages" [value]="lang.isoCode">{{ lang.name }}</ion-option>
|
||||
</ion-select>
|
||||
</ion-item>
|
||||
<ion-item>
|
||||
<ion-item (click)="altCurrencyModal()">
|
||||
<ion-icon name="cash" item-start></ion-icon>
|
||||
<ion-label>
|
||||
Alternative currency
|
||||
</ion-label>
|
||||
<ion-select>
|
||||
<ion-option>USD</ion-option>
|
||||
<ion-option>ARS</ion-option>
|
||||
</ion-select>
|
||||
</ion-item>
|
||||
<ion-item>
|
||||
<ion-icon name="git-merge" item-start></ion-icon>
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
import { Component } from '@angular/core';
|
||||
import { NavController, NavParams } from 'ionic-angular';
|
||||
|
||||
import { ModalController } from 'ionic-angular';
|
||||
import { AppProvider } from '../../providers/app/app';
|
||||
import { LanguageProvider } from '../../providers/language/language';
|
||||
import { RateProvider } from '../../providers/rate/rate';
|
||||
import { AltCurrencyPage } from './alt-currency/alt-currency';
|
||||
|
||||
import { AboutPage } from './about/about';
|
||||
|
||||
@Component({
|
||||
selector: 'page-settings',
|
||||
templateUrl: 'settings.html',
|
||||
providers: [RateProvider]
|
||||
})
|
||||
export class SettingsPage {
|
||||
appName: string;
|
||||
|
@ -16,10 +19,12 @@ export class SettingsPage {
|
|||
languages: Array<any>;
|
||||
|
||||
constructor(
|
||||
public modalCtrl: ModalController,
|
||||
public navCtrl: NavController,
|
||||
public navParams: NavParams,
|
||||
private app: AppProvider,
|
||||
private language: LanguageProvider
|
||||
private language: LanguageProvider,
|
||||
private rate: RateProvider
|
||||
) {
|
||||
this.appName = this.app.info.nameCase;
|
||||
|
||||
|
@ -31,6 +36,11 @@ export class SettingsPage {
|
|||
console.log('ionViewDidLoad SettingsPage');
|
||||
}
|
||||
|
||||
altCurrencyModal() {
|
||||
let modal = this.modalCtrl.create(AltCurrencyPage);
|
||||
modal.present();
|
||||
}
|
||||
|
||||
setLanguage(lang: string) {
|
||||
this.currentLanguage = lang;
|
||||
this.language.set(lang);
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { Http } from '@angular/http';
|
||||
import 'rxjs/add/operator/map';
|
||||
import 'rxjs/add/operator/toPromise';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
@Injectable()
|
||||
export class RateProvider {
|
||||
|
||||
private _rates: Object;
|
||||
private _alternatives: Array<any>;
|
||||
private _ratesBCH: Object;
|
||||
private SAT_TO_BTC: any;
|
||||
private BTC_TO_SAT: any;
|
||||
|
||||
private rateServiceUrl = 'https://bitpay.com/api/rates';
|
||||
private bchRateServiceUrl = 'https://api.kraken.com/0/public/Ticker?pair=BCHUSD,BCHEUR';
|
||||
|
||||
constructor(public http: Http) {
|
||||
console.log('Hello RateProvider Provider');
|
||||
this._rates = {};
|
||||
this._alternatives = [];
|
||||
this._ratesBCH = {};
|
||||
this.SAT_TO_BTC = 1 / 1e8;
|
||||
this.BTC_TO_SAT = 1e8;
|
||||
}
|
||||
|
||||
updateRates(): Promise<any> {
|
||||
return new Promise ((resolve, reject) => {
|
||||
let self = this;
|
||||
this.getBTC().then((dataBTC) => {
|
||||
|
||||
_.each(dataBTC, (currency) => {
|
||||
self._rates[currency.code] = currency.rate;
|
||||
self._alternatives.push({
|
||||
name: currency.name,
|
||||
isoCode: currency.code,
|
||||
rate: currency.rate
|
||||
});
|
||||
});
|
||||
|
||||
this.getBCH().then((dataBCH) => {
|
||||
|
||||
_.each(dataBCH.result, (data, paircode) => {
|
||||
var code = paircode.substr(3,3);
|
||||
var rate =data.c[0];
|
||||
self._ratesBCH[code] = rate;
|
||||
});
|
||||
|
||||
resolve();
|
||||
})
|
||||
.catch((errorBCH) => {
|
||||
console.log("Error: ", errorBCH);
|
||||
reject(errorBCH);
|
||||
});
|
||||
})
|
||||
.catch((errorBTC) => {
|
||||
console.log("Error: ", errorBTC);
|
||||
reject(errorBTC);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getBTC(): Promise<any> {
|
||||
return this.http.get(this.rateServiceUrl)
|
||||
.map((response) => response.json())
|
||||
.toPromise()
|
||||
.catch((error) => console.log("Error", error));
|
||||
}
|
||||
|
||||
getBCH(): Promise<any> {
|
||||
return this.http.get(this.bchRateServiceUrl)
|
||||
.map((response) => response.json())
|
||||
.toPromise()
|
||||
.catch((error) => console.log("Error", error));
|
||||
}
|
||||
|
||||
getRate(code, chain) {
|
||||
if (chain == 'bch')
|
||||
return this._ratesBCH[code];
|
||||
else
|
||||
return this._rates[code];
|
||||
};
|
||||
|
||||
getAlternatives() {
|
||||
return this._alternatives;
|
||||
};
|
||||
|
||||
toFiat(satoshis, code, chain) {
|
||||
return satoshis * this.SAT_TO_BTC * this.getRate(code, chain);
|
||||
};
|
||||
|
||||
fromFiat(amount, code, chain) {
|
||||
return amount / this.getRate(code, chain) * this.BTC_TO_SAT;
|
||||
};
|
||||
|
||||
listAlternatives(sort: boolean) {
|
||||
var self = this;
|
||||
|
||||
var alternatives = _.map(this.getAlternatives(), (item) => {
|
||||
return {
|
||||
name: item.name,
|
||||
isoCode: item.isoCode
|
||||
}
|
||||
});
|
||||
if (sort) {
|
||||
alternatives.sort( (a, b) => {
|
||||
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
|
||||
});
|
||||
}
|
||||
return _.uniqBy(alternatives, 'isoCode');
|
||||
};
|
||||
|
||||
}
|
Loading…
Reference in New Issue