Skip to content

Commit

Permalink
fix: moved lighting action method to lightning store
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasonvdb committed Jul 27, 2023
1 parent 141fd9b commit 230ef2d
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 67 deletions.
68 changes: 67 additions & 1 deletion src/store/actions/lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ import {
addPeers,
createPaymentRequest,
getClaimableBalance,
getClaimedLightningPayments,
getCustomLightningPeers,
getLightningChannels,
getNodeIdFromStorage,
getNodeVersion,
getPendingInvoice,
getSentLightningPayments,
hasOpenLightningChannels,
parseUri,
} from '../../utils/lightning';
Expand All @@ -21,7 +24,9 @@ import {
TCreateLightningInvoice,
TLightningNodeVersion,
} from '../types/lightning';
import { TWalletName } from '../types/wallet';
import { EPaymentType, TWalletName } from '../types/wallet';
import { EActivityType, TLightningActivityItem } from '../types/activity';
import { getActivityItemById } from '../../utils/activity';

const dispatch = getDispatch();

Expand Down Expand Up @@ -347,3 +352,64 @@ export const updateClaimableBalance = async ({
});
return ok('Successfully Updated Claimable Balance.');
};

export const syncLightningTxsWithActivityList = async (): Promise<
Result<string>
> => {
let items: TLightningActivityItem[] = [];

const claimedTxs = await getClaimedLightningPayments();
for (const tx of claimedTxs) {
//Required to add in bolt11 and description
const invoice = await getPendingInvoice(tx.payment_hash);

items.push({
id: tx.payment_hash,
activityType: EActivityType.lightning,
txType: EPaymentType.received,
message: invoice?.description ?? '',
address: invoice?.to_str ?? '',
confirmed: tx.state === 'successful',
value: tx.amount_sat,
timestamp: tx.unix_timestamp * 1000,
});
}

const sentTxs = await getSentLightningPayments();
for (const tx of sentTxs) {
const sats = tx.amount_sat;
if (!sats) {
continue;
}

items.push({
id: tx.payment_hash,
activityType: EActivityType.lightning,
txType: EPaymentType.sent,
message: '',
address: '',
confirmed: tx.state === 'successful',
value: -sats,
timestamp: tx.unix_timestamp * 1000,
});
}

//TODO remove temp hack when this is complete and descriptions/bolt11 can be added from stored tx: https://github.com/synonymdev/react-native-ldk/issues/156
items.forEach((item) => {
const res = getActivityItemById(item.id);
if (res.isOk()) {
const existingItem = res.value;
if (existingItem.activityType === EActivityType.lightning) {
item.message = existingItem.message;
item.address = existingItem.address;
}
}
});

dispatch({
type: actions.UPDATE_ACTIVITY_ITEMS,
payload: items,
});

return ok('Stored lightning transactions synced with activity list.');
};
71 changes: 5 additions & 66 deletions src/utils/lightning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ import {
import { TAvailableNetworks } from '../networks';
import {
getBlocktankStore,
getDispatch,
getFeesStore,
getLightningStore,
getWalletStore,
} from '../../store/helpers';
import { defaultHeader } from '../../store/shapes/wallet';
import {
removePeer,
syncLightningTxsWithActivityList,
updateClaimableBalance,
updateLightningChannels,
updateLightningNodeId,
Expand All @@ -73,10 +73,6 @@ import { TLightningNodeVersion } from '../../store/types/lightning';
import { getBlocktankInfo, isGeoBlocked } from '../blocktank';
import { updateOnchainFeeEstimates } from '../../store/actions/fees';
import { addTodo, removeTodo } from '../../store/actions/todos';
import actions from '../../store/actions/actions';
import { getActivityItemById } from '../activity';

const dispatch = getDispatch();

let LDKIsStayingSynced = false;

Expand Down Expand Up @@ -1197,6 +1193,10 @@ export const getClaimedLightningPayments = async (): Promise<
TChannelManagerClaim[]
> => lm.getLdkPaymentsClaimed();

export const getSentLightningPayments = async (): Promise<
TChannelManagerPaymentSent[]
> => lm.getLdkPaymentsSent();

export const decodeLightningInvoice = ({
paymentRequest,
}: TPaymentReq): Promise<Result<TInvoice>> => {
Expand Down Expand Up @@ -1494,64 +1494,3 @@ export const getLightningBalance = ({

return { localBalance, remoteBalance };
};

export const syncLightningTxsWithActivityList = async (): Promise<
Result<string>
> => {
let items: TLightningActivityItem[] = [];

const claimedTxs = await lm.getLdkPaymentsClaimed();
for (const tx of claimedTxs) {
//Required to add in bolt11 and description
const invoice = await getPendingInvoice(tx.payment_hash);

items.push({
id: tx.payment_hash,
activityType: EActivityType.lightning,
txType: EPaymentType.received,
message: invoice?.description ?? '',
address: invoice?.to_str ?? '',
confirmed: tx.state === 'successful',
value: tx.amount_sat,
timestamp: tx.unix_timestamp * 1000,
});
}

const sentTxs = await lm.getLdkPaymentsSent();
for (const tx of sentTxs) {
const sats = tx.amount_sat;
if (!sats) {
continue;
}

items.push({
id: tx.payment_hash,
activityType: EActivityType.lightning,
txType: EPaymentType.sent,
message: '',
address: '',
confirmed: tx.state === 'successful',
value: -sats,
timestamp: tx.unix_timestamp * 1000,
});
}

//TODO remove temp hack when this is complete and descriptions/bolt11 can be added from stored tx: https://github.com/synonymdev/react-native-ldk/issues/156
items.forEach((item) => {
const res = getActivityItemById(item.id);
if (res.isOk()) {
const existingItem = res.value;
if (existingItem.activityType === EActivityType.lightning) {
item.message = existingItem.message;
item.address = existingItem.address;
}
}
});

dispatch({
type: actions.UPDATE_ACTIVITY_ITEMS,
payload: items,
});

return ok('Stored lightning transactions synced with activity list.');
};

0 comments on commit 230ef2d

Please sign in to comment.