-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[app] implemented money service, fetching balance and sources
- Loading branch information
1 parent
9d6a3b8
commit 42705d5
Showing
16 changed files
with
247 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,6 @@ lerna-debug.log* | |
# Config | ||
/config/* | ||
!/config/default.json | ||
|
||
# Docs | ||
/documentation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export interface BalanceAmount { | ||
amount: number; | ||
currency: string; | ||
} | ||
|
||
export interface Balance { | ||
object: string; | ||
available: BalanceAmount[]; | ||
connect_reserved?: BalanceAmount[]; | ||
livemode: boolean; | ||
pending: BalanceAmount[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export enum CurrencyDecimalPlaces { | ||
DKK = 2, | ||
EUR = 2, | ||
NOK = 2, | ||
PLN = 2, | ||
SEK = 2, | ||
CHF = 2, | ||
AUD = 2, | ||
CAD = 2, | ||
HKD = 2, | ||
INR = 2, | ||
MXN = 2, | ||
NZD = 2, | ||
SGD = 2, | ||
GBP = 2, | ||
USD = 2, | ||
JPY = 0, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
export enum CurrencyMinimumAmount { | ||
DKK = 2.50, | ||
EUR = 1, | ||
NOK = 3, | ||
PLN = 1, | ||
SEK = 3, | ||
CHF = 1, | ||
AUD = 1, | ||
CAD = 1, | ||
HKD = 4, | ||
INR = 1, | ||
MXN = 10, | ||
NZD = 1, | ||
SGD = 1, | ||
GBP = 1, | ||
USD = 1, | ||
JPY = 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface OpenExchangeRates { | ||
timestamp: number; | ||
base: string; | ||
rates: { | ||
[key: string]: number | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Injectable, HttpService } from '@nestjs/common'; | ||
import Decimal from 'decimal.js'; | ||
import * as fx from 'money'; | ||
import { Observable, timer } from 'rxjs'; | ||
import { map, share, mergeMap } from 'rxjs/operators'; | ||
import config from 'config'; | ||
import { OpenExchangeRates } from 'src/discord/core/open-exchange-rates'; | ||
import { CurrencyDecimalPlaces } from 'src/discord/core/currency-decimal-places.enum'; | ||
|
||
@Injectable() | ||
export class MoneyService { | ||
constructor(private http: HttpService) { | ||
timer(0, 1000 * 60 * 60).pipe( | ||
mergeMap(() => this.updateRates()), | ||
share() | ||
); | ||
} | ||
|
||
// Take base currency and convert to a displayable decimal | ||
format(amount: number, currency: string): string { | ||
return new Intl.NumberFormat('en-US', { | ||
style: 'currency', | ||
currency | ||
}).format(this.compress(amount, currency)); | ||
} | ||
|
||
// Take the number input and convert to base currency | ||
unformat(amount: number, currency: string): number { | ||
return this.uncompress(amount, currency); | ||
} | ||
|
||
// Compress format to decimal amount | ||
compress(amount: number, currency: string): number { | ||
return new Decimal(amount).div(new Decimal(10).pow(CurrencyDecimalPlaces[currency.toUpperCase()])).toNumber(); | ||
} | ||
|
||
// Uncompress format to base denomination | ||
uncompress(amount: number, currency: string): number { | ||
return new Decimal(amount).times(new Decimal(10).pow(CurrencyDecimalPlaces[currency.toUpperCase()])).toNumber(); | ||
} | ||
|
||
// Take a base currency and convert to a secondary currency | ||
convert(amount: number, from: string, to: string): Observable<number> { | ||
return this.updateRates().pipe( | ||
// Compress in base currency, convert to fx and uncompress using fx | ||
map(() => Math.round( | ||
this.uncompress( | ||
fx(this.compress(amount, from)).convert({ from: from.toUpperCase(), to: to.toUpperCase() }), | ||
from | ||
) | ||
)) | ||
) | ||
} | ||
|
||
// API: Update exchange rates | ||
updateRates(): Observable<OpenExchangeRates> { | ||
return this.http.get<OpenExchangeRates>( | ||
`${config.get('fx.url')}`, { | ||
params: { app_id: config.get('fx.client_id') } | ||
} | ||
).pipe( | ||
map(d => d.data), | ||
map((data: OpenExchangeRates) => { | ||
console.log('Open exchange rates:', data) | ||
fx.base = data.base; | ||
fx.rates = data.rates; | ||
return data | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import { HttpService } from "@nestjs/common"; | ||
|
||
export class Profile { | ||
public token: string; | ||
public balance: { | ||
available: number; | ||
pending: number; | ||
} | ||
public currency: string; | ||
public sources: any; | ||
public id: string; | ||
public linked = false; | ||
|
||
constructor (data?: object) { | ||
constructor (data: {id?: string; token?: string; currency?: string}, private readonly http?: HttpService) { | ||
Object.assign(this, data) | ||
this.linked = !!this.token | ||
} | ||
|
||
get linked(): boolean { | ||
return !!this.token; | ||
} | ||
|
||
set linked(linked: boolean) { | ||
return | ||
} | ||
} |
Oops, something went wrong.