| | 1 | | using NLightning.Domain.Bitcoin.ValueObjects; |
| | 2 | | using NLightning.Domain.Channels.Interfaces; |
| | 3 | | using NLightning.Domain.Channels.ValueObjects; |
| | 4 | | using NLightning.Domain.Enums; |
| | 5 | | using NLightning.Domain.Money; |
| | 6 | | using NLightning.Infrastructure.Persistence.Contexts; |
| | 7 | | using NLightning.Infrastructure.Persistence.Entities.Channel; |
| | 8 | |
|
| | 9 | | namespace NLightning.Infrastructure.Repositories.Database.Channel; |
| | 10 | |
|
| | 11 | | public class ChannelConfigDbRepository(NLightningDbContext context) |
| 0 | 12 | | : BaseDbRepository<ChannelConfigEntity>(context), IChannelConfigDbRepository |
| | 13 | | { |
| | 14 | | public void Add(ChannelId channelId, ChannelConfig config) |
| 0 | 15 | | { |
| 0 | 16 | | var configEntity = MapDomainToEntity(channelId, config); |
| 0 | 17 | | Insert(configEntity); |
| 0 | 18 | | } |
| | 19 | |
|
| | 20 | | public void Update(ChannelId channelId, ChannelConfig config) |
| 0 | 21 | | { |
| 0 | 22 | | var configEntity = MapDomainToEntity(channelId, config); |
| 0 | 23 | | base.Update(configEntity); |
| 0 | 24 | | } |
| | 25 | |
|
| | 26 | | public Task DeleteAsync(ChannelId channelId) |
| 0 | 27 | | { |
| 0 | 28 | | return DeleteByIdAsync(channelId); |
| 0 | 29 | | } |
| | 30 | |
|
| | 31 | | public async Task<ChannelConfig?> GetByChannelIdAsync(ChannelId channelId) |
| 0 | 32 | | { |
| 0 | 33 | | var configEntity = await GetByIdAsync(channelId); |
| | 34 | |
|
| 0 | 35 | | return configEntity == null ? null : MapEntityToDomain(configEntity); |
| 0 | 36 | | } |
| | 37 | |
|
| | 38 | | internal static ChannelConfigEntity MapDomainToEntity(ChannelId channelId, ChannelConfig config) |
| 0 | 39 | | { |
| 0 | 40 | | return new ChannelConfigEntity |
| 0 | 41 | | { |
| 0 | 42 | | ChannelId = channelId, |
| 0 | 43 | | ChannelReserveAmountSats = config.ChannelReserveAmount?.Satoshi, |
| 0 | 44 | | FeeRatePerKwSatoshis = config.FeeRateAmountPerKw.Satoshi, |
| 0 | 45 | | HtlcMinimumMsat = config.HtlcMinimumAmount.MilliSatoshi, |
| 0 | 46 | | LocalDustLimitAmountSats = config.LocalDustLimitAmount.Satoshi, |
| 0 | 47 | | LocalUpfrontShutdownScript = config.LocalUpfrontShutdownScript, |
| 0 | 48 | | MaxAcceptedHtlcs = config.MaxAcceptedHtlcs, |
| 0 | 49 | | MaxHtlcAmountInFlight = config.MaxHtlcAmountInFlight.MilliSatoshi, |
| 0 | 50 | | MinimumDepth = config.MinimumDepth, |
| 0 | 51 | | OptionAnchorOutputs = config.OptionAnchorOutputs, |
| 0 | 52 | | RemoteDustLimitAmountSats = config.RemoteDustLimitAmount.Satoshi, |
| 0 | 53 | | RemoteUpfrontShutdownScript = config.RemoteShutdownScriptPubKey, |
| 0 | 54 | | ToSelfDelay = config.ToSelfDelay, |
| 0 | 55 | | UseScidAlias = (byte)config.UseScidAlias |
| 0 | 56 | | }; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | internal static ChannelConfig MapEntityToDomain(ChannelConfigEntity entity) |
| 0 | 60 | | { |
| 0 | 61 | | LightningMoney? channelReserveAmount = null; |
| 0 | 62 | | if (entity.ChannelReserveAmountSats.HasValue) |
| 0 | 63 | | channelReserveAmount = LightningMoney.Satoshis(entity.ChannelReserveAmountSats.Value); |
| | 64 | |
|
| 0 | 65 | | BitcoinScript? localUpfrontShutdownScript = null; |
| 0 | 66 | | if (entity.LocalUpfrontShutdownScript is not null) |
| 0 | 67 | | localUpfrontShutdownScript = entity.LocalUpfrontShutdownScript; |
| | 68 | |
|
| 0 | 69 | | BitcoinScript? remoteUpfrontShutdownScript = null; |
| 0 | 70 | | if (entity.RemoteUpfrontShutdownScript is not null) |
| 0 | 71 | | remoteUpfrontShutdownScript = entity.RemoteUpfrontShutdownScript; |
| | 72 | |
|
| 0 | 73 | | return new ChannelConfig(channelReserveAmount, LightningMoney.Satoshis(entity.FeeRatePerKwSatoshis), |
| 0 | 74 | | LightningMoney.MilliSatoshis(entity.HtlcMinimumMsat), |
| 0 | 75 | | LightningMoney.Satoshis(entity.LocalDustLimitAmountSats), entity.MaxAcceptedHtlcs, |
| 0 | 76 | | LightningMoney.MilliSatoshis(entity.MaxHtlcAmountInFlight), entity.MinimumDepth, |
| 0 | 77 | | entity.OptionAnchorOutputs, LightningMoney.Satoshis(entity.RemoteDustLimitAmountSats), |
| 0 | 78 | | entity.ToSelfDelay, (FeatureSupport)entity.UseScidAlias, localUpfrontShutdownScript, |
| 0 | 79 | | remoteUpfrontShutdownScript |
| 0 | 80 | | ); |
| 0 | 81 | | } |
| | 82 | | } |