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(); } }
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); }
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(); }
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)); }
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)); }