Ejemplo n.º 1
0
        public void PerformAccount2AccountTransferChecking()
        {
            if (this.ethSystem == null)
            {
                Setup();
            }

            if (FetchBallanceInEth(this.fromAccount).Result == 0)
            {
                PerformInitialTransferChecking();
            }

            BigInteger checkB = ethSystem.GetRapidBalance(this.fromAccount).Result;

            _testOutputHelper.WriteLine($"Source Account has got ETH = {checkB} available.");

            BigDecimal beforeOps = FetchBallanceInEth(this.toAccount).Result;

            BigDecimal amnt = this.ethSystem.FromEtherToUsd(this.ethSystem.FromWeiToEther(checkB));

            double amount = double.Parse(amnt.Floor().ToString());

            bool tStatus = PerformTransfer(this.fromAccount, this.toAccount, amount).Result;

            if (!tStatus)
            {
                throw new Exception("error occured during transfer");
            }

            BigDecimal afterOps = FetchBallanceInEth(this.toAccount).Result;

            Assert.True(beforeOps < afterOps);
        }
Ejemplo n.º 2
0
        public void Floor(string result, string value)
        {
            var resultDecimal = BigDecimal.Parse(result, CultureInfo.InvariantCulture);
            var valueDecimal  = BigDecimal.Parse(value, CultureInfo.InvariantCulture);

            Assert.Equal(resultDecimal, BigDecimal.Floor(valueDecimal));
        }
        public Fee1559[] SuggestFees(FeeHistoryResult feeHistory, BigInteger tip)
        {
            var baseFee      = feeHistory.BaseFeePerGas.Select(x => x == null? 0 : x.Value).ToArray();
            var gasUsedRatio = feeHistory.GasUsedRatio;


            // If a block is full then the baseFee of the next block is copied. The reason is that in full blocks the minimal tip might not be enough to get included.
            // The last (pending) block is also assumed to end up being full in order to give some upwards bias for urgent suggestions.
            baseFee[baseFee.Length - 1] *= 9 / 8;
            for (var i = gasUsedRatio.Length - 1; i >= 0; i--)
            {
                if (gasUsedRatio[i] > (decimal)0.9)
                {
                    baseFee[i] = baseFee[i + 1];
                }
            }

            var order = new int[baseFee.Length];

            for (var i = 0; i < baseFee.Length; i++)
            {
                order[i] = i;
            }

#if DOTNET35
            var comparer = Comparer.Create <int>
#else
            var comparer = Comparer <int> .Create
#endif
                               ((int x, int y) =>
            {
                var aa = baseFee[x];
                var bb = baseFee[y];
                if (aa < bb)
                {
                    return(-1);
                }

                if (aa > bb)
                {
                    return(1);
                }

                return(0);
            });

            Array.Sort(order, comparer);


            var        result     = new List <Fee1559>();
            BigDecimal maxBaseFee = 0;
            for (var timeFactor = MaxTimeFactor; timeFactor >= 0; timeFactor--)
            {
                var bf = SuggestBaseFee(baseFee, order, timeFactor);
                var t  = new BigDecimal(tip, 0);
                if (bf > maxBaseFee)
                {
                    maxBaseFee = bf;
                }
                else
                {
                    // If a narrower time window yields a lower base fee suggestion than a wider window then we are probably in a price dip.
                    // In this case getting included with a low tip is not guaranteed; instead we use the higher base fee suggestion
                    // and also offer extra tip to increase the chance of getting included in the base fee dip.
                    t += (maxBaseFee - bf) * ExtraTipRatio;
                    bf = maxBaseFee;
                }
                result.Add(new Fee1559()
                {
                    BaseFee              = bf.Floor().Mantissa,
                    MaxFeePerGas         = (bf + t).Floor().Mantissa,
                    MaxPriorityFeePerGas = t.Floor().Mantissa
                });
            }

            result.Reverse();
            return(result.ToArray());
        }
Ejemplo n.º 4
0
    public void TestTruncateOnAllArithmeticOperations()
    {
        var savePrecision = BigDecimal.Precision;

        BigDecimal mod1 = BigDecimal.Parse("3141592653589793238462643383279502");
        BigDecimal mod2 = BigDecimal.Parse("27182818284590452");
        BigDecimal neg1 = BigDecimal.Parse("-3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647");
        BigDecimal lrg1 = BigDecimal.Parse("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282306647");
        BigDecimal lrg2 = BigDecimal.Parse("2.718281828459045235360287471352662497757247093699959574966967");

        var expected1  = "5.859874482";
        var expected2  = "0.4233108251";
        var expected3  = "8.5397342226";
        var expected4  = "0.8652559794";
        var expected5  = "9.869604401";
        var expected6  = "148.4131591";
        var expected7  = "8003077319547306";
        var expected8  = "-3.1415926535";
        var expected9  = "3";
        var expected10 = "4";
        var expected11 = "3.1415926535";

        var actual1  = "";
        var actual2  = "";
        var actual3  = "";
        var actual4  = "";
        var actual5  = "";
        var actual6  = "";
        var actual7  = "";
        var actual8  = "";
        var actual9  = "";
        var actual10 = "";
        var actual11 = "";

        try
        {
            BigDecimal.Precision      = 10;
            BigDecimal.AlwaysTruncate = true;

            TestContext.WriteLine($"E = {BigDecimal.E}");
            TestContext.WriteLine($"{new BigDecimal(lrg1.Mantissa, lrg1.Exponent)}");
            TestContext.WriteLine($"{new BigDecimal(lrg2.Mantissa, lrg2.Exponent)}");

            BigDecimal result1  = BigDecimal.Add(lrg1, lrg2);
            BigDecimal result2  = BigDecimal.Subtract(lrg1, lrg2);
            BigDecimal result3  = BigDecimal.Multiply(lrg1, lrg2);
            BigDecimal result4  = BigDecimal.Divide(lrg2, lrg1);
            BigDecimal result5  = BigDecimal.Pow(lrg1, 2);
            BigDecimal result6  = BigDecimal.Exp(new BigInteger(5));
            BigDecimal result7  = BigDecimal.Mod(mod1, mod2);
            BigDecimal result8  = BigDecimal.Negate(lrg1);
            BigDecimal result9  = BigDecimal.Floor(lrg1);
            BigDecimal result10 = BigDecimal.Ceiling(lrg1);
            BigDecimal result11 = BigDecimal.Abs(lrg1);

            actual1  = result1.ToString();
            actual2  = result2.ToString();
            actual3  = result3.ToString();
            actual4  = result4.ToString();
            actual5  = result5.ToString();
            actual6  = result6.ToString();
            actual7  = result7.ToString();
            actual8  = result8.ToString();
            actual9  = result9.ToString();
            actual10 = result10.ToString();
            actual11 = result11.ToString();
        }
        finally
        {
            BigDecimal.Precision      = savePrecision;
            BigDecimal.AlwaysTruncate = false;
        }

        Assert.AreEqual(expected1, actual1, $"Test Truncate On All Arithmetic Operations  - #1: ");
        Assert.AreEqual(expected2, actual2, $"Test Truncate On All Arithmetic Operations  - #2: ");
        Assert.AreEqual(expected3, actual3, $"Test Truncate On All Arithmetic Operations  - #3: ");
        Assert.AreEqual(expected4, actual4, $"Test Truncate On All Arithmetic Operations  - #4: ");
        Assert.AreEqual(expected5, actual5, $"Test Truncate On All Arithmetic Operations  - #5: ");
        StringAssert.StartsWith(expected6, actual6, $"Test Truncate On All Arithmetic Operations  - #6: ");
        Assert.AreEqual(expected7, actual7, $"Test Truncate On All Arithmetic Operations  - #7: ");
        Assert.AreEqual(expected8, actual8, $"Test Truncate On All Arithmetic Operations  - #8: ");
        Assert.AreEqual(expected9, actual9, $"Test Truncate On All Arithmetic Operations  - #9: ");
        Assert.AreEqual(expected10, actual10, $"Test Truncate On All Arithmetic Operations - #10: ");
        Assert.AreEqual(expected11, actual11, $"Test Truncate On All Arithmetic Operations - #11: ");

        Assert.AreEqual(5000, BigDecimal.Precision, "Restore Precision to 5000");
    }