示例#1
0
        public void Sync()
        {
            try
            {
                // To get an account information from the address.
                var accountClient   = new AccountClient(connection);
                var accountResult   = accountClient.BeginGetAccountInfoFromAddress(Address);
                var accountResponse = accountClient.EndGetAccountInfo(accountResult);

                Balance         = ((double)accountResponse.Account.Balance / 1000000).ToString("N6");
                PublicKey       = accountResponse.Account.PublicKey.Substring(0, 10) + "**********...";
                HarvestedBlocks = accountResponse.Account.HarvestedBlocks;
                Importance      = accountResponse.Account.Importance;
                VestedBalance   = ((double)accountResponse.Account.VestedBalance / 1000000).ToString("N6");

                // To get mosaic information of the account from the address.
                var mosaicClient   = new NamespaceMosaicClient(connection);
                var mosaicResult   = mosaicClient.BeginGetMosaicsOwned(Address);
                var mosaicResponse = mosaicClient.EndGetMosaicsOwned(mosaicResult);

                Mosaics.Clear();

                foreach (var data in mosaicResponse.Data)
                {
                    Mosaics.Add(new Mosaic(data.MosaicId.NamespaceId, data.MosaicId.Name, data.Quantity));
                }
            }
            catch (Exception ex)
            {
            }
        }
        internal static int GetMosaicDivisibility(Connection Con, string nameSpace, string id)
        {
            var mosaicDivisibility = 0;

            var mosaicClient = new NamespaceMosaicClient(Con);

            // get mosaic definition, specifically divisbility
            mosaicClient.BeginGetMosaicsByNameSpace(ar2 =>
            {
                if (ar2.Ex != null)
                {
                    Console.WriteLine(ar2.Ex);
                }
                else
                {
                    mosaicDivisibility = int.Parse(ar2.Content.Data[0].Mosaic.Properties[0].Value);
                }
            }, nameSpace, id).AsyncWaitHandle.WaitOne();

            if (mosaicDivisibility == 0)
            {
                throw new Exception("divisbility not found");
            }

            return(mosaicDivisibility);
        }
示例#3
0
        public IHttpActionResult GetBalance([FromUri] Models.Address address) {
            List<Models.MosaicBalanceModel> balance = new List<Models.MosaicBalanceModel>();
            var connection = new Connection();
            connection.SetTestnet();
            try
            {
                // To get mosaic information of the account from the address.
                var mosaicClient = new NamespaceMosaicClient(connection);
                var mosaicResult = mosaicClient.BeginGetMosaicsOwned(address.MosaicAddress);
                var mosaicResponse = mosaicClient.EndGetMosaicsOwned(mosaicResult);

                foreach (var data in mosaicResponse.Data)
                {
                    if (data.MosaicId.Name == "tfc")
                    {
                        balance.Add(new Models.MosaicBalanceModel
                        {
                            Name = data.MosaicId.Name,
                            Amount = data.Quantity / 10000

                        });
                    }
                }
            }
            catch (Exception e) { }

            if (balance.Count == 0) {
                return NotFound();
            }

            return Ok(balance);
        }
示例#4
0
        public String Transaction(Models.TransferData data)
        {
            String message = "";
            List <CSharp2nem.Model.Transfer.Mosaics.Mosaic> MosaicList = new List <CSharp2nem.Model.Transfer.Mosaics.Mosaic>();
            var connection = new Connection();

            connection.SetTestnet();

            var mosaicClient   = new NamespaceMosaicClient(connection);
            var mosaicResult   = mosaicClient.BeginGetMosaicsOwned(data.Sender);
            var mosaicResponse = mosaicClient.EndGetMosaicsOwned(mosaicResult);

            foreach (var mosaic in mosaicResponse.Data)
            {
                if (mosaic.MosaicId.Name == "tfc")
                {
                    MosaicList.Add(new CSharp2nem.Model.Transfer.Mosaics.Mosaic(mosaic.MosaicId.NamespaceId, mosaic.MosaicId.Name, (data.Amount * 10000)));
                }
            }

            var accountFactory = new PrivateKeyAccountClientFactory(connection);
            var accClient      = accountFactory.FromPrivateKey(data.SenderPrivateKey);

            var transData = new TransferTransactionData()
            {
                Amount           = 1000000, // amount should always be 1000000 micro xem when attaching mosaics as it acts as a multiplier.
                RecipientAddress = data.Receiver,
                ListOfMosaics    = MosaicList
            };

            try
            {
                accClient.BeginSendTransaction(body =>
                {
                    try
                    {
                        if (body.Ex != null)
                        {
                            throw body.Ex;
                        }
                        message = body.Content.Message;
                        Debug.WriteLine(message);
                    }
                    catch (Exception e)
                    {
                        message = e.Message;
                    }
                }, transData);
            }
            catch (Exception e)
            {
                message = e.Message;
            }
            Thread.Sleep(5000); //to make sure the message variable gets the content message before terminating
            return(message);
        }
        internal bool ScanTxAgainstRuleSet(Transactions.TransactionData t)
        {
            var multisigAccInfo = new AccountClient(Con);

            var valid = true;

            multisigAccInfo.BeginGetAccountInfoFromPublicKey(ar => {
                if (_black.Count > 0 && _black.Contains(StringUtils.GetResultsWithoutHyphen(t.transaction.otherTrans.recipient)))
                {
                    Console.WriteLine("failed on blacklist");
                    valid = false;
                }

                if (_white.Count > 0 && !_white.Contains(StringUtils.GetResultsWithoutHyphen(t.transaction.otherTrans.recipient)))
                {
                    Console.WriteLine("failed on white list");
                    valid = false;
                }


                if (long.Parse(ConfigurationManager.AppSettings["maxTx"]) != 0 && t.transaction.otherTrans.amount >= long.Parse(ConfigurationManager.AppSettings["maxTx"]) * 1000000)
                {
                    Console.WriteLine("failed max transaction size");
                    valid = false;
                }

                if (long.Parse(ConfigurationManager.AppSettings["minTx"]) != 0 && t.transaction.otherTrans.amount <= long.Parse(ConfigurationManager.AppSettings["minTx"]) * 1000000)
                {
                    Console.WriteLine("failed on min transaction size");
                    valid = false;
                }

                if (long.Parse(ConfigurationManager.AppSettings["maxBal"]) != 0 && ar.Content.Account.Balance >= long.Parse(ConfigurationManager.AppSettings["maxBal"]) * 1000000)
                {
                    Console.WriteLine("failed on max balance");
                    valid = false;
                }

                if (long.Parse(ConfigurationManager.AppSettings["minBal"]) != 0 && ar.Content.Account.Balance <= long.Parse(ConfigurationManager.AppSettings["minBal"]) * 1000000)
                {
                    Console.WriteLine("failed on min balance");
                    valid = false;
                }

                if (ConfigurationManager.AppSettings["secretCode"] != "" && t.transaction.otherTrans.message.payload != ConfigurationManager.AppSettings["secretCode"])
                {
                    Console.WriteLine("failed on secret code");
                    valid = false;
                }

                var sum1 = TxSummaryController.GetSummaryForAccount(ar.Content.Account.Address, 1);

                if (int.Parse(ConfigurationManager.AppSettings["maxDayTx"]) != 0 && sum1 != null && sum1.Count >= int.Parse(ConfigurationManager.AppSettings["maxDayTx"]))
                {
                    Console.WriteLine("failed on daily max transactions");
                    valid = false;
                }

                var sum7 = TxSummaryController.GetSummaryForAccount(ar.Content.Account.Address, 7);

                if (int.Parse(ConfigurationManager.AppSettings["maxWeekTx"]) != 0 && sum7 != null && sum7.Count >= int.Parse(ConfigurationManager.AppSettings["maxWeekTx"]))
                {
                    Console.WriteLine("failed on weekly max transactions");
                    valid = false;
                }

                var sum31 = TxSummaryController.GetSummaryForAccount(ar.Content.Account.Address, 31);

                if (int.Parse(ConfigurationManager.AppSettings["maxMonthTx"]) != 0 && sum31 != null && sum31.Count >= int.Parse(ConfigurationManager.AppSettings["maxMonthTx"]))
                {
                    Console.WriteLine("failed on monthly max transactions");
                    valid = false;
                }

                if (long.Parse(ConfigurationManager.AppSettings["maxDayAmount"]) != 0 && sum1 != null && sum1.Sum(tx => tx.Amount) >= long.Parse(ConfigurationManager.AppSettings["maxDayAmount"]) * 1000000)
                {
                    Console.WriteLine("failed on daily max transactions");
                    valid = false;
                }

                if (long.Parse(ConfigurationManager.AppSettings["maxWeekAmount"]) != 0 && sum7 != null && sum7.Sum(tx => tx.Amount) >= long.Parse(ConfigurationManager.AppSettings["maxWeekAmount"]) * 1000000)
                {
                    Console.WriteLine("failed on weekly max transactions");
                    valid = false;
                }

                if (long.Parse(ConfigurationManager.AppSettings["maxMonthAmount"]) != 0 && sum31 != null && sum31.Sum(tx => tx.Amount) >= long.Parse(ConfigurationManager.AppSettings["maxMonthAmount"]) * 1000000)
                {
                    Console.WriteLine("failed on monthly max transactions");
                    valid = false;
                }

                if (ConfigurationManager.AppSettings["ICOAccountPubKey"] != "")
                {
                    valid = false;

                    // get all incoming transactions to the ICO deposit account
                    var txs = GetTransactions(AddressEncoding.ToEncoded(Con.GetNetworkVersion(), new CSharp2nem.Model.AccountSetup.PublicKey(ConfigurationManager.AppSettings["ICOAccountPubKey"])), null);

                    if ((t.transaction.type == 257 ? t.transaction.message?.payload : t.transaction.otherTrans?.message?.payload) != null)
                    {
                        while (!txs.Exists(e => e.meta.hash.data == Encoding.UTF8.GetString(CryptoBytes.FromHexString((t.transaction.type == 257 ? t.transaction.message?.payload : t.transaction.otherTrans?.message?.payload)))))
                        {
                            var tx = GetTransactions(AddressEncoding.ToEncoded(Con.GetNetworkVersion(), new CSharp2nem.Model.AccountSetup.PublicKey(ConfigurationManager.AppSettings["ICOAccountPubKey"])), txs[txs.Count - 1].meta.hash.data);

                            txs.AddRange(tx);
                        }
                    }
                    else
                    {
                        valid = false;
                        Console.WriteLine("failed on transaction verification: missing message hash");
                    }

                    // fail if deposit hash not present in payout transaction
                    if ((t.transaction.otherTrans == null ? t.transaction.message?.payload : t.transaction.otherTrans.message?.payload) != null)
                    {
                        // find deposit transaction based on hash in message
                        var tx = txs.Single(x => x.meta.hash.data == Encoding.UTF8.GetString(CryptoBytes.FromHexString((t.transaction.type == 257 ? t.transaction.message.payload : t.transaction.otherTrans.message.payload))));

                        // initiate mosaic client
                        var mosaicClient = new NamespaceMosaicClient(Con);

                        var mosaicDivisibility = 0;

                        var mosaics = ConfigurationManager.GetSection("MosaicConfigElement") as MyMosaicConfigSection;

                        foreach (MosaicConfigElement m in mosaics.Mosaics)
                        {
                            // retrieve mosaic definition for rate calculation
                            mosaicClient.BeginGetMosaicsByNameSpace(ar3 =>
                            {
                                foreach (var m2 in t.transaction.otherTrans.mosaics)
                                {
                                    if (!(m2.mosaicId.namespaceId == m.MosaicNameSpace && m2.mosaicId.name == m.MosaicID))
                                    {
                                        continue;
                                    }

                                    if (ar3.Ex != null)
                                    {
                                        Console.WriteLine(ar3.Ex);
                                    } // if no error, set mosaic divisibility
                                    else
                                    {
                                        mosaicDivisibility = int.Parse(ar3.Content.Data[0].Mosaic.Properties[0].Value);
                                    }

                                    var depositedAmount = Calculations.GetDepositedAmount(tx);

                                    // calculate based on pre-set currency denomination
                                    if (m.CostDenomination == "USD")
                                    {
                                        // fail if no mosaics present in ico payout transaction
                                        if (t.transaction.otherTrans.mosaics != null)
                                        {
                                            // check if the mosaic quantity paid out is correct for the deposit
                                            var amountValid = Calculations.RateCalculation(depositedAmount, mosaicDivisibility, m) == m2.quantity;

                                            // check the recipient of the payout is the signer of the deposit, fail if not.
                                            if (amountValid && t.transaction.otherTrans.recipient == AddressEncoding.ToEncoded(Con.GetNetworkVersion(), new CSharp2nem.Model.AccountSetup.PublicKey(tx.transaction.otherTrans?.signer == null ? tx.transaction.signer : tx.transaction.otherTrans.signer)))
                                            {
                                                continue;
                                            }
                                            else
                                            {
                                                Console.WriteLine("failed on transaction verification: amount or recipient is invalid");
                                                valid = false;
                                            }
                                        }
                                        else
                                        {
                                            Console.WriteLine("failed on transaction verification: invalid or missing mosaic");
                                        }
                                    }
                                    else if (m.CostDenomination == "XEM")
                                    {
                                        // set mosaic amount paid out.
                                        var q = m2.quantity;

                                        // get amount that should be paid out for the deposit
                                        var a = Calculations.BonusCalculation(m, Math.Ceiling((depositedAmount / decimal.Parse(m.MosaicCost)) / (decimal)Math.Pow(10, 6 - mosaicDivisibility)));

                                        // if incorrect, fail
                                        valid = a == q ? true : false;

                                        if (!valid)
                                        {
                                            Console.WriteLine("failed on transaction verification: amounts do not match");
                                        }
                                    }
                                    else
                                    {
                                        Console.WriteLine("failed on transaction verification: incorrect currency");
                                    }
                                }
                            }, m.MosaicNameSpace, m.MosaicID).AsyncWaitHandle.WaitOne();
                        }
                    }
                    else
                    {
                        Console.WriteLine("failed on transaction verification: missing hash");
                    }
                }
            }, t.transaction.otherTrans.signer).AsyncWaitHandle.WaitOne();

            return(valid);
        }