public void TestNachoPurchaseBonus()
        {
            var owner     = KeyPair.Generate();
            var simulator = new NexusSimulator(owner, 1234);

            var tokenSupply = ToBigInteger(69931640.63m, NACHO_TOKEN_DECIMALS);

            simulator.BeginBlock();
            simulator.GenerateToken(owner, NACHO_SYMBOL, NACHO_SYMBOL, Nexus.PlatformName, Hash.FromString(NACHO_SYMBOL), tokenSupply, NACHO_TOKEN_DECIMALS, Fungible | Transferable | Finite | Divisible);
            simulator.MintTokens(owner, owner.Address, NACHO_SYMBOL, tokenSupply);
            simulator.EndBlock();

            var buyer    = KeyPair.Generate();
            var receiver = KeyPair.Generate();
            var nexus    = simulator.Nexus;

            simulator.BeginBlock();
            simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, FuelTokenSymbol, 100000000);
            simulator.MintTokens(owner, owner.Address, FiatTokenSymbol,
                                 new BigInteger("1000000000000000000000000000000000000000000000000", 10) * GetUnitValue(FiatTokenDecimals));
            //simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, Nexus.FiatTokenSymbol, 1000000 * UnitConversion.GetUnitValue(Nexus.FiatTokenDecimals));
            simulator.EndBlock();

            var ownerFiat = simulator.Nexus.RootChain.GetTokenBalance(Nexus.FiatTokenSymbol, owner.Address);

            decimal milestoneRequiredMoney = 0;

            decimal[] purchaseAmounts = { 1, 2, 5, 10, 20, 50, 100, 250, 500 };
            decimal[] purchaseBonus   = { 0, 5, 10, 15, 20, 25, 30, 35, 40 };

            for (int stage = 0; stage < stageCount; stage++)
            {
                for (int milestone = 1; milestone <= milestoneCount; milestone++)
                {
                    decimal milestoneTokens = stageTokenAmount[stage] * milestone / 10m;
                    milestoneTokens = Math.Round(milestoneTokens, 2, AwayFromZero);

                    decimal milestoneTokensPerUsd = stageTokensPerUsd[stage] * tokenDecreaseFactor[stage];
                    milestoneTokensPerUsd = Math.Round(milestoneTokensPerUsd, 3, AwayFromZero);

                    for (int purchase = 0; purchase < purchaseAmounts.Length; purchase++)
                    {
                        var purchaseCoef = purchaseBonus[purchase] / 100;

                        var expectedPurchasedTokens =
                            purchaseAmounts[purchase] * milestoneTokensPerUsd * (1 + purchaseCoef);
                        var expectedPurchasedTokensBigint = ToBigInteger(expectedPurchasedTokens, NACHO_TOKEN_DECIMALS);

                        var purchaseAmountsBigint =
                            ToBigInteger(purchaseAmounts[purchase], FiatTokenDecimals);

                        simulator.BeginBlock();
                        simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, FiatTokenSymbol, purchaseAmountsBigint);
                        simulator.EndBlock();

                        var initialNachos = simulator.Nexus.RootChain.GetTokenBalance(NACHO_SYMBOL, receiver.Address);
                        var initialFiat   = simulator.Nexus.RootChain.GetTokenBalance(FiatTokenSymbol, receiver.Address);

                        simulator.BeginBlock();
                        simulator.GenerateCustomTransaction(buyer, ProofOfWork.None, () =>
                                                            ScriptUtils.BeginScript().AllowGas(buyer.Address, Address.Null, 1, 9999)
                                                            .CallContract("nacho", "BuyInApp", buyer.Address, FiatTokenSymbol, purchaseAmountsBigint).
                                                            SpendGas(buyer.Address).EndScript());
                        simulator.EndBlock();

                        var finalNachos = simulator.Nexus.RootChain.GetTokenBalance(NACHO_SYMBOL, receiver.Address);
                        var finalFiat   = simulator.Nexus.RootChain.GetTokenBalance(Nexus.FiatTokenSymbol, receiver.Address);

                        Assert.IsTrue(finalNachos == initialNachos + expectedPurchasedTokensBigint);
                        Assert.IsTrue(finalFiat == initialFiat - purchaseAmountsBigint);

                        milestoneTokens -= expectedPurchasedTokens;
                    }

                    //do a single order to clean up the rest of the current milestone
                    var requiredMoney = milestoneTokens / milestoneTokensPerUsd;

                    Assert.IsTrue(requiredMoney >= 500, "unexpected order size: not enough for 40% bonus");

                    requiredMoney = requiredMoney / 1.4m;

                    var bigintMoney = ToBigInteger(requiredMoney, FiatTokenDecimals);

                    simulator.BeginBlock();
                    simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, FiatTokenSymbol, bigintMoney);
                    simulator.EndBlock();

                    simulator.BeginBlock();
                    simulator.GenerateCustomTransaction(buyer, ProofOfWork.None, () =>
                                                        ScriptUtils.BeginScript().AllowGas(buyer.Address, Address.Null, 1, 9999)
                                                        .CallContract("nacho", "BuyInApp", buyer.Address, FiatTokenSymbol, bigintMoney).
                                                        SpendGas(buyer.Address).EndScript());
                    simulator.EndBlock();
                }
            }
        }
Beispiel #2
0
        public void TestNachoPurchase()
        {
            var owner     = KeyPair.Generate();
            var simulator = new NexusSimulator(owner, 1234);

            var tokenSupply = ToBigInteger(69931640.63m, NACHO_TOKEN_DECIMALS);

            simulator.BeginBlock();
            simulator.GenerateToken(owner, NACHO_SYMBOL, NACHO_SYMBOL, DomainSettings.PlatformName, Hash.FromString(NACHO_SYMBOL), tokenSupply, NACHO_TOKEN_DECIMALS, Fungible | Transferable | Finite | Divisible);
            simulator.MintTokens(owner, owner.Address, NACHO_SYMBOL, tokenSupply);
            simulator.EndBlock();

            var buyer    = KeyPair.Generate();
            var receiver = KeyPair.Generate();
            var nexus    = simulator.Nexus;

            simulator.BeginBlock();
            simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, FuelTokenSymbol, 100000000);
            simulator.MintTokens(owner, owner.Address, FiatTokenSymbol,
                                 new BigInteger("1000000000000000000000000000000000000000000000000", 10) * GetUnitValue(FiatTokenDecimals));
            //simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, DomainSettings.FiatTokenSymbol, 1000000 * UnitConversion.GetUnitValue(DomainSettings.FiatTokenDecimals));
            simulator.EndBlock();

            var ownerFiat = simulator.Nexus.RootChain.GetTokenBalance(simulator.Nexus.RootStorage, DomainSettings.FiatTokenSymbol, owner.Address);

            decimal requiredMoney = 0;

            for (int stage = 0; stage < stageCount; stage++)
            {
                for (int milestone = 1; milestone <= milestoneCount; milestone++)
                {
                    decimal milestoneTokens = stageTokenAmount[stage] * milestone / 10m;
                    milestoneTokens = Math.Round(milestoneTokens, 2, AwayFromZero);

                    decimal milestoneTokensPerUsd = stageTokensPerUsd[stage] * tokenDecreaseFactor[stage];
                    milestoneTokensPerUsd = Math.Round(milestoneTokensPerUsd, 3, AwayFromZero);

                    requiredMoney += milestoneTokens / milestoneTokensPerUsd;

                    Assert.IsTrue(requiredMoney >= 500, "unexpected order size: not enough for 40% bonus");

                    requiredMoney = requiredMoney / 1.4m;

                    var bigintMoney = ToBigInteger(requiredMoney, FiatTokenDecimals);

                    simulator.BeginBlock();
                    simulator.GenerateTransfer(owner, buyer.Address, nexus.RootChain, FiatTokenSymbol, bigintMoney);
                    simulator.EndBlock();

                    var initialNachos = simulator.Nexus.RootChain.GetTokenBalance(simulator.Nexus.RootStorage, NACHO_SYMBOL, receiver.Address);
                    var initialFiat   = simulator.Nexus.RootChain.GetTokenBalance(simulator.Nexus.RootStorage, DomainSettings.FiatTokenSymbol, receiver.Address);

                    simulator.BeginBlock();
                    simulator.GenerateCustomTransaction(buyer, ProofOfWork.None, () =>
                                                        ScriptUtils.BeginScript().AllowGas(buyer.Address, Address.Null, 1, 9999)
                                                        .CallContract("nacho", "BuyInApp", buyer.Address, FiatTokenSymbol, bigintMoney).
                                                        SpendGas(buyer.Address).EndScript());
                    simulator.EndBlock();

                    var finalNachos = simulator.Nexus.RootChain.GetTokenBalance(simulator.Nexus.RootStorage, NACHO_SYMBOL, receiver.Address);
                    var finalFiat   = simulator.Nexus.RootChain.GetTokenBalance(simulator.Nexus.RootStorage, DomainSettings.FiatTokenSymbol, receiver.Address);

                    var milestoneTokensBigInt = ToBigInteger(milestoneTokens, NACHO_TOKEN_DECIMALS);

                    Assert.IsTrue(finalNachos == initialNachos + milestoneTokensBigInt);
                    Assert.IsTrue(finalFiat == initialFiat - bigintMoney);
                }
            }
        }