-
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
46 changed files
with
1,128 additions
and
337 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
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
import { useNetworkPreferenceStacksNetwork } from '@/store/settings/settings.read'; | ||
import { AnchorMode } from '@stacks/transactions'; | ||
|
||
import { | ||
StacksUnsignedTokenTransferOptions, | ||
TransactionTypes, | ||
generateUnsignedTransaction, | ||
} from '@leather.io/stacks'; | ||
import { createMoney } from '@leather.io/utils'; | ||
|
||
export function useStxAccountTransferDetails(address: string, publicKey: string) { | ||
const network = useNetworkPreferenceStacksNetwork(); | ||
|
||
return { | ||
network, | ||
publicKey, | ||
// Fallback for fee estimation | ||
recipient: address, | ||
}; | ||
} | ||
|
||
const defaultRequiredStxTokenTransferOptions = { | ||
amount: createMoney(0, 'STX'), | ||
anchorMode: AnchorMode.Any, | ||
fee: createMoney(0, 'STX'), | ||
nonce: '', | ||
}; | ||
|
||
export function useGenerateStxTokenTransferUnsignedTransaction( | ||
stxAccountDetails: ReturnType<typeof useStxAccountTransferDetails> | ||
) { | ||
return (values: Partial<StacksUnsignedTokenTransferOptions>) => | ||
generateUnsignedTransaction({ | ||
txType: TransactionTypes.StxTokenTransfer, | ||
...defaultRequiredStxTokenTransferOptions, | ||
...stxAccountDetails, | ||
...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
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
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'; | ||
|
||
type CurrentRoute = CreateCurrentSendRoute<'send-form-btc'>; | ||
|
||
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 onInitSendTransfer(values: SendFormStxSchema) { | ||
// eslint-disable-next-line no-console, lingui/no-unlocalized-strings | ||
console.log('Send form data:', values); | ||
}, | ||
}; | ||
} |
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
import { | ||
useGenerateStxTokenTransferUnsignedTransaction, | ||
useStxAccountTransferDetails, | ||
} from '@/common/transactions/stacks-transactions.hooks'; | ||
import { bytesToHex } from '@noble/hashes/utils'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { createMoneyFromDecimal } 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: createMoneyFromDecimal(new BigNumber(values.amount), 'STX'), | ||
fee: createMoneyFromDecimal(new BigNumber(values.fee), 'STX'), | ||
memo: values.memo, | ||
nonce: values.nonce, | ||
recipient: values.recipient, | ||
}; | ||
} | ||
|
||
export function useSendFormStx() { | ||
const route = useSendSheetRoute<CurrentRoute>(); | ||
const navigation = useSendSheetNavigation<CurrentRoute>(); | ||
const stxAccountDetails = useStxAccountTransferDetails( | ||
route.params.address, | ||
route.params.publicKey | ||
); | ||
|
||
const generateTx = useGenerateStxTokenTransferUnsignedTransaction(stxAccountDetails); | ||
|
||
return { | ||
onGoBack() { | ||
navigation.navigate('send-select-asset', { account: route.params.account }); | ||
}, | ||
// Temporary logs until we can hook up to approver flow | ||
async onInitSendTransfer(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); | ||
}, | ||
}; | ||
} |
29 changes: 29 additions & 0 deletions
29
apps/mobile/src/features/send/send-form/loaders/send-form-btc-loader.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,29 @@ | ||
import { AccountId } from '@/models/domain.model'; | ||
import { useBitcoinAccountTotalBitcoinBalance } from '@/queries/balance/bitcoin-balance.query'; | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Money } from '@leather.io/models'; | ||
import { useAverageBitcoinFeeRates } from '@leather.io/query'; | ||
|
||
interface SendFormBtcData { | ||
availableBalance: Money; | ||
fiatBalance: Money; | ||
feeRates: Record<string, BigNumber>; | ||
} | ||
|
||
interface SendFormBtcLoaderProps { | ||
account: AccountId; | ||
children({ availableBalance, fiatBalance, feeRates }: SendFormBtcData): React.ReactNode; | ||
} | ||
export function SendFormBtcLoader({ account, children }: SendFormBtcLoaderProps) { | ||
// Not sure if we need to load feeRates here? | ||
const { data: feeRates } = useAverageBitcoinFeeRates(); | ||
const { availableBalance, fiatBalance } = useBitcoinAccountTotalBitcoinBalance({ | ||
accountIndex: account.accountIndex, | ||
fingerprint: account.fingerprint, | ||
}); | ||
|
||
if (!feeRates) return null; | ||
|
||
return children({ availableBalance, fiatBalance, feeRates }); | ||
} |
32 changes: 32 additions & 0 deletions
32
apps/mobile/src/features/send/send-form/loaders/send-form-stx-loader.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,32 @@ | ||
import { AccountId } from '@/models/domain.model'; | ||
import { useStxBalance } from '@/queries/balance/stacks-balance.query'; | ||
import { useStacksSignerAddressFromAccountIndex } from '@/store/keychains/stacks/stacks-keychains.read'; | ||
|
||
import { Money } from '@leather.io/models'; | ||
import { useNextNonce } from '@leather.io/query'; | ||
|
||
interface SendFormStxData { | ||
availableBalance: Money; | ||
fiatBalance: Money; | ||
nonce: number | string; | ||
} | ||
|
||
interface SendFormStxLoaderProps { | ||
account: AccountId; | ||
children({ availableBalance, fiatBalance, nonce }: SendFormStxData): React.ReactNode; | ||
} | ||
export function SendFormStxLoader({ account, children }: SendFormStxLoaderProps) { | ||
const address = | ||
useStacksSignerAddressFromAccountIndex(account.fingerprint, account.accountIndex) ?? ''; | ||
const { availableBalance, fiatBalance } = useStxBalance([address]); | ||
|
||
const { data: nextNonce } = useNextNonce(address); | ||
|
||
if (!address || !nextNonce) return null; | ||
|
||
return children({ | ||
availableBalance, | ||
fiatBalance, | ||
nonce: nextNonce?.nonce ?? '', | ||
}); | ||
} |
Oops, something went wrong.