Ejemplo n.º 1
0
        public static BigInteger AssetPerGold(TradableCurrency ethereumToken, long centsPerAssetRate, long centsPerGoldRate)
        {
            var decimals = 0;

            if (ethereumToken == TradableCurrency.Eth)
            {
                decimals = TokensPrecision.Ethereum;
            }
            else
            {
                throw new NotImplementedException($"Not implemented for { ethereumToken.ToString() }");
            }

            // round down
            return(new BigInteger(centsPerGoldRate) * BigInteger.Pow(10, decimals) / new BigInteger(centsPerAssetRate));
        }
Ejemplo n.º 2
0
        public static async Task <SellGoldCryptoResult> SellGoldCryptoRev(
            IServiceProvider services,
            TradableCurrency ethereumToken,
            FiatCurrency fiatCurrency,
            BigInteger requiredCryptoAmountWithFee,
            long?knownGoldRateCents   = null,
            long?knownCryptoRateCents = null
            )
        {
            if (requiredCryptoAmountWithFee <= 0)
            {
                return(new SellGoldCryptoResult());
            }

            var pricer     = services.GetRequiredService <IPriceSource>();
            var cryptoRate = (long?)0L;
            var decimals   = 0;

            if (ethereumToken == TradableCurrency.Eth)
            {
                decimals   = TokensPrecision.Ethereum;
                cryptoRate = pricer.GetPriceInFiat(CurrencyPrice.Eth, fiatCurrency);
            }
            else
            {
                throw new NotImplementedException($"Not implemented for { ethereumToken.ToString() }");
            }

            cryptoRate = knownCryptoRateCents ?? cryptoRate;
            if (cryptoRate == null || cryptoRate <= 0)
            {
                return(new SellGoldCryptoResult()
                {
                    Status = SellGoldStatus.TradingDisallowed,
                });
            }

            var goldRate = knownGoldRateCents ?? pricer.GetPriceInFiat(CurrencyPrice.Gold, fiatCurrency);

            if (goldRate == null || goldRate <= 0)
            {
                return(new SellGoldCryptoResult()
                {
                    Status = SellGoldStatus.TradingDisallowed,
                });
            }

            var assetPerGold = AssetPerGold(ethereumToken, cryptoRate.Value, goldRate.Value);

            // round up
            var goldAmount = (requiredCryptoAmountWithFee * BigInteger.Pow(10, decimals) + assetPerGold - 1) / assetPerGold;

            return(new SellGoldCryptoResult()
            {
                Allowed = true,
                Status = SellGoldStatus.Success,
                Asset = ethereumToken,
                ExchangeCurrency = fiatCurrency,
                CentsPerGoldRate = goldRate.Value,
                CentsPerAssetRate = cryptoRate.Value,
                CryptoPerGoldRate = assetPerGold,
                ResultGoldAmount = goldAmount,
                ResultAssetAmount = requiredCryptoAmountWithFee,
            });
        }
Ejemplo n.º 3
0
        public static async Task <BuyGoldCryptoResult> BuyGoldCryptoRev(
            IServiceProvider services,
            TradableCurrency ethereumToken,
            FiatCurrency fiatCurrency,
            BigInteger requiredGoldAmount,
            long?knownGoldRateCents   = null,
            long?knownCryptoRateCents = null,
            double discount           = 0d
            )
        {
            if (requiredGoldAmount <= 0)
            {
                return(new BuyGoldCryptoResult());
            }

            var pricer     = services.GetRequiredService <IPriceSource>();
            var cryptoRate = (long?)0L;

            if (ethereumToken == TradableCurrency.Eth)
            {
                cryptoRate = pricer.GetPriceInFiat(CurrencyPrice.Eth, fiatCurrency);
            }
            else
            {
                throw new NotImplementedException($"Not implemented for { ethereumToken.ToString() }");
            }

            cryptoRate = knownCryptoRateCents ?? cryptoRate;
            if (cryptoRate == null || cryptoRate <= 0)
            {
                return(new BuyGoldCryptoResult()
                {
                    Status = BuyGoldStatus.TradingDisallowed,
                });
            }

            var goldRate = knownGoldRateCents ?? pricer.GetPriceInFiat(CurrencyPrice.Gold, fiatCurrency);

            if (goldRate == null || goldRate <= 0)
            {
                return(new BuyGoldCryptoResult()
                {
                    Status = BuyGoldStatus.TradingDisallowed,
                });
            }

            var assetPerGold = AssetPerGold(ethereumToken, cryptoRate.Value, goldRate.Value);

            // round up
            var cryptoAmount = (requiredGoldAmount * assetPerGold + BigInteger.Pow(10, TokensPrecision.Sumus) - 1) / BigInteger.Pow(10, TokensPrecision.Sumus);

            // discount
            var cryptoAmountMinusDiscount = GetDiscountRevBody(discount, cryptoAmount);

            if (cryptoAmountMinusDiscount < 1)
            {
                cryptoAmountMinusDiscount = 1;
            }

            return(new BuyGoldCryptoResult()
            {
                Allowed = true,
                Status = BuyGoldStatus.Success,
                Asset = ethereumToken,
                ExchangeCurrency = fiatCurrency,
                Discount = discount,
                CentsPerAssetRate = cryptoRate.Value,
                CentsPerGoldRate = goldRate.Value,
                CryptoPerGoldRate = assetPerGold,
                ResultAssetAmount = cryptoAmountMinusDiscount,
                ResultGoldAmount = requiredGoldAmount,
            });
        }