예제 #1
0
        /// <summary>
        /// Reserves/locks away one S and F asset and sets the recommendation to reference these assets.
        /// </summary>
        /// <param name="db">DataContext</param>
        /// <param name="recommendation">Recommendation</param>
        public static void MakeRecommendation(DataContext db, TrustLessModelLib.Recommendation recommendation)
        {
            string chainName = WebConfigurationManager.AppSettings["ChainName"];
            string nodeIp = WebConfigurationManager.AppSettings["NodeIp"];
            MultiChainClient client = new MultiChainClient(chainName,nodeIp);
            Dictionary<string,int> dictionary = new Dictionary<string, int>();
            AddressBalance[] addressBalances = client.GetTotalBalances ().Balances;
            var lastFasset = addressBalances.OrderBy (x => x.name).LastOrDefault (x => x.name.StartsWith ("F_"));
            var lastSasset = addressBalances.OrderBy (x => x.name).LastOrDefault (x => x.name.StartsWith ("S_"));
            if (lastFasset == null || lastSasset == null) {
                GenerateFunds (client);
                MakeRecommendation (db, recommendation);
                return;
            }
            string lastF = lastFasset.name;
            string lastS = lastSasset.name;

            dictionary.Add(lastF,1);
            dictionary.Add(lastS,1);
            try
            {
                var resp = client.PrepareLockUnspent(dictionary);

                recommendation.Transaction = new ReservedTransaction()
                {
                    Tx = resp.txid,
                    Vout = resp.vout
                };
            }
            catch {
                //Insurficient funds
                GenerateFunds(client);
                MakeRecommendation (db, recommendation);
                return;
            }

            db.SaveChanges ();
        }
예제 #2
0
        public int GetF()
        {
            //TODO ask client node for F'er

            //Make RPC connection to servernode
            string chainName = WebConfigurationManager.AppSettings["ChainName"];
            string nodeIp = WebConfigurationManager.AppSettings["NodeIp"];
            MultiChainClient client = new MultiChainClient(chainName,nodeIp);
            var resp = client.GetTotalBalances ();
            return resp.Balances.Where (x => x.name.StartsWith("F_")).Sum(y => (int)y.qty);
        }
예제 #3
0
        /// <summary>
        /// If the balance of S_X or F_X is equals zero, a new unique asset S_X or F_X will be issued.
        /// </summary>
        /// <param name="client">MultichainClient</param>
        private static void GenerateFunds(MultiChainClient client)
        {
            lock (issueLock) {
                AddressBalance[] addressBalances = client.GetTotalBalances ().Balances;
                ListAssetsResponseAsset[] assets = client.ListAssets ().Assets;
                    string rootKey = WebConfigurationManager.AppSettings["MultichainPublicKey"];
                    ListAssetsResponseAsset lastSasset = assets.OrderBy (x => x.name).LastOrDefault (x => x.name.StartsWith ("S_"));
                    string lastS = lastSasset != null ? lastSasset.name : "S_0";
                    ListAssetsResponseAsset lastFasset = assets.OrderBy (x => x.name).LastOrDefault (x => x.name.StartsWith ("F_"));
                    string lastF = lastSasset != null ? lastSasset.name : "F_0";
                    string newS = "S_" + (Convert.ToInt32 (lastS.Substring (2)) + 1);
                    string newF = "F_" + (Convert.ToInt32 (lastS.Substring (2)) + 1);
                var balanceS = addressBalances.FirstOrDefault (x => x.name == lastS);
                var balanceF = addressBalances.FirstOrDefault (x => x.name == lastF);

                if (balanceS == null || balanceS.qty == 0)
                    client.Issue (rootKey, newS, 100000, 1.0);
                if (balanceF == null || balanceF.qty == 0)
                    client.Issue (rootKey, newF, 100000, 1.0);
            }
        }