Beispiel #1
0
        //查询标志
        async Task test_symbol()
        {
            var result = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "symbol", null);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine(item.subItem[0].AsString());
        }
Beispiel #2
0
        //查询最小单位
        async Task test_decimals()
        {
            var result = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "decimals", null);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine(item.subItem[0].AsInteger());
        }
Beispiel #3
0
        //查询名字
        async Task test_name()
        {
            var result = await sneo_common.api_InvokeScript(Config.sneo, "name", null);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine(item.subItem[0].AsString());
        }
Beispiel #4
0
        //查询总量
        async Task test_totalSupply()
        {
            var result = await sneo_common.api_InvokeScript(Config.sneo, "totalSupply", null);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine(Helper.changeDecimals(item.subItem[0].AsInteger(), 8));
        }
Beispiel #5
0
        async Task test_getRefundTarget()
        {
            Console.WriteLine("Input txid:");
            string txid   = Console.ReadLine();
            var    result = await sneo_common.api_InvokeScript(Config.sneo, "getRefundTarget", "(hex256)" + txid);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine("value:" + ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160()));
        }
Beispiel #6
0
        //查询余额
        async Task test_BalanceOf()
        {
            Console.WriteLine("Input target address:");
            string addr = Console.ReadLine();


            var result = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "balanceOf", "(addr)" + addr);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine(item.subItem[0].AsInteger());
        }
Beispiel #7
0
        async Task test_getTXInfo()
        {
            Console.WriteLine("Input txid:");
            string txid   = Console.ReadLine();
            var    result = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "getTXInfo", "(hex256)" + txid);

            sneo_common.ResultItem   item  = result.value;
            sneo_common.ResultItem[] items = item.subItem[0].subItem;

            //查询交易详细信息
            Console.WriteLine("from:" + ThinNeo.Helper.GetAddressFromScriptHash(items[0].AsHash160()));
            Console.WriteLine("to:" + ThinNeo.Helper.GetAddressFromScriptHash(items[1].AsHash160()));
            Console.WriteLine("value:" + items[2].AsInteger());
        }
Beispiel #8
0
        async Task test_getAccount()
        {
            var result = await sneo_common.api_InvokeScript(Config.sneo, "getAccount",
                                                            "(str)admin_account");

            sneo_common.ResultItem item = result.value;
            Console.WriteLine("admin:" + ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160()));

            result = await sneo_common.api_InvokeScript(Config.sneo, "getAccount",
                                                        "(str)claim_account");

            item = result.value;
            Console.WriteLine("claim:" + ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160()));
        }
Beispiel #9
0
        //查询余额
        async Task test_BalanceOf()
        {
            Console.WriteLine("Input target address:");
            string addr = Console.ReadLine();

            if (addr.Length == 0)
            {
                addr = this.address;
            }

            var result = await sneo_common.api_InvokeScript(Config.sneo, "balanceOf", "(addr)" + addr);

            sneo_common.ResultItem item = result.value;

            Console.WriteLine(Helper.changeDecimals(item.subItem[0].AsInteger(), 8));
        }
Beispiel #10
0
        async Task test_getRefund()
        {
            //if (lasttxid == null)
            //{
            //    Console.WriteLine("你还没有正确执行Refund");
            //    return;
            //}
            Console.WriteLine("refund txid:");
            var lastTxid = Console.ReadLine();

            string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(sneo_common.sc_sneo);

            Console.WriteLine("address=" + nep55_address);

            //获取地址的资产列表
            Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address);

            if (dir.ContainsKey(Config.id_NEO) == false)
            {
                Console.WriteLine("no neo");
                return;
            }
            List <Utxo> newlist = new List <Utxo>();

            foreach (var utxo in dir[Config.id_NEO])
            {
                if (utxo.n == 0 && utxo.txid.ToString().Equals(lastTxid))
                {
                    newlist.Add(utxo);
                }
            }
            if (newlist.Count == 0)
            {
                Console.WriteLine("找不到要使用的UTXO");
                return;
            }
            else
            {
                lastTxid = newlist[0].txid.ToString();
            }


            {//检查是否是前面交易存储的
                var ret = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "getRefundTarget", "(hex256)" + lastTxid);

                sneo_common.ResultItem item = ret.value;

                var value = ThinNeo.Helper.GetAddressFromScriptHash(item.subItem[0].AsHash160());
                if (value.Length == 0)//未标记的UTXO,不能使用
                {
                    Console.WriteLine("这个utxo没有标记");
                    return;
                }
                if (value != this.address)
                {
                    Console.WriteLine("这个utxo不是标记给你用的");
                    return;
                }
            }

            ThinNeo.Transaction tran = Helper.makeTran(newlist, address, new ThinNeo.Hash256(Config.id_NEO), newlist[0].value);
            tran.type    = ThinNeo.TransactionType.ContractTransaction;
            tran.version = 0;


            //sign and broadcast
            {//做智能合约的签名
                byte[] iscript = null;
                using (var sb = new ThinNeo.ScriptBuilder())
                {
                    sb.EmitPushNumber(0);
                    sb.EmitPushNumber(0);
                    iscript = sb.ToArray();
                }
                tran.AddWitnessScript(n55contract, iscript);
            }


            var trandata    = tran.GetRawData();
            var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata);

            ThinNeo.Transaction testde = new ThinNeo.Transaction();
            testde.Deserialize(new System.IO.MemoryStream(trandata));

            byte[] postdata;
            var    url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata));

            string poststr = System.Text.Encoding.UTF8.GetString(postdata);
            //Console.WriteLine("-----post info begin----");
            //Console.WriteLine(poststr);
            //Console.WriteLine("-----post info end----");

            var result = await Helper.HttpPost(url, postdata);

            Console.WriteLine("得到的结果是:" + result);
            var json = MyJson.Parse(result).AsDict();

            if (json.ContainsKey("result"))
            {
                bool bSucc = false;
                if (json["result"].type == MyJson.jsontype.Value_Number)
                {
                    bSucc = json["result"].AsBool();
                    Console.WriteLine("cli=" + json["result"].ToString());
                }
                else
                {
                    var resultv = json["result"].AsList()[0].AsDict();
                    var txid    = resultv["txid"].AsString();
                    bSucc = txid.Length > 0;
                    Console.WriteLine("txid=" + txid);
                }
                if (bSucc)
                {
                    Nep55_1.lastNep5Tran = tran.GetHash();
                    Console.WriteLine("besucc txid=" + tran.GetHash().ToString());
                }
            }
        }
Beispiel #11
0
        //退款操作
        async Task test_refund()
        {
            Console.WriteLine("Input refund tokens:");
            string refund = Console.ReadLine();

            string nep55_address = ThinNeo.Helper.GetAddressFromScriptHash(sneo_common.sc_sneo);

            Console.WriteLine("nep55_address=" + nep55_address);

            //获取地址的资产列表
            Dictionary <string, List <Utxo> > dir = await Helper.GetBalanceByAddress(Config.api, nep55_address);

            if (dir.ContainsKey(Config.id_NEO) == false)
            {
                Console.WriteLine("no neo");
                return;
            }
            List <Utxo> newlist = new List <Utxo>(dir[Config.id_NEO]);

            for (var i = newlist.Count - 1; i >= 0; i--)
            {
                var ret = await sneo_common.api_InvokeScript(sneo_common.sc_sneo, "getRefundTarget", "(hex256)" + newlist[i].txid.ToString());

                sneo_common.ResultItem item = ret.value;

                if (newlist[i].n > 0)
                {
                    continue;
                }

                var value = item.subItem[0].AsString();
                if (value.Length > 0)//已经标记的UTXO,不能使用
                {
                    newlist.RemoveAt(i);
                }
            }


            ThinNeo.Transaction tran = null;
            {
                byte[] script = null;
                using (var sb = new ThinNeo.ScriptBuilder())
                {
                    var array = new MyJson.JsonNode_Array();
                    array.AddArrayValue("(bytes)" + ThinNeo.Helper.Bytes2HexString(scripthash));
                    sb.EmitParamJson(array);                                          //参数倒序入
                    sb.EmitParamJson(new MyJson.JsonNode_ValueString("(str)refund")); //参数倒序入
                    sb.EmitAppCall(sneo_common.sc_sneo);                              //nep5脚本
                    script = sb.ToArray();
                }
                Console.WriteLine("contract address=" + nep55_address);//往合约地址转账

                //生成交易
                tran      = Helper.makeTran(newlist, nep55_address, new ThinNeo.Hash256(Config.id_NEO), Decimal.Parse(refund));
                tran.type = ThinNeo.TransactionType.InvocationTransaction;
                var idata = new ThinNeo.InvokeTransData();
                tran.extdata = idata;
                idata.script = script;

                //附加鉴证
                tran.attributes          = new ThinNeo.Attribute[1];
                tran.attributes[0]       = new ThinNeo.Attribute();
                tran.attributes[0].usage = ThinNeo.TransactionAttributeUsage.Script;
                tran.attributes[0].data  = scripthash;
            }

            //sign and broadcast
            {//做智能合约的签名
                byte[] iscript = null;
                using (var sb = new ThinNeo.ScriptBuilder())
                {
                    sb.EmitPushString("whatever");
                    sb.EmitPushNumber(250);
                    iscript = sb.ToArray();
                }
                tran.AddWitnessScript(n55contract, iscript);
            }
            {//做提款人的签名
                var signdata = ThinNeo.Helper.Sign(tran.GetMessage(), prikey);
                tran.AddWitness(signdata, pubkey, address);
            }
            var trandata    = tran.GetRawData();
            var strtrandata = ThinNeo.Helper.Bytes2HexString(trandata);

            ThinNeo.Transaction testde = new ThinNeo.Transaction();
            testde.Deserialize(new System.IO.MemoryStream(trandata));

            byte[] postdata;
            var    url = Helper.MakeRpcUrlPost(Config.api, "sendrawtransaction", out postdata, new MyJson.JsonNode_ValueString(strtrandata));

            string poststr = System.Text.Encoding.UTF8.GetString(postdata);
            //Console.WriteLine("-----post info begin----");
            //Console.WriteLine(poststr);
            //Console.WriteLine("-----post info end----");
            var result = await Helper.HttpPost(url, postdata);

            Console.WriteLine("得到的结果是:" + result);
            var json = MyJson.Parse(result).AsDict();

            if (json.ContainsKey("result"))
            {
                bool bSucc = false;
                if (json["result"].type == MyJson.jsontype.Value_Number)
                {
                    bSucc = json["result"].AsBool();
                    Console.WriteLine("cli=" + json["result"].ToString());
                }
                else
                {
                    var resultv = json["result"].AsList()[0].AsDict();
                    var txid    = resultv["txid"].AsString();
                    bSucc = txid.Length > 0;
                    Console.WriteLine("txid=" + txid);
                }
                if (bSucc)
                {
                    lasttxid             = tran.GetHash();
                    Nep55_1.lastNep5Tran = tran.GetHash();
                    Console.WriteLine("你可以从这个UTXO拿走NEO了 txid=" + lasttxid.ToString() + "[0]");
                }
                else
                {
                    lasttxid = null;
                }
            }
        }