Skip to content

Commit

Permalink
feat: add the order to the success and failure actions (#2155)
Browse files Browse the repository at this point in the history
* feat: add the order to the success and failure actions

* fix: eslint issue

* fix: rollback latest change

* fix: make balance and tokens match by symbol
  • Loading branch information
juanmahidalgo committed Feb 27, 2024
1 parent 95a02af commit b880f0b
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const mapDispatch = (
onExecuteOrderCrossChain: (route: Route) =>
buyItemCrossChainRequest(
(ownProps.metadata.nft as unknown) as Item,
route
route,
ownProps.metadata.order
),
onExecuteOrderWithCard: executeOrderWithCardRequest
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ const ChainAndTokenSelector = (props: Props) => {
const balances = await marketplaceAPI.fetchWalletTokenBalances(
currentChain,
wallet.address
)
)
setIsFetchingBalances(false)
setBalances(
balances.reduce((acc, balance) => {
acc[balance.contract_address] = balance
acc[balance.contract_ticker_symbol] = balance
return acc
}, {} as Record<string, Balance>)
)
Expand All @@ -79,8 +79,8 @@ const ChainAndTokenSelector = (props: Props) => {
)
// this sortes the tokens by USD balance
filtered?.sort((a, b) => {
const aQuote = balances[a.address.toLowerCase()]?.quote ?? '0'
const bQuote = balances[b.address.toLowerCase()]?.quote ?? '0'
const aQuote = balances[a.symbol]?.quote ?? '0'
const bQuote = balances[b.symbol]?.quote ?? '0'
if (aQuote === bQuote) return 0
return aQuote < bQuote ? 1 : -1
})
Expand Down Expand Up @@ -117,45 +117,45 @@ const ChainAndTokenSelector = (props: Props) => {
<span>{chain.networkName}</span>
</div>
))}
{filteredTokens?.map(token => (
<div
key={`${token.symbol}-${token.address}`}
className={styles.rowItem}
onClick={() => onSelect(token)}
>
<div className={styles.tokenDataContainer}>
<img src={token.logoURI} alt={token.symbol} />
<div className={styles.tokenNameAndSymbolContainer}>
<span>{token.symbol}</span>
<span className={styles.tokenName}>{token.name}</span>
{filteredTokens?.map(token => {
return (
<div
key={`${token.symbol}-${token.address}`}
className={styles.rowItem}
onClick={() => onSelect(token)}
>
<div className={styles.tokenDataContainer}>
<img src={token.logoURI} alt={token.symbol} />
<div className={styles.tokenNameAndSymbolContainer}>
<span>{token.symbol}</span>
<span className={styles.tokenName}>{token.name}</span>
</div>
</div>
<span className={styles.balance}>
{!!balances[token.symbol] ? (
<>
{Number(
ethers.utils.formatUnits(
balances[token.symbol]
.balance as string,
balances[token.symbol]
.contract_decimals
)
).toFixed(5)}{' '}
{balances[token.symbol].quote ? (
<span className={styles.tokenName}>
$
{balances[token.symbol].quote.toLocaleString()}
</span>
) : null}
</>
) : (
0
)}
</span>
</div>
<span className={styles.balance}>
{!!balances[token.address.toLocaleLowerCase()] ? (
<>
{Number(
ethers.utils.formatUnits(
balances[token.address.toLocaleLowerCase()]
.balance as string,
balances[token.address.toLocaleLowerCase()]
.contract_decimals
)
).toLocaleString()}{' '}
{balances[token.address.toLocaleLowerCase()].quote ? (
<span className={styles.tokenName}>
$
{balances[
token.address.toLocaleLowerCase()
].quote.toLocaleString()}
</span>
) : null}
</>
) : (
0
)}
</span>
</div>
))}
)
})}
</div>
)}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Item } from '@dcl/schemas'
import { Dispatch, bindActionCreators } from 'redux'
import { Route } from 'decentraland-transactions/crossChain'
import { isLoadingType } from 'decentraland-dapps/dist/modules/loading'
Expand Down Expand Up @@ -43,7 +42,7 @@ const mapDispatch = (
{
onBuyItem: buyItemRequest,
onBuyItemCrossChain: (route: Route) =>
buyItemCrossChainRequest(ownProps.metadata.item as Item, route),
buyItemCrossChainRequest(ownProps.metadata.item, route),
onBuyWithCard: buyItemWithCardRequest
},
dispatch
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Item } from '@dcl/schemas'
import { WithAuthorizedActionProps } from 'decentraland-dapps/dist/containers/withAuthorizedAction'
import { ModalProps } from 'decentraland-dapps/dist/providers/ModalProvider/ModalProvider.types'
import type { Route } from 'decentraland-transactions/crossChain'
Expand All @@ -8,7 +9,8 @@ import {
buyItemWithCardRequest
} from '../../../../modules/item/actions'

export type Props = WithAuthorizedActionProps & ModalProps & {
export type Props = WithAuthorizedActionProps & Omit<ModalProps, 'metadata'> & {
metadata: { item: Item }
isBuyingItemNatively: boolean
isBuyingItemCrossChain: boolean
getContract: (query: Partial<Contract>) => ReturnType<typeof getContract>
Expand Down
8 changes: 5 additions & 3 deletions webapp/src/modules/analytics/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ track<BuyItemCrossChainSuccessAction>(
const {
route: { route },
item,
order,
txHash
} = payload
return {
Expand All @@ -312,7 +313,7 @@ track<BuyItemCrossChainSuccessAction>(
rarity: item.rarity,
network: item.network,
chainId: item.chainId,
price: Number(ethers.utils.formatEther(item.price)),
price: Number(ethers.utils.formatEther(order?.price ?? item.price)),
data: item.data,
txHash
}
Expand Down Expand Up @@ -363,7 +364,8 @@ track<BuyItemCrossChainFailureAction>(
({ payload }) => {
const {
route: { route },
item
item,
price
} = payload
return {
fromAmount: ethers.utils.formatUnits(
Expand Down Expand Up @@ -396,7 +398,7 @@ track<BuyItemCrossChainFailureAction>(
rarity: item.rarity,
network: item.network,
chainId: item.chainId,
price: Number(ethers.utils.formatEther(item.price)),
price: Number(ethers.utils.formatEther(price)),
data: item.data
}
}
Expand Down
15 changes: 9 additions & 6 deletions webapp/src/modules/item/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainId, Item, ItemFilters } from '@dcl/schemas'
import { ChainId, Item, ItemFilters, Order } from '@dcl/schemas'
import { NFTPurchase } from 'decentraland-dapps/dist/modules/gateway/types'
import {
buildTransactionWithFromPayload,
Expand Down Expand Up @@ -118,33 +118,36 @@ export const BUY_ITEM_CROSS_CHAIN_REQUEST = '[Request] Buy item cross-chain'
export const BUY_ITEM_CROSS_CHAIN_SUCCESS = '[Success] Buy item cross-chain'
export const BUY_ITEM_CROSS_CHAIN_FAILURE = '[Failure] Buy item cross-chain'

export const buyItemCrossChainRequest = (item: Item, route: Route) =>
action(BUY_ITEM_CROSS_CHAIN_REQUEST, { item, route })
export const buyItemCrossChainRequest = (item: Item, route: Route, order?: Order) =>
action(BUY_ITEM_CROSS_CHAIN_REQUEST, { item, route, order })

export const buyItemCrossChainSuccess = (
route: Route,
chainId: ChainId,
txHash: string,
item: Item
item: Item,
order?: Order
) =>
action(BUY_ITEM_CROSS_CHAIN_SUCCESS, {
route,
item,
txHash,
order,
...buildTransactionWithReceiptPayload(chainId, txHash, {
itemId: item.itemId,
contractAddress: item.contractAddress,
network: item.network,
name: getAssetName(item),
price: formatWeiMANA(item.price)
price: formatWeiMANA(order?.price ?? item.price)
})
})

export const buyItemCrossChainFailure = (
route: Route,
item: Item,
price: string,
error: string
) => action(BUY_ITEM_CROSS_CHAIN_FAILURE, { route, item, error })
) => action(BUY_ITEM_CROSS_CHAIN_FAILURE, { route, item, price, error })

export type BuyItemCrossChainRequestAction = ReturnType<
typeof buyItemCrossChainRequest
Expand Down
6 changes: 4 additions & 2 deletions webapp/src/modules/item/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export function* itemSaga(getIdentity: () => AuthIdentity | undefined) {
}

function* handleBuyItemCrossChain(action: BuyItemCrossChainRequestAction) {
const { item, route } = action.payload
const { item, route, order } = action.payload
try {
const wallet: ReturnType<typeof getWallet> = yield select(getWallet)

Expand All @@ -314,7 +314,8 @@ export function* itemSaga(getIdentity: () => AuthIdentity | undefined) {
route,
item.chainId,
txRespose.transactionHash,
item
item,
order
)
)
}
Expand All @@ -323,6 +324,7 @@ export function* itemSaga(getIdentity: () => AuthIdentity | undefined) {
buyItemCrossChainFailure(
route,
item,
order?.price || item.price,
isErrorWithMessage(error) ? error.message : t('global.unknown_error')
)
)
Expand Down

0 comments on commit b880f0b

Please sign in to comment.