public async void InitializeContract() { var api = new DigibyteAPI(new APIOptions { BaseURL = Program.InsightAPI }); contractService = new ContractService(@"tsc-wallet.db", api); MyContract = contractService.LoadContract(currentContract); if (MyContract != null) { StatusLabel.Text = "สร้างสำเร็จ"; ContractIDtextBox.Text = MyContract.ID; NameLabel.Text = MyContract.NameString; TokenLabel.Text = MyContract.TokenString; TotalSupplyLabel.Text = MyContract.TotalSupply.ToString(); NoOfDecimalLabel.Text = MyContract.NoOfDecimal.ToString(); OwnerPublicLabel.Text = MyContract.OwnerPublicAddress; GenerateQrCode(); CoppyButton = true; } else { StatusLabel.Text = "สร้างไม่สำเร็จ"; StatusLabel.ForeColor = Color.Red; ContractIDtextBox.Text = currentContract; NameLabel.Text = "None"; TokenLabel.Text = "None"; TotalSupplyLabel.Text = "None"; NoOfDecimalLabel.Text = "None"; OwnerPublicLabel.Text = "None"; CoppyButton = false; } }
private void InitializeMoney() { keyService = new KeyService("1234"); saveKey = keyService.GetKey(); //Prepare KeyPair of Sender api = new DigibyteAPI(new APIOptions { BaseURL = Program.InsightAPI }); LoadData(); }
/// <summary> /// A default constructor of contract service /// </summary> /// <returns>A contract service is used to manage electronic money contract in data store.</returns> public ContractService() { var connectionString = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}tsc-wallet.db"; //var connectionString = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "tsc-wallet.db"); var api = new DigibyteAPI(); var network = NBitcoin.Altcoins.Digibyte.Instance.Mainnet; ContractSetup(connectionString, api, network); }
private void InitializeMoney() { keyService = new KeyService("1234"); saveKey = keyService.GetKey(); //Prepare KeyPair of Sender api = new DigibyteAPI(new APIOptions { BaseURL = Program.InsightAPI }); t = new Thread(new ThreadStart(SendDGB)); t.Start(); }
public MainForm() { InitializeComponent(); progressBar1.Maximum = 7; progressBar1.Visible = false; this.refreshbutton.Image = (Image)(new Bitmap(ContractCreate.Properties.Resources.reload1, new Size(32, 32))); client = new HttpClient(); client.BaseAddress = new System.Uri(Program.InsightAPI); api = new DigibyteAPI(new APIOptions { BaseURL = Program.InsightAPI }); string path = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}CurrentContract.txt"; if (!File.Exists(path)) { using (StreamWriter sw = File.CreateText(path)) { } } connectionString = $"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}tsc-wallet.db"; InitializeEMoney(); }
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); }