160 lines
5.1 KiB
Go
160 lines
5.1 KiB
Go
|
|
||
|
// convertCurrencies provides functions to converting between different currencies
|
||
|
// Currency rate data is sourced from the network parameters. The admin(s) update the rates manually.
|
||
|
|
||
|
package convertCurrencies
|
||
|
|
||
|
// All currency rates use 1 kilogram of gold as the exchange pair
|
||
|
// The crypto/gold rates are used to calculate identity scores, so the parameters must contain historical gold rates.
|
||
|
// The parameters do not contain historical fiat rates.
|
||
|
// Fiat is only used to display message/profile/report funding costs and user income/wealth in a user's currency
|
||
|
|
||
|
// Each networkType has its own parameters, and thus its own currency exchange rates.
|
||
|
// The currency exchange rates might be updated less often for test networks than for Mainnet.
|
||
|
|
||
|
import "seekia/internal/helpers"
|
||
|
|
||
|
import "errors"
|
||
|
|
||
|
//TODO: Complete package
|
||
|
// Each function will get its data from the getParameters package
|
||
|
// We have to consider if we should replace kilograms/grams with a better unit (milligrams?)
|
||
|
|
||
|
func CheckIfExchangeRatesAreDownloaded(networkType byte)(bool, error){
|
||
|
|
||
|
//TODO
|
||
|
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
|
// Converts cryptocurrency atomic units to whole units
|
||
|
// For example, an Ethereum atomic unit is called Wei and a whole unit is called an Ether
|
||
|
func ConvertCryptocurrencyAtomicUnitsToWholeUnits(cryptocurrencyName string, inputAtomicUnits int64)(float64, error){
|
||
|
|
||
|
if (cryptocurrencyName == "Ethereum"){
|
||
|
|
||
|
// 1 Ether = 10^18 Wei = 1,000,000,000,000,000,000
|
||
|
|
||
|
ethereumAmount := float64(inputAtomicUnits) / float64(1000000000000000000)
|
||
|
|
||
|
return ethereumAmount, nil
|
||
|
|
||
|
} else if (cryptocurrencyName == "Cardano"){
|
||
|
|
||
|
// 1 ADA = 1,000,000 Lovelace
|
||
|
|
||
|
cardanoAmount := float64(inputAtomicUnits) / float64(1000000)
|
||
|
|
||
|
return cardanoAmount, nil
|
||
|
}
|
||
|
|
||
|
return 0, errors.New("ConvertCryptocurrencyAtomicUnitsToWholeUnits called with invalid cryptocurrencyName: " + cryptocurrencyName)
|
||
|
}
|
||
|
|
||
|
//Outputs:
|
||
|
// -bool: Parameters exist
|
||
|
// -float64: Output Currency units
|
||
|
// -error
|
||
|
func ConvertCryptoAtomicUnitsToAnyCurrency(networkType byte, inputCryptocurrency string, inputAtomicUnits int64, outputCurrencyCode string)(bool, float64, error){
|
||
|
|
||
|
isValid := helpers.VerifyNetworkType(networkType)
|
||
|
if (isValid == false){
|
||
|
networkTypeString := helpers.ConvertByteToString(networkType)
|
||
|
return false, 0, errors.New("ConvertCryptoAtomicUnitsToAnyCurrency called with invalid networkType: " + networkTypeString)
|
||
|
}
|
||
|
|
||
|
if (inputCryptocurrency != "Ethereum" && inputCryptocurrency != "Cardano"){
|
||
|
return false, 0, errors.New("ConvertCryptoAtomicUnitsToAnyCurrency called with invalid inputCryptocurrency: " + inputCryptocurrency)
|
||
|
}
|
||
|
|
||
|
//TODO
|
||
|
|
||
|
result := float64(inputAtomicUnits/2)
|
||
|
|
||
|
return true, result, nil
|
||
|
}
|
||
|
|
||
|
//Outputs:
|
||
|
// -bool: Parameters exist
|
||
|
// -float64: Output Cryptocurrency atomic units
|
||
|
// -error
|
||
|
func ConvertAnyCurrencyToCryptoAtomicUnits(networkType byte, inputCurrencyCode string, inputCurrencyAmount float64, outputCryptocurrency string)(bool, int64, error){
|
||
|
|
||
|
isValid := helpers.VerifyNetworkType(networkType)
|
||
|
if (isValid == false){
|
||
|
networkTypeString := helpers.ConvertByteToString(networkType)
|
||
|
return false, 0, errors.New("ConvertAnyCurrencyToCryptoAtomicUnits called with invalid networkType: " + networkTypeString)
|
||
|
}
|
||
|
|
||
|
if (outputCryptocurrency != "Ethereum" && outputCryptocurrency != "Cardano"){
|
||
|
return false, 0, errors.New("ConvertAnyCurrencyToCryptoAtomicUnits called with invalid outputCryptocurrency: " + outputCryptocurrency)
|
||
|
}
|
||
|
|
||
|
//TODO
|
||
|
|
||
|
result := int64(inputCurrencyAmount*2)
|
||
|
|
||
|
return true, result, nil
|
||
|
}
|
||
|
|
||
|
// This function will still return a valid amount, even if parameters are missing
|
||
|
// It will use a fallback currency exchange rate, coded within the client
|
||
|
//Outputs:
|
||
|
// -bool: Parameters exist
|
||
|
// -float64: Currency value in kilograms of gold
|
||
|
// -error
|
||
|
func ConvertCurrencyToKilogramsOfGold(networkType byte, inputCurrencyCode string, inputValue float64)(bool, float64, error){
|
||
|
|
||
|
//TODO
|
||
|
|
||
|
result := inputValue/3
|
||
|
|
||
|
return true, result, nil
|
||
|
}
|
||
|
|
||
|
// This function will still return a valid amount, even if parameters are missing
|
||
|
// It will use a fallback currency exchange rate, coded within the client
|
||
|
//Outputs:
|
||
|
// -bool: Parameters exist
|
||
|
// -float64: Value in input currency
|
||
|
// -error:
|
||
|
func ConvertKilogramsOfGoldToAnyCurrency(networkType byte, inputKilogramsAmount float64, outputCurrencyCode string)(bool, float64, error){
|
||
|
|
||
|
//TODO
|
||
|
|
||
|
result := inputKilogramsAmount*3000
|
||
|
|
||
|
return true, result, nil
|
||
|
}
|
||
|
|
||
|
//Outputs:
|
||
|
// -bool: Parameters exist
|
||
|
// -float64: Crypto atomic units amount converted to kilograms of gold
|
||
|
// -error
|
||
|
func ConvertCryptoAtomicUnitsToKilogramsOfGold(networkType byte, inputCryptocurrency string, depositTime int64, cryptoAtomicUnitsAmount int64)(bool, float64, error){
|
||
|
|
||
|
//TODO
|
||
|
// This function will use the parameters which contain historical data
|
||
|
|
||
|
result := float64(cryptoAtomicUnitsAmount/500)
|
||
|
|
||
|
return true, result, nil
|
||
|
}
|
||
|
|
||
|
//Outputs:
|
||
|
// -bool: Parameters exist
|
||
|
// -int64: Input kilograms amount converted to crypto atomic units
|
||
|
// -error
|
||
|
func ConvertKilogramsOfGoldToCryptoAtomicUnits(networkType byte, inputKilogramsAmount float64, outputCryptocurrency string)(bool, int64, error){
|
||
|
|
||
|
//TODO
|
||
|
|
||
|
result := int64(inputKilogramsAmount/1000)
|
||
|
|
||
|
return true, result, nil
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|