Exemplo n.º 1
0
        private static void ParseBlock(ulong currentHeight)
        {
            WebClient wc = new WebClient();
            //try
            //{
            JObject blockInfo = GetBlock(wc, currentHeight);

            JArray txArray = blockInfo["tx"] as JArray;

            ulong      blockTime  = ulong.Parse(blockInfo["time"].ToString());
            BlockState blockState = new BlockState()
            {
                BlockHeight  = currentHeight,
                BlockTime    = blockTime,
                ContractHash = Config._destroyAddress
            };
            List <TransState> transStates = new List <TransState>();

            for (int i = 0; i < txArray.Count; i++)
            {
                if (txArray[i]["type"].ToString() == "InvocationTransaction")
                {
                    TransState transState = TransParse(wc, txArray[i], currentHeight, blockTime);
                    if (transState != null)
                    {
                        transStates.Add(transState);
                    }
                }
            }

            wc.Dispose();

            if (transStates.Count > 0)
            {
                blockState.TransStates = transStates;

                string jsonData = JsonConvert.SerializeObject(blockState);
                SendTransTimer(jsonData, Config._apiDict["refund"]);
            }
            //}
            //catch (Exception ex)
            //{
            //    wc.Dispose();
            //    Logger.Error("zoro: " + ex.Message);
            //    Logger.Error("stack: " + ex.StackTrace);
            //}
        }
Exemplo n.º 2
0
        private static TransState TransParse(WebClient wc, JToken tx, ulong currentHeight, ulong blockTime)
        {
            TransState          transState       = new TransState();
            List <WithdrawInfo> withdrawInfoList = new List <WithdrawInfo>();
            string txid = tx["txid"].ToString();

            var applicationlogInfo = GetApplicationlog(wc, txid);

            var executions = applicationlogInfo["executions"] as JToken;

            if (executions[0]["vmstate"].ToString() == "FAULT, BREAK")
            {
                return(null);
            }

            var notifications = executions[0]["notifications"] as JArray;

            if (notifications == null || notifications.Count == 0)
            {
                return(null);
            }

            for (int i = 0; i < notifications.Count; i++)
            {
                string contract = notifications[i]["contract"].ToString();

                //nep5
                foreach (var token in Config._nep5TokenHashDict)
                {
                    if (contract == token.Value)
                    {
                        var    jValue = notifications[i]["state"]["value"] as JArray;
                        string method = Encoding.UTF8.GetString(ZoroHelper.HexString2Bytes(jValue[0]["value"].ToString()));

                        if (method == "transfer")
                        {
                            //游戏发奖 退款
                            if (jValue[1]["value"].ToString() == Config._gameConfig.IssueAddressHexString)
                            {
                                SendGameIssueResult(currentHeight, blockTime, txid, token.Key, jValue, method);
                                break;
                            }
                            //销毁 提币使用
                            if (jValue[2]["value"].ToString() == Config._destroyAddressHexString)
                            {
                                var withdrawInfo = GetTransferInfo(token.Key.ToUpper(), txid, jValue, tx);
                                if (withdrawInfo != null)
                                {
                                    withdrawInfoList.Add(withdrawInfo);
                                }
                                break;
                            }
                            //游戏中付款
                            else if (jValue[2]["value"].ToString() == Config._gameConfig.CollectionAddressHexString)
                            {
                                SendGameSpendResult(currentHeight, blockTime, txid, token.Key, jValue, method);
                                break;
                            }
                        }
                        break;
                    }
                }

                //nep10
                foreach (var token in Config._nftHashDict)
                {
                    if (contract == token.Value)
                    {
                        var jValue = notifications[i]["state"]["value"] as JArray;

                        string  method   = Encoding.UTF8.GetString(ZoroHelper.HexString2Bytes(jValue[0]["value"].ToString()));
                        dynamic transLog = null;
                        switch (method)
                        {
                        case "mintToken":
                            transLog = MintTokenResult(jValue);
                            break;

                        case "transfer":
                            transLog = TransferTokenResult(jValue);
                            break;

                        //case "modifyRwData":
                        //    transLog = ModifyRwDataResult(jValue);
                        //    break;
                        case "modifyProperties":
                            transLog = ModifyPropertiesResult(jValue);
                            break;

                        case "freeze":
                            transLog = FreezeTokenResult(jValue);
                            break;

                        case "unfreeze":
                            transLog = UnfreezeTokenResult(jValue);
                            break;
                        }

                        if (transLog != null && !string.IsNullOrEmpty((string)transLog.tokenId))
                        {
                            transLog.method      = method;
                            transLog.txid        = txid;
                            transLog.blockTime   = blockTime;
                            transLog.blockHeight = zoroHeight;

                            string url      = Config._gameConfig.GameUrl + "/sysGame/syncNFTInfo";
                            string jsonData = JsonConvert.SerializeObject(transLog);
                            SendNftTrans(jsonData, url);
                        }

                        break;
                    }
                }

                #region nftex
                //nft exchange
                //if (contract == Config._nftExchangeHash)
                //{
                //    var jValue = notifications[i]["state"]["value"] as JArray;
                //    string method = Encoding.UTF8.GetString(ZoroHelper.HexString2Bytes(jValue[0]["value"].ToString()));

                //    dynamic transLog = null;
                //    switch (method)
                //    {
                //        case "deposit":
                //            transLog = DepositResult(jValue);
                //            break;
                //        case "withdraw":
                //            transLog = WithdrawResult(jValue);
                //            break;
                //        case "makeOffer":
                //            transLog = MakeOfferResult(jValue);
                //            break;
                //        case "fillOffer":
                //            transLog = FillOfferResult(jValue);
                //            break;
                //        case "cancelOffer":
                //            transLog = CancelOffer(jValue);
                //            break;
                //    }

                //    if (transLog != null)
                //    {
                //        transLog.Method = method;
                //        transLog.Txid = txid;
                //        transLog.blockTime = blockTime;
                //        transLog.blockHeight = currentHeight;

                //        //string url = Config._gameConfig.GameUrl + "/sysGame/transConfirm";
                //        //string jsonData = JsonConvert.SerializeObject(transLog);
                //        //SendTransTimer(jsonData, url);
                //    }

                //    break;
                //}
                #endregion
            }

            if (withdrawInfoList.Count > 0)
            {
                transState.Txid          = txid;
                transState.VmState       = true;
                transState.Notifications = withdrawInfoList;

                return(transState);
            }

            return(null);
        }