-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move btc tx summary and runes summary parse logic into cont…
…ext (#417) * refactor: move btc tx summary and runes summary parse logic into context refactor: move btc tx summary and runes summary parse logic into context refactor: move btc tx summary and runes summary parse logic into context and split the transferSection with youWillSendSection refactor: move btc tx summary and runes summary parse logic into context refactor: move btc tx summary and runes summary parse logic into context and split the transferSection with youWillSendSection * chore: do TODO for a core type * apply suggestions * use newest core * unused import * refactor naming * prepare todos * commit work * revert * rebase new work * fix some bugs * fix linting * Update src/app/screens/sendRune/index.tsx Co-authored-by: Tim Man <[email protected]> * review * rename * touchup + add next todo * update todo * remove unnecessary data imo * Fix playwright-report upload for PR workflow * touchup --------- Co-authored-by: Terence Ng <[email protected]> Co-authored-by: Christine Pinto <[email protected]>
- Loading branch information
1 parent
dd0592a
commit 0ecb8ad
Showing
26 changed files
with
819 additions
and
562 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
src/app/components/confirmBtcTransaction/hooks/useParsedTxSummaryContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { | ||
getInputsWithAssetsFromUserAddress, | ||
getNetAmount, | ||
getOutputsWithAssetsFromUserAddress, | ||
getOutputsWithAssetsToUserAddress, | ||
isScriptOutput, | ||
} from '@components/confirmBtcTransaction/utils'; | ||
import useSelectedAccount from '@hooks/useSelectedAccount'; | ||
import useWalletSelector from '@hooks/useWalletSelector'; | ||
import type { TransactionSummary } from '@screens/sendBtc/helpers'; | ||
import { | ||
type btcTransaction, | ||
type RuneSummary, | ||
type RuneSummaryActions, | ||
} from '@secretkeylabs/xverse-core'; | ||
import { createContext, useContext } from 'react'; | ||
|
||
export type ParsedTxSummaryContextProps = { | ||
summary?: TransactionSummary | btcTransaction.PsbtSummary; | ||
runeSummary?: RuneSummary | RuneSummaryActions; | ||
}; | ||
|
||
export const ParsedTxSummaryContext = createContext<ParsedTxSummaryContextProps>({ | ||
summary: undefined, | ||
runeSummary: undefined, | ||
}); | ||
|
||
export const useParsedTxSummaryContext = (): { | ||
summary: | ||
| (TransactionSummary & { dustFiltered?: boolean }) | ||
| btcTransaction.PsbtSummary | ||
| undefined; | ||
runeSummary: RuneSummary | RuneSummaryActions | undefined; | ||
hasExternalInputs: boolean; | ||
isUnconfirmedInput: boolean; | ||
showCenotaphCallout: boolean; | ||
netBtcAmount: number; | ||
hasInsufficientRunes: boolean; | ||
hasSigHashSingle: boolean; | ||
transactionIsFinal: boolean; | ||
hasOutputScript: boolean; | ||
hasSigHashNone: boolean; | ||
validMintingRune: undefined | boolean; | ||
sendSection: { | ||
showSendSection: boolean; | ||
showBtcAmount: boolean; | ||
showRuneTransfers: boolean; | ||
hasInscriptionsRareSatsInOrdinal: boolean; | ||
outputsFromOrdinal: ( | ||
| btcTransaction.TransactionOutput | ||
| btcTransaction.TransactionPubKeyOutput | ||
)[]; | ||
inputFromOrdinal: btcTransaction.EnhancedInput[]; | ||
inscriptionsFromPayment: btcTransaction.IOInscription[]; | ||
satributesFromPayment: btcTransaction.IOSatribute[]; | ||
}; | ||
receiveSection: { | ||
showBtcAmount: boolean; | ||
showOrdinalSection: boolean; | ||
showPaymentSection: boolean; | ||
outputsToOrdinal: btcTransaction.TransactionOutput[]; | ||
showPaymentRunes: boolean; | ||
ordinalRuneReceipts: RuneSummary['receipts']; | ||
inscriptionsRareSatsInPayment: btcTransaction.TransactionOutput[]; | ||
outputsToPayment: btcTransaction.TransactionOutput[]; | ||
showOrdinalRunes: boolean; | ||
paymentRuneReceipts: RuneSummary['receipts']; | ||
}; | ||
} => { | ||
const { summary, runeSummary } = useContext(ParsedTxSummaryContext); | ||
const { btcAddress, ordinalsAddress } = useSelectedAccount(); | ||
const { hasActivatedRareSatsKey } = useWalletSelector(); | ||
|
||
const showCenotaphCallout = !!summary?.runeOp?.Cenotaph?.flaws; | ||
const hasInsufficientRunes = | ||
runeSummary?.transfers?.some((transfer) => !transfer.hasSufficientBalance) ?? false; | ||
const validMintingRune = | ||
!runeSummary?.mint || | ||
(runeSummary?.mint && runeSummary.mint.runeIsOpen && runeSummary.mint.runeIsMintable); | ||
const hasOutputScript = summary?.outputs.some((output) => isScriptOutput(output)) ?? false; | ||
const hasExternalInputs = | ||
summary?.inputs.some( | ||
(input) => | ||
input.extendedUtxo.address !== btcAddress && input.extendedUtxo.address !== ordinalsAddress, | ||
) ?? false; | ||
const isUnconfirmedInput = | ||
summary?.inputs.some( | ||
(input) => !input.extendedUtxo.utxo.status.confirmed && input.walletWillSign, | ||
) ?? false; | ||
|
||
const netBtcAmount = getNetAmount({ | ||
inputs: summary?.inputs, | ||
outputs: summary?.outputs, | ||
btcAddress, | ||
ordinalsAddress, | ||
}); | ||
|
||
// defaults for non-psbt transactions | ||
const transactionIsFinal = (summary as btcTransaction.PsbtSummary)?.isFinal ?? true; | ||
const hasSigHashNone = (summary as btcTransaction.PsbtSummary)?.hasSigHashNone ?? false; | ||
const hasSigHashSingle = (summary as btcTransaction.PsbtSummary)?.hasSigHashSingle ?? false; | ||
|
||
/* Send/Transfer section */ | ||
|
||
const { inputFromPayment, inputFromOrdinal } = getInputsWithAssetsFromUserAddress({ | ||
inputs: summary?.inputs, | ||
btcAddress, | ||
ordinalsAddress, | ||
}); | ||
|
||
const { outputsFromPayment, outputsFromOrdinal } = getOutputsWithAssetsFromUserAddress({ | ||
outputs: summary?.outputs, | ||
btcAddress, | ||
ordinalsAddress, | ||
}); | ||
|
||
// TODO move to utils | ||
const inscriptionsFromPayment: btcTransaction.IOInscription[] = []; | ||
const satributesFromPayment: btcTransaction.IOSatribute[] = []; | ||
(transactionIsFinal ? outputsFromPayment : inputFromPayment).forEach( | ||
( | ||
item: | ||
| btcTransaction.EnhancedInput | ||
| btcTransaction.TransactionOutput | ||
| btcTransaction.TransactionPubKeyOutput, | ||
) => { | ||
inscriptionsFromPayment.push(...item.inscriptions); | ||
satributesFromPayment.push(...item.satributes); | ||
}, | ||
); | ||
|
||
const showSendBtcAmount = netBtcAmount < 0; | ||
|
||
// if transaction is not final, then runes will be delegated and will show up in the delegation section | ||
const showRuneTransfers = transactionIsFinal && (runeSummary?.transfers ?? []).length > 0; | ||
|
||
const hasInscriptionsRareSatsInOrdinal = | ||
(!transactionIsFinal && inputFromOrdinal.length > 0) || outputsFromOrdinal.length > 0; | ||
|
||
/* Receive section */ | ||
|
||
const showReceiveBtcAmount = netBtcAmount > 0; | ||
const { outputsToPayment, outputsToOrdinal } = getOutputsWithAssetsToUserAddress({ | ||
outputs: summary?.outputs, | ||
btcAddress, | ||
ordinalsAddress, | ||
}); | ||
|
||
// if receiving runes from own addresses, hide it because it is change, unless it swap addresses (recover runes) | ||
const filteredRuneReceipts: RuneSummary['receipts'] = | ||
runeSummary?.receipts?.filter( | ||
(receipt) => | ||
!receipt.sourceAddresses.some( | ||
(address) => | ||
(address === ordinalsAddress && receipt.destinationAddress === ordinalsAddress) || | ||
(address === btcAddress && receipt.destinationAddress === btcAddress), | ||
), | ||
) ?? []; | ||
const ordinalRuneReceipts = filteredRuneReceipts.filter( | ||
(receipt) => receipt.destinationAddress === ordinalsAddress, | ||
); | ||
const paymentRuneReceipts = filteredRuneReceipts.filter( | ||
(receipt) => receipt.destinationAddress === btcAddress, | ||
); | ||
|
||
const inscriptionsRareSatsInPayment = outputsToPayment.filter( | ||
(output) => | ||
output.inscriptions.length > 0 || (hasActivatedRareSatsKey && output.satributes.length > 0), | ||
); | ||
|
||
// if transaction is not final, then runes will be delegated and will show up in the delegation section | ||
const showOrdinalRunes = !!(transactionIsFinal && ordinalRuneReceipts.length); | ||
|
||
// if transaction is not final, then runes will be delegated and will show up in the delegation section | ||
const showPaymentRunes = !!(transactionIsFinal && paymentRuneReceipts.length); | ||
|
||
return { | ||
summary, | ||
runeSummary, | ||
hasExternalInputs, | ||
hasInsufficientRunes, | ||
hasOutputScript, | ||
hasSigHashNone, | ||
hasSigHashSingle, | ||
isUnconfirmedInput, | ||
netBtcAmount, | ||
showCenotaphCallout, | ||
transactionIsFinal, | ||
validMintingRune, | ||
sendSection: { | ||
showSendSection: showSendBtcAmount || showRuneTransfers || hasInscriptionsRareSatsInOrdinal, | ||
showBtcAmount: showSendBtcAmount, | ||
showRuneTransfers, | ||
hasInscriptionsRareSatsInOrdinal, | ||
outputsFromOrdinal, | ||
inputFromOrdinal, | ||
inscriptionsFromPayment, | ||
satributesFromPayment, | ||
}, | ||
receiveSection: { | ||
showOrdinalSection: showOrdinalRunes || outputsToOrdinal.length > 0, | ||
showPaymentSection: | ||
showReceiveBtcAmount || showPaymentRunes || inscriptionsRareSatsInPayment.length > 0, | ||
showBtcAmount: showReceiveBtcAmount, | ||
inscriptionsRareSatsInPayment, | ||
ordinalRuneReceipts, | ||
outputsToOrdinal, | ||
outputsToPayment, | ||
paymentRuneReceipts, | ||
showOrdinalRunes, | ||
showPaymentRunes, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.