private async Task ProcessBroadcastedCommitment(ICommitment commitment, ICoin spendingCoin)
        {
            var lastCommitment = await _commitmentRepository.GetLastCommitment(commitment.Multisig, commitment.AssetId, commitment.Type);

            if (lastCommitment.CommitmentId == commitment.CommitmentId)
            {
                await _logger.WriteInfoAsync("OffchainCommitmentMonitoringFunction", "ProcessBroadcastedCommitment",
                                             $"CommitmentId: {commitment.CommitmentId}", "Last commitment was broadcasted");

                await _offchainService.CloseChannel(commitment);

                await _commitmentBroadcastRepository.InsertCommitmentBroadcast(commitment.CommitmentId, spendingCoin.Outpoint.Hash.ToString(),
                                                                               CommitmentBroadcastType.Valid, commitment.ClientAmount, commitment.HubAmount, commitment.ClientAmount, commitment.HubAmount, null);

                return;
            }
            await _logger.WriteWarningAsync("OffchainCommitmentMonitoringFunction", "ProcessBroadcastedCommitment",
                                            $"CommitmentId: {commitment.CommitmentId}", "Commitment is not last.");

            if (commitment.Type == CommitmentType.Client)
            {
                var assetSettings = await _offchainService.GetAssetSetting(commitment.AssetId);

                var hash = await _offchainService.SpendCommitmemtByMultisig(commitment, spendingCoin, !string.IsNullOrEmpty(assetSettings.ChangeWallet)?assetSettings.ChangeWallet : assetSettings.HotWallet);

                await _offchainService.CloseChannel(commitment);

                await _commitmentBroadcastRepository.InsertCommitmentBroadcast(commitment.CommitmentId, spendingCoin.Outpoint.Hash.ToString(),
                                                                               CommitmentBroadcastType.Revoked, commitment.ClientAmount, commitment.HubAmount, lastCommitment.ClientAmount, lastCommitment.HubAmount, hash);
            }
            else
            {
                await _slackNotifier.WarningAsync($"Hub commitment with id {commitment.CommitmentId} was broadcasted but it's not last");
            }
        }
Exemplo n.º 2
0
        private void BuildCommitmentEntity(ICommitment commitment)
        {
            int res = 0;

            if (ViewState["CommID"] == null)
            {
                commitment.CommitmentId = -1;
            }
            else
            {
                commitment.CommitmentId = Convert.ToInt32(ViewState["CommID"]) - 1;
            }

            ViewState["CommID"] = commitment.CommitmentId;

            commitment.CallId     = 1; // get call id after save header call section.
            commitment.CustomerId = Convert.ToInt32(ddlCustomer.SelectedValue);
            int.TryParse(txtFEU.Text.Trim(), out res);
            commitment.FEU      = res;
            commitment.PortCode = txtDestination.Text.Trim();
            commitment.PortId   = _portId;
            int.TryParse(txtTEU.Text.Trim(), out res);
            commitment.TEU    = res;
            commitment.WeekNo = Convert.ToInt32(txtWeekNo.Text);
            commitment.UserId = _userId;
        }
        public async Task CloseChannel(ICommitment commitment)
        {
            await _lykkeTransactionBuilderService.SaveSpentOutputs(commitment.CommitmentId, new Transaction(commitment.InitialTransaction));

            await _offchainChannelRepository.CloseChannel(commitment.Multisig, commitment.AssetId, commitment.ChannelId);

            await _commitmentRepository.CloseCommitmentsOfChannel(commitment.Multisig, commitment.AssetId, commitment.ChannelId);
        }
Exemplo n.º 4
0
        public void QueueCommitment(ICommitment commitment)
        {
            if (this.PendingCommitment != null)
            {
                return;
            }

            this.PendingCommitment = commitment;
        }
Exemplo n.º 5
0
        private void DeleteCommitmentDetails(int CommitmentId)
        {
            if (ViewState["CommittmentDetails"] != null)
            {
                commitmentDetails = ViewState["CommittmentDetails"] as List <ICommitment>;
            }

            ICommitment committment = commitmentDetails.Single(c => c.CommitmentId == CommitmentId);

            commitmentDetails.Remove(committment);
            ViewState["CommittmentDetails"] = commitmentDetails;

            ClearCommitmentDetail();
            RefreshGridView();
        }
Exemplo n.º 6
0
        private void EditCommitment(int CommitmentId)
        {
            if (ViewState["CommittmentDetails"] != null)
            {
                commitmentDetails = ViewState["CommittmentDetails"] as List <ICommitment>;
            }

            ICommitment committment = commitmentDetails.Single(c => c.CommitmentId == CommitmentId);

            txtDestination.Text = committment.PortCode;
            txtFEU.Text         = Convert.ToString(committment.FEU);
            txtTEU.Text         = Convert.ToString(committment.TEU);
            txtWeekNo.Text      = Convert.ToString(committment.WeekNo);

            ViewState["CommitmentId"] = CommitmentId;
            btnAddToGrid.Text         = "Save Record";
        }
Exemplo n.º 7
0
        public static int SaveCommitment(ICommitment commitment)
        {
            string strExecution = "[common].[uspSaveCommitmentDetails]";
            int    result       = 0;

            using (DbQuery oDq = new DbQuery(strExecution))
            {
                oDq.AddIntegerParam("@fk_CallID", commitment.CallId);
                oDq.AddIntegerParam("@fk_CustID", commitment.CustomerId);
                oDq.AddIntegerParam("@WeekNo", commitment.WeekNo);
                oDq.AddIntegerParam("@fk_PortID", commitment.PortId);
                oDq.AddIntegerParam("@TEU", commitment.TEU);
                oDq.AddIntegerParam("@FEU", commitment.FEU);
                oDq.AddIntegerParam("@Result", result, QueryParameterDirection.Output);
                oDq.RunActionQuery();
                result = Convert.ToInt32(oDq.GetParaValue("@Result"));
            }
            return(result);
        }
        public async Task SpendCommitmemtByMultisig(ICommitment commitment, ICoin spendingCoin, string destination)
        {
            TransactionBuildContext context = new TransactionBuildContext(_connectionParams.Network, _pregeneratedOutputsQueueFactory);

            var destinationAddress = BitcoinAddress.Create(destination);

            await context.Build(async() =>
            {
                TransactionBuilder builder = new TransactionBuilder();
                builder.AddCoins(spendingCoin);
                if (OpenAssetsHelper.IsBitcoin(commitment.AssetId))
                {
                    builder.Send(destinationAddress, spendingCoin.Amount);
                }
                else
                {
                    builder.SendAsset(destinationAddress, ((ColoredCoin)spendingCoin).Amount);
                }
                await _transactionBuildHelper.AddFee(builder, context);

                var tr = builder.BuildTransaction(false);

                var scriptParams = new OffchainScriptParams
                {
                    IsMultisig   = true,
                    RedeemScript = commitment.LockedScript.ToScript().ToBytes(),
                    Pushes       = new[] { new byte[0], new byte[0], new byte[0] }
                };

                tr.Inputs[0].ScriptSig = OffchainScriptCommitmentTemplate.GenerateScriptSig(scriptParams);

                var signed = await _signatureApiProvider.SignTransaction(tr.ToHex());

                var signedTr = new Transaction(signed);
                var id       = Guid.NewGuid();
                await _rpcBitcoinClient.BroadcastTransaction(signedTr, id);

                await _lykkeTransactionBuilderService.SaveSpentOutputs(id, signedTr);

                return(Task.CompletedTask);
            });
        }