Esempio n. 1
0
        // Method to Initialize Saved Settings.
        public static void InitializeSettings()
        {
            // Initialize contract variables.
            Configuration configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            KeyValueConfigurationCollection Settings = configFile.AppSettings.Settings;

            // Initialize Contract Address and ABI variables.
            ScratchContractAddress = Settings["ScratchContractAddress"].Value;

            RoundContractAddress = Settings["RoundContractAddress"].Value;

            CsService = new ScratchService(web3, ScratchContractAddress);

            RService = new ScratchCardRoundService(web3, RoundContractAddress);
        }
Esempio n. 2
0
        private static async Task CardInfo()
        {
            var cardRoundAddress = await CsService.GetCardRoundQueryAsync();

            var cardRoundService = new ScratchCardRoundService(web3, cardRoundAddress);

            var cardPrice = await cardRoundService.GetCardPriceQueryAsync();

            Console.WriteLine($"The current Price of a card is: {Web3.Convert.FromWei(cardPrice)}");

            var remainingPrizes = await cardRoundService.UnclaimedPrizesQueryAsync();

            for (int i = 0; i < remainingPrizes.Num.Count; i++)
            {
                Console.WriteLine($"Remaining cards: {remainingPrizes.Num[i]},  with pay: {Web3.Convert.FromWei(remainingPrizes.Pays[i])}");
            }

            Console.WriteLine();
        }
Esempio n. 3
0
        private static async Task Deploy()
        {
            Console.WriteLine("Deploying contract...");

            //GetBalance().Wait();

            ScratchDeployment SD = new ScratchDeployment
            {
                InitialLink = Web3.Convert.ToWei(1)
                              //AmountToSend = 100000000000
            };

            try
            {
                var deployReciept = await ScratchService.DeployContractAndWaitForReceiptAsync(web3, SD);

                ScratchContractAddress = deployReciept.ContractAddress;
                Console.WriteLine($"gas: {Web3.Convert.FromWei(Web3.Convert.ToWei(deployReciept.GasUsed, UnitConversion.EthUnit.Gwei))}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine($"Scratch contract address: {ScratchContractAddress}");

            //var balance = await web3.Eth.GetBalance.SendRequestAsync(ScratchContractAddress);

            //Console.WriteLine($"Scratch contract Ether: {balance}");

            // Open Settings.
            var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            var Settings   = configFile.AppSettings.Settings;

            Settings["ScratchContractAddress"].Value = ScratchContractAddress;

            CsService = new ScratchService(web3, ScratchContractAddress);

            var rec1 = CsService.GetLinkBalanceQueryAsync(account.Address);

            Console.WriteLine($"Account Link Balance: {Web3.Convert.FromWei(rec1.Result)}\n Account Eth Balance: {Web3.Convert.FromWei(await web3.Eth.GetBalance.SendRequestAsync(account.Address))}");

            /*
             * var deposit = new DepositLinkFunction()
             * {
             *  Value = 1
             * };*/

            var transferHandler = web3.Eth.GetContractTransactionHandler <TransferFunction>();
            var transfer        = new TransferFunction()
            {
                To          = ScratchContractAddress,
                TokenAmount = Web3.Convert.ToWei(1)
            };

            Console.WriteLine($"Token amount: {transfer.TokenAmount} in wei");

            var transactionReceipt = await transferHandler.SendRequestAndWaitForReceiptAsync("0xa36085F69e2889c224210F603D836748e7dC0088", transfer);

            rec1 = CsService.GetLinkBalanceQueryAsync(ScratchContractAddress);

            Console.WriteLine($"Link sent to Contract: {Web3.Convert.FromWei(rec1.Result)}");

            RoundContractAddress = await CsService.GetCardRoundQueryAsync();

            Settings["RoundContractAddress"].Value = RoundContractAddress;

            RService = new ScratchCardRoundService(web3, RoundContractAddress);


            // Save Settings
            configFile.Save(ConfigurationSaveMode.Modified);

            ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);

            Console.WriteLine("\n");
        }
Esempio n. 4
0
        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();
        }
Esempio n. 5
0
        private static async Task BuyCard()
        {
            var accountEtherBalance = await web3.Eth.GetBalance.SendRequestAsync(account.Address);

            var accountLinkBalance = Web3.Convert.FromWei(await CsService.GetLinkBalanceQueryAsync(account.Address));

            Console.WriteLine($" Account: {account.Address}\n  Ether Balance: {Web3.Convert.FromWei(accountEtherBalance)}\n  Link Balance: {accountLinkBalance}\n");

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

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

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

            // Get Price of a card.
            //var LinkPrice = await CsService.RequestLinkPriceQueryAsync();

            //var CardValue = Web3.Convert.FromWei((2000000000000000000 / LinkPrice) * 100000000);

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

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

            ScratchCardRoundService RS = new ScratchCardRoundService(web3, address);

            var CardValue = Web3.Convert.FromWei(await RS.GetCardPriceQueryAsync() + 100000000000000000);

            Console.WriteLine($"Price of Card: {CardValue} \n");

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

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

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

            var buyCard = await CsService.BuyScatchCardRequestAndWaitForReceiptAsync(seed);

            // Getting the RequestID emited from the BuyScratchCard Request.
            var RequestID = buyCard.DecodeAllEvents <RequestIDEventDTO>()[0].Event.RequestId.ToHex();

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

            /*
             * var fulfillHandler = CsService.ContractHandler.GetEvent<RequestFulfilledEventDTO>();
             *
             * var filterFulfill = fulfillHandler.CreateFilterInput();
             *
             * while (true)
             * {
             *  var requests = await fulfillHandler.GetAllChanges(filterFulfill);
             *  if(requests.Count > 0)
             *  {
             *      Console.WriteLine($"Raw Random Number: {requests[0].Event.Randomness}");
             *      break;
             *  }
             *
             * }*/

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

            var filterPrizeEvents = PrizeEventHandler.CreateFilterInput();


            decimal prize = 0;

            // Getting the PrizeClaim event with the correct RequestID
            while (true)
            {
                var allRequestEvents = await PrizeEventHandler.GetAllChanges(filterPrizeEvents);

                if (allRequestEvents.Count > 0)
                {
                    bool brk = false;
                    foreach (EventLog <PrizeClaimEventDTO> e in allRequestEvents)
                    {
                        //Console.WriteLine($"Scratch Card result, Address: {e.Event.Player},  RequestID: {e.Event.RequestId.ToHex()},  Prize Number: {e.Event.Number}, Prize: {Web3.Convert.FromWei(e.Event.Prize)}\n");
                        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}, Prize: {Web3.Convert.FromWei(e.Event.Prize)}\n");
                            prize = Web3.Convert.FromWei(e.Event.Prize);
                            brk   = true;
                        }
                    }
                    if (brk)
                    {
                        break;
                    }
                }
            }

            Console.WriteLine($"Initial Account Link Balance: {accountLinkBalance},  Card Value: {CardValue}, Prize value: {prize}");

            Console.WriteLine($"Expected Link Balance: {accountLinkBalance - CardValue + prize}\n");

            accountLinkBalance = Web3.Convert.FromWei(await CsService.GetLinkBalanceQueryAsync(account.Address));
            Console.WriteLine($"Link Balance: {accountLinkBalance}\n");

            var ScratchTokenAddress = await CsService.GetTokenQueryAsync();

            ScratchTokenService tokenService = new ScratchTokenService(web3, ScratchTokenAddress);

            var ScratchTokenBalance = await tokenService.BalanceOfQueryAsync(account.Address);

            Console.WriteLine($"Scratch Token Balance: {ScratchTokenBalance}");
        }