-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2707 from threefoldtech/development_add_tft_class
Add currency abstraction
- Loading branch information
Showing
7 changed files
with
360 additions
and
7 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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { CurrencyModel } from "../src"; | ||
import { currency as TFTUSDConversionService } from "../src"; | ||
import { getClient } from "./client_loader"; | ||
import { log } from "./utils"; | ||
|
||
let currency: TFTUSDConversionService; | ||
|
||
function convertTFTtoUSD(amount) { | ||
const res = currency.convertTFTtoUSD(amount); | ||
log("================= Convert TFT ================="); | ||
log(res); | ||
log("================= Convert TFT ================="); | ||
} | ||
|
||
function convertUSDtoTFT(amount) { | ||
const res = currency.convertUSDtoTFT(amount); | ||
log("================= Convert USD ================="); | ||
log(res); | ||
log("================= Convert USD ================="); | ||
} | ||
|
||
function dailyTFT(hourlyTFT) { | ||
const res = currency.dailyTFT(hourlyTFT); | ||
log("================= Daily TFT ================="); | ||
log(res); | ||
log("================= Daily TFT ================="); | ||
} | ||
|
||
function monthlyTFT(hourlyTFT) { | ||
const res = currency.monthlyTFT(hourlyTFT); | ||
log("================= Monthly TFT ================="); | ||
log(res); | ||
log("================= Monthly TFT ================="); | ||
} | ||
|
||
function yearlyTFT(hourlyTFT) { | ||
const res = currency.yearlyTFT(hourlyTFT); | ||
log("================= Yearly TFT ================="); | ||
log(res); | ||
log("================= Yearly TFT ================="); | ||
} | ||
|
||
function dailyUSD(hourlyUSD) { | ||
const res = currency.dailyUSD(hourlyUSD); | ||
log("================= Daily USD ================="); | ||
log(res); | ||
log("================= Daily USD ================="); | ||
} | ||
|
||
function monthlyUSD(hourlyUSD) { | ||
const res = currency.monthlyUSD(hourlyUSD); | ||
log("================= Monthly USD ================="); | ||
log(res); | ||
log("================= Monthly USD ================="); | ||
} | ||
|
||
function yearlyUSD(hourlyUSD) { | ||
const res = currency.yearlyUSD(hourlyUSD); | ||
log("================= Yearly USD ================="); | ||
log(res); | ||
log("================= Yearly USD ================="); | ||
} | ||
async function main() { | ||
const grid = await getClient(); | ||
const rate = await grid.tfclient.tftPrice.get(); | ||
const decimals = 3; | ||
currency = new TFTUSDConversionService(rate, decimals); | ||
|
||
const amount: CurrencyModel = { | ||
amount: 1, | ||
}; | ||
|
||
convertTFTtoUSD(amount); | ||
convertUSDtoTFT(amount); | ||
dailyTFT(amount); | ||
monthlyTFT(amount); | ||
yearlyTFT(amount); | ||
dailyUSD(amount); | ||
monthlyUSD(amount); | ||
yearlyUSD(amount); | ||
|
||
await grid.disconnect(); | ||
} | ||
|
||
main(); |
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
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,77 @@ | ||
import Decimal from "decimal.js"; | ||
|
||
import { expose, validateInput } from "../helpers"; | ||
import { CurrencyModel } from "./models"; | ||
|
||
class TFTUSDConversionService { | ||
// TFT rate: 1 TFT = x USD | ||
constructor(protected rate: number, private decimals = 2) {} | ||
|
||
get _rate() { | ||
return this.rate; | ||
} | ||
|
||
@expose | ||
@validateInput | ||
normalizeCurrency(options: CurrencyModel): string { | ||
return new Decimal(options.amount).toFixed(this.decimals); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
convertUSDtoTFT(options: CurrencyModel): string { | ||
const amount = options.amount / this.rate; | ||
return this.normalizeCurrency({ amount }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
convertTFTtoUSD(options: CurrencyModel): string { | ||
const amount = options.amount * this.rate; | ||
return this.normalizeCurrency({ amount }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
dailyTFT(options: CurrencyModel): string { | ||
const hours = options.amount * 24; | ||
return this.normalizeCurrency({ amount: hours }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
monthlyTFT(options: CurrencyModel): string { | ||
const months = +this.dailyTFT(options) * 30; | ||
return this.normalizeCurrency({ amount: months }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
yearlyTFT(options: CurrencyModel): string { | ||
const years = +this.monthlyTFT(options) * 12; | ||
return this.normalizeCurrency({ amount: years }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
dailyUSD(options: CurrencyModel): string { | ||
const hours = options.amount * 24; | ||
return this.normalizeCurrency({ amount: hours }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
monthlyUSD(options: CurrencyModel): string { | ||
const months = +this.dailyUSD(options) * 30; | ||
return this.normalizeCurrency({ amount: months }); | ||
} | ||
|
||
@expose | ||
@validateInput | ||
yearlyUSD(options: CurrencyModel): string { | ||
const years = +this.monthlyUSD(options) * 12; | ||
return this.normalizeCurrency({ amount: years }); | ||
} | ||
} | ||
|
||
export { TFTUSDConversionService as currency }; |
Oops, something went wrong.