Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
[PAY-1753] Implements Withdrawals Page (#4043)
Browse files Browse the repository at this point in the history
  • Loading branch information
schottra authored Sep 8, 2023
1 parent 17284b7 commit 27089bd
Show file tree
Hide file tree
Showing 15 changed files with 469 additions and 77 deletions.
96 changes: 94 additions & 2 deletions packages/common/src/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
import { full } from '@audius/sdk'

import { createApi } from 'audius-query'
import { ID, Kind } from 'models'
import { ID, Kind, StringUSDC } from 'models'
import {
USDCTransactionDetails,
USDCTransactionMethod,
USDCTransactionType
} from 'models/USDCTransactions'
import { Nullable } from 'utils/typeUtils'

import { Id } from './utils'

type GetUSDCTransactionListArgs = {
userId: Nullable<ID>
offset: number
limit: number
sortMethod?: full.GetUSDCTransactionsSortMethodEnum
sortDirection?: full.GetUSDCTransactionsSortDirectionEnum
type?: full.GetUSDCTransactionsTypeEnum
method?: full.GetUSDCTransactionsMethodEnum
}

const parseTransaction = (
transaction: full.TransactionDetails
): USDCTransactionDetails => {
const { change, balance, transactionType, method, ...rest } = transaction
return {
...rest,
transactionType: transactionType as USDCTransactionType,
method: method as USDCTransactionMethod,
change: change as StringUSDC,
balance: balance as StringUSDC
}
}

const userApi = createApi({
reducerPath: 'userApi',
Expand All @@ -17,9 +50,68 @@ const userApi = createApi({
kind: Kind.USERS,
schemaKey: 'user'
}
},
getUSDCTransactions: {
fetch: async (
{
offset,
limit,
userId,
sortDirection,
sortMethod,
type,
method
}: GetUSDCTransactionListArgs,
context
) => {
const { data: encodedDataMessage, signature: encodedDataSignature } =
await context.audiusBackend.signDiscoveryNodeRequest()
const sdk = await context.audiusSdk()
const { data = [] } = await sdk.full.users.getUSDCTransactions({
limit,
offset,
sortDirection,
sortMethod,
id: Id.parse(userId!),
type,
method,
encodedDataMessage,
encodedDataSignature
})

return data.map(parseTransaction)
},
options: {}
},
getUSDCTransactionsCount: {
fetch: async (
{
userId,
type,
method
}: Pick<GetUSDCTransactionListArgs, 'userId' | 'type' | 'method'>,
{ audiusSdk, audiusBackend }
) => {
const { data: encodedDataMessage, signature: encodedDataSignature } =
await audiusBackend.signDiscoveryNodeRequest()
const sdk = await audiusSdk()
const { data } = await sdk.full.users.getUSDCTransactionCount({
id: Id.parse(userId!),
type,
method,
encodedDataMessage,
encodedDataSignature
})
return data ?? 0
},
options: {}
}
}
})

export const { useGetUserById } = userApi.hooks
export const {
useGetUserById,
useGetUSDCTransactions,
useGetUSDCTransactionsCount
} = userApi.hooks
export const userApiReducer = userApi.reducer
2 changes: 1 addition & 1 deletion packages/common/src/audius-query/createApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ const fetchData = async <Args, Data>(
endpoint.onQueryStarted?.(fetchArgs, { dispatch })

const apiData = await endpoint.fetch(fetchArgs, context)
if (!apiData) {
if (apiData == null) {
throw new Error('Remote data not found')
}

Expand Down
71 changes: 13 additions & 58 deletions packages/common/src/models/USDCTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@ import { Nullable } from '../utils/typeUtils'
import { StringUSDC } from './Wallet'

export enum USDCTransactionType {
PURCHASE_USDC = 'PURCHASE_USDC',
PURCHASE_CONTENT = 'PURCHASE_CONTENT',
TRANSFER = 'TRANSFER'
PURCHASE_STRIPE = 'purchase_stripe',
PURCHASE_CONTENT = 'purchase_content',
TRANSFER = 'transfer'
}

export enum USDCTransactionMethod {
// Transfer methods
SEND = 'SENT',
RECEIVE = 'RECEIVED',

// Purchase Methods
STRIPE = 'STRIPE'
}

export type InAppUSDCPurchaseMetadata = {
discriminator: USDCTransactionType.PURCHASE_USDC
usd: string
usdc: StringUSDC
purchaseTransactionId: string
SEND = 'send',
RECEIVE = 'receive'
}

export enum USDCContentPurchaseType {
Expand All @@ -38,46 +27,12 @@ export type USDCPurchaseDetails = {
createdAt: string
}

export type USDCPurchaseContentMetadata = {
discriminator: USDCTransactionType.PURCHASE_CONTENT
amount: StringUSDC
senderUserId: string
receiverUserId: string
purchaseType: USDCContentPurchaseType
contentId: number
}

export type USDCTransferMetadata = {
discriminator: USDCTransactionType.TRANSFER
amount: StringUSDC
destination: string
export type USDCTransactionDetails = {
signature: string
transactionType: USDCTransactionType
method: USDCTransactionMethod
transactionDate: string
change: StringUSDC
balance: StringUSDC
metadata?: Nullable<object> | string
}

export type USDCTransactionDetails =
| {
signature: string
transactionType: USDCTransactionType.PURCHASE_USDC
method: USDCTransactionMethod.STRIPE
date: string
change: StringUSDC
balance: StringUSDC
metadata?: Nullable<InAppUSDCPurchaseMetadata>
}
| {
signature: string
transactionType: USDCTransactionType.PURCHASE_CONTENT
method: USDCTransactionMethod.SEND | USDCTransactionMethod.RECEIVE
date: string
change: StringUSDC
balance: StringUSDC
metadata?: Nullable<USDCPurchaseContentMetadata>
}
| {
signature: string
transactionType: USDCTransactionType.TRANSFER
method: USDCTransactionMethod.SEND | USDCTransactionMethod.RECEIVE
date: string
change: StringUSDC
balance: StringUSDC
metadata?: Nullable<USDCTransferMetadata>
}
4 changes: 3 additions & 1 deletion packages/common/src/utils/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,13 @@ export const ceilingBNUSDCToNearestCent = (value: BNUSDC): BNUSDC => {
/** Formats a USDC wei string (full precision) to a fixed string suitable for
display as a dollar amount. Note: will lose precision by rounding _up_ to nearest cent */
export const formatUSDCWeiToUSDString = (amount: StringUSDC, precision = 2) => {
// remove negative sign if present.
const amountPos = amount.replace('-', '')
// Since we only need two digits of precision, we will multiply up by 1000
// with BN, divide by $1 Wei, ceiling up to the nearest cent,
// and then convert to JS number and divide back down before formatting to
// two decimal places.
const cents = formatUSDCWeiToNumber(new BN(amount) as BNUSDC)
const cents = formatUSDCWeiToNumber(new BN(amountPos) as BNUSDC)
return formatNumberCommas(cents.toFixed(precision))
}

Expand Down
9 changes: 8 additions & 1 deletion packages/web/src/pages/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ import {
CHAT_PAGE,
PROFILE_PAGE_AI_ATTRIBUTED_TRACKS,
PURCHASES_PAGE,
SALES_PAGE
SALES_PAGE,
WITHDRAWALS_PAGE
} from 'utils/route'
import { getTheme as getSystemTheme } from 'utils/theme/theme'

Expand All @@ -170,6 +171,7 @@ import { FbSharePage } from './fb-share-page/FbSharePage'
import FollowersPage from './followers-page/FollowersPage'
import FollowingPage from './following-page/FollowingPage'
import { PurchasesPage, SalesPage } from './purchases-and-sales'
import { WithdrawalsPage } from './purchases-and-sales/WithdrawalsPage'
import SettingsPage from './settings-page/SettingsPage'
import { SubPage } from './settings-page/components/mobile/SettingsPage'
import SmartCollectionPage from './smart-collection/SmartCollectionPage'
Expand Down Expand Up @@ -725,6 +727,11 @@ class App extends Component {
/>
<Route exact path={PURCHASES_PAGE} component={PurchasesPage} />
<Route exact path={SALES_PAGE} component={SalesPage} />
<Route
exact
path={WITHDRAWALS_PAGE}
component={WithdrawalsPage}
/>
<Route
exact
path={CHAT_PAGE}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import BN from 'bn.js'
import { Icon } from 'components/Icon'
import { Text } from 'components/typography'
import { useGoToRoute } from 'hooks/useGoToRoute'
import { SALES_PAGE } from 'utils/route'
import { SALES_PAGE, WITHDRAWALS_PAGE } from 'utils/route'

import styles from './USDCCard.module.css'

Expand All @@ -45,13 +45,11 @@ export const USDCCard = ({ balance }: { balance: BNUSDC }) => {
const menuItems: PopupMenuItem[] = [
{
text: messages.salesSummary,
// TODO: link to sales page https://linear.app/audius/issue/PAY-1763/wire-up-salespurchases-pages-on-artist-dashboard
onClick: () => goToRoute(SALES_PAGE)
},
{
text: messages.withdrawalHistory,
// TODO: link to withdraw history page https://linear.app/audius/issue/PAY-1763/wire-up-salespurchases-pages-on-artist-dashboard
onClick: () => {}
onClick: () => goToRoute(WITHDRAWALS_PAGE)
}
]

Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/pages/purchases-and-sales/PurchasesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
PurchasesTableSortDirection,
PurchasesTableSortMethod
} from './PurchasesTable'
import { NoPurchasesContent } from './components/NoPurchasesContent'
import { NoTransactionsContent } from './components/NoTransactionsContent'

const { getUserId } = accountSelectors

Expand Down Expand Up @@ -69,7 +69,7 @@ const NoPurchases = () => {
}, [dispatch])

return (
<NoPurchasesContent
<NoTransactionsContent
headerText={messages.noPurchasesHeader}
bodyText={messages.noPurchasesBody}
ctaText={messages.findSongs}
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/pages/purchases-and-sales/SalesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
SalesTableSortDirection,
SalesTableSortMethod
} from './SalesTable'
import { NoPurchasesContent } from './components/NoPurchasesContent'
import { NoTransactionsContent } from './components/NoTransactionsContent'

const { getUserId } = accountSelectors

Expand Down Expand Up @@ -67,7 +67,7 @@ const NoSales = () => {
dispatch(pushRoute(UPLOAD_PAGE))
}, [dispatch])
return (
<NoPurchasesContent
<NoTransactionsContent
headerText={messages.noSalesHeader}
bodyText={messages.noSalesBody}
ctaText={messages.upload}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.container {
display: flex;
flex-direction: column;
position: relative;
}
Loading

0 comments on commit 27089bd

Please sign in to comment.