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> CheckPublicKeyValidity(string PublicKey) { //check the legality of the public-key in order to save it in the db table var YourPublicKey = regulatorAcc.publicKey; var PublicKeyToCheck = PublicKey; double Balance = await DappAccountController.get_ETH_BalanceOfAnyAccount(YourPublicKey, PublicKeyToCheck); return(Balance); }
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); }
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)); }
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 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); }
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); }
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 async Task <string> DeployContract(ContractOffer offer) { //deploy (=create) the contract, save it in the blockahin and in the DB table - AssetInContracts double beforeBalanceETH = await DappAccountController.get_ETH_Balance(offer.SellerPublicKey, offer.SellerPublicKey); double beforeBalanceILS = await DappAccountController.get_ILS_Balance(offer.SellerPublicKey, offer.SellerPublicKey); double exchangeRate = DappAccountController.getExchangeRate_ETH_To_ILS(); double afterBalanceETH; double afterBalanceILS; double feeETH; double feeILS; try { InsertAssetInContractToDB(offer, "Busy"); var account = DappAccountController.openWith[offer.SellerPublicKey.ToLower()]; var ContractAddress = await SmartContractService.Deploy(account, offer); afterBalanceETH = await DappAccountController.get_ETH_Balance(offer.SellerPublicKey, offer.SellerPublicKey); afterBalanceILS = await DappAccountController.get_ILS_Balance(offer.SellerPublicKey, offer.SellerPublicKey); feeETH = beforeBalanceETH - afterBalanceETH; feeILS = beforeBalanceILS - afterBalanceILS; InsertAssetInContractToDB(offer, ContractAddress); RemoveBusyAssetInContractFromDB(offer); DeploymentRecipt recipt = new DeploymentRecipt(); 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); return(ReciptJson); } catch (Exception e) { RemoveBusyAssetInContractFromDB(offer); return("Error"); } }
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 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"); }
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"); }
public async Task <IActionResult> RegulatorMainPage() { //here the login succeeded await DappAccountController.RefreshAccountData(_regulator.publicKey); return(RedirectToAction("ShowHomePage", "Regulator")); }
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)); }