Skip to content

Commit

Permalink
feat: track if user rejects tx in cross chain operation
Browse files Browse the repository at this point in the history
  • Loading branch information
juanmahidalgo committed Dec 4, 2023
1 parent 3e5aeb3 commit 8d85269
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 7 deletions.
69 changes: 68 additions & 1 deletion webapp/src/modules/analytics/track.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ import {
FetchItemsSuccessAction,
FETCH_ITEMS_SUCCESS,
BUY_ITEM_CROSS_CHAIN_SUCCESS,
BuyItemCrossChainSuccessAction
BuyItemCrossChainSuccessAction,
BuyItemCrossChainFailureAction,
BUY_ITEM_CROSS_CHAIN_FAILURE
} from '../item/actions'
import { SetIsTryingOnAction, SET_IS_TRYING_ON } from '../ui/preview/actions'
import { isParcel } from '../nft/utils'
Expand Down Expand Up @@ -104,6 +106,7 @@ import {
FETCH_CREATORS_ACCOUNT_SUCCESS,
FetchCreatorsAccountSuccessAction
} from '../account/actions'
import { isUserRejectedTransactionError } from '../transaction/utils'

function track<T extends PayloadAction<string, any>>(
actionType: string,
Expand Down Expand Up @@ -305,6 +308,70 @@ track<BuyItemCrossChainSuccessAction>(
}
)

track<BuyItemCrossChainFailureAction>(
BUY_ITEM_CROSS_CHAIN_FAILURE,
({ payload }) =>
isUserRejectedTransactionError(payload.error)
? events.BUY_ITEM_CROSS_CHAIN_TRANSACTION_DENIED
: events.BUY_ITEM_CROSS_CHAIN_ERROR,
({ payload }) => {
const {
route: { route },
item
} = payload
console.log({
fromAmount: ethers.utils.formatUnits(
route.estimate.fromAmount,
route.estimate.fromToken.decimals
),
gasCosts: route.estimate.gasCosts[0]
? ethers.utils.formatUnits(
route.estimate.gasCosts.reduce((acc, gasCost) => {
acc = acc.add(ethers.BigNumber.from(gasCost.amount))
return acc
}, ethers.BigNumber.from(0)),
route.estimate.gasCosts[0].token.decimals
)
: 0,
feeCosts: route.estimate.feeCosts[0]
? ethers.utils.formatUnits(
route.estimate.feeCosts.reduce((acc, feeCost) => {
acc = acc.add(ethers.BigNumber.from(feeCost.amount))
return acc
}, ethers.BigNumber.from(0)),
route.estimate.feeCosts[0].token.decimals
)
: 0,
fromTokenName: route.estimate.fromToken.name,
fromToken: route.params.fromToken,
fromChain: route.params.fromChain,
itemId: item.itemId,
contractAddress: item.contractAddress,
rarity: item.rarity,
network: item.network,
chainId: item.chainId,
price: Number(ethers.utils.formatEther(item.price)),
data: item.data
})
return {
fromAmount: ethers.utils.formatUnits(
route.estimate.fromAmount,
route.estimate.fromToken.decimals
),
fromTokenName: route.estimate.fromToken.name,
fromToken: route.params.fromToken,
fromChain: route.params.fromChain,
itemId: item.itemId,
contractAddress: item.contractAddress,
rarity: item.rarity,
network: item.network,
chainId: item.chainId,
price: Number(ethers.utils.formatEther(item.price)),
data: item.data
}
}
)

track<BuyItemWithCardSuccessAction>(
BUY_ITEM_WITH_CARD_SUCCESS,
events.BUY_ITEM_WITH_CARD,
Expand Down
7 changes: 5 additions & 2 deletions webapp/src/modules/item/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,11 @@ export const buyItemCrossChainSuccess = (
})
})

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

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

function* handleBuyItemCrossChain(action: BuyItemCrossChainRequestAction) {
const { item, route } = action.payload
try {
const { item, route } = action.payload

const wallet: ReturnType<typeof getWallet> = yield select(getWallet)

const provider: Provider | null = yield call(getConnectedProvider)
Expand Down Expand Up @@ -318,9 +317,10 @@ export function* itemSaga(getIdentity: () => AuthIdentity | undefined) {
)
}
} catch (error) {
console.log('error: ', error);
yield put(
buyItemCrossChainFailure(
route,
item,
isErrorWithMessage(error) ? error.message : t('global.unknown_error')
)
)
Expand Down
6 changes: 5 additions & 1 deletion webapp/src/modules/transaction/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ export function isUserCanceled(error: string) {
export function isUserDeniedSignatureError(error: string) {
return (
error.search(
/User (denied|rejected the) (transaction|message)( signature)?/
/User (denied|rejected) (transaction|message)( signature)?/
) !== -1
)
}

export function isUserRejectedTransactionError(error: string) {
return error.search(/user rejected transaction/) !== -1
}

// TODO: This is a replacement for future `ErrorCode`s. Needs an overhaul on decentraland-dapps
export function isContractAccountError(error: string) {
return error.search('Contract accounts are not supported') !== -1
Expand Down
3 changes: 3 additions & 0 deletions webapp/src/utils/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ export const SEARCH_ALL = 'Search all results'

// Buy Cross-chain
export const BUY_ITEM_CROSS_CHAIN = 'Buy Item Cross Chain'
export const BUY_ITEM_CROSS_CHAIN_ERROR = 'Buy Item Cross Chain Error'
export const BUY_ITEM_CROSS_CHAIN_TRANSACTION_DENIED =
'Buy Item Cross Chain Transaction Denied'
export const CROSS_CHAIN_TOKEN_SELECTION = 'Cross Chain token selection'
export const CROSS_CHAIN_CHAIN_SELECTION = 'Cross Chain chain selection'
export const ERROR_GETTING_ROUTE = 'Error getting Route'

0 comments on commit 8d85269

Please sign in to comment.