Skip to content

Commit

Permalink
Refactor calculaed state
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Oct 5, 2023
1 parent db0f741 commit d3590db
Show file tree
Hide file tree
Showing 36 changed files with 473 additions and 881 deletions.
10 changes: 5 additions & 5 deletions apps/wallet-mobile/src/TxHistory/ActionsBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export const ActionsBanner = ({disabled = false}: {disabled: boolean}) => {
const navigateTo = useNavigateTo()
const wallet = useSelectedWallet()
const {resetForm} = useSend()
const {createOrder} = useSwap()
const {orderData} = useSwap()
const {track} = useMetrics()
const sellTokenInfo = useTokenInfo({
wallet,
tokenId: createOrder.amounts.sell.tokenId,
tokenId: orderData.amounts.sell.tokenId,
})
const buyTokenInfo = useTokenInfo({
wallet,
tokenId: createOrder.amounts.buy.tokenId,
tokenId: orderData.amounts.buy.tokenId,
})

const handleOnBuy = () => {
Expand All @@ -60,8 +60,8 @@ export const ActionsBanner = ({disabled = false}: {disabled: boolean}) => {
{asset_name: sellTokenInfo.name, asset_ticker: sellTokenInfo.ticker, policy_id: sellTokenInfo.group},
],
to_asset: [{asset_name: buyTokenInfo.name, asset_ticker: buyTokenInfo.ticker, policy_id: buyTokenInfo.group}],
order_type: createOrder.type,
slippage_tolerance: createOrder.slippage,
order_type: orderData.type,
slippage_tolerance: orderData.slippage,
})

navigateTo.swap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export const AmountCard = ({
const amountInputRef = useRef<TextInput>(inputRef?.current ?? null)

const tokenInfo = useTokenInfo({wallet, tokenId})
const {createOrder} = useSwap()
const {orderData} = useSwap()

const isSell = tokenId === createOrder.amounts.sell.tokenId
const isSell = tokenId === orderData.amounts.sell.tokenId

const noTokenSelected = !touched

Expand Down Expand Up @@ -118,7 +118,7 @@ export const AmountCard = ({
<Spacer height={4} />

<Text style={styles.errorText}>
{createOrder.selectedPool === undefined
{orderData.calculatedPool === undefined
? strings.noPool
: isSell
? strings.notEnoughBalance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const Provider = ({children, wallet}: {children: React.ReactNode; wallet: YoroiW
initialState={{
...mockSwapStateDefault,
unsignedTx: walletMocks.yoroiUnsignedTx,
createOrder: {...mocks.confirmTx.createOrder},
orderData: {...mocks.confirmTx.orderData},
}}
swapManager={{
...mockSwapManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type Props = {
export const SelectPoolFromList = ({data = []}: Props) => {
const strings = useStrings()
const wallet = useSelectedWallet()
const {selectedPoolChanged, createOrder} = useSwap()
const {selectedPoolChanged, orderData} = useSwap()
const {poolTouched} = useSwapTouched()
const [selectedCardIndex, setSelectedCardIndex] = useState(createOrder.selectedPool?.poolId)
const [selectedCardIndex, setSelectedCardIndex] = useState(orderData.selectedPoolId)
const navigate = useNavigateTo()
const {track} = useMetrics()

const handleCardSelect = (pool: Swap.Pool) => {
track.swapPoolChanged()
selectedPoolChanged(pool)
selectedPoolChanged(pool.poolId)
setSelectedCardIndex(pool.poolId)
poolTouched()
navigate.startSwap()
Expand Down
47 changes: 22 additions & 25 deletions apps/wallet-mobile/src/features/Swap/common/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,42 @@ import {mockSwapStateDefault, SwapState} from '@yoroi/swap'
import {mocks as walletMocks} from '../../../yoroi-wallets/mocks/wallet'
import {asQuantity} from '../../../yoroi-wallets/utils'

type Type = 'market' | 'limit'

export const mocks = {
confirmTx: {
...mockSwapStateDefault,
yoroiUnsignedTx: walletMocks.yoroiUnsignedTx,
createOrder: {
address: '',
orderData: {
...mockSwapStateDefault.orderData,
amounts: {
buy: {
quantity: asQuantity(20467572) as `${number}`,
tokenId: '208a2ca888886921513cb777bb832a8dc685c04de990480151f12150.53484942414441',
},
sell: {quantity: asQuantity(2000000), tokenId: ''},
},
datum: '',
datumHash: '',
limitPrice: '0.089' as `${number}`,
marketPrice: '0.089' as `${number}`,
selectedPool: {
batcherFee: {quantity: asQuantity(2500000), tokenId: ''},
deposit: {quantity: asQuantity(2000000), tokenId: ''},
fee: '0.05',
lastUpdate: '2023-09-08 09:56:13',
lpToken: {
quantity: asQuantity(68917682),
tokenId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.6c702083',
},
poolId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.702083',
price: 0.0890390378168252,
provider: 'sundaeswap',
tokenA: {quantity: asQuantity(20630071), tokenId: ''},
tokenB: {
quantity: asQuantity(231696922),
tokenId: '208a2ca888886921513cb777bb832a8dc685c04de990480151f12150.53484942414441',
selectedPool: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.702083',
pools: [
{
batcherFee: {quantity: asQuantity(2500000), tokenId: ''},
deposit: {quantity: asQuantity(2000000), tokenId: ''},
fee: '0.05',
lastUpdate: '2023-09-08 09:56:13',
lpToken: {
quantity: asQuantity(68917682),
tokenId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.6c702083',
},
poolId: '0029cb7c88c7567b63d1a512c0ed626aa169688ec980730c0473b913.702083',
price: 0.0890390378168252,
provider: 'sundaeswap',
tokenA: {quantity: asQuantity(20630071), tokenId: ''},
tokenB: {
quantity: asQuantity(231696922),
tokenId: '208a2ca888886921513cb777bb832a8dc685c04de990480151f12150.53484942414441',
},
},
},
],
slippage: 1,
type: 'market' as Type,
},
} as SwapState,
}
20 changes: 10 additions & 10 deletions apps/wallet-mobile/src/features/Swap/common/useSwapTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import {YoroiEntry, YoroiUnsignedTx} from '../../../yoroi-wallets/types'
import {splitStringInto64CharArray} from '../../../yoroi-wallets/utils'

export const useSwapTx = (options?: UseMutationOptions<YoroiUnsignedTx, Error, {entry: YoroiEntry; datum: Datum}>) => {
const {createOrder} = useSwap()

const {orderData} = useSwap()
const pool = orderData.calculatedPool?.pool
const metadata = [
{
label: '674',
data: {
msg: splitStringInto64CharArray(
JSON.stringify({
provider: createOrder.selectedPool?.provider,
sellTokenId: createOrder.amounts.sell.tokenId,
sellQuantity: createOrder.amounts.sell.quantity,
buyTokenId: createOrder.amounts.buy.tokenId,
buyQuantity: createOrder.amounts.buy.quantity,
depositFee: createOrder.selectedPool?.deposit.quantity,
feeTokenId: createOrder.selectedPool?.deposit.tokenId,
poolId: createOrder.selectedPool?.poolId,
provider: pool?.provider,
sellTokenId: orderData.amounts.sell.tokenId,
sellQuantity: orderData.amounts.sell.quantity,
buyTokenId: orderData.amounts.buy.tokenId,
buyQuantity: orderData.amounts.buy.quantity,
depositFee: pool?.deposit.quantity,
feeTokenId: pool?.deposit.tokenId,
poolId: pool?.poolId,
}),
),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ConfirmTxWithPasswaordScreen = () => {
initialState={{
...mockSwapStateDefault,
unsignedTx: walletMocks.yoroiUnsignedTx,
createOrder: {...mocks.confirmTx.createOrder},
orderData: {...mocks.confirmTx.orderData},
}}
swapManager={{
...mockSwapManager,
Expand All @@ -46,7 +46,7 @@ const ConfirmTxWithOSScreen = () => {
initialState={{
...mockSwapStateDefault,
unsignedTx: walletMocks.yoroiUnsignedTx,
createOrder: {...mocks.confirmTx.createOrder},
orderData: {...mocks.confirmTx.orderData},
}}
swapManager={{
...mockSwapManager,
Expand All @@ -66,7 +66,7 @@ const ConfirmTxWithHWcreen = () => {
initialState={{
...mockSwapStateDefault,
unsignedTx: walletMocks.yoroiUnsignedTx,
createOrder: {...mocks.confirmTx.createOrder},
orderData: {...mocks.confirmTx.orderData},
}}
swapManager={{
...mockSwapManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export const ConfirmTxScreen = () => {
const navigate = useNavigateTo()
const {track} = useMetrics()

const {unsignedTx, createOrder} = useSwap()
const {unsignedTx, orderData} = useSwap()
const sellTokenInfo = useTokenInfo({
wallet,
tokenId: createOrder.amounts.sell.tokenId,
tokenId: orderData.amounts.sell.tokenId,
})
const buyTokenInfo = useTokenInfo({
wallet,
tokenId: createOrder.amounts.buy.tokenId,
tokenId: orderData.amounts.buy.tokenId,
})

const {authWithOs, isLoading: authenticating} = useAuthOsWithEasyConfirmation(
Expand All @@ -51,20 +51,20 @@ export const ConfirmTxScreen = () => {
signTx: {useErrorBoundary: true},
submitTx: {
onSuccess: () => {
if (!createOrder.selectedPool) return
if (orderData.calculatedPool === undefined) return
track.swapOrderSubmitted({
from_asset: [
{asset_name: sellTokenInfo.name, asset_ticker: sellTokenInfo.ticker, policy_id: sellTokenInfo.group},
],
to_asset: [
{asset_name: buyTokenInfo.name, asset_ticker: buyTokenInfo.ticker, policy_id: buyTokenInfo.group},
],
order_type: createOrder.type,
slippage_tolerance: createOrder.slippage,
from_amount: createOrder.amounts.sell.quantity,
to_amount: createOrder.amounts.buy.quantity,
pool_source: createOrder.selectedPool.provider,
swap_fees: Number(createOrder.selectedPool.fee),
order_type: orderData.type,
slippage_tolerance: orderData.slippage,
from_amount: orderData.amounts.sell.quantity,
to_amount: orderData.amounts.buy.quantity,
pool_source: orderData.calculatedPool.pool.provider,
swap_fees: Number(orderData.calculatedPool.cost.batcherFee),
})

navigate.submittedTx()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const TxSummary = () => {
initialState={{
...mockSwapStateDefault,
unsignedTx: walletMocks.yoroiUnsignedTx,
createOrder: {...mocks.confirmTx.createOrder},
orderData: {...mocks.confirmTx.orderData},
}}
swapManager={{
...mockSwapManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const TransactionSummary = () => {
const strings = useStrings()
const wallet = useSelectedWallet()
const {numberLocale} = useLanguage()
const {createOrder} = useSwap()
const {amounts, selectedPool} = createOrder
const {orderData} = useSwap()
const {amounts, calculatedPool} = orderData

const buyTokenInfo = useTokenInfo({wallet, tokenId: amounts.buy.tokenId})
const tokenToBuyName = buyTokenInfo.ticker ?? buyTokenInfo.name
Expand All @@ -32,7 +32,7 @@ export const TransactionSummary = () => {
{
label: strings.swapMinAdaTitle,
value: `${Quantities.format(
selectedPool?.deposit?.quantity ?? Quantities.zero,
calculatedPool?.cost?.deposit?.quantity ?? Quantities.zero,
Number(wallet.primaryTokenInfo.decimals),
)} ${wallet.primaryTokenInfo.ticker}`,
info: strings.swapMinAda,
Expand All @@ -41,7 +41,7 @@ export const TransactionSummary = () => {
label: strings.swapMinReceivedTitle,
value: `${getMinAdaReceiveAfterSlippage(
amounts.buy.quantity,
createOrder.slippage,
orderData.slippage,
buyTokenInfo.decimals ?? 0,
numberLocale,
)} ${tokenToBuyName}`,
Expand All @@ -50,7 +50,7 @@ export const TransactionSummary = () => {
{
label: strings.swapFeesTitle,
value: `${Quantities.format(
createOrder.selectedPool?.batcherFee?.quantity ?? Quantities.zero,
calculatedPool?.cost?.batcherFee?.quantity ?? Quantities.zero, // TODO: Show all fees
Number(wallet.primaryTokenInfo.decimals),
)} ${wallet.primaryTokenInfo.ticker}`,
info: strings.swapFees,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const Initial = () => {

const MarketOrder = () => {
const initialState = produce(mockSwapStateDefault, (draft) => {
draft.createOrder.type = 'market'
draft.orderData.type = 'market'
})
return (
<SelectedWalletProvider wallet={walletMocks.wallet}>
Expand All @@ -42,7 +42,7 @@ const MarketOrder = () => {

const LimitOrder = () => {
const initialState = produce(mockSwapStateDefault, (draft) => {
draft.createOrder.type = 'limit'
draft.orderData.type = 'limit'
})
return (
<SelectedWalletProvider wallet={walletMocks.wallet}>
Expand Down
Loading

0 comments on commit d3590db

Please sign in to comment.