/// <summary> /// Init ethereum transaction. /// </summary> /// <param name="trx">Transaction data model.</param> /// <param name="model">Transaction view model.</param> private async Task InitializationEthereum(Transaction trx, TransactionViewModel model) { var merchant = await _merchantRepository.GetByUser(trx.UserId); if (string.IsNullOrWhiteSpace(trx.Address) && !string.IsNullOrWhiteSpace(trx.XPubKey)) { var account = await this._ethereumService.CreateAddress(trx.EtherId); trx.Address = account.Item1; trx.PassPhrase = account.Item2; var foundsWithdraw = MAD.Withdraw(trx.Amount); var foundsDeposit = MAD.Deposit(trx.Amount); var estimateGasSize = await _ethereumService.EstimateGasSize(trx.EtherId, merchant.EthereumAddress, trx.XPubKey, foundsWithdraw.Item1, foundsWithdraw.Item2, foundsDeposit.Item1, trx.Address, trx.PassPhrase); trx.Fee = CurrencyConverter.WeiToEther(estimateGasSize.Value); await this._transactionRepository.Update(trx); } if (!string.IsNullOrWhiteSpace(trx.Address)) { model.ABI = this._ethereumService.GetABI(); model.Balance = CurrencyConverter.WeiToEther((await this._ethereumService.GetBalance(trx.Address)).Value).ToString(); } model.BuyerAddress = trx.XPubKey; model.FeeAddress = trx.Address; model.FeeValue = trx.Fee.ToString(); }
public async Task <IActionResult> DeployContract([FromBody] ContractModel model) { var trx = await _transactionRepository.GetByHash(model.Hash); var userMerchant = await _merchantRepository.GetByUser(trx.UserId); var user = await _userManager.FindByIdAsync(trx.UserId); if (trx == null) { throw new ApplicationException($"Unable to load transaction '{model.Hash}'."); } var contract = new Tuple <string, string>(null, null); if (await CompareBalance(trx)) { var foundsWithdraw = MAD.Withdraw(trx.Amount); var foundsDeposit = MAD.Deposit(trx.Amount); var balance = await this._ethereumService.GetBalance(trx.Address); contract = await _ethereumService.DeployContract(trx.EtherId, userMerchant.EthereumAddress, trx.XPubKey, foundsWithdraw.Item1, foundsWithdraw.Item2, foundsDeposit.Item1, balance, trx.Address, trx.PassPhrase); trx.ContractAddress = contract.Item1; trx.Status = StatusTransaction.Deployed; var message = "Buyer has deploy contract on blockchain."; await _transactionRepository.Update(trx); await _logRepository.Add(trx.Id, message); await _emailSender.SendEmailTransactionAsync(user.Email, message); } return(Json(new { Abi = contract.Item2, ContractAddress = contract.Item1 })); }
/// <summary> /// Init electrum transaction. /// </summary> /// <param name="trx">Transaction data model.</param> /// <param name="model">Transaction view model.</param> private async Task InitializationElectrum(Transaction trx, TransactionViewModel model) { if (trx.Status == StatusTransaction.FullDeposit && _bitcoinService.CheckInTransaction(trx.TrxDepositId, trx.RedeemScript)) { var userMerchant = await _merchantRepository.GetByUser(trx.UserId); var founds = MAD.Withdraw(trx.Amount); var script = _bitcoinService.CreateOutTransactionElectrum( trx.RedeemScript, trx.XPubKey, userMerchant.XPubKey, founds.Item1, founds.Item2); if (script != null) { trx.UnconfirmedWithdrawTx = script; trx.Status = StatusTransaction.UnconfirmedWithdraw; model.UnconfirmedWithdrawTx = script; await _transactionRepository.Update(trx); } } }