|
| 1 | +using Angor.Sdk.Common; |
| 2 | +using Angor.Sdk.Funding.Investor.Domain; |
| 3 | +using Angor.Sdk.Funding.Projects.Domain; |
| 4 | +using Angor.Sdk.Funding.Services; |
| 5 | +using Angor.Sdk.Funding.Shared; |
| 6 | +using Angor.Sdk.Funding.Shared.TransactionDrafts; |
| 7 | +using Angor.Shared; |
| 8 | +using Angor.Shared.Models; |
| 9 | +using Angor.Shared.Protocol; |
| 10 | +using Angor.Shared.Services; |
| 11 | +using Blockcore.NBitcoin; |
| 12 | +using Blockcore.NBitcoin.DataEncoders; |
| 13 | +using CSharpFunctionalExtensions; |
| 14 | +using MediatR; |
| 15 | +using Angor.Sdk.Funding.Projects; |
| 16 | + |
| 17 | +namespace Angor.Sdk.Funding.Investor.Operations; |
| 18 | + |
| 19 | +/// <summary> |
| 20 | +/// Builds a transaction to release funds from penalty timelock. |
| 21 | +/// This is the second step after recovery — once the penalty period has expired, |
| 22 | +/// the investor can spend the penalty-locked outputs back to themselves. |
| 23 | +/// Uses <see cref="IInvestorTransactionActions.BuildAndSignRecoverReleaseFundsTransaction"/>. |
| 24 | +/// </summary> |
| 25 | +public static class BuildPenaltyReleaseTransaction |
| 26 | +{ |
| 27 | + public record BuildPenaltyReleaseTransactionRequest(WalletId WalletId, ProjectId ProjectId, DomainFeerate SelectedFeeRate) : IRequest<Result<BuildPenaltyReleaseTransactionResponse>>; |
| 28 | + |
| 29 | + public record BuildPenaltyReleaseTransactionResponse(ReleaseTransactionDraft TransactionDraft); |
| 30 | + |
| 31 | + public class BuildPenaltyReleaseTransactionHandler( |
| 32 | + ISeedwordsProvider provider, |
| 33 | + IDerivationOperations derivationOperations, |
| 34 | + IProjectService projectService, |
| 35 | + IPortfolioService investmentService, |
| 36 | + INetworkConfiguration networkConfiguration, |
| 37 | + IInvestorTransactionActions investorTransactionActions, |
| 38 | + ITransactionService transactionService, |
| 39 | + IWalletAccountBalanceService walletAccountBalanceService |
| 40 | + ) : IRequestHandler<BuildPenaltyReleaseTransactionRequest, Result<BuildPenaltyReleaseTransactionResponse>> |
| 41 | + { |
| 42 | + public async Task<Result<BuildPenaltyReleaseTransactionResponse>> Handle(BuildPenaltyReleaseTransactionRequest request, CancellationToken cancellationToken) |
| 43 | + { |
| 44 | + var project = await projectService.GetAsync(request.ProjectId); |
| 45 | + if (project.IsFailure) |
| 46 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>(project.Error); |
| 47 | + |
| 48 | + var investments = await investmentService.GetByWalletId(request.WalletId.Value); |
| 49 | + if (investments.IsFailure) |
| 50 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>(investments.Error); |
| 51 | + |
| 52 | + var investment = investments.Value.ProjectIdentifiers |
| 53 | + .FirstOrDefault(p => p.ProjectIdentifier == request.ProjectId.Value); |
| 54 | + if (investment is null) |
| 55 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>("No investment found for this project"); |
| 56 | + |
| 57 | + if (string.IsNullOrEmpty(investment.RecoveryTransactionId)) |
| 58 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>("No recovery transaction found — recovery must be done before releasing from penalty"); |
| 59 | + |
| 60 | + if (!string.IsNullOrEmpty(investment.RecoveryReleaseTransactionId)) |
| 61 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>("Penalty release transaction has already been published"); |
| 62 | + |
| 63 | + var words = await provider.GetSensitiveData(request.WalletId.Value); |
| 64 | + if (words.IsFailure) |
| 65 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>(words.Error); |
| 66 | + |
| 67 | + // Get account info from database |
| 68 | + var accountBalanceResult = await walletAccountBalanceService.GetAccountBalanceInfoAsync(request.WalletId); |
| 69 | + if (accountBalanceResult.IsFailure) |
| 70 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>(accountBalanceResult.Error); |
| 71 | + |
| 72 | + var accountInfo = accountBalanceResult.Value.AccountInfo; |
| 73 | + |
| 74 | + var changeAddress = accountInfo.GetNextChangeReceiveAddress(); |
| 75 | + if (changeAddress == null) |
| 76 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>("Could not get a change address"); |
| 77 | + |
| 78 | + var investorPrivateKey = derivationOperations.DeriveInvestorPrivateKey(words.Value.ToWalletWords(), project.Value.FounderKey); |
| 79 | + |
| 80 | + // Get the investment transaction |
| 81 | + var investmentTrxHex = investment.InvestmentTransactionHex; |
| 82 | + if (string.IsNullOrEmpty(investmentTrxHex)) |
| 83 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>("Investment transaction hex not found"); |
| 84 | + |
| 85 | + var investmentTransaction = networkConfiguration.GetNetwork().CreateTransaction(investmentTrxHex); |
| 86 | + |
| 87 | + // Get the recovery transaction from the indexer |
| 88 | + var recoveryTrxHex = await transactionService.GetTransactionHexByIdAsync(investment.RecoveryTransactionId); |
| 89 | + if (string.IsNullOrEmpty(recoveryTrxHex)) |
| 90 | + return Result.Failure<BuildPenaltyReleaseTransactionResponse>("Recovery transaction not found on the network"); |
| 91 | + |
| 92 | + var recoveryTransaction = networkConfiguration.GetNetwork().CreateTransaction(recoveryTrxHex); |
| 93 | + |
| 94 | + // Build and sign the penalty release transaction |
| 95 | + var feeEstimation = new FeeEstimation { FeeRate = request.SelectedFeeRate.SatsPerKilobyte }; |
| 96 | + |
| 97 | + var releaseTransactionInfo = investorTransactionActions.BuildAndSignRecoverReleaseFundsTransaction( |
| 98 | + project.Value.ToProjectInfo(), |
| 99 | + investmentTransaction, |
| 100 | + recoveryTransaction, |
| 101 | + changeAddress, |
| 102 | + feeEstimation, |
| 103 | + Encoders.Hex.EncodeData(investorPrivateKey.ToBytes())); |
| 104 | + |
| 105 | + return Result.Success(new BuildPenaltyReleaseTransactionResponse(new ReleaseTransactionDraft |
| 106 | + { |
| 107 | + SignedTxHex = releaseTransactionInfo.Transaction.ToHex(), |
| 108 | + TransactionFee = new Amount(releaseTransactionInfo.TransactionFee), |
| 109 | + TransactionId = releaseTransactionInfo.Transaction.GetHash().ToString() |
| 110 | + })); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments