public static async Task <double> get_ETH_BalanceOfAnyAccount(String PublicKey, String PublicKeyToCheck)
        { //read the balance of any account (ETH), from the blockchain, used after blockchain operation
            PublicKey = PublicKey.ToLower();
            DappAccount account = openWith[PublicKey];

            if (PublicKey == null || account.IsValidated == false)
            {
                return(-1);
            }
            if (account.IsConnectedToBlockChain == false)
            {
                return(-1);
            }
            try
            {
                var balance = await account.Blockchain.Eth.GetBalance.SendRequestAsync(PublicKeyToCheck);

                var    etherAmount = Web3.Convert.FromWei(balance.Value);
                double tempBalance = (double)etherAmount;
                tempBalance = Math.Truncate(tempBalance * 1000) / 1000; //make the double number to be with 3 digits after dot
                return(tempBalance);
            }
            catch (Exception e)
            {
                return(-1);
            }
        }
Ejemplo n.º 2
0
        private async Task SwitchOwnership(string ContractAddress)
        { //replace the ownership upon the asset - buyer = new owner
            string               PublicKeySeller, PublicKeyNewOwner;
            DappAccount          account          = RegulatorController._regulator;
            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            var report = (from d in _AssetInContractsContext.AssetsInContract
                          where d.ContractAddress == ContractAddress
                          select d).Single();

            PublicKeySeller   = report.SellerPublicKey;
            PublicKeyNewOwner = await deployedContract.getNewAssetOwner();

            Asset dealAsset = await deployedContract.getAssetDestails();

            int dealAssetID = dealAsset.AssetID;
            var PublicKeyNewOwnerToCheck = report.BuyerPublicKey.ToLower();


            if (PublicKeyNewOwnerToCheck.Equals(PublicKeyNewOwner))
            {
                PublicKeyNewOwner = report.BuyerPublicKey;
                int newOwnerID = await GetAddressID(PublicKeyNewOwner);

                var report2 = (from d in _AssetsContext.Assets
                               where d.AssetID == dealAssetID
                               select d).Single();
                report2.OwnerPublicKey = PublicKeyNewOwner;
                report2.Price          = dealAsset.Price;
                report2.OwnerID        = newOwnerID;
                _AssetsContext.Assets.Update(report2);
                _AssetsContext.SaveChanges();
            }
        }
        public async Task <IActionResult> CreateContractPage(string PublicKey)
        { //here we load the page that responsible for the contracts deployment (=creation), we read all the asset which owned by the account
            await DappAccountController.RefreshAccountData(PublicKey);

            PublicKey = PublicKey.ToLower();
            DappAccount account = DappAccountController.openWith[PublicKey];

            List <AssetInContract> openContractsToCheck      = new List <AssetInContract>(); //we will check if there are asset the included in the table, if there are, we will delete the assets from OwnAssetsList in the Create contract View
            List <int>             AssetsNumsIncludedInDeals = new List <int>();
            List <Asset>           AssetsToDelete            = new List <Asset>();

            openContractsToCheck = await _AssetInContractsContext.AssetsInContract.FromSqlRaw("select * from AssetsInContract where SellerPublicKey = {0} and (Status ='Ongoing' or Status ='Pending' )", account.publicKey).ToListAsync();

            foreach (AssetInContract cnrt in openContractsToCheck) //Here we take all the assets ID that included in open contracts
            {
                AssetsNumsIncludedInDeals.Add(cnrt.AssetID);
            }


            foreach (Asset ast in account.OwnAssetsList) //Here we check each one of our assets if it is included in a deal, if yes, we will take the instance of this asset
            {
                if (AssetsNumsIncludedInDeals.Contains(ast.AssetID))
                {
                    AssetsToDelete.Add(ast);
                }
            }

            foreach (Asset astDel in AssetsToDelete) //We return to our assets list, and delete all the assets that we collected before (assets that included in the deal)
            {
                account.OwnAssetsList.Remove(astDel);
            }

            return(View(account)); //Here we will return own assets in the account that not included in open contracts
        }
        public async Task <double> CheckBuyerPublicKeyLegality(string BuyerPublicKey, string YourPublicKey)
        { //check legality of the public key form
            YourPublicKey = YourPublicKey.ToLower();
            DappAccount account      = DappAccountController.openWith[YourPublicKey];
            double      buyerBalance = await DappAccountController.get_ETH_BalanceOfAnyAccount(YourPublicKey, BuyerPublicKey);

            return(buyerBalance);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> ShowInGoingRequestsAsync()
        { //show the pending contracts page for regulator approval
            DappAccount account = RegulatorController._regulator;
            await DappAccountController.RefreshAccountData(account.publicKey);

            account.DeployedContractList = await GetPendingContracts();

            return(View("InGoingRequests", account));
        }
        public async Task <IActionResult> ShowOwnAssets(string PublicKey)
        { //get the assets owned by the accounts
            PublicKey = PublicKey.ToLower();
            DappAccount account = openWith[PublicKey];

            account.OwnAssetsList = null;
            account.OwnAssetsList = await _AssetsContext.Assets.FromSqlRaw("select * from Assets where OwnerPublicKey = {0}", account.publicKey).ToListAsync();

            await RefreshAccountData(PublicKey);

            return(View("AccountMainPage", account));
        }
Ejemplo n.º 7
0
        private async Task <List <ContractOffer> > GetPendingContracts()
        { //get all the open contracts (pending) that waits for the regulaotr approval
            DappAccount account = RegulatorController._regulator;
            await DappAccountController.RefreshAccountData(account.publicKey);

            List <AssetInContract> deployedContractsFromDB = new List <AssetInContract>();

            deployedContractsFromDB = await _AssetInContractsContext.AssetsInContract.FromSqlRaw("select * from AssetsInContract where Status= 'Pending'").ToListAsync();

            List <ContractOffer> deployedContractsFromBlockchain = new List <ContractOffer>();

            foreach (AssetInContract assCon in deployedContractsFromDB)
            {
                ContractOffer offer           = new ContractOffer();
                string        contractAddress = assCon.ContractAddress;
                offer.ContractAddress = contractAddress;
                SmartContractService deployedContract = new SmartContractService(account, contractAddress);
                Asset assetInContract = await deployedContract.getAssetDestails(); //read from blockchain

                offer.AssetID        = assetInContract.AssetID;
                offer.Loaction       = assetInContract.Loaction;
                offer.Rooms          = assetInContract.Rooms;
                offer.AreaIn         = assetInContract.AreaIn;
                offer.ImageURL       = assetInContract.ImageURL;
                offer.PriceETH       = assetInContract.Price;
                offer.PriceILS       = offer.PriceETH * account.exchangeRateETH_ILS;
                offer.PriceILS       = Math.Truncate(offer.PriceILS * 1000) / 1000;
                offer.BuyerPublicKey = await deployedContract.getBuyerAddress();

                offer.SellerPublicKey = await deployedContract.getOldAssetOwner();

                offer.SellerSign = await deployedContract.getSellerSign();

                offer.BuyerSign = await deployedContract.getBuyerSign();

                offer.RegulatorSign = await deployedContract.getRegulatorSign();

                offer.Tax = await deployedContract.getTax();

                offer.BuyerID = await GetAddressID(offer.BuyerPublicKey);

                offer.OwnerID = await GetAddressID(offer.SellerPublicKey);

                offer.NewOwnerPublicKey = await deployedContract.getNewAssetOwner();

                offer.NewOwnerID = await GetAddressID(offer.NewOwnerPublicKey);

                offer.EtherscanURL = "https://ropsten.etherscan.io/address/" + assCon.ContractAddress;
                deployedContractsFromBlockchain.Add(offer);
            }
            return(deployedContractsFromBlockchain);
        }
        public static async Task RefreshAccountData(string publicKey)
        { //update the balance of the account (read it from the blockchain)
            string publicKeyToCheck = publicKey;

            publicKey = publicKey.ToLower();
            DappAccount account = DappAccountController.openWith[publicKey];

            account.EthBalance = await get_ETH_Balance(publicKey, publicKeyToCheck);

            account.IlsBalance = await get_ILS_Balance(publicKey, publicKeyToCheck);

            account.exchangeRateETH_ILS = getExchangeRate_ETH_To_ILS();
        }
Ejemplo n.º 9
0
        public async Task <string> ApproveContract(string ContractAddress, string PublicKey)
        {
            //approve the contract seny by the seller (send money and sign the contract)

            DappAccount          account          = DappAccountController.openWith[PublicKey.ToLower()];
            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            double beforeBalanceETH = await DappAccountController.get_ETH_Balance(PublicKey, PublicKey);

            double beforeBalanceILS = await DappAccountController.get_ILS_Balance(PublicKey, PublicKey);

            double exchangeRate = DappAccountController.getExchangeRate_ETH_To_ILS();
            double afterBalanceETH;
            double afterBalanceILS;
            double feeETH;
            double feeILS;
            Asset  dealAsset = await deployedContract.getAssetDestails();

            double ethToPay = dealAsset.Price;
            bool   isPaid   = false;
            bool   isSigned = false;

            isPaid = await deployedContract.sendEtherToContract(ethToPay);

            if (isPaid == true)
            {
                isSigned = await deployedContract.setBuyerSign();
            }
            else
            {
                throw new Exception("Out of money");
            }

            afterBalanceETH = await DappAccountController.get_ETH_Balance(PublicKey, PublicKey);

            afterBalanceILS = await DappAccountController.get_ILS_Balance(PublicKey, PublicKey);

            feeETH = beforeBalanceETH - (afterBalanceETH + ethToPay);

            feeILS = exchangeRate * feeETH;
            ConfirmationRecipt recipt = new ConfirmationRecipt();

            recipt.ContractAddress = ContractAddress;
            recipt.feeETH          = feeETH;
            feeILS        = Math.Truncate(feeILS * 100) / 100; //make the double number to be with 3 digits after dot
            recipt.feeILS = feeILS;
            var ReciptJson = Newtonsoft.Json.JsonConvert.SerializeObject(recipt);

            updateOfferToPending(ContractAddress);
            return(ReciptJson);
        }
Ejemplo n.º 10
0
        public async Task <int> GetTimeLeft(string ContractAddress, string PublicKey)
        {
            //get the time left in seconds for the contract to be signed buy the buyer
            DappAccount          account          = DappAccountController.openWith[PublicKey.ToLower()];
            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            ulong time = await deployedContract.getTimeLeftInSeconds();

            int timeLeft = (int)time;

            if (timeLeft > 21)
            {
                timeLeft = timeLeft - 15;
            }
            return(timeLeft);
        }
Ejemplo n.º 11
0
        public async Task <string> ApproveContractAsRegulatorAsync(string ContractAddress)
        { //approve the contract (regulator do it)
            DappAccount account = RegulatorController._regulator;
            await DappAccountController.RefreshAccountData(account.publicKey);

            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            Asset assetDetials = await deployedContract.getAssetDestails();

            double DealPriceEth     = assetDetials.Price;
            double taxToGet         = DealPriceEth * 0.17;
            double beforeBalanceETH = await DappAccountController.get_ETH_Balance(account.publicKey, account.publicKey);

            double beforeBalanceILS = await DappAccountController.get_ILS_Balance(account.publicKey, account.publicKey);

            double exchangeRate = DappAccountController.getExchangeRate_ETH_To_ILS();
            double afterBalanceETH;
            double feeETH;
            double feeILS;
            var    isApproved = await deployedContract.approveAndExcecute(0.17);

            if (isApproved == true)
            {
                afterBalanceETH = await DappAccountController.get_ETH_Balance(account.publicKey, account.publicKey);

                feeETH = afterBalanceETH - beforeBalanceETH - taxToGet;
                feeILS = feeETH * exchangeRate;
                feeILS = Math.Truncate(feeILS * 100) / 100; //make the double number to be with 3 digits after dot
                await UpdateContractToApprovedInDB(ContractAddress);
                await SwitchOwnership(ContractAddress);
            }

            else
            {
                throw new Exception("Out of money");
            }


            RegulatorConfirmationRecipt recipt = new RegulatorConfirmationRecipt();

            recipt.ContractAddress = ContractAddress;
            recipt.feeETH          = feeETH;
            recipt.feeILS          = feeILS;
            var ReciptJson = Newtonsoft.Json.JsonConvert.SerializeObject(recipt);

            return(ReciptJson);
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> ShowAccountsPage()
        {   //show the public-key -> ID table in the page, read all web accounts
            DappAccount account = RegulatorController._regulator;
            await DappAccountController.RefreshAccountData(account.publicKey);

            regulatorAcc                     = new RegulatorAccount();
            regulatorAcc.publicKey           = account.publicKey;
            regulatorAcc.IsValidated         = account.IsValidated;
            regulatorAcc.EthBalance          = account.EthBalance;
            regulatorAcc.IlsBalance          = account.IlsBalance;
            regulatorAcc.exchangeRateETH_ILS = account.exchangeRateETH_ILS;
            regulatorAcc.BlockchainAcount    = account.BlockchainAcount;
            regulatorAcc.Blockchain          = account.Blockchain;

            regulatorAcc.AllAccounts = await _AccountsContext.Accounts.FromSqlRaw("select * from Accounts").ToListAsync();

            return(View("AccountsManagment", regulatorAcc));
        }
        public bool CheckLogin(string _publicKey, string _privateKey)
        { //check login details
            DappAccount tempAccount = new DappAccount();

            try
            {
                tempAccount.BlockchainAcount = new Nethereum.Web3.Accounts.Account(_privateKey);
                String declaredAddress = _publicKey;                           //address from son = textbox of our web dApp = Account class that Haim created
                String realAddress     = tempAccount.BlockchainAcount.Address; //BlockchainAcount.Address is the real address of the private key
                if (!declaredAddress.Equals(realAddress))                      //If the addresses are matched => Login details are correct = > dApp will show wallt(account) content in the next view
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                return(false);
            }

            return(true);
        }
        public bool ConnectToBlockchain(String PublicKey)
        { //connect to blockchain API using Nethereum
            PublicKey = PublicKey.ToLower();
            DappAccount account = openWith[PublicKey];

            if (account.IsValidated == true)
            {
                try
                {
                    var infuraURL = "https://ropsten.infura.io/v3/4dc41c6f591d4d61a3a2e32a219c6635";
                    account.Blockchain = new Web3(account.BlockchainAcount, infuraURL);
                }
                catch (Exception e)
                {
                    account.IsConnectedToBlockChain = false;
                    return(false);
                }
            }
            account.IsConnectedToBlockChain = true;
            return(account.IsConnectedToBlockChain);
        }
Ejemplo n.º 15
0
        public async Task <string> CancelContractAsRegulator(string ContractAddress, string DenyNotes)
        {   //cancel the contract and return the money to the buyer
            DappAccount account = RegulatorController._regulator;
            await DappAccountController.RefreshAccountData(account.publicKey);

            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            double beforeBalanceETH = await DappAccountController.get_ETH_Balance(account.publicKey, account.publicKey);

            double beforeBalanceILS = await DappAccountController.get_ILS_Balance(account.publicKey, account.publicKey);

            double exchangeRate = DappAccountController.getExchangeRate_ETH_To_ILS();
            double afterBalanceETH;
            double feeETH;
            double feeILS;
            var    ReciptJson = "";
            var    isCanceled = await deployedContract.cancelDeal();

            if (isCanceled == true)
            {
                afterBalanceETH = await DappAccountController.get_ETH_Balance(account.publicKey, account.publicKey);

                feeETH = beforeBalanceETH - afterBalanceETH;
                feeILS = feeETH * exchangeRate;
                feeILS = Math.Truncate(feeILS * 100) / 100; //make the double number to be with 3 digits after dot
                RegulatorConfirmationRecipt recipt = new RegulatorConfirmationRecipt();
                recipt.ContractAddress = ContractAddress;
                recipt.feeETH          = feeETH;
                recipt.feeILS          = feeILS;
                ReciptJson             = Newtonsoft.Json.JsonConvert.SerializeObject(recipt);
                UpdateContractToDeniedAsRegulatorInDB(ContractAddress, DenyNotes);


                return(ReciptJson);
            }

            else
            {
                throw new Exception("No money the sign the transaction");
            }
        }
        public IActionResult AccountMainPage(DappAccount account)
        {   //here the login succeeded , we initialized the basic details of the blockchain account
            account.BlockchainAcount = new Nethereum.Web3.Accounts.Account(account.privateKey);
            account.IsValidated      = true;
            string myPublicKey = account.publicKey.ToLower();

            if (openWith.ContainsKey(myPublicKey))
            {
                openWith.Remove(myPublicKey);
            }
            openWith.Add(myPublicKey, account);

            ConnectToBlockchain(account.publicKey);

            if (String.Compare(account.publicKey, REGULATOR_PUBLIC_KEY) == 0)
            {// account is regulator
                RegulatorController._regulator = account;
                return(RedirectToAction("RegulatorMainPage", "Regulator"));
            }


            return(RedirectToAction("ShowOwnAssets", "DappAccount", new { PublicKey = account.publicKey }));
        }
Ejemplo n.º 17
0
        public async Task <string> CancelDealAsBuyer(string ContractAddress, string Notes, string PublicKey)
        { //deny the contract /cancel the contract sent by seller
            DappAccount          account          = DappAccountController.openWith[PublicKey];
            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            double beforeBalanceETH = await DappAccountController.get_ETH_Balance(PublicKey, PublicKey);

            double beforeBalanceILS = await DappAccountController.get_ILS_Balance(PublicKey, PublicKey);

            double exchangeRate = DappAccountController.getExchangeRate_ETH_To_ILS();
            double afterBalanceETH;
            double afterBalanceILS;
            double feeETH;
            double feeILS;
            bool   isDenied = false;

            isDenied = await deployedContract.denyDeal();

            if (isDenied == true)
            {
                afterBalanceETH = await DappAccountController.get_ETH_Balance(PublicKey, PublicKey);

                afterBalanceILS = await DappAccountController.get_ILS_Balance(PublicKey, PublicKey);

                feeETH = beforeBalanceETH - afterBalanceETH;
                feeILS = beforeBalanceILS - afterBalanceILS;
                ConfirmationRecipt recipt = new ConfirmationRecipt();
                recipt.ContractAddress = ContractAddress;
                recipt.feeETH          = feeETH;
                feeILS        = Math.Truncate(feeILS * 100) / 100; //make the double number to be with 3 digits after dot
                recipt.feeILS = feeILS;
                var ReciptJson = Newtonsoft.Json.JsonConvert.SerializeObject(recipt);
                updateOfferToDenied(ContractAddress, Notes);
                return(ReciptJson);
            }

            return("Fail");
        }
Ejemplo n.º 18
0
        private async Task UpdateContractToApprovedInDB(string ContractAddress)
        { //update registry - contract in "InGoing state" to "Approve" state
            DappAccount          account          = RegulatorController._regulator;
            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            Asset dealAsset = await deployedContract.getAssetDestails();

            int             dealAssetID = dealAsset.AssetID;
            AssetInContract report;

            try
            {
                report = (from d in _AssetInContractsContext.AssetsInContract
                          where d.AssetID == dealAssetID && d.Status.Equals("Approved")
                          select d).Single();
            }
            catch (Exception e)
            {
                report = null;
            }


            if (report != null)
            {
                _AssetInContractsContext.AssetsInContract.Remove(report);
                _AssetInContractsContext.SaveChanges();
            }



            var report2 = (from d in _AssetInContractsContext.AssetsInContract
                           where d.ContractAddress == ContractAddress
                           select d).Single();

            report2.Status = "Approved";
            _AssetInContractsContext.AssetsInContract.Update(report2);
            _AssetInContractsContext.SaveChanges();
        }
Ejemplo n.º 19
0
        public async Task <string> CancelDealAsSeller(string ContractAddress, string PublicKey)
        {   //cancel the contract if the time is over (the buyer didn`t sign in time)
            DappAccount          account          = DappAccountController.openWith[PublicKey];
            SmartContractService deployedContract = new SmartContractService(account, ContractAddress);
            double beforeBalanceETH = await DappAccountController.get_ETH_Balance(PublicKey, PublicKey);

            double beforeBalanceILS = await DappAccountController.get_ILS_Balance(PublicKey, PublicKey);

            double exchangeRate = DappAccountController.getExchangeRate_ETH_To_ILS();
            double afterBalanceETH;
            double afterBalanceILS;
            double feeETH;
            double feeILS;
            bool   isCanceled = false;

            isCanceled = await deployedContract.abort();

            if (isCanceled == true)
            {
                afterBalanceETH = await DappAccountController.get_ETH_Balance(PublicKey, PublicKey);

                afterBalanceILS = await DappAccountController.get_ILS_Balance(PublicKey, PublicKey);

                feeETH = beforeBalanceETH - afterBalanceETH;
                feeILS = beforeBalanceILS - afterBalanceILS;
                ConfirmationRecipt recipt = new ConfirmationRecipt();
                recipt.ContractAddress = ContractAddress;
                recipt.feeETH          = feeETH;
                feeILS        = Math.Truncate(feeILS * 100) / 100; //make the double number to be with 3 digits after dot
                recipt.feeILS = feeILS;
                var ReciptJson = Newtonsoft.Json.JsonConvert.SerializeObject(recipt);
                deleteCanceledOfferBySeller(ContractAddress);
                return(ReciptJson);
            }

            return("Fail");
        }
Ejemplo n.º 20
0
        public async Task <IActionResult> ShowHomePage()
        {   //read all closed contracts and show it in page
            DappAccount            account = _regulator;
            List <AssetInContract> deployedContractsFromDB = new List <AssetInContract>();

            deployedContractsFromDB = await _AssetInContractsContext.AssetsInContract.FromSqlRaw("select * from AssetsInContract where Status= 'Approved' or (Status= 'Denied' and DeniedBy = 'Regulator') ").ToListAsync();

            List <ContractOffer> deployedContractsFromBlockchain = new List <ContractOffer>();

            foreach (AssetInContract assCon in deployedContractsFromDB)
            {
                ContractOffer offer           = new ContractOffer();
                string        contractAddress = assCon.ContractAddress;
                offer.ContractAddress = contractAddress;
                if (!assCon.Status.Equals("Denied"))  //For the non-destroyed contracts, "Denied" => Self Destruction
                {
                    SmartContractService deployedContract = new SmartContractService(account, contractAddress);
                    Asset assetInContract = await deployedContract.getAssetDestails(); //read from blockchain

                    offer.AssetID        = assetInContract.AssetID;
                    offer.Loaction       = assetInContract.Loaction;
                    offer.Rooms          = assetInContract.Rooms;
                    offer.AreaIn         = assetInContract.AreaIn;
                    offer.ImageURL       = assetInContract.ImageURL;
                    offer.PriceETH       = assetInContract.Price;
                    offer.PriceILS       = offer.PriceETH * account.exchangeRateETH_ILS;
                    offer.PriceILS       = Math.Truncate(offer.PriceILS * 1000) / 1000;
                    offer.BuyerPublicKey = await deployedContract.getBuyerAddress();

                    offer.SellerPublicKey = await deployedContract.getOldAssetOwner();

                    offer.SellerSign = await deployedContract.getSellerSign();

                    offer.BuyerSign = await deployedContract.getBuyerSign();

                    offer.RegulatorSign = await deployedContract.getRegulatorSign();

                    offer.Tax = await deployedContract.getTax();

                    offer.BuyerID = await GetAddressID(offer.BuyerPublicKey);

                    offer.OwnerID = await GetAddressID(offer.SellerPublicKey);

                    offer.NewOwnerPublicKey = await deployedContract.getNewAssetOwner();

                    offer.NewOwnerID = await GetAddressID(offer.NewOwnerPublicKey);
                }

                else
                {
                    offer.SellerPublicKey     = assCon.SellerPublicKey;
                    offer.BuyerPublicKey      = assCon.BuyerPublicKey;
                    offer.PriceETH            = assCon.DealPrice;
                    offer.PriceILS            = offer.PriceETH * account.exchangeRateETH_ILS;
                    offer.BuyerSign           = true;
                    offer.RegulatorSign       = false;
                    offer.IsDeniedByBuyer     = false;
                    offer.IsDeniedByRegulator = true;
                    offer.SellerSign          = true;
                    offer.DenyReason          = assCon.Reason;
                    offer.AssetID             = assCon.AssetID;
                    List <Asset> AssetsInDB = new List <Asset>();
                    AssetsInDB = await _AssetsContext.Assets.FromSqlRaw("select * from Assets where AssetID = {0}", offer.AssetID).ToListAsync();

                    offer.Loaction = AssetsInDB[0].Loaction;
                    offer.OwnerID  = await GetAddressID(offer.SellerPublicKey);

                    offer.BuyerID = await GetAddressID(offer.BuyerPublicKey);

                    offer.AreaIn     = AssetsInDB[0].AreaIn;
                    offer.Rooms      = AssetsInDB[0].Rooms;
                    offer.ImageURL   = AssetsInDB[0].ImageURL;
                    offer.DenyReason = assCon.Reason;
                }

                offer.EtherscanURL = "https://ropsten.etherscan.io/address/" + assCon.ContractAddress;
                deployedContractsFromBlockchain.Add(offer);
            }


            account.DeployedContractList = deployedContractsFromBlockchain;
            return(View("RegulatorMainPage", _regulator));
        }
Ejemplo n.º 21
0
        public async Task <IActionResult> ContractsStatusPage(string PublicKey) //now
        {                                                                       //load the page of the open/closed contracts, we read all the related open contracts and sort them
            await DappAccountController.RefreshAccountData(PublicKey);

            DappAccount            account = DappAccountController.openWith[PublicKey.ToLower()];
            List <AssetInContract> deployedContractsFromDB = new List <AssetInContract>();

            deployedContractsFromDB = await _AssetInContractsContext.AssetsInContract.FromSqlRaw("select * from AssetsInContract where ( SellerPublicKey = {0} or BuyerPublicKey = {0} )", account.publicKey).ToListAsync();

            List <ContractOffer> deployedContractsFromBlockchain = new List <ContractOffer>();

            foreach (AssetInContract assCon in deployedContractsFromDB)
            {
                ContractOffer offer           = new ContractOffer();
                string        contractAddress = assCon.ContractAddress;
                offer.ContractAddress = contractAddress;
                if (!assCon.Status.Equals("Denied"))  //For the non-destroyed contracts, "Denied" => Self Destruction
                {
                    SmartContractService deployedContract = new SmartContractService(account, contractAddress);
                    Asset assetInContract = await deployedContract.getAssetDestails(); //read from blockchain

                    offer.AssetID        = assetInContract.AssetID;
                    offer.Loaction       = assetInContract.Loaction;
                    offer.Rooms          = assetInContract.Rooms;
                    offer.AreaIn         = assetInContract.AreaIn;
                    offer.ImageURL       = assetInContract.ImageURL;
                    offer.PriceETH       = assetInContract.Price;
                    offer.PriceILS       = offer.PriceETH * account.exchangeRateETH_ILS;
                    offer.PriceILS       = Math.Truncate(offer.PriceILS * 1000) / 1000;
                    offer.BuyerPublicKey = await deployedContract.getBuyerAddress();

                    offer.SellerPublicKey = await deployedContract.getOldAssetOwner();

                    offer.SellerSign = await deployedContract.getSellerSign();

                    offer.BuyerSign = await deployedContract.getBuyerSign();

                    offer.RegulatorSign = await deployedContract.getRegulatorSign();

                    offer.Tax = await deployedContract.getTax();

                    offer.BuyerID = await GetAddressID(offer.BuyerPublicKey);

                    offer.OwnerID = await GetAddressID(offer.SellerPublicKey);

                    if (assCon.Status.Equals("Approved"))
                    {
                        offer.NewOwnerPublicKey = await deployedContract.getNewAssetOwner();

                        offer.NewOwnerID = await GetAddressID(offer.NewOwnerPublicKey);
                    }

                    if (assCon.Status.Equals("Ongoing"))
                    {
                        ulong time = await deployedContract.getTimeLeftInSeconds();

                        int timeLeft = (int)time;
                        if (timeLeft > 21)
                        {
                            timeLeft = timeLeft - 20;
                        }
                        offer.TimeToBeOpen = timeLeft;
                    }
                }

                else
                {
                    offer.SellerPublicKey = assCon.SellerPublicKey;
                    offer.BuyerPublicKey  = assCon.BuyerPublicKey;
                    offer.PriceETH        = assCon.DealPrice;
                    offer.PriceILS        = offer.PriceETH * account.exchangeRateETH_ILS;
                    if (assCon.DeniedBy.Equals("Buyer"))
                    {
                        offer.BuyerSign           = false;
                        offer.RegulatorSign       = false;
                        offer.IsDeniedByBuyer     = true;
                        offer.IsDeniedByRegulator = false;
                    }

                    else if (assCon.DeniedBy.Equals("Regulator"))
                    {
                        offer.BuyerSign           = true;
                        offer.RegulatorSign       = false;
                        offer.IsDeniedByBuyer     = false;
                        offer.IsDeniedByRegulator = true;
                    }

                    offer.SellerSign = true;
                    offer.DenyReason = assCon.Reason;
                    offer.AssetID    = assCon.AssetID;
                    List <Asset> AssetsInDB = new List <Asset>();
                    AssetsInDB = await _AssetsContext.Assets.FromSqlRaw("select * from Assets where AssetID = {0}", offer.AssetID).ToListAsync();

                    offer.Loaction = AssetsInDB[0].Loaction;
                    offer.OwnerID  = await GetAddressID(offer.SellerPublicKey);

                    offer.BuyerID = await GetAddressID(offer.BuyerPublicKey);

                    offer.AreaIn     = AssetsInDB[0].AreaIn;
                    offer.Rooms      = AssetsInDB[0].Rooms;
                    offer.ImageURL   = AssetsInDB[0].ImageURL;
                    offer.DenyReason = assCon.Reason;
                }

                offer.EtherscanURL = "https://ropsten.etherscan.io/address/" + assCon.ContractAddress;
                deployedContractsFromBlockchain.Add(offer);
            }


            account.DeployedContractList = deployedContractsFromBlockchain;
            return(View(account));
        }