|
| 1 | +import assert from 'node:assert'; |
| 2 | +import cloneDeep from 'lodash/cloneDeep'; |
| 3 | +import { TChannel } from '@synonymdev/react-native-ldk'; |
| 4 | + |
| 5 | +import '../src/utils/i18n'; |
| 6 | +import store from '../src/store'; |
| 7 | +import Store from '../src/store/types'; |
| 8 | +import { createNewWallet } from '../src/utils/startup'; |
| 9 | +import { updateWallet } from '../src/store/actions/wallet'; |
| 10 | +import { |
| 11 | + balanceSelector, |
| 12 | + lnSetupSelector, |
| 13 | +} from '../src/store/reselect/aggregations'; |
| 14 | + |
| 15 | +describe('Reselect', () => { |
| 16 | + let s: Store; |
| 17 | + |
| 18 | + beforeAll(async () => { |
| 19 | + require('../nodejs-assets/nodejs-project/main.js'); |
| 20 | + let res = await createNewWallet(); |
| 21 | + if (res.isErr()) { |
| 22 | + throw res.error; |
| 23 | + } |
| 24 | + updateWallet({ selectedNetwork: 'bitcoinRegtest' }); |
| 25 | + s = store.getState(); |
| 26 | + }); |
| 27 | + |
| 28 | + describe('balanceSelector', () => { |
| 29 | + it('should return zeros by default', () => { |
| 30 | + const state = cloneDeep(s); |
| 31 | + assert.deepEqual(balanceSelector(state), { |
| 32 | + lightningBalance: 0, |
| 33 | + lightningClaimableBalance: 0, |
| 34 | + lightningReserveBalance: 0, |
| 35 | + lightningSpendingBalance: 0, |
| 36 | + onchainBalance: 0, |
| 37 | + totalBalance: 0, |
| 38 | + totalSpendableBalance: 0, |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should return onchain balance for current wallet', () => { |
| 43 | + const state = cloneDeep(s); |
| 44 | + state.wallet.wallets.wallet0.balance.bitcoinRegtest = 1; |
| 45 | + assert.deepEqual(balanceSelector(state), { |
| 46 | + lightningBalance: 0, |
| 47 | + lightningClaimableBalance: 0, |
| 48 | + lightningReserveBalance: 0, |
| 49 | + lightningSpendingBalance: 0, |
| 50 | + onchainBalance: 1, |
| 51 | + totalBalance: 1, |
| 52 | + totalSpendableBalance: 1, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should calculate balance for LN wallet', () => { |
| 57 | + const state = cloneDeep(s); |
| 58 | + state.wallet.wallets.wallet0.balance.bitcoinRegtest = 1; |
| 59 | + |
| 60 | + const channel1 = { |
| 61 | + channel_id: 'channel1', |
| 62 | + is_channel_ready: true, |
| 63 | + outbound_capacity_sat: 1, |
| 64 | + balance_sat: 2, |
| 65 | + } as TChannel; |
| 66 | + const channel2 = { |
| 67 | + channel_id: 'channel2', |
| 68 | + is_channel_ready: true, |
| 69 | + outbound_capacity_sat: 1, |
| 70 | + balance_sat: 2, |
| 71 | + } as TChannel; |
| 72 | + const channel3 = { |
| 73 | + channel_id: 'channel3', |
| 74 | + is_channel_ready: false, |
| 75 | + outbound_capacity_sat: 1, |
| 76 | + balance_sat: 2, |
| 77 | + } as TChannel; |
| 78 | + |
| 79 | + const lnWallet = state.lightning.nodes.wallet0; |
| 80 | + lnWallet.channels.bitcoinRegtest = { channel1, channel2, channel3 }; |
| 81 | + lnWallet.openChannelIds.bitcoinRegtest = [ |
| 82 | + 'channel1', |
| 83 | + 'channel2', |
| 84 | + 'channel3', |
| 85 | + ]; |
| 86 | + lnWallet.claimableBalance.bitcoinRegtest = 3; |
| 87 | + |
| 88 | + assert.deepEqual(balanceSelector(state), { |
| 89 | + lightningBalance: 7, |
| 90 | + lightningClaimableBalance: 3, |
| 91 | + lightningReserveBalance: 2, |
| 92 | + lightningSpendingBalance: 2, |
| 93 | + onchainBalance: 1, |
| 94 | + totalBalance: 8, |
| 95 | + totalSpendableBalance: 3, |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('lnSetupSelector', () => { |
| 101 | + it('should throw if onchain balance is 0', () => { |
| 102 | + const state = cloneDeep(s); |
| 103 | + expect(() => lnSetupSelector(state, 0)).toThrow(TypeError); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should calculate percentage corectly', () => { |
| 107 | + const state = cloneDeep(s); |
| 108 | + // balance under maxChannelSizeSat |
| 109 | + state.wallet.wallets.wallet0.balance.bitcoinRegtest = 20000; |
| 110 | + |
| 111 | + expect(lnSetupSelector(state, 0)).toMatchObject({ |
| 112 | + percentage: { |
| 113 | + savings: 100, |
| 114 | + spendings: 0, |
| 115 | + }, |
| 116 | + canContinue: false, |
| 117 | + }); |
| 118 | + |
| 119 | + expect(lnSetupSelector(state, 10000)).toMatchObject({ |
| 120 | + percentage: { |
| 121 | + savings: 50, |
| 122 | + spendings: 50, |
| 123 | + }, |
| 124 | + canContinue: true, |
| 125 | + }); |
| 126 | + |
| 127 | + expect(lnSetupSelector(state, 16000)).toMatchObject({ |
| 128 | + percentage: { |
| 129 | + savings: 20, |
| 130 | + spendings: 80, |
| 131 | + }, |
| 132 | + canContinue: true, |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should calculate btSpendingLimitBalanced without LN channels', () => { |
| 137 | + // max value is limited by btMaxChannelSizeSat / 2 - btMaxChannelSizeSat * DIFF |
| 138 | + const s1 = cloneDeep(s); |
| 139 | + s1.wallet.wallets.wallet0.balance.bitcoinRegtest = 1000; |
| 140 | + s1.blocktank.info.options = { |
| 141 | + ...s1.blocktank.info.options, |
| 142 | + minChannelSizeSat: 10, |
| 143 | + maxChannelSizeSat: 200, |
| 144 | + maxClientBalanceSat: 100, |
| 145 | + }; |
| 146 | + |
| 147 | + expect(lnSetupSelector(s1, 0)).toMatchObject({ |
| 148 | + slider: { |
| 149 | + startValue: 0, |
| 150 | + maxValue: 98, |
| 151 | + endValue: 1000, |
| 152 | + }, |
| 153 | + btSpendingLimitBalanced: 98, |
| 154 | + spendableBalance: 800, |
| 155 | + defaultClientBalance: 98, |
| 156 | + }); |
| 157 | + |
| 158 | + // max value is limited by btMaxClientBalanceSat |
| 159 | + const s2 = cloneDeep(s); |
| 160 | + s2.wallet.wallets.wallet0.balance.bitcoinRegtest = 1000; |
| 161 | + s2.blocktank.info.options = { |
| 162 | + ...s2.blocktank.info.options, |
| 163 | + minChannelSizeSat: 10, |
| 164 | + maxChannelSizeSat: 200, |
| 165 | + maxClientBalanceSat: 50, |
| 166 | + }; |
| 167 | + |
| 168 | + expect(lnSetupSelector(s2, 0)).toMatchObject({ |
| 169 | + slider: { |
| 170 | + startValue: 0, |
| 171 | + maxValue: 50, |
| 172 | + endValue: 1000, |
| 173 | + }, |
| 174 | + btSpendingLimitBalanced: 50, |
| 175 | + spendableBalance: 800, |
| 176 | + defaultClientBalance: 50, |
| 177 | + }); |
| 178 | + |
| 179 | + // max value is limited by onchain balance |
| 180 | + const s3 = cloneDeep(s); |
| 181 | + s3.wallet.wallets.wallet0.balance.bitcoinRegtest = 50; |
| 182 | + s3.blocktank.info.options = { |
| 183 | + ...s3.blocktank.info.options, |
| 184 | + minChannelSizeSat: 10, |
| 185 | + maxChannelSizeSat: 200, |
| 186 | + maxClientBalanceSat: 100, |
| 187 | + }; |
| 188 | + |
| 189 | + expect(lnSetupSelector(s3, 0)).toMatchObject({ |
| 190 | + slider: { |
| 191 | + startValue: 0, |
| 192 | + maxValue: 40, |
| 193 | + endValue: 50, |
| 194 | + }, |
| 195 | + btSpendingLimitBalanced: 98, |
| 196 | + spendableBalance: 40, |
| 197 | + defaultClientBalance: 10, |
| 198 | + }); |
| 199 | + }); |
| 200 | + |
| 201 | + it('should calculate btSpendingLimitBalanced with LN channels', () => { |
| 202 | + // max value is limited by btMaxChannelSizeSat / 2 - btMaxChannelSizeSat * DIFF |
| 203 | + const s1 = cloneDeep(s); |
| 204 | + s1.wallet.wallets.wallet0.balance.bitcoinRegtest = 1000; |
| 205 | + s1.blocktank.info.options = { |
| 206 | + ...s1.blocktank.info.options, |
| 207 | + minChannelSizeSat: 10, |
| 208 | + maxChannelSizeSat: 200, |
| 209 | + maxClientBalanceSat: 100, |
| 210 | + }; |
| 211 | + const channel1 = { |
| 212 | + channel_id: 'channel1', |
| 213 | + is_channel_ready: true, |
| 214 | + outbound_capacity_sat: 1, |
| 215 | + balance_sat: 2, |
| 216 | + } as TChannel; |
| 217 | + const lnWallet = s1.lightning.nodes.wallet0; |
| 218 | + lnWallet.channels.bitcoinRegtest = { channel1 }; |
| 219 | + lnWallet.openChannelIds.bitcoinRegtest = ['channel1']; |
| 220 | + lnWallet.claimableBalance.bitcoinRegtest = 3; |
| 221 | + |
| 222 | + expect(lnSetupSelector(s1, 20)).toMatchObject({ |
| 223 | + slider: { |
| 224 | + startValue: 0, |
| 225 | + maxValue: 98, |
| 226 | + endValue: 1005, |
| 227 | + }, |
| 228 | + btSpendingLimitBalanced: 98, |
| 229 | + spendableBalance: 804, |
| 230 | + defaultClientBalance: 5, |
| 231 | + canContinue: true, |
| 232 | + }); |
| 233 | + }); |
| 234 | + }); |
| 235 | +}); |
0 commit comments