private void Button_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         this.output             = new Tools.Output();
         this.output.isTheChange = false;
         this.output.assetID     = (this.tokens.SelectedItem as Asset).assetid;
         if (this.netfee.IsChecked == true)
         {
             this.output.Target = "netfee";
         }
         else if (this.systemfee.IsChecked == true)
         {
             this.output.Target = "systemfee";
         }
         else
         {
             var hash = ThinNeo.Helper.GetPublicKeyHashFromAddress(this.tboxAddr.Text);
             this.output.Target = ThinNeo.Helper.GetAddressFromScriptHash(hash);
         }
         this.output.Fix8 = decimal.Parse(tboxValue.Text);
         if (this.output.Fix8 <= 0)
         {
             throw new Exception("must have a value greatthan zero.");
         }
         this.DialogResult = true;
     }
     catch (Exception err)
     {
         MessageBox.Show(err.Message);
     }
 }
        private void Button_Click_8(object sender, RoutedEventArgs e)
        {
            listTestScript.Items.Clear();

            try
            {
                var    symbol = ThinNeo.Helper.Bytes2HexString(lastScript);
                byte[] data;
                //var strget = WWW.MakeRpcUrl(labelApi.Text, "invokescript", new MyJson.JsonNode_ValueString(symbol));
                //var resultstr = WWW.GetWithDialog(this, strget);

                var str       = WWW.MakeRpcUrlPost(labelApi.Text, "invokescript", out data, new MyJson.JsonNode_ValueString(symbol));
                var resultstr = WWW.PostWithDialog(this, str, data);

                var json = MyJson.Parse(resultstr).AsDict();
                var gas  = json["result"].AsList()[0].AsDict()["gas_consumed"].ToString();
                lastFee       = decimal.Parse(gas);
                labelFee.Text = "Fee:" + lastFee;
                StringBuilder sb = new StringBuilder();
                json["result"].AsList()[0].AsDict().ConvertToStringWithFormat(sb, 4);

                //移除撒GAS
                foreach (Tools.Output list in listOutput.Items)
                {
                    if (string.IsNullOrEmpty(list.Target) && Tools.CoinTool.id_GAS == list.assetID)
                    {
                        listOutput.Items.Remove(list);
                        updateOutput();
                        break;
                    }
                }
                if (lastFee > 10)
                {
                    Tools.Output newsb = new Tools.Output();
                    newsb.assetID     = Tools.CoinTool.id_GAS;
                    newsb.isTheChange = false;
                    newsb.Target      = "";
                    newsb.Fix8        = (lastFee.Value - (decimal)10.0);
                    listOutput.Items.Add(newsb);
                    updateOutput();
                }
                var lines = sb.ToString().Split('\n');
                foreach (var l in lines)
                {
                    listTestScript.Items.Add(l);
                }
            }
            catch (Exception err)
            {
                listTestScript.Items.Add("error:" + err.Message);
            }
        }
Exemple #3
0
 //delete output
 private void MenuItem_Click(object sender, RoutedEventArgs e)
 {
     Tools.Output output = listOutput.SelectedItem as Tools.Output;
     if (output == null)
     {
         return;
     }
     if (output.isTheChange)
     {
         return;
     }
     listOutput.Items.Remove(output);
     updateOutput();
 }
Exemple #4
0
        void _updateOutput()
        {
            Dictionary <string, Dictionary <string, decimal> > incoins = new Dictionary <string, Dictionary <string, decimal> >();
            Dictionary <string, bool> witnesses = new Dictionary <string, bool>();

            foreach (Tools.Input input in listInput.Items)
            {
                if (incoins.ContainsKey(input.From) == false)
                {
                    incoins[input.From] = new Dictionary <string, decimal>();
                }
                witnesses[input.From] = input.IsSmartContract;

                var incoin = incoins[input.From];
                if (incoin.ContainsKey(input.Coin.assetID) == false)
                {
                    incoin[input.Coin.assetID] = input.Coin.value;
                }
                else
                {
                    incoin[input.Coin.assetID] += input.Coin.value;
                }
            }

            {//处理见证人
                List <string>        witness  = new List <string>();
                List <Tools.Witnees> needdelw = new List <Tools.Witnees>();

                foreach (Tools.Witnees w in listWitness.Items)
                {
                    //无用鉴证人清除
                    if (w.IsSmartContract == false && witnesses.ContainsKey(w.address) == false)
                    {
                        needdelw.Add(w);
                    }
                    else
                    {
                        witness.Add(w.address);
                    }
                }
                foreach (var d in needdelw)
                {
                    listWitness.Items.Remove(d);
                }

                foreach (var f in witnesses.Keys)
                {
                    if (witness.Contains(f) == false)
                    {
                        Tools.Witnees wit = new Tools.Witnees();
                        wit.IsSmartContract = witnesses[f];
                        wit.address         = f;
                        wit.iscript         = null;
                        listWitness.Items.Add(wit);
                    }
                }
            }
            //去除无法完成的输出
            List <Tools.Output> needdel = new List <Tools.Output>();

            foreach (Tools.Output output in listOutput.Items)
            {
                bool bCon = false;
                foreach (var incoin in incoins)
                {
                    if (incoin.Value.ContainsKey(output.assetID))
                    {
                        bCon = true;
                        break;
                    }
                }
                if (bCon == false)
                {
                    needdel.Add(output);
                }
            }
            foreach (var del in needdel)
            {
                listOutput.Items.Remove(del);
            }

            //交换合约(utxo来自多个人),不自动找零
            if (incoins.Count > 1)
            {
                return;
            }

            //清除找零
            needdel.Clear();
            foreach (Tools.Output output in listOutput.Items)
            {
                if (output.isTheChange)
                {
                    needdel.Add(output);
                }
            }
            foreach (var del in needdel)
            {
                listOutput.Items.Remove(del);
            }


            var moneys = incoins.First().Value;

            foreach (var money in moneys)
            {
                decimal value = 0;
                foreach (Tools.Output output in listOutput.Items)
                {
                    if (output.assetID == money.Key)
                    {
                        value += (decimal)output.Fix8;
                    }
                }
                decimal last = money.Value - value;
                if (last > 0)
                {
                    Tools.Output vout = new Tools.Output();
                    vout.assetID     = money.Key;
                    vout.isTheChange = true;
                    vout.Target      = incoins.First().Key;
                    vout.Fix8        = last;
                    listOutput.Items.Add(vout);
                }
                else if (last < 0)
                {
                    throw new Exception("too mush output money.");
                }
            }
        }
        void _updateOutput()
        {
            Dictionary <string, Dictionary <string, decimal> > incoins = new Dictionary <string, Dictionary <string, decimal> >();
            List <string> needWitness = new List <string>();

            //智能合約輸入,記錄
            Dictionary <string, byte[]> hadscript = new Dictionary <string, byte[]>();

            foreach (Tools.Input input in listInput.Items)
            {
                if (incoins.ContainsKey(input.Address) == false)
                {
                    incoins[input.Address] = new Dictionary <string, decimal>();
                }

                var incoin = incoins[input.Address];
                if (incoin.ContainsKey(input.Coin.assetID) == false)
                {
                    incoin[input.Coin.assetID] = input.Coin.value;
                }
                else
                {
                    incoin[input.Coin.assetID] += input.Coin.value;
                }

                if (needWitness.Contains(input.Address) == false)
                {
                    needWitness.Add(input.Address);
                }
                if (input.Script != null)
                {
                    hadscript[input.Address] = input.Script;
                }
            }

            var pubkey = this.privatekey != null?ThinNeo.Helper.GetPublicKeyFromPrivateKey(this.privatekey) : null;

            var address = pubkey != null?ThinNeo.Helper.GetAddressFromPublicKey(pubkey) : null;

            {//处理见证人
                List <string> witness = new List <string>();
                foreach (ThinNeo.Witness w in listWitness.Items)
                {
                    witness.Add(w.Address);
                }


                foreach (var f in needWitness)
                {
                    if (witness.Contains(f) == false)
                    {
                        ThinNeo.Witness wit = new ThinNeo.Witness();
                        if (f == address)
                        {
                            wit.VerificationScript = ThinNeo.Helper.GetScriptFromPublicKey(pubkey);
                        }
                        else if (hadscript.ContainsKey(f))//自帶脚本
                        {
                            wit.VerificationScript = hadscript[f];
                        }
                        else
                        {
                            byte[] script = rpc_getScript(ThinNeo.Helper.GetPublicKeyHashFromAddress(f));
                            if (script != null)//并不知道试试是不是公開的智能合約
                            {
                                wit.VerificationScript = script;
                            }
                        }
                        listWitness.Items.Add(wit);
                    }
                }
            }
            //去除无法完成的输出
            List <Tools.Output> needdel = new List <Tools.Output>();

            foreach (Tools.Output output in listOutput.Items)
            {
                bool bCon = false;
                foreach (var incoin in incoins)
                {
                    if (incoin.Value.ContainsKey(output.assetID))
                    {
                        bCon = true;
                        break;
                    }
                }
                if (bCon == false)
                {
                    needdel.Add(output);
                }
            }
            foreach (var del in needdel)
            {
                listOutput.Items.Remove(del);
            }

            //交换合约(utxo来自多个人),不自动找零
            if (incoins.Count > 1)
            {
                return;
            }

            //清除找零
            needdel.Clear();
            foreach (Tools.Output output in listOutput.Items)
            {
                if (output.isTheChange)
                {
                    needdel.Add(output);
                }
            }
            foreach (var del in needdel)
            {
                listOutput.Items.Remove(del);
            }


            var moneys = incoins.First().Value;

            foreach (var money in moneys)
            {
                decimal value = 0;
                foreach (Tools.Output output in listOutput.Items)
                {
                    if (output.assetID == money.Key)
                    {
                        value += (decimal)output.Fix8;
                    }
                }
                decimal last = money.Value - value;
                if (last > 0)
                {
                    Tools.Output vout = new Tools.Output();
                    vout.assetID     = money.Key;
                    vout.isTheChange = true;
                    vout.Target      = incoins.First().Key;
                    vout.Fix8        = last;
                    listOutput.Items.Add(vout);
                }
                else if (last < 0)
                {
                    throw new Exception("too mush output money.");
                }
            }
        }