| | | 1 | | using System.Linq.Expressions; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Infrastructure.Repositories.Database.Bitcoin; |
| | | 5 | | |
| | | 6 | | using Domain.Bitcoin.Interfaces; |
| | | 7 | | using Domain.Bitcoin.ValueObjects; |
| | | 8 | | using Domain.Bitcoin.Wallet.Models; |
| | | 9 | | using Domain.Money; |
| | | 10 | | using Persistence.Contexts; |
| | | 11 | | using Persistence.Entities.Bitcoin; |
| | | 12 | | |
| | | 13 | | public class UtxoDbRepository(NLightningDbContext context) |
| | 0 | 14 | | : BaseDbRepository<UtxoEntity>(context), IUtxoDbRepository |
| | | 15 | | { |
| | | 16 | | public void Add(UtxoModel utxoModel) |
| | 0 | 17 | | { |
| | 0 | 18 | | var utxoEntity = MapDomainToEntity(utxoModel); |
| | 0 | 19 | | Insert(utxoEntity); |
| | 0 | 20 | | } |
| | | 21 | | |
| | | 22 | | public void Spend(UtxoModel utxoModel) |
| | 0 | 23 | | { |
| | 0 | 24 | | var utxoEntity = MapDomainToEntity(utxoModel); |
| | 0 | 25 | | Delete(utxoEntity); |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | public void Update(UtxoModel utxoModel) |
| | 0 | 29 | | { |
| | 0 | 30 | | var utxoEntity = MapDomainToEntity(utxoModel); |
| | 0 | 31 | | Update(utxoEntity); |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | public async Task<IEnumerable<UtxoModel>> GetUnspentAsync(bool includeWalletAddress = false) |
| | 0 | 35 | | { |
| | 0 | 36 | | var query = Get(asNoTracking: true).AsQueryable(); |
| | 0 | 37 | | if (includeWalletAddress) |
| | 0 | 38 | | query = query.Include(x => x.WalletAddress); |
| | | 39 | | |
| | 0 | 40 | | var utxoSet = await query.ToListAsync(); |
| | | 41 | | |
| | 0 | 42 | | return utxoSet.Select(MapEntityToModel); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | public async Task<UtxoModel?> GetByIdAsync(TxId txId, uint index, bool includeWalletAddress = false) |
| | 0 | 46 | | { |
| | 0 | 47 | | Expression<Func<UtxoEntity, object>>? include = includeWalletAddress |
| | 0 | 48 | | ? entity => entity.WalletAddress! |
| | 0 | 49 | | : null; |
| | 0 | 50 | | var utxoEntity = await GetByIdAsync(new { txId, index }, true, include); |
| | 0 | 51 | | return utxoEntity is null |
| | 0 | 52 | | ? null |
| | 0 | 53 | | : MapEntityToModel(utxoEntity); |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | private UtxoEntity MapDomainToEntity(UtxoModel model) |
| | 0 | 57 | | { |
| | 0 | 58 | | return new UtxoEntity |
| | 0 | 59 | | { |
| | 0 | 60 | | TransactionId = model.TxId, |
| | 0 | 61 | | Index = model.Index, |
| | 0 | 62 | | AmountSats = model.Amount.Satoshi, |
| | 0 | 63 | | BlockHeight = model.BlockHeight, |
| | 0 | 64 | | AddressIndex = model.AddressIndex, |
| | 0 | 65 | | IsAddressChange = model.IsAddressChange, |
| | 0 | 66 | | AddressType = model.AddressType |
| | 0 | 67 | | }; |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | private UtxoModel MapEntityToModel(UtxoEntity entity) |
| | 0 | 71 | | { |
| | 0 | 72 | | var utxoModel = new UtxoModel(entity.TransactionId, entity.Index, LightningMoney.Satoshis(entity.AmountSats), |
| | 0 | 73 | | entity.BlockHeight, entity.AddressIndex, entity.IsAddressChange, |
| | 0 | 74 | | entity.AddressType); |
| | | 75 | | |
| | 0 | 76 | | if (entity.WalletAddress is null) |
| | 0 | 77 | | return utxoModel; |
| | | 78 | | |
| | 0 | 79 | | var walletAddressModel = WalletAddressesDbRepository.MapEntityToModel(entity.WalletAddress); |
| | 0 | 80 | | utxoModel.SetWalletAddress(walletAddressModel); |
| | | 81 | | |
| | 0 | 82 | | return utxoModel; |
| | 0 | 83 | | } |
| | | 84 | | } |