Skip to content

Commit

Permalink
Handle insufficient funds in assert txn when also burning hnt.
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewcarlreetz committed Sep 8, 2023
1 parent 4ff72c7 commit 2bc12c9
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 42 deletions.
13 changes: 7 additions & 6 deletions src/features/browser/BrowserWebViewScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const BrowserWebViewScreen = () => {
const decision = await walletSignBottomSheetRef.current?.show({
type,
url: currentUrl,
serializedTxs: undefined,
serializedTxs: [],
})

if (!decision) {
Expand All @@ -122,7 +122,7 @@ const BrowserWebViewScreen = () => {
const decision = await walletSignBottomSheetRef?.current?.show({
type,
url: currentUrl,
serializedTxs: undefined,
serializedTxs: [],
})

if (!decision) {
Expand Down Expand Up @@ -254,12 +254,13 @@ const BrowserWebViewScreen = () => {
),
)

const txBuffers: Buffer[] = inputs.map(
({ transaction }: SolanaSignAndSendTransactionInput) =>
new Uint8Array(
const txBuffers: { tx: Buffer }[] = inputs.map(
({ transaction }: SolanaSignAndSendTransactionInput) => ({
tx: new Uint8Array(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Object.keys(transaction).map((k) => (transaction as any)[k]),
),
}),
)

const decision = await walletSignBottomSheetRef.current?.show({
Expand Down Expand Up @@ -329,7 +330,7 @@ const BrowserWebViewScreen = () => {
const decision = await walletSignBottomSheetRef.current?.show({
type,
url: currentUrl,
serializedTxs: undefined,
serializedTxs: [],
})

if (!decision) {
Expand Down
21 changes: 14 additions & 7 deletions src/features/collectables/AssertLocationScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import useAlert from '@hooks/useAlert'
import { useForwardGeo } from '@hooks/useForwardGeo'
import { useReverseGeo } from '@hooks/useReverseGeo'
import useSubmitTxn from '@hooks/useSubmitTxn'
import { RouteProp, useRoute } from '@react-navigation/native'
import { RouteProp, useNavigation, useRoute } from '@react-navigation/native'
import MapboxGL from '@rnmapbox/maps'
import turfBbox from '@turf/bbox'
import { points } from '@turf/helpers'
Expand Down Expand Up @@ -82,6 +82,7 @@ const AssertLocationScreen = () => {
const reverseGeo = useReverseGeo(mapCenter)
const forwardGeo = useForwardGeo()
const { submitUpdateEntityInfo } = useSubmitTxn()
const nav = useNavigation()

const {
content: { metadata },
Expand Down Expand Up @@ -260,22 +261,28 @@ const AssertLocationScreen = () => {
decimalGain: gain,
})
setAsserting(false)

await showOKAlert({
title: t('assertLocationScreen.success.title'),
message: t('assertLocationScreen.success.message'),
})
nav.goBack()
} catch (error) {
setAsserting(false)
Logger.error(error)
setTransactionError((error as Error).message)
}
},
[
entityKey,
mapCenter,
elevation,
gain,
entityKey,
hideElevGain,
setAsserting,
setTransactionError,
submitUpdateEntityInfo,
// nav,
elevation,
gain,
showOKAlert,
t,
nav,
],
)

Expand Down
30 changes: 17 additions & 13 deletions src/hooks/useSubmitTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ export default () => {
type: WalletStandardMessageTypes.signTransaction,
url: '',
additionalMessage: t('transactions.signPaymentTxn'),
serializedTxs: txns.map((tx) =>
tx.serialize({
serializedTxs: txns.map((tx) => ({
tx: tx.serialize({
requireAllSignatures: false,
}),
),
})),
})

if (!decision) {
Expand Down Expand Up @@ -138,7 +138,7 @@ export default () => {
type: WalletStandardMessageTypes.signTransaction,
url: '',
additionalMessage: t('transactions.signTransferCollectableTxn'),
serializedTxs: [Buffer.from(serializedTx)],
serializedTxs: [{ tx: Buffer.from(serializedTx) }],
})

if (!decision) {
Expand Down Expand Up @@ -191,7 +191,7 @@ export default () => {
url: '',
warning: recipientExists ? '' : t('transactions.recipientNonExistent'),
additionalMessage: t('transactions.signSwapTxn'),
serializedTxs: [Buffer.from(serializedTx)],
serializedTxs: [{ tx: Buffer.from(serializedTx) }],
})

if (!decision) {
Expand Down Expand Up @@ -230,7 +230,7 @@ export default () => {
type: WalletStandardMessageTypes.signTransaction,
url: '',
additionalMessage: t('transactions.signGenericTxn'),
serializedTxs: [Buffer.from(serializedTx)],
serializedTxs: [{ tx: Buffer.from(serializedTx) }],
})

if (!decision) {
Expand Down Expand Up @@ -272,7 +272,7 @@ export default () => {
type: WalletStandardMessageTypes.signTransaction,
url: '',
additionalMessage: t('transactions.signClaimRewardsTxn'),
serializedTxs: serializedTxs.map(Buffer.from),
serializedTxs: serializedTxs.map((tx) => ({ tx: Buffer.from(tx) })),
})

if (!decision) {
Expand Down Expand Up @@ -361,7 +361,7 @@ export default () => {
url: '',
warning: recipientExists ? '' : t('transactions.recipientNonExistent'),
additionalMessage: t('transactions.signMintDataCreditsTxn'),
serializedTxs: [Buffer.from(serializedTx)],
serializedTxs: [{ tx: Buffer.from(serializedTx) }],
})

if (!decision) {
Expand Down Expand Up @@ -413,7 +413,7 @@ export default () => {
type: WalletStandardMessageTypes.signTransaction,
url: '',
additionalMessage: t('transactions.signDelegateDCTxn'),
serializedTxs: [Buffer.from(serializedTx)],
serializedTxs: [{ tx: Buffer.from(serializedTx) }],
})

if (!decision) {
Expand Down Expand Up @@ -472,9 +472,13 @@ export default () => {
throw new Error('Failed to get assert data')
}

const serializedTxs = assertData.solanaTransactions.map((txn) =>
Buffer.from(txn, 'base64'),
)
// If there's more than one txn, the first item is the burn txn. We only want to check for insufficient funds on it
const includesBurn = assertData.solanaTransactions.length > 1

const serializedTxs = assertData.solanaTransactions.map((txn, index) => ({
tx: Buffer.from(txn, 'base64'),
ignoreInsufficientFunds: includesBurn && index !== 0,
}))

const decision = await walletSignBottomSheetRef.show({
type: WalletStandardMessageTypes.signTransaction,
Expand All @@ -487,7 +491,7 @@ export default () => {
throw new Error('User rejected transaction')
}

const txns = serializedTxs.map((tx) => Transaction.from(tx))
const txns = serializedTxs.map((tx) => Transaction.from(tx.tx))

if (type === 'IOT') {
await dispatch(
Expand Down
4 changes: 4 additions & 0 deletions src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ export default {
elevationPlaceholder: 'Elevation (meters)',
locationNotFound: 'Location not found, Please try again.',
mobileTitle: 'MOBILE',
success: {
title: 'Location Asserted!',
message: 'Location has successfully been asserted.',
},
},
antennaSetupScreen: {
title: 'Antenna Setup',
Expand Down
26 changes: 13 additions & 13 deletions src/solana/WalletSignBottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ const WalletSignBottomSheet = forwardRef(
type: WalletStandardMessageTypes.connect,
url: '',
additionalMessage: '',
serializedTxs: undefined,
serializedTxs: [],
})

const itemsPerPage = 5
const [currentPage, setCurrentPage] = useState(1)
const [currentTxs, hasMore] = useMemo(() => {
const totalPages = Math.ceil(
(walletSignOpts?.serializedTxs?.length || 0) / itemsPerPage,
(walletSignOpts.serializedTxs.length || 0) / itemsPerPage,
)
const more = currentPage < totalPages
let scopedTxs
let scopedTxs: { tx: Buffer; ignoreInsufficientFunds?: boolean }[] = []

if (walletSignOpts.serializedTxs) {
if (walletSignOpts.serializedTxs.length) {
const endIndex = currentPage * itemsPerPage
scopedTxs = walletSignOpts.serializedTxs.slice(0, endIndex)
}
Expand All @@ -88,7 +88,7 @@ const WalletSignBottomSheet = forwardRef(
const estimatedTotalSolByLamports = useMemo(() => {
const { serializedTxs } = walletSignOpts

if (serializedTxs) {
if (serializedTxs.length) {
if (currentTxs && currentTxs.length < serializedTxs.length) {
// we have unsimulated transactions, do rough estimate
const diff = serializedTxs.length - currentTxs.length
Expand Down Expand Up @@ -255,7 +255,7 @@ const WalletSignBottomSheet = forwardRef(
color="secondaryText"
textAlign="center"
>
{walletSignOpts?.url || ''}
{walletSignOpts.url || ''}
</Text>
</Box>

Expand Down Expand Up @@ -359,9 +359,7 @@ const WalletSignBottomSheet = forwardRef(
<Box
flex={1}
maxHeight={
(walletSignOpts?.serializedTxs?.length || 0) > 1
? 214
: 180
(walletSignOpts.serializedTxs.length || 0) > 1 ? 214 : 180
}
paddingTop="m"
>
Expand All @@ -384,21 +382,23 @@ const WalletSignBottomSheet = forwardRef(
{isVisible && currentTxs && (
<ScrollView
scrollEnabled={
(walletSignOpts?.serializedTxs?.length || 0) > 1
(walletSignOpts.serializedTxs.length || 0) > 1
}
>
{currentTxs.map((tx, idx) => (
<WalletSignBottomSheetTransaction
// eslint-disable-next-line react/no-array-index-key
key={`transaction-${idx}`}
transaction={tx}
transaction={tx.tx}
transactionIdx={idx}
totalTransactions={
walletSignOpts?.serializedTxs?.length || 0
walletSignOpts.serializedTxs.length || 0
}
incrementTotalSolFee={incrementTotalSolFee}
setNestedInsufficentFunds={
setNestedInsufficentFunds
tx.ignoreInsufficientFunds
? undefined
: setNestedInsufficentFunds
}
/>
))}
Expand Down
4 changes: 2 additions & 2 deletions src/solana/WalletSignBottomSheetTransaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const WalletSignBottomSheetTransaction = ({
transactionIdx: number
totalTransactions: number
incrementTotalSolFee: (fee: number) => void
setNestedInsufficentFunds: React.Dispatch<React.SetStateAction<boolean>>
setNestedInsufficentFunds?: React.Dispatch<React.SetStateAction<boolean>>
}) => {
const { anchorProvider } = useSolana()
const { t } = useTranslation()
Expand All @@ -33,7 +33,7 @@ const WalletSignBottomSheetTransaction = ({

useEffect(() => {
if (insufficientFunds) {
setNestedInsufficentFunds(true)
setNestedInsufficentFunds?.(true)
}
}, [insufficientFunds, setNestedInsufficentFunds])

Expand Down
2 changes: 1 addition & 1 deletion src/solana/walletSignBottomSheetTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type BalanceChange = {
export type WalletSignOpts = {
type: WalletStandardMessageTypes
url: string
serializedTxs: Buffer[] | undefined
serializedTxs: { tx: Buffer; ignoreInsufficientFunds?: boolean }[]
warning?: string
additionalMessage?: string
}
Expand Down

0 comments on commit 2bc12c9

Please sign in to comment.