-
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.
feat: migrate stacks generate txs, closes LEA-1732
- Loading branch information
Showing
36 changed files
with
619 additions
and
150 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 { 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 { AnchorMode } from '@stacks/transactions'; | ||
|
||
import { useNextNonce } from '@leather.io/query'; | ||
import { | ||
GenerateUnsignedStxTokenTransferArgs, | ||
TransactionTypes, | ||
generateUnsignedTransaction, | ||
} from '@leather.io/stacks'; | ||
|
||
export function useStxTokenTransferDetails({ fingerprint, accountIndex }: AccountId) { | ||
const account = useAccountByIndex(fingerprint, accountIndex); | ||
const network = useNetworkPreferenceStacksNetwork(); | ||
const stxSigner = useStacksSigners().fromAccountIndex(fingerprint, accountIndex)[0]; | ||
const stxAddress = stxSigner?.address ?? ''; | ||
const { data: nextNonce } = useNextNonce(stxAddress); | ||
|
||
if (!account || !stxSigner) return; | ||
|
||
return { | ||
network, | ||
nonce: nextNonce?.nonce, | ||
publicKey: bytesToHex(stxSigner.publicKey), | ||
// stxAddress as fallback for fee estimation | ||
recipient: stxAddress, | ||
}; | ||
} | ||
|
||
const defaultRequiredStxTokenTransferOptions = { | ||
amount: 0, | ||
anchorMode: AnchorMode.Any, | ||
}; | ||
|
||
export function useGenerateStxTokenTransferUnsignedTransaction(account: AccountId) { | ||
const stxAccountSignerDetails = useStxTokenTransferDetails(account); | ||
|
||
return (values: Partial<GenerateUnsignedStxTokenTransferArgs>) => { | ||
if (!stxAccountSignerDetails) return; | ||
|
||
return generateUnsignedTransaction({ | ||
txType: TransactionTypes.StxTokenTransfer, | ||
...defaultRequiredStxTokenTransferOptions, | ||
...stxAccountSignerDetails, | ||
...values, | ||
}); | ||
}; | ||
} |
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
24 changes: 24 additions & 0 deletions
24
apps/mobile/src/features/send/send-form/hooks/use-send-form-btc.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,24 @@ | ||
import { | ||
CreateCurrentSendRoute, | ||
useSendSheetNavigation, | ||
useSendSheetRoute, | ||
} from '../../send-form.utils'; | ||
import { SendFormStxSchema } from '../schemas/send-form-stx.schema'; | ||
|
||
export type CurrentRoute = CreateCurrentSendRoute<'send-form-stx'>; | ||
|
||
export function useSendFormBtc() { | ||
const route = useSendSheetRoute<CurrentRoute>(); | ||
const navigation = useSendSheetNavigation<CurrentRoute>(); | ||
|
||
return { | ||
onGoBack() { | ||
navigation.navigate('send-select-asset', { account: route.params.account }); | ||
}, | ||
// Temporary logs until we can hook up to approver flow | ||
async onSubmit(values: SendFormStxSchema) { | ||
// eslint-disable-next-line no-console, lingui/no-unlocalized-strings | ||
console.log('Send form data:', values); | ||
}, | ||
}; | ||
} |
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
import { useGenerateStxTokenTransferUnsignedTransaction } from '@/common/transactions/stacks-transactions.hooks'; | ||
import { bytesToHex } from '@noble/hashes/utils'; | ||
|
||
import { stxToMicroStx } from '@leather.io/utils'; | ||
|
||
import { | ||
CreateCurrentSendRoute, | ||
useSendSheetNavigation, | ||
useSendSheetRoute, | ||
} from '../../send-form.utils'; | ||
import { SendFormStxSchema } from '../schemas/send-form-stx.schema'; | ||
|
||
export type CurrentRoute = CreateCurrentSendRoute<'send-form-stx'>; | ||
|
||
function parseSendFormValues(values: SendFormStxSchema) { | ||
return { | ||
amount: values.amount ? stxToMicroStx(values.amount).toString(10) : '0', | ||
fee: stxToMicroStx(values.fee || 0).toNumber(), | ||
memo: values.memo, | ||
recipient: values.recipient, | ||
}; | ||
} | ||
|
||
export function useSendFormStx() { | ||
const route = useSendSheetRoute<CurrentRoute>(); | ||
const navigation = useSendSheetNavigation<CurrentRoute>(); | ||
|
||
const generateTx = useGenerateStxTokenTransferUnsignedTransaction({ | ||
accountIndex: route.params.account.accountIndex, | ||
fingerprint: route.params.account.fingerprint, | ||
}); | ||
|
||
return { | ||
onGoBack() { | ||
navigation.navigate('send-select-asset', { account: route.params.account }); | ||
}, | ||
// Temporary logs until we can hook up to approver flow | ||
async onSubmit(values: SendFormStxSchema) { | ||
// eslint-disable-next-line no-console, lingui/no-unlocalized-strings | ||
console.log('Send form data:', values); | ||
const tx = await generateTx(parseSendFormValues(values)); | ||
// 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); | ||
}, | ||
}; | ||
} |
40 changes: 40 additions & 0 deletions
40
apps/mobile/src/features/send/send-form/providers/send-form-bitcoin-provider.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 { useBitcoinAccountTotalBitcoinBalance } from '@/queries/balance/bitcoin-balance.query'; | ||
import { HasChildren } from '@/utils/types'; | ||
import { t } from '@lingui/macro'; | ||
|
||
import { useSendSheetRoute } from '../../send-form.utils'; | ||
import { useSendFormBtc } from '../hooks/use-send-form-btc'; | ||
import { CurrentRoute } from '../hooks/use-send-form-stx'; | ||
import { defaultSendFormBtcValues, sendFormBtcSchema } from '../schemas/send-form-btc.schema'; | ||
import { SendFormProvider } from '../send-form-context'; | ||
|
||
export function SendFormBitcoinProvider({ children }: HasChildren) { | ||
const route = useSendSheetRoute<CurrentRoute>(); | ||
|
||
const { onSubmit } = useSendFormBtc(); | ||
|
||
const { availableBalance, fiatBalance } = useBitcoinAccountTotalBitcoinBalance({ | ||
accountIndex: route.params.account.accountIndex, | ||
fingerprint: route.params.account.fingerprint, | ||
}); | ||
|
||
return ( | ||
<SendFormProvider | ||
value={{ | ||
name: t({ | ||
id: 'asset_name.bitcoin', | ||
message: 'Bitcoin', | ||
}), | ||
protocol: 'nativeBtc', | ||
symbol: 'BTC', | ||
availableBalance, | ||
fiatBalance, | ||
defaultValues: defaultSendFormBtcValues, | ||
schema: sendFormBtcSchema, | ||
onSubmit, | ||
}} | ||
> | ||
{children} | ||
</SendFormProvider> | ||
); | ||
} |
Oops, something went wrong.