-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
996 additions
and
97 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
51 changes: 51 additions & 0 deletions
51
apps/mobile/src/common/transactions/stacks-transactions.hooks.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,51 @@ | ||
import { useCallback } from 'react'; | ||
import { FieldValues } from 'react-hook-form'; | ||
|
||
import { AccountId } from '@/models/domain.model'; | ||
import { useAccountByIndex } from '@/store/accounts/accounts.read'; | ||
import { useStacksSigners } from '@/store/keychains/stacks/stacks-keychains.read'; | ||
import { useNetworkPreferenceStacksNetwork } from '@/store/settings/settings.read'; | ||
import { bytesToHex } from '@noble/hashes/utils'; | ||
|
||
import { useNextNonce } from '@leather.io/query'; | ||
import { | ||
GenerateUnsignedTransactionOptions, | ||
TransactionTypes, | ||
generateUnsignedTransaction, | ||
} from '@leather.io/stacks'; | ||
import { stxToMicroStx } from '@leather.io/utils'; | ||
|
||
export function useGenerateStxTokenTransferUnsignedTx({ fingerprint, accountIndex }: AccountId) { | ||
const network = useNetworkPreferenceStacksNetwork(); | ||
const account = useAccountByIndex(fingerprint, accountIndex); | ||
const stxSigner = useStacksSigners().fromAccountIndex(fingerprint, accountIndex)[0]; | ||
const stxAddress = stxSigner?.address ?? ''; | ||
const { data: nextNonce } = useNextNonce(stxAddress); | ||
|
||
return useCallback( | ||
async (values?: FieldValues) => { | ||
if (!account || !stxSigner) return; | ||
|
||
const options: GenerateUnsignedTransactionOptions = { | ||
publicKey: bytesToHex(stxSigner.publicKey), | ||
nonce: Number(values?.nonce) ?? nextNonce?.nonce, | ||
fee: stxToMicroStx(values?.fee || 0).toNumber(), | ||
txData: { | ||
txType: TransactionTypes.STXTransfer, | ||
// Using account address here as a fallback for a fee estimation | ||
recipient: values?.recipient ?? stxAddress, | ||
amount: values?.amount ? stxToMicroStx(values?.amount).toString(10) : '0', | ||
memo: values?.memo || undefined, | ||
network, | ||
// Coercing type here as we don't have the public key | ||
// as expected by STXTransferPayload type. | ||
// This code will likely need to change soon with Ledger | ||
// work, and coercion allows us to remove lots of type mangling | ||
// and types are out of sync with @stacks/connect | ||
} as any, | ||
}; | ||
return generateUnsignedTransaction(options); | ||
}, | ||
[account, stxSigner, nextNonce?.nonce, stxAddress, network] | ||
); | ||
} |
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
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
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
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
File renamed without changes.
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
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
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
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
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
40 changes: 40 additions & 0 deletions
40
apps/mobile/src/features/send/send-form/components/send-form-stacks-setter.tsx
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,40 @@ | ||
import { useEffect } from 'react'; | ||
import { useFormContext } from 'react-hook-form'; | ||
|
||
import { useSendFormContext } from '@/features/send/send-form/send-form-context'; | ||
import { HasChildren } from '@/utils/types'; | ||
import BigNumber from 'bignumber.js'; | ||
import { z } from 'zod'; | ||
|
||
import { defaultStacksFees, useNextNonce } from '@leather.io/query'; | ||
import { useOnMount } from '@leather.io/ui/native'; | ||
import { microStxToStx } from '@leather.io/utils'; | ||
|
||
const defaultFeeFallback = 2500; | ||
|
||
interface SendFormStacksSetterProps extends HasChildren { | ||
address: string; | ||
} | ||
export function SendFormStacksSetter({ address, children }: SendFormStacksSetterProps) { | ||
const { schema } = useSendFormContext(); | ||
const { setValue, formState, getValues } = useFormContext<z.infer<typeof schema>>(); | ||
const { data: nextNonce } = useNextNonce(address); | ||
|
||
// Temporary default fee | ||
const defaultFee = | ||
defaultStacksFees.estimates[0]?.fee.amount ?? new BigNumber(defaultFeeFallback); | ||
|
||
useOnMount(() => setValue('fee', microStxToStx(defaultFee).toString())); | ||
|
||
useEffect(() => { | ||
if ( | ||
nextNonce?.nonce && | ||
!formState.dirtyFields.nonce && | ||
getValues('nonce') !== nextNonce.nonce | ||
) { | ||
return setValue('nonce', nextNonce?.nonce); | ||
} | ||
}, [formState.dirtyFields.nonce, getValues, nextNonce?.nonce, setValue]); | ||
|
||
return children; | ||
} |
64 changes: 64 additions & 0 deletions
64
apps/mobile/src/features/send/send-form/hooks/use-send-form-stx.tsx
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,64 @@ | ||
import { useForm } from 'react-hook-form'; | ||
|
||
import { useGenerateStxTokenTransferUnsignedTx } from '@/common/transactions/stacks-transactions.hooks'; | ||
import { useGetStacksAddresses } from '@/features/balances/stacks/use-get-stacks-addresses'; | ||
import { useStxBalance } from '@/queries/balance/stacks-balance.query'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { bytesToHex } from '@noble/hashes/utils'; | ||
|
||
import { | ||
CreateCurrentSendRoute, | ||
useSendSheetNavigation, | ||
useSendSheetRoute, | ||
} from '../../send-form.utils'; | ||
import { | ||
SendFormStxSchema, | ||
defaultSendFormStxValues, | ||
sendFormStxSchema, | ||
} from '../schemas/send-form-stx.schema'; | ||
|
||
export type CurrentRoute = CreateCurrentSendRoute<'send-form-stx'>; | ||
|
||
export function useSendFormStx() { | ||
const route = useSendSheetRoute<CurrentRoute>(); | ||
const navigation = useSendSheetNavigation<CurrentRoute>(); | ||
|
||
const addresses = useGetStacksAddresses({ | ||
accountIndex: route.params.account.accountIndex, | ||
fingerprint: route.params.account.fingerprint, | ||
}); | ||
const { availableBalance, fiatBalance } = useStxBalance(addresses); | ||
|
||
const formMethods = useForm<SendFormStxSchema>({ | ||
mode: 'onChange', | ||
defaultValues: defaultSendFormStxValues, | ||
resolver: zodResolver(sendFormStxSchema), | ||
}); | ||
|
||
const generateTx = useGenerateStxTokenTransferUnsignedTx({ | ||
accountIndex: route.params.account.accountIndex, | ||
fingerprint: route.params.account.fingerprint, | ||
}); | ||
|
||
return { | ||
address: addresses[0] ?? '', | ||
availableBalance, | ||
fiatBalance, | ||
formMethods, | ||
onGoBack() { | ||
navigation.navigate('send-select-asset', { account: route.params.account }); | ||
}, | ||
async onSubmit(data: SendFormStxSchema) { | ||
// eslint-disable-next-line no-console, lingui/no-unlocalized-strings | ||
console.log('Send form data:', data); | ||
const tx = await generateTx(data); | ||
// eslint-disable-next-line no-console, lingui/no-unlocalized-strings | ||
console.log('Unsigned tx:', tx); | ||
// Show an error toast here? | ||
if (!tx) throw new Error('Attempted to generate unsigned tx, but tx is undefined'); | ||
const txHex = bytesToHex(tx.serialize()); | ||
// eslint-disable-next-line no-console, lingui/no-unlocalized-strings | ||
console.log('tx hex:', txHex); | ||
}, | ||
}; | ||
} |
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
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.