Example #1
0
        //拼交易体
        static Transaction makeTran(Dictionary <string, List <Utxo> > dir_utxos, string[] targetaddrs, ThinNeo.Hash256 assetid, decimal sendcount)
        {
            if (!dir_utxos.ContainsKey(assetid.ToString()))
            {
                throw new Exception("no enough money.");
            }

            List <Utxo> utxos = dir_utxos[assetid.ToString()];

            var tran = new Transaction();

            tran.type    = TransactionType.ContractTransaction;
            tran.version = 0;//0 or 1
            tran.extdata = null;

            tran.attributes = new ThinNeo.Attribute[0];
            utxos.Sort((a, b) =>
            {
                if (a.value > b.value)
                {
                    return(1);
                }
                else if (a.value < b.value)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });

            //十进制小数
            decimal count   = decimal.Zero;
            string  scraddr = "";
            List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>();

            for (var i = 0; i < utxos.Count; i++)
            {
                ThinNeo.TransactionInput input = new ThinNeo.TransactionInput();
                input.hash  = utxos[i].txid;
                input.index = (ushort)utxos[i].n;
                list_inputs.Add(input);
                count  += utxos[i].value;
                scraddr = utxos[i].addr;
                if (count >= (sendcount))
                {
                    break;
                }
            }

            tran.inputs = list_inputs.ToArray();

            if (count >= sendcount)//输入大于等于输出
            {
                List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>();
                //输出
                if (sendcount > decimal.Zero && targetaddrs.Length > 0)
                {
                    foreach (string targetaddr in targetaddrs)
                    {
                        ThinNeo.TransactionOutput output = new ThinNeo.TransactionOutput();
                        output.assetId   = assetid;
                        output.value     = sendcount;
                        output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(targetaddr);
                        list_outputs.Add(output);
                    }
                }

                //找零
                var change = count - sendcount;
                if (change > decimal.Zero)
                {
                    ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput();
                    outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(scraddr);
                    outputchange.value     = change;
                    outputchange.assetId   = assetid;
                    list_outputs.Add(outputchange);
                }
                tran.outputs = list_outputs.ToArray();
            }
            else
            {
                throw new Exception("no enough money.");
            }
            return(tran);
        }
Example #2
0
        private static ThinNeo.Transaction makeTran(List <Utxo> utxos, string targetaddr, ThinNeo.Hash256 assetid, decimal sendcount, decimal extgas = 0, List <Utxo> utxos_ext = null, string extaddr = null)
        {
            var tran = new ThinNeo.Transaction();

            tran.type = ThinNeo.TransactionType.ContractTransaction;
            if (extgas >= 1)
            {
                tran.version = 1;//0 or 1
            }
            else
            {
                tran.version = 0;//0 or 1
            }
            tran.extdata = null;

            tran.attributes = new ThinNeo.Attribute[0];
            var scraddr = "";

            utxos.Sort((a, b) =>
            {
                if (a.value > b.value)
                {
                    return(1);
                }
                else if (a.value < b.value)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });
            decimal count = decimal.Zero;
            List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>();

            for (var i = 0; i < utxos.Count; i++)
            {
                ThinNeo.TransactionInput input = new ThinNeo.TransactionInput();
                input.hash  = utxos[i].txid;
                input.index = (ushort)utxos[i].n;
                list_inputs.Add(input);
                count  += utxos[i].value;
                scraddr = utxos[i].addr;
                if (count >= sendcount)
                {
                    break;
                }
            }
            decimal count_ext = decimal.Zero;

            if (utxos_ext != null)
            {
                //手续费
                ThinNeo.TransactionInput input = new ThinNeo.TransactionInput();
                input.hash  = utxos_ext[0].txid;
                input.index = (ushort)utxos_ext[0].n;
                count_ext   = utxos_ext[0].value;
                list_inputs.Add(input);
            }

            tran.inputs = list_inputs.ToArray();
            if (count >= sendcount)//输入大于等于输出
            {
                List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>();
                //输出
                if (sendcount > decimal.Zero && targetaddr != null)
                {
                    ThinNeo.TransactionOutput output = new ThinNeo.TransactionOutput();
                    output.assetId   = assetid;
                    output.value     = sendcount;
                    output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(targetaddr);
                    list_outputs.Add(output);
                }
                var     change    = count - sendcount - extgas;
                decimal extchange = decimal.Zero;
                //找零
                if (utxos_ext != null)
                {
                    change    = count - sendcount;
                    extchange = count_ext - extgas;
                }
                else
                {
                    change = count - sendcount - extgas;
                }
                if (change > decimal.Zero)
                {
                    ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput();
                    outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(scraddr);
                    outputchange.value     = change;
                    outputchange.assetId   = assetid;
                    list_outputs.Add(outputchange);
                }
                if (extchange > decimal.Zero)
                {
                    ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput();
                    outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(extaddr);
                    outputchange.value     = extchange;
                    outputchange.assetId   = assetid;
                    list_outputs.Add(outputchange);
                }
                tran.outputs = list_outputs.ToArray();
            }
            else
            {
                throw new Exception("no enough money.");
            }
            return(tran);
        }
Example #3
0
        public static ThinNeo.Transaction makeGasTran(ref List <Utxo> list_Gas, Dictionary <string, string> usedUtxoDic, Hash256 assetid, decimal gasfee)
        {
            var tran = new ThinNeo.Transaction();

            tran.type    = ThinNeo.TransactionType.ContractTransaction;
            tran.version = 0;//0 or 1

            tran.attributes = new ThinNeo.Attribute[0];
            var scraddr = "";

            decimal count = decimal.Zero;
            List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>();

            for (var i = list_Gas.Count - 1; i >= 0; i--)
            {
                if (usedUtxoDic.ContainsKey(list_Gas[i].txid.ToString() + list_Gas[i].n))
                {
                    continue;
                }

                ThinNeo.TransactionInput input = new ThinNeo.TransactionInput();
                input.hash  = list_Gas[i].txid;
                input.index = (ushort)list_Gas[i].n;
                list_inputs.Add(input);
                count  += list_Gas[i].value;
                scraddr = list_Gas[i].addr;
                list_Gas.Remove(list_Gas[i]);
                if (count >= gasfee)
                {
                    break;
                }
            }

            tran.inputs = list_inputs.ToArray();
            if (count >= gasfee)//输入大于等于输出
            {
                List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>();
                //输出
                //if (gasfee > decimal.Zero && targetaddr != null)
                //{
                //    ThinNeo.TransactionOutput output = new ThinNeo.TransactionOutput();
                //    output.assetId = assetid;
                //    output.value = gasfee;
                //    output.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(targetaddr);
                //    list_outputs.Add(output);
                //}

                //找零
                var change = count - gasfee;
                if (change > decimal.Zero)
                {
                    decimal splitvalue = (decimal)0.01;
                    int     i          = 0;
                    while (change > splitvalue && list_Gas.Count - 10 < usedUtxoDic.Count)
                    {
                        ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput();
                        outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(scraddr);
                        outputchange.value     = splitvalue;
                        outputchange.assetId   = assetid;
                        list_outputs.Add(outputchange);
                        change -= splitvalue;
                        i      += 1;
                        if (i > 50)
                        {
                            break;
                        }
                    }

                    if (change > 0)
                    {
                        ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput();
                        outputchange.toAddress = ThinNeo.Helper.GetPublicKeyHashFromAddress(scraddr);
                        outputchange.value     = change;
                        outputchange.assetId   = assetid;
                        list_outputs.Add(outputchange);
                    }
                }

                tran.outputs = list_outputs.ToArray();
            }
            else
            {
                throw new Exception("no enough money.");
            }
            return(tran);
        }
Example #4
0
        private Transaction MakeTran_Hasfee(Transaction tran, decimal systemfee, decimal netfee)
        {
            string      assetid = id_GAS;
            List <Utxo> utxos   = GetUtxo(Address, assetid);

            tran.attributes = new ThinNeo.Attribute[0];
            utxos.Sort((a, b) =>
            {
                if (a.value > b.value)
                {
                    return(1);
                }
                else if (a.value < b.value)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });
            decimal count = decimal.Zero;
            List <ThinNeo.TransactionInput> list_inputs = new List <ThinNeo.TransactionInput>();

            for (var i = 0; i < utxos.Count; i++)
            {
                ThinNeo.TransactionInput input = new ThinNeo.TransactionInput();
                input.hash  = utxos[i].txid;
                input.index = (ushort)utxos[i].n;
                list_inputs.Add(input);
                count += utxos[i].value;
                if (count >= netfee + systemfee)
                {
                    break;
                }
            }

            tran.inputs = list_inputs.ToArray();
            if (count >= netfee + systemfee)//输入大于等于输出
            {
                List <ThinNeo.TransactionOutput> list_outputs = new List <ThinNeo.TransactionOutput>();

                //找零
                decimal change = count - netfee - systemfee;

                if (change > decimal.Zero)
                {
                    ThinNeo.TransactionOutput outputchange = new ThinNeo.TransactionOutput();
                    outputchange.toAddress = SciptHash;
                    outputchange.value     = change;
                    outputchange.assetId   = new Hash256(id_GAS);
                    list_outputs.Add(outputchange);
                }
                tran.outputs = list_outputs.ToArray();
            }
            else
            {
                throw new Exception("no enough money.");
            }
            return(tran);
        }