Beispiel #1
0
        public async Task <UInt160> CreateNativeNEP5Async(string ChainHash, string WIF)
        {
            string symbol = "InvokeContractTest";
            string name   = "InvokeContractTest_NativeNEP5";

            BigInteger presion     = new BigInteger(8);
            Decimal    totalsupply = 2_000_000_000;
            Decimal    amount      = totalsupply * new Decimal(Math.Pow(10, (long)presion));

            KeyPair keypair    = ZoroHelper.GetKeyPairFromWIF(WIF);
            UInt160 scriptHash = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);

            UInt160 hash = new UInt160();

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitPush(scriptHash);
                sb.EmitPush(keypair.PublicKey.EncodePoint(true));
                sb.EmitPush(presion);
                sb.EmitPush(new BigInteger(amount));
                sb.EmitPush(symbol);
                sb.EmitPush(name);
                sb.EmitSysCall("Zoro.NativeNEP5.Create");

                hash = sb.ToArray().ToScriptHash();
                Console.WriteLine("NativeNEP5 Hash:" + hash);

                string result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, ChainHash, Config.GasPrice);

                Console.WriteLine(result);
            }

            return(hash);
        }
        public async Task SendRawTransactionTestAsync(string contractHash, string WIF, UInt160 nativeBcp, int transType)
        {
            Console.WriteLine("Transfer Amount:");
            string  transferValue    = Console.ReadLine();
            Decimal value            = Decimal.Parse(transferValue, NumberStyles.Float) * new Decimal(Math.Pow(10, 8));
            string  chainHash        = Config.getValue("ChainHash");
            KeyPair keypair          = ZoroHelper.GetKeyPairFromWIF(WIF);
            UInt160 scriptHash       = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);
            UInt160 targetscripthash = UInt160.Parse(contractHash);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                if (transType == 1)
                {
                    sb.EmitAppCall(ZoroHelper.Parse(contractHash), "transfer", nativeBcp, scriptHash, targetscripthash, (BigInteger)value);
                }
                if (transType == 2)
                {
                    sb.EmitAppCall(ZoroHelper.Parse(contractHash), "transfer_app", nativeBcp, scriptHash, (BigInteger)value);
                }

                var result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, chainHash, Config.GasPrice);

                MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result);
                Console.WriteLine(resJO.ToString());
            }
        }
        private void Test()
        {
            Console.Write("选择交易类型,0 - NEP5 SmartContract, 1 - NativeNEP5, 2 - BCP, 3 - InvokeNEP5:");
            var param1 = Console.ReadLine();
            Console.Write("输入并发的数量:");
            var param2 = Console.ReadLine();
            Console.Write("发送几次交易:");
            var param3 = Console.ReadLine();
            Console.Write("转账金额:");
            var param4 = Console.ReadLine();
            Console.Write("目标账户随机, 0 - no, 1 - yes:");
            var param5 = Console.ReadLine();
            Console.Write("GasPrice随机, 0 - no, 1 - yes:");
            var param6 = Console.ReadLine();

            transactionType = int.Parse(param1);
            transferCount = int.Parse(param3);
            concurrencyCount = int.Parse(param2);
            transferValue = param4;
            randomTargetAddress = int.Parse(param5) == 1;
            randomGasPrice = int.Parse(param6) == 1;

            string[] chainHashList = Config.getStringArray("ChainHashList");
            string WIF = Config.getValue("WIF");
            string targetWIF = Config.getValue("targetWIF");

            keypair = ZoroHelper.GetKeyPairFromWIF(WIF);
            scriptHash = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);
            targetAddress = ZoroHelper.GetPublicKeyHashFromWIF(targetWIF);

            string contractHash = Config.getValue("ContractHash");
            nep5ContractHash = UInt160.Parse(contractHash);

            string nativeNEP5Hash = Config.getValue("NativeNEP5");
            nativeNEP5AssetId = UInt160.Parse(nativeNEP5Hash);

            BCPAssetId = Genesis.BcpContractAddress;

            if (randomTargetAddress)
            {
                InitializeRandomTargetAddressList(transferCount);
            }

            if (transactionType == 0 || transactionType == 1 || transactionType == 2)
            {
                Console.WriteLine($"From:{WIF}");
                Console.WriteLine($"To:{targetWIF}");
                Console.WriteLine($"Count:{transferCount}");
                Console.WriteLine($"Value:{transferValue}");
            }

            cancelTokenSource = new CancellationTokenSource();

            Task.Run(() => RunTask(chainHashList));

            Console.WriteLine("输入回车键停止:");
            var input = Console.ReadLine();
            cancelTokenSource.Cancel();
        }
        public async Task StartAsync()
        {
            Console.WriteLine("AppChainName:");
            string name = Console.ReadLine();
            string WIF  = Config.getValue("WIF");

            Console.Write("validators Length: ");
            string vlength = Console.ReadLine();

            string[] validators = new string[int.Parse(vlength)];
            for (int i = 0; i < validators.Length; i++)
            {
                Console.Write("validator " + (i + 1) + ": ");
                validators[i] = Console.ReadLine();
            }
            Console.Write("seedList Length: ");
            string slength = Console.ReadLine();

            string[] seedList = new string[int.Parse(slength)];
            for (int i = 0; i < seedList.Length; i++)
            {
                Console.Write("seed " + (i + 1) + ": ");
                seedList[i] = Console.ReadLine();
            }
            Console.Write("IsPrivate: ");
            string isprivate = Console.ReadLine();

            KeyPair keypair = ZoroHelper.GetKeyPairFromWIF(WIF);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                foreach (string validator in validators)
                {
                    sb.EmitPush(validator);
                }
                sb.EmitPush(validators.Length);
                foreach (string seed in seedList)
                {
                    sb.EmitPush(seed);
                }
                sb.EmitPush(seedList.Length);
                sb.EmitPush(DateTime.UtcNow.ToTimestamp());
                sb.EmitPush(isprivate == "1" ? 1 : 0);
                sb.EmitPush(keypair.PublicKey.EncodePoint(true));
                sb.EmitPush(name);

                UInt160 chainHash = new UInt160(Crypto.Default.Hash160(sb.ToArray()));
                sb.EmitPush(chainHash.ToArray());
                sb.EmitSysCall("Zoro.AppChain.Create");

                Console.WriteLine("Appchain hash:" + chainHash.ToArray().Reverse().ToHexString());

                string result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, "", Config.GasPrice);

                Console.WriteLine(result);
            }
        }
        private void Test()
        {
            string WIF       = Config.getValue("WIF");
            string targetWIF = Config.getValue("targetWIF");

            keypair          = ZoroHelper.GetKeyPairFromWIF(WIF);
            scriptHash       = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);
            targetscripthash = ZoroHelper.GetPublicKeyHashFromWIF(targetWIF);

            string   path     = Config.getValue("LevelDB");
            Store    appStore = new LevelDBStore(path);
            Snapshot snapshot = appStore.GetSnapshot();

            Console.WriteLine("请输入数量:");
            var param = Console.ReadLine();

            int count = int.Parse(param);

            if (count <= 0)
            {
                return;
            }

            Random             rnd  = new Random();
            DateTime           dt   = DateTime.Now;
            List <Transaction> txns = new List <Transaction>();

            for (int i = 0; i < count; i++)
            {
                txns.Add(MakeTestTransaction(rnd));
            }

            Console.Write("maketxn, ");
            PrintTimeCost(dt);

            dt = DateTime.Now;
            foreach (var tx in txns)
            {
                tx.Verify(snapshot);
            }

            Console.Write("verify, ");
            PrintTimeCost(dt);

            dt = DateTime.Now;
            foreach (var tx in txns)
            {
                tx.Reverify(snapshot);
            }

            Console.Write("reverify, ");
            PrintTimeCost(dt);

            snapshot.Dispose();
            appStore.Dispose();
        }
Beispiel #6
0
        public async Task DeployNEP5Async(string ChainHash, string WIF, string ContractHash)
        {
            KeyPair keypair = ZoroHelper.GetKeyPairFromWIF(WIF);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitAppCall(ZoroHelper.Parse(ContractHash), "deploy", "1");

                string result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, ChainHash, Config.GasPrice);

                Console.WriteLine(result);
            }
        }
        private void Test()
        {
            string WIF       = Config.getValue("WIF");
            string targetWIF = Config.getValue("targetWIF");

            keypair          = ZoroHelper.GetKeyPairFromWIF(WIF);
            scriptHash       = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);
            targetscripthash = ZoroHelper.GetPublicKeyHashFromWIF(targetWIF);

            Console.WriteLine("请输入数量:");
            var param = Console.ReadLine();

            int count = int.Parse(param);

            if (count <= 0)
            {
                return;
            }

            Random             rnd  = new Random();
            DateTime           dt   = DateTime.Now;
            List <Transaction> txns = new List <Transaction>();

            for (int i = 0; i < count; i++)
            {
                txns.Add(MakeTestTransaction(rnd));
            }

            Console.Write("maketxn, ");
            PrintTimeCost(dt);

            dt = DateTime.Now;

            IEnumerable <CompressedTransactionPayload> payloads = CompressedTransactionPayload.CreateGroup(txns.ToArray());

            Console.Write("compress, ");
            PrintTimeCost(dt);

            dt = DateTime.Now;

            foreach (var payload in payloads)
            {
                Transaction[] txn = CompressedTransactionPayload.DecompressTransactions(payload.CompressedData);
                foreach (var tx in txn)
                {
                }
            }

            Console.Write("decompress, ");
            PrintTimeCost(dt);
        }
        public async Task TransferNativeNEP5Async(string chainHash, string WIF, string targetWIF, string assetId, BigInteger value)
        {
            KeyPair keypair          = ZoroHelper.GetKeyPairFromWIF(WIF);
            UInt160 scriptHash       = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);
            UInt160 targetscripthash = ZoroHelper.GetPublicKeyHashFromWIF(targetWIF);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall("Zoro.NativeNEP5.Call", "Transfer", UInt160.Parse(assetId), scriptHash, targetscripthash, value);

                var result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, chainHash, Config.GasPrice);

                MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result);
                Console.WriteLine(resJO.ToString());
            }
        }
        async Task SendTransaction(UInt160 assetId, string[] wif_list, string targetWIF, BigInteger value, string chainHash)
        {
            KeyPair[] keypairs = wif_list.Select(p => ZoroHelper.GetKeyPairFromWIF(p)).ToArray();
            int       m        = keypairs.Length - (keypairs.Length - 1) / 3;

            UInt160 scriptHash = ZoroHelper.GetMultiSigRedeemScriptHash(m, keypairs);

            UInt160 targetscripthash = ZoroHelper.GetPublicKeyHashFromWIF(targetWIF);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                sb.EmitSysCall("Zoro.NativeNEP5.Call", "Transfer", assetId, scriptHash, targetscripthash, value);

                var result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), m, keypairs, chainHash, Config.GasPrice);

                MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result);
                Console.WriteLine(resJO.ToString());
            }
        }
Beispiel #10
0
        private void Test()
        {
            Console.Write("输入并发的数量:");
            var param2 = Console.ReadLine();

            Console.Write("发送几次交易:");
            var param3 = Console.ReadLine();

            Console.Write("目标账户随机, 0 - no, 1 - yes:");
            var param5 = Console.ReadLine();

            Console.Write("购买数量随机, 0 - no, 1 - yes:");
            var param6 = Console.ReadLine();

            transferCount       = int.Parse(param3);
            concurrencyCount    = int.Parse(param2);
            randomTargetAddress = int.Parse(param5) == 1;
            randomBuyCount      = int.Parse(param6) == 1;
            string[] chainHashList = Config.getStringArray("ChainHashList");
            string   WIF           = Config.getValue("WIF");
            string   targetWIF     = Config.getValue("targetWIF");

            keypair       = ZoroHelper.GetKeyPairFromWIF(WIF);
            scriptHash    = ZoroHelper.GetPublicKeyHash(keypair.PublicKey);
            targetAddress = ZoroHelper.GetPublicKeyHashFromWIF(targetWIF);
            nftHash       = UInt160.Parse(Config.getValue("nftHash"));
            BCPAssetId    = Genesis.BcpContractAddress;

            if (randomTargetAddress)
            {
                InitializeRandomTargetAddressList(transferCount);
            }

            cancelTokenSource = new CancellationTokenSource();

            Task.Run(() => RunTask(chainHashList));

            Console.WriteLine("输入回车键停止:");
            var input = Console.ReadLine();

            cancelTokenSource.Cancel();
        }
Beispiel #11
0
        public async Task CreateContractAsync(string ChainHash, string WIF, string ContractPath)
        {
            KeyPair keypair = ZoroHelper.GetKeyPairFromWIF(WIF);

            byte[] script = System.IO.File.ReadAllBytes(ContractPath);
            Console.WriteLine("合约脚本Hash:" + script.ToScriptHash());
            byte[] parameter__list = ZoroHelper.HexString2Bytes("0710");
            byte[] return_type     = ZoroHelper.HexString2Bytes("05");
            int    need_storage    = 0;
            int    need_nep4       = 0;
            int    need_canCharge  = 4;
            string name            = "test contract";
            string version         = "1.0";
            string auther          = "zz";
            string email           = "0";
            string description     = "0";

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                var ss = need_storage | need_nep4 | need_canCharge;
                sb.EmitPush(description);
                sb.EmitPush(email);
                sb.EmitPush(auther);
                sb.EmitPush(version);
                sb.EmitPush(name);
                sb.EmitPush(ss);
                sb.EmitPush(return_type);
                sb.EmitPush(parameter__list);
                sb.EmitPush(script);
                sb.EmitSysCall("Zoro.Contract.Create");

                Console.WriteLine($"ScriptHash:{script.ToScriptHash()}");

                var result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, ChainHash, Config.GasPrice);

                MyJson.JsonNode_Object resJO = (MyJson.JsonNode_Object)MyJson.Parse(result);
                Console.WriteLine(resJO.ToString());
            }
        }
Beispiel #12
0
        public async Task StartAsync()
        {
            Console.WriteLine("AppChainHash:");
            string appchainHash = Console.ReadLine();

            var url    = Helper.MakeRpcUrl(Program.local, "getappchainstate", new MyJson.JsonNode_ValueString(appchainHash));
            var result = await Helper.HttpGet(url);

            MyJson.JsonNode_Object json_result_array = MyJson.Parse(result) as MyJson.JsonNode_Object;
            MyJson.JsonNode_Object json_result_obj;

            bool exists = true;

            if (json_result_array.TryGetValue("result", out MyJson.IJsonNode json_result))
            {
                json_result_obj = json_result as MyJson.JsonNode_Object;

                string appchainName = json_result_obj["name"].AsString();

                if (appchainName == "null")
                {
                    exists = false;
                }
            }
            else
            {
                exists = false;
            }

            if (!exists)
            {
                Console.WriteLine("Can't find appchain " + appchainHash);
                return;
            }

            string WIF = Config.getValue("WIF");

            Console.Write("validators Length: ");
            string vlength = Console.ReadLine();

            string[] validators = new string[int.Parse(vlength)];
            for (int i = 0; i < validators.Length; i++)
            {
                Console.Write("validator " + (i + 1) + ": ");
                validators[i] = Console.ReadLine();
            }

            KeyPair keypair = ZoroHelper.GetKeyPairFromWIF(WIF);

            using (ScriptBuilder sb = new ScriptBuilder())
            {
                foreach (string validator in validators)
                {
                    sb.EmitPush(validator);
                }
                sb.EmitPush(validators.Length);
                sb.EmitSysCall("Zoro.AppChain.ChangeValidators");

                result = await ZoroHelper.SendInvocationTransaction(sb.ToArray(), keypair, appchainHash, Config.GasPrice);

                Console.WriteLine(result);
            }
        }