コード例 #1
0
ファイル: Program.cs プロジェクト: StephenSTB/ScratchConsole
        private static async Task Test()
        {
            // Getting link price
            var requestPrice = await CsService.RequestLinkPriceQueryAsync();

            Console.WriteLine($"Link Price: {(decimal)requestPrice / 100000000}");

            // Getting contract link balance
            var balance = await CsService.GetLinkBalanceQueryAsync(ScratchContractAddress);

            Console.WriteLine($"Contract Link Balance: {Web3.Convert.FromWei(balance)}");


            // Approving the contract to use 1 link.
            var approveHandler = web3.Eth.GetContractTransactionHandler <ApproveFunction>();

            var approve = new ApproveFunction()
            {
                Spender     = ScratchContractAddress,
                TokenAmount = Web3.Convert.ToWei(1),
                FromAddress = account.Address
            };

            var approveReciept = await approveHandler.SendRequestAndWaitForReceiptAsync("0xa36085F69e2889c224210F603D836748e7dC0088", approve);

            // Confirming contract link allowance.
            var allowance = await CsService.GetContractAllowanceQueryAsync();

            Console.WriteLine($"Allowance: {Web3.Convert.FromWei(allowance)}");

            Console.WriteLine($"Card cost in LINK: {Web3.Convert.FromWei((2000000000000000000 / requestPrice) * 100000000)}");

            Console.WriteLine($"Allowance after buying card should be: {Web3.Convert.FromWei(allowance - (2000000000000000000 / requestPrice) * 100000000)}\n");

            Console.WriteLine("Buying card...");

            // Generating a seed
            Random rand = new Random();

            uint seed = (uint)rand.Next();

            Console.WriteLine($"Seed: {seed}");

            var gas = await CsService.ContractHandler.EstimateGasAsync <BuyScatchCardFunction>();

            var fullgas = Web3.Convert.ToWei(gas.Value, Nethereum.Util.UnitConversion.EthUnit.Gwei);

            Console.WriteLine($"expected gas to buy card: {Web3.Convert.FromWei(fullgas)}");



            var accountBalance = await web3.Eth.GetBalance.SendRequestAsync(account.Address);

            Console.WriteLine($"{account.Address} balance: {Web3.Convert.FromWei(accountBalance)}");


            // Buy Card Testing

            var buyCardReceipt = await CsService.BuyScatchCardRequestAndWaitForReceiptAsync(seed);

            var gasUsed = Web3.Convert.ToWei(buyCardReceipt.GasUsed, Nethereum.Util.UnitConversion.EthUnit.Gwei);

            Console.WriteLine($"Total gas consumed: {Web3.Convert.FromWei(gasUsed)}, {Web3.Convert.FromWei(Web3.Convert.ToWei(buyCardReceipt.CumulativeGasUsed, Nethereum.Util.UnitConversion.EthUnit.Gwei))}");

            Console.WriteLine($"Expected balance upon lose: {Web3.Convert.FromWei(accountBalance - gasUsed)} \n");

            var events = buyCardReceipt.DecodeAllEvents <RequestIDEventDTO>();

            var RequestID = events[0].Event.RequestId.ToHex();

            Console.WriteLine($"RequestID: {RequestID}");


            // Getting ScratchCardRound handler.
            var address = await CsService.GetCardRoundQueryAsync();

            ScratchCardRoundService RS = new ScratchCardRoundService(web3, address);

            var PrizeEventHandler = RS.ContractHandler.GetEvent <PrizeClaimEventDTO>();

            var filterPrizeEvents = PrizeEventHandler.CreateFilterInput();

            while (true)
            {
                var allRequestEvents = await PrizeEventHandler.GetAllChanges(filterPrizeEvents);

                if (allRequestEvents.Count > 0)
                {
                    bool brk = false;
                    foreach (EventLog <PrizeClaimEventDTO> e in allRequestEvents)
                    {
                        if (e.Event.RequestId.ToHex().Equals(RequestID))
                        {
                            Console.WriteLine($"Scratch Card result, Address: {e.Event.Player},  RequestID: {e.Event.RequestId.ToHex()},  Prize Number: {e.Event.Number}");
                            brk = true;
                        }
                    }
                    if (brk)
                    {
                        break;
                    }
                }
            }

            accountBalance = await web3.Eth.GetBalance.SendRequestAsync(account.Address);

            Console.WriteLine($"{account.Address} balance: {Web3.Convert.FromWei(accountBalance)}");

            allowance = await CsService.GetContractAllowanceQueryAsync();

            Console.WriteLine($"Allowance after buying card: {Web3.Convert.FromWei(allowance)}");

            //var transfer = await CsService.TransferToContractRequestAndWaitForReceiptAsync();

            balance = await CsService.GetLinkBalanceQueryAsync(ScratchContractAddress);

            Console.WriteLine($"Contract Balance: {Web3.Convert.FromWei(balance)}");


            // Testing Card Round Stats.

            Console.WriteLine($"Card Round Address: {address}");


            var prize = await RS.UnclaimedPrizesQueryAsync();

            foreach (BigInteger i in prize.Num)
            {
                Console.WriteLine(i);
            }
            Console.WriteLine();

            foreach (BigInteger i in prize.Pays)
            {
                Console.WriteLine(Web3.Convert.FromWei(i));
            }


            // Calling Claim Prize from outside of the Scratch contract

            /*
             * Console.WriteLine("attemptin to call getprize from outside contract...");
             *
             * try
             * {
             *  var address = await CsService.GetCardRoundQueryAsync();
             *
             *  Console.WriteLine($"Card Round Address: {address}");
             *
             *  ScratchCardRoundService RS = new ScratchCardRoundService(web3, address);
             *
             *  ClaimPrizeFunction cpf = new ClaimPrizeFunction
             *  {
             *      Player = address,
             *      RandomNumber = new BigInteger(999999)
             *  };
             *
             *  await RS.ClaimPrizeRequestAndWaitForReceiptAsync(cpf);
             * }
             * catch(Exception e)
             * {
             *  Console.WriteLine(e.Message);
             * }
             */

            Console.WriteLine();
        }