public async Task <NewTransactionDTO> LoadInfoFromAPI()
        {
            if (string.IsNullOrEmpty(WalletName))
            {
                return(null);
            }
            if (string.IsNullOrEmpty(Address))
            {
                return(null);
            }
            if (string.IsNullOrEmpty(TxId))
            {
                return(null);
            }

            try
            {
                var txd = await NeblioTransactionHelpers.TransactionInfoAsync(null, TransactionTypes.Neblio, TxId, Address);

                if (txd != null)
                {
                    var dto = new NewTransactionDTO();
                    dto.AccountAddress = Address;
                    dto.WalletName     = WalletName;
                    dto.Type           = TransactionTypes.Neblio;
                    dto.TxId           = TxId;

                    dto.TransactionDetails = txd;

                    VinTokens     = dto.TransactionDetails.VinTokens;
                    VoutTokens    = dto.TransactionDetails.VoutTokens;
                    Amount        = dto.TransactionDetails.Amount;
                    Confirmations = dto.TransactionDetails.Confirmations;
                    Direction     = dto.TransactionDetails.Direction;
                    From          = dto.TransactionDetails.From;
                    To            = dto.TransactionDetails.To;
                    TimeStamp     = dto.TransactionDetails.TimeStamp;
                    Metadata      = dto.TransactionDetails.Metadata;

                    if (dto.TransactionDetails != null)
                    {
                        DetailsLoaded?.Invoke(this, dto);
                    }

                    return(dto);
                }
            }
            catch (Exception ex)
            {
                attempts--;
                //log.Error("Cannot load tx details: ", ex);
            }

            return(null);
        }
        private void Tx_ConfirmedTransaction(object sender, NewTransactionDTO dto)
        {
            var tx = sender as NeblioTransaction;

            if (Transactions.TryGetValue(tx.TxId, out var t))
            {
                t.ConfirmedTransaction -= Tx_ConfirmedTransaction;
                LastConfirmedTxId       = t.TxId;
                ConfirmedTransaction?.Invoke(this, dto);
            }
        }
        private void Tx_DetailsLoaded(object sender, NewTransactionDTO dto)
        {
            var tx = sender as NeblioTransaction;

            transactionsInLoading.TryRemove(tx.TxId, out var txid);

            if (tx.InvokeLoadFinish)
            {
                if (Transactions.TryGetValue(tx.TxId, out var t))
                {
                    t.DetailsLoaded  -= Tx_DetailsLoaded;
                    LastProcessedTxId = t.TxId;
                    TxDetailsLoaded?.Invoke(this, dto);
                }
            }
        }
        private void WalletHandler_NewTransactionDetailsReceived(object sender, NewTransactionDTO data)
        {
            if (firstLoadAfterStart)
            {
                return;
            }

            var    tokenReceived = false;
            IToken token         = null;

            if (data.TransactionDetails.Confirmations > 0)
            {
                if (data.TransactionDetails.VinTokens.Count > 0)
                {
                    token = data.TransactionDetails?.VoutTokens?.FirstOrDefault();
                    if (token != null)
                    {
                        tokenReceived = true;
                    }
                }

                try
                {
                    if (EconomyMainContext.MQTTClient.IsConnected)
                    {
                        EconomyMainContext.MQTTClient.PostObjectAsJSON <NewTransactionDTO>(
                            $"VEF/NewTransactionWithDetails",
                            data, false).GetAwaiter().GetResult();


                        if (tokenReceived && token != null)
                        {
                            var o = new
                            {
                                data.AccountAddress,
                                data.WalletName,
                                data.Type,
                                data.TransactionDetails.Direction,
                                token,
                                data
                            };

                            EconomyMainContext.MQTTClient.PostObjectAsJSON <object>(
                                $"VEF/TokensReceived",
                                o, false).GetAwaiter().GetResult();

                            // Tx token Node trigger
                            var res = string.Empty;
                            if (data.TransactionDetails.Direction == TransactionDirection.Incoming)
                            {
                                res = NodeHandler.TriggerNodesActions(NodeActionTriggerTypes.TokenTxArrived, data, o).GetAwaiter().GetResult();
                            }
                            else
                            {
                                res = NodeHandler.TriggerNodesActions(NodeActionTriggerTypes.TokenTxSent, data, o).GetAwaiter().GetResult();
                            }
                        }
                        else
                        {
                            var o = new
                            {
                                data.AccountAddress,
                                data.WalletName,
                                data.Type,
                                data.TransactionDetails.Direction,
                                data
                            };

                            // Tx Node trigger
                            var res = string.Empty;
                            if (data.TransactionDetails.Direction == TransactionDirection.Incoming)
                            {
                                res = NodeHandler.TriggerNodesActions(NodeActionTriggerTypes.NewTxArrived, data, o).GetAwaiter().GetResult();
                            }
                            else
                            {
                                res = NodeHandler.TriggerNodesActions(NodeActionTriggerTypes.TxSent, data, o).GetAwaiter().GetResult();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error("Wallet handler cannot send MQTT post when new tx arrived, check MQTT Broker and connection.", ex);
                }
            }
        }
        public override async Task <string> TriggerNodesActions(NodeActionTriggerTypes type, NewTransactionDTO txdata, object payload)
        {
            var result = "ERROR";

            if (EconomyMainContext.Accounts.TryGetValue(txdata.AccountAddress, out var account))
            {
                var pld = JsonConvert.SerializeObject(payload);
                var acc = JsonConvert.SerializeObject(account);

                foreach (var node in EconomyMainContext.Nodes)
                {
                    if (node.Value.AccountId == account.Id)
                    {
                        var res = await node.Value.InvokeNodeFunction(type, new string[] { pld, acc });;
                    }
                }
            }

            return(result);
        }
Esempio n. 6
0
 public abstract Task <string> TriggerNodesActions(NodeActionTriggerTypes type, NewTransactionDTO txdata, object payload);
 private void A_ConfirmedTransaction(object sender, NewTransactionDTO dto)
 {
     NewConfirmedTransactionDetailsReceived?.Invoke(this, dto);
 }
 private void A_TxDetailsLoaded(object sender, NewTransactionDTO dto)
 {
     NewTransaction?.Invoke(this, dto);
 }