private async void SendDGB()
        {
            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Visible", true);
            InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", true);
            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 1);
            var txid = await BroadcastTransaction(tx.ToHex());

            int issuedRepeat = 30;

            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 2);
start:
            string verifyTransaction = "ตรวจสอบธุรกรรมการส่งเงินครั้งที่ " + (31 - issuedRepeat);

            InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Text", verifyTransaction);
            var txContract = await api.GetTransactionInfo(txid.txid);

            if (txContract.time == 0 && txContract.blocktime == 0)
            {
                if (issuedRepeat > 0)
                {
                    issuedRepeat--;
                    Thread.Sleep(5000);
                    goto start;
                }
                else
                {
                    InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                    MessageBox.Show("การออกเงินให้เจ้าของเงินไม่สมบูรณ์ กรุณาตรวจสอบรหัสธุรกรรมการออกเงิน :" + txid.txid);
                    triggerFinish = true;
                    Action action1 = new Action(FinishCreating);
                    this.BeginInvoke(action1);
                    return;
                }
            }
            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 3);
            MessageBox.Show("ส่งเงินสำเร็จ");

            triggerFinish = true;

            Action action = new Action(FinishCreating);

            this.BeginInvoke(action);
        }
        private async void SendDGB()
        {
            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 1);
            Network       network = NBitcoin.Altcoins.Digibyte.Instance.Mainnet;
            BitcoinSecret sender  = saveKey.SecretKey;

            var myTransaction = Transaction.Create(network);

            var fee     = new Money(Fee);
            var allUtxo = await GetReceivedCoinFromDigibyte(sender.GetAddress().ToString());

            var utxo = allUtxo.OrderByDescending(u => u.amount).First().AsCoin();
            //var utxo = allUtxo[0].ToCoin();

            var txBuilder = network.CreateTransactionBuilder();

            for (int i = 0; i < allUtxo.Count(); i++)
            {
                txBuilder = txBuilder.AddCoins(allUtxo[i].AsCoin());
            }

            txBuilder.AddKeys(sender);

            foreach (var a in LowMoneyWallet)
            {
                try
                {
                    BitcoinAddress receiver;
                    if (a.account_id.publickey.StartsWith("dgb1"))
                    {
                        receiver = new BitcoinWitPubKeyAddress(a.account_id.publickey, network);
                    }
                    else
                    {
                        receiver = new BitcoinPubKeyAddress(a.account_id.publickey, network);
                    }

                    txBuilder
                    .Send(receiver, new Money(MoneyToSend));
                }
                catch
                {
                    continue;
                }
            }
            tx = txBuilder
                 .SetChange(sender.GetAddress())
                 .SendFees(fee)
                 .BuildTransaction(true);

            var verified = txBuilder.Verify(tx);

            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 2);

            var txid = await BroadcastTransaction(tx.ToHex());

            int issuedRepeat = 20;

            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 3);
start:
            var txContract = await api.GetTransactionInfo(txid.txid);

            if (txContract.time == 0 && txContract.blocktime == 0)
            {
                if (issuedRepeat > 0)
                {
                    issuedRepeat--;
                    Thread.Sleep(5000);
                    goto start;
                }
                else
                {
                    InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                    MessageBox.Show("การออกเงินให้เจ้าของเงินไม่สมบูรณ์ กรุณาตรวจสอบรหัสธุรกรรมการออกเงิน :" + txid.txid);
                    triggerFinish = true;
                    Action action1 = new Action(FinishCreating);
                    this.BeginInvoke(action1);
                    return;
                }
            }
            InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Value", 4);
            MessageBox.Show("ส่งเงินสำเร็จ");

            triggerFinish = true;
            Action action = new Action(FinishCreating);

            this.BeginInvoke(action);
        }
示例#3
0
        private async void CreateContract()
        {
            InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", true);
            InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Text", "กำลังสร้างสัญญาเงินอิเล็กทรอนิกส์");
            var     contract    = Encoding.UTF8.GetBytes(contracttextbox.Text);;
            var     token       = Encoding.UTF8.GetBytes(tokentextbox.Text);;
            decimal amount      = Decimal.Parse(AmountTextBox.Text);
            ushort  noOfdecimal = Convert.ToUInt16(NoOfDecimalTextBox.Text);

            WalletContract mycontract = null;
            var            api        = new DigibyteAPI(new APIOptions {
                BaseURL = Program.InsightAPI
            });

            contractService = new ContractService(@"tsc-wallet.db", api);
            mycontract      = await contractService.CreateContract(privateKey, contract, token, amount, noOfdecimal);

            SetControlPropertyThreadSafe(progressBar1, "Value", 2);
            contractID = mycontract.ID;
            MessageBox.Show("รหัสสัญญา :" + contractID);        //test Show Contract ID

            int  CountConnect    = 0;
            bool triggerContract = false;

            transactions = new TransBlockchain();
            while (CountConnect <= 15)        //find contractID in blockchain
            {
                InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Text", "กำลังตรวจสอบธุรกรรมการสร้างสัญญา");
                try
                {
                    var response = await client.GetAsync("/api/tx/" + contractID);

                    response.EnsureSuccessStatusCode();
                    var result = await response.Content.ReadAsStringAsync();

                    transactions = JsonConvert.DeserializeObject <TransBlockchain>(result);


                    if (transactions.time != 0 && transactions.blocktime != 0)
                    {
                        triggerContract = true;
                        break;
                    }

                    else
                    {
                        CountConnect++;
                        Thread.Sleep(5000);
                    }
                }
                catch
                {
                    CountConnect++;
                    Thread.Sleep(5000);
                }
            }
            SetControlPropertyThreadSafe(progressBar1, "Value", 3);
            if (!triggerContract)
            {
                InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", false);
                SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                MessageBox.Show("ไม่ค้นพบสัญญาเงินอิเล็กทรอนิกส์ในบล็อกเชน :" + contractID);
                return;
            }

            resultContract = await contractService.FindContract(contractID);

            SetControlPropertyThreadSafe(progressBar1, "Value", 4);

            if (resultContract == null)      //if dont seen this transaction. exite method
            {
                InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", false);
                SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                MessageBox.Show("การสร้างรหัสสัญญาไม่สำเร็จ");
                return;
            }


            //Check Block of Contract and LastBlock. because if block of contract is lastblock, calculating of cryptocurrency balance will be issue
            bool triggerAllow = false;

            CountConnect = 0;
            while (CountConnect <= 10)
            {
                try
                {
                    int ContractBlock = await api.GetBlockHeight(transactions.blockhash);

                    var LastSync = await api.GetSync();

                    int Lastblock = LastSync.blockChainHeight;
                    if (ContractBlock != Lastblock)
                    {
                        triggerAllow = true;
                        break;
                    }
                    else
                    {
                        CountConnect++;
                        Thread.Sleep(3000);
                    }
                }
                catch
                {
                    CountConnect++;
                    Thread.Sleep(3000);
                }
            }
            SetControlPropertyThreadSafe(progressBar1, "Value", 5);
            if (!triggerAllow)
            {
                InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", false);
                InvokeHelp.SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                MessageBox.Show("ขณะนี้ยังไม่สามารถออกเงินได้ ลองอีกครั้ง");
                return;
            }

            Thread.Sleep(5000);                     //wait mined
            int issuedRepeat = 2;

start:
            InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Text", "กำลังออกเงิน");
            var walletService = contractService.CreateWalletService(resultContract, privateKey);    //Create issued Ledger
            var ledger1       = walletService.CreateLedger(OperationCode.Issue);

            await walletService.BroadcastLedger(ledger1);

            SetControlPropertyThreadSafe(progressBar1, "Value", 6);
            MessageBox.Show("รหัสการออกเงิน :" + ledger1.TxId);        //test Show issued ID

            transactions = new TransBlockchain();
            int  Count        = 0;
            bool IssueTrigger = false;

            while (Count <= 15)
            {
                InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Text", "กำลังตรวจสอบธุรกรรมการออกเงิน");
                try
                {
                    var response2 = await client.GetAsync("/api/tx/" + ledger1.TxId);

                    response2.EnsureSuccessStatusCode();
                    var result = await response2.Content.ReadAsStringAsync();

                    transactions = JsonConvert.DeserializeObject <TransBlockchain>(result);


                    if (transactions.time != 0 && transactions.blocktime != 0)
                    {
                        IssueTrigger = true;
                        break;
                    }

                    else
                    {
                        Count++;
                        Thread.Sleep(5000);
                    }
                }
                catch
                {
                    Count++;
                    Thread.Sleep(5000);
                }
            }
            SetControlPropertyThreadSafe(progressBar1, "Value", 7);
            if (!IssueTrigger)     //if
            {
                InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", false);
                SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                MessageBox.Show("หมดเวลาการรอ กรุณาตรวจสอบรหัสธุรกรรมการออกเงินด้วยตัวเองที่ตัวสำรวจบล็อก :" + transactions.txid);
                return;         //exite ,method
            }

            var txContract = await api.GetTransactionInfo(contractID);

            var txIssued = await api.GetTransactionInfo(ledger1.TxId);

            if (txContract.blocktime > txIssued.blocktime)         //if Contract blocktime more than Issued blocktime. New Issued again.
            {
                if (issuedRepeat > 0)
                {
                    issuedRepeat--;
                    goto start;
                }
                else
                {
                    InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Visible", false);
                    SetControlPropertyThreadSafe(progressBar1, "Visible", false);
                    MessageBox.Show("หมดเวลาการรอ กรุณาตรวจสอบรหัสธุรกรรมการออกเงินด้วยตัวเองที่ตัวสำรวจบล็อก :" + txIssued.txid);
                    return;
                }
            }
            InvokeHelp.SetControlPropertyThreadSafe(progressLabel, "Text", "สร้างสัญญาเงินอิเล็กทรอนิกส์สำเร็จ");
            triggerFinish = true;
            Action action = new Action(FinishCreating);

            this.BeginInvoke(action);
        }
示例#4
0
        private async void SendDigibyte()
        {
            InvokeHelp.SetControlPropertyThreadSafe(Sendinglabel, "Text", "กำลังส่งเงินอัตโนมัติ");
            InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Visible", true);
            InvokeHelp.SetControlPropertyThreadSafe(refreshbutton, "Enabled", false);
            InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Value", 1);
            Network       network = NBitcoin.Altcoins.Digibyte.Instance.Mainnet;
            BitcoinSecret sender  = saveKey.SecretKey;

            var myTransaction = Transaction.Create(network);

            var fee     = new Money(Fee);
            var allUtxo = await GetReceivedCoinFromDigibyte(sender.GetAddress().ToString());

            var utxo = allUtxo.OrderByDescending(u => u.amount).First().AsCoin();
            //var utxo = allUtxo[0].ToCoin();

            var txBuilder = network.CreateTransactionBuilder();

            for (int i = 0; i < allUtxo.Count(); i++)
            {
                txBuilder = txBuilder.AddCoins(allUtxo[i].AsCoin());
            }

            txBuilder.AddKeys(sender);

            foreach (var a in LowMoneyWallet)
            {
                try
                {
                    BitcoinAddress receiver;
                    if (a.account_id.publickey.StartsWith("dgb1"))
                    {
                        receiver = new BitcoinWitPubKeyAddress(a.account_id.publickey, network);
                    }
                    else
                    {
                        receiver = new BitcoinPubKeyAddress(a.account_id.publickey, network);
                    }

                    txBuilder
                    .Send(receiver, new Money(MoneyToSend));
                }
                catch
                {
                    continue;
                }
            }
            tx = txBuilder
                 .SetChange(sender.GetAddress())
                 .SendFees(fee)
                 .BuildTransaction(true);

            var verified = txBuilder.Verify(tx);

            InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Value", 2);

            var txid = await BroadcastTransaction(tx.ToHex());

            int issuedRepeat = 30;

            InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Value", 3);
start:
            var txContract = await api.GetTransactionInfo(txid.txid);

            if (txContract.time == 0 && txContract.blocktime == 0)
            {
                if (issuedRepeat > 0)
                {
                    issuedRepeat--;
                    Thread.Sleep(5000);
                    goto start;
                }
                else
                {
                    InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Visible", false);
                    InvokeHelp.SetControlPropertyThreadSafe(Sendinglabel, "Text", "ส่งเงินไม่สำเร็จ");
                    timer.Start();
                    return;
                }
            }


            InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Value", 4);
            InvokeHelp.SetControlPropertyThreadSafe(progressBar2, "Visible", false);

            InvokeHelp.SetControlPropertyThreadSafe(Sendinglabel, "ForeColor", Color.Green);
            InvokeHelp.SetControlPropertyThreadSafe(Sendinglabel, "Text", "ส่งเงินอัตโนมัติสำเร็จ");

            InvokeHelp.SetControlPropertyThreadSafe(timeSendinglabel, "Text", DateTime.Now.ToString("HH:mm dd/MM/yyyy"));
            InvokeHelp.SetControlPropertyThreadSafe(NumOfWalletSendlabel, "Text", LowMoneyWallet.Count().ToString());

            InvokeHelp.SetControlPropertyThreadSafe(totalSendlabel, "ForeColor", Color.BlueViolet);

            decimal totaltSent = LowMoneyWallet.Count() * (MoneyToSend / 100000000m);

            totaltSent = totaltSent + (Fee / 100000000m);
            InvokeHelp.SetControlPropertyThreadSafe(totalSendlabel, "Text", totaltSent.ToString());

            InvokeHelp.SetControlPropertyThreadSafe(refreshbutton, "Enabled", true);

            LoadData();
            t.Abort();
        }