Commit ed58c39b authored by cartcrom's avatar cartcrom Committed by GitHub

feat: parse moonpay purchases (#6677)

* feat: parse moonpay purchases

* fix: update comment

* fix: use new coned function

* refactor: simplify nested if statements
parent 5d2254be
<svg width="36" height="36" viewBox="0 0 36 36" fill="none" xmlns="http://www.w3.org/2000/svg"><rect width="36" height="36" rx="18" fill="#7D00FF"/><path d="M24.933 14.14a3.07 3.07 0 0 0 0-6.14 3.07 3.07 0 0 0 0 6.14ZM15.5 28A7.495 7.495 0 0 1 8 20.493a7.495 7.495 0 0 1 7.5-7.506c4.149 0 7.5 3.354 7.5 7.506A7.495 7.495 0 0 1 15.5 28Z" fill="#fff"/></svg>
\ No newline at end of file
import { t } from '@lingui/macro' import { t } from '@lingui/macro'
import { formatNumberOrString, NumberType } from '@uniswap/conedison/format' import { formatFiatPrice, formatNumberOrString, NumberType } from '@uniswap/conedison/format'
import { SupportedChainId } from '@uniswap/sdk-core' import { SupportedChainId } from '@uniswap/sdk-core'
import moonpayLogoSrc from 'assets/svg/moonpay.svg'
import { NONFUNGIBLE_POSITION_MANAGER_ADDRESSES, UNI_ADDRESS } from 'constants/addresses' import { NONFUNGIBLE_POSITION_MANAGER_ADDRESSES, UNI_ADDRESS } from 'constants/addresses'
import { nativeOnChain } from 'constants/tokens' import { nativeOnChain } from 'constants/tokens'
import { import {
ActivityType, ActivityType,
AssetActivityPartsFragment, AssetActivityPartsFragment,
Currency,
NftApprovalPartsFragment, NftApprovalPartsFragment,
NftApproveForAllPartsFragment, NftApproveForAllPartsFragment,
NftTransferPartsFragment, NftTransferPartsFragment,
...@@ -17,6 +19,7 @@ import ms from 'ms.macro' ...@@ -17,6 +19,7 @@ import ms from 'ms.macro'
import { useEffect, useState } from 'react' import { useEffect, useState } from 'react'
import { isAddress } from 'utils' import { isAddress } from 'utils'
import { MOONPAY_SENDER_ADDRESSES } from '../constants'
import { Activity } from './types' import { Activity } from './types'
type TransactionChanges = { type TransactionChanges = {
...@@ -106,6 +109,17 @@ function getSwapTitle(sent: TokenTransferPartsFragment, received: TokenTransferP ...@@ -106,6 +109,17 @@ function getSwapTitle(sent: TokenTransferPartsFragment, received: TokenTransferP
} }
} }
/**
*
* @param transactedValue Transacted value amount from TokenTransfer API response
* @returns parsed & formatted USD value as a string if currency is of type USD
*/
function formatTransactedValue(transactedValue: TokenTransferPartsFragment['transactedValue']): string {
if (!transactedValue) return '-'
const price = transactedValue?.currency === Currency.Usd ? transactedValue.value ?? undefined : undefined
return formatFiatPrice(price)
}
function parseSwap(changes: TransactionChanges) { function parseSwap(changes: TransactionChanges) {
if (changes.NftTransfer.length > 0 && changes.TokenTransfer.length === 1) { if (changes.NftTransfer.length > 0 && changes.TokenTransfer.length === 1) {
const collectionCounts = getCollectionCounts(changes.NftTransfer) const collectionCounts = getCollectionCounts(changes.NftTransfer)
...@@ -175,17 +189,27 @@ function parseSendReceive(changes: TransactionChanges, assetActivity: AssetActiv ...@@ -175,17 +189,27 @@ function parseSendReceive(changes: TransactionChanges, assetActivity: AssetActiv
} }
if (transfer && assetName && amount) { if (transfer && assetName && amount) {
return transfer.direction === 'IN' const isMoonpayPurchase = MOONPAY_SENDER_ADDRESSES.some((address) => isSameAddress(address, transfer?.sender))
? {
title: t`Received`, if (transfer.direction === 'IN') {
descriptor: `${amount} ${assetName} ${t`from`} `, return isMoonpayPurchase && transfer.__typename === 'TokenTransfer'
otherAccount: isAddress(transfer.sender) || undefined, ? {
} title: t`Purchased`,
: { descriptor: `${amount} ${assetName} ${t`for`} ${formatTransactedValue(transfer.transactedValue)}`,
title: t`Sent`, logos: [moonpayLogoSrc],
descriptor: `${amount} ${assetName} ${t`to`} `, }
otherAccount: isAddress(transfer.recipient) || undefined, : {
} title: t`Received`,
descriptor: `${amount} ${assetName} ${t`from`} `,
otherAccount: isAddress(transfer.sender) || undefined,
}
} else {
return {
title: t`Sent`,
descriptor: `${amount} ${assetName} ${t`to`} `,
otherAccount: isAddress(transfer.recipient) || undefined,
}
}
} }
return { title: t`Unknown Send` } return { title: t`Unknown Send` }
} }
......
...@@ -153,3 +153,11 @@ export function getActivityTitle(type: TransactionType, status: TransactionStatu ...@@ -153,3 +153,11 @@ export function getActivityTitle(type: TransactionType, status: TransactionStatu
} }
return TransactionTitleTable[type][status] return TransactionTitleTable[type][status]
} }
// Non-exhaustive list of addresses Moonpay uses when sending purchased tokens
export const MOONPAY_SENDER_ADDRESSES = [
'0x8216874887415e2650d12d53ff53516f04a74fd7',
'0x151b381058f91cf871e7ea1ee83c45326f61e96d',
'0xb287eac48ab21c5fb1d3723830d60b4c797555b0',
'0xd108fd0e8c8e71552a167e7a44ff1d345d233ba6',
]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment