Skip to content

Commit

Permalink
Migrate withdraw USDC saga to web common (#3928)
Browse files Browse the repository at this point in the history
  • Loading branch information
dharit-tan committed Aug 23, 2023
1 parent 8569801 commit 415eb1e
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ export const isValidSolDestinationAddress = async (
}
}

export const isSolWallet = async (
audiusBackendInstance: AudiusBackend,
destinationWallet: SolanaWalletAddress
) => {
const audiusLibs: AudiusLibs = await audiusBackendInstance.getAudiusLibs()
const solanaweb3 = audiusLibs.solanaWeb3Manager?.solanaWeb3
if (!solanaweb3) {
console.error('No solana web3 found')
return false
}
try {
const destination = new solanaweb3.PublicKey(destinationWallet)
return solanaweb3.PublicKey.isOnCurve(destination.toBytes())
} catch (err) {
console.log(err)
return false
}
}

export const getRootSolanaAccount = async (
audiusBackendInstance: AudiusBackend
) => {
Expand Down
1 change: 0 additions & 1 deletion apps/audius-client/packages/common/src/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
export * from './account'
export * from './average-color'
export * from './buy-usdc'
export * from './withdraw-usdc'
export * from './cache'
export * from './cast'
export * from './challenges'
Expand Down
6 changes: 2 additions & 4 deletions apps/audius-client/packages/common/src/store/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ import {
searchUsersModalReducer,
SearchUsersModalState,
ToastState,
TransactionDetailsState
TransactionDetailsState,
withdrawUSDCReducer
} from './ui'
import addToPlaylistReducer, {
AddToPlaylistState
Expand Down Expand Up @@ -127,7 +128,6 @@ import repostsUserListReducer from './user-list/reposts/reducers'
import supportingUserListReducer from './user-list/supporting/reducers'
import topSupportersUserListReducer from './user-list/top-supporters/reducers'
import wallet from './wallet/slice'
import { withdrawUSDCReducer } from './withdraw-usdc'

/**
* A function that creates common reducers. The function takes
Expand Down Expand Up @@ -255,8 +255,6 @@ export const reducers = () => ({
buyUSDC: buyUSDCReducer,
premiumContent,
purchaseContent: purchaseContentReducer,

// USDC withdrawals
withdrawUSDC: withdrawUSDCReducer,

// Collectibles
Expand Down
2 changes: 0 additions & 2 deletions apps/audius-client/packages/common/src/store/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
shareModalUISagas,
stripeModalUISagas
} from 'store/ui'
import { withdrawUSDCSagas } from 'store/withdraw-usdc'

import { playlistUpdatesSagas } from './playlist-updates'
import { CommonStoreContext } from './storeContext'
Expand All @@ -45,7 +44,6 @@ export const sagas = (_ctx: CommonStoreContext) => ({
// users: usersSagas,
account: accountSagas,
buyUSDC: buyUSDCSagas,
withdrawUSDC: withdrawUSDCSagas,
remoteConfig: remoteConfigSagas,
cast: castSagas,
premiumContent: premiumContentSagas,
Expand Down
6 changes: 6 additions & 0 deletions apps/audius-client/packages/common/src/store/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ export * from './buy-audio/types'
export * from './buy-audio/constants'
export * as buyAudioSelectors from './buy-audio/selectors'

export {
default as withdrawUSDCReducer,
actions as withdrawUSDCActions
} from './withdraw-usdc/slice'
export * as withdrawUSDCSelectors from './withdraw-usdc/selectors'

export {
default as transactionDetailsReducer,
actions as transactionDetailsActions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'

import { Status } from '../../models/Status'
import { Status } from 'models/Status'

type WithdrawUSDCState = {
withdrawStatus: Status
destinationAddress?: string
amount?: number
withdrawError?: Error
destinationError?: Error
amountError?: Error
}
Expand Down Expand Up @@ -64,6 +65,14 @@ const slice = createSlice({
beginWithdrawUSDC: (state) => {
state.withdrawStatus = Status.LOADING
},
withdrawUSDCSucceeded: (state) => {
state.withdrawError = undefined
state.withdrawStatus = Status.SUCCESS
},
withdrawUSDCFailed: (state, action: PayloadAction<{ error: Error }>) => {
state.withdrawStatus = Status.ERROR
state.withdrawError = action.payload.error
},
cleanup: () => initialState
}
})
Expand All @@ -76,6 +85,8 @@ export const {
setAmountSucceeded,
setAmountFailed,
beginWithdrawUSDC,
withdrawUSDCSucceeded,
withdrawUSDCFailed,
cleanup
} = slice.actions

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import BN from 'bn.js'
import { takeLatest } from 'redux-saga/effects'
import { call, put } from 'typed-redux-saga'

import { ErrorLevel } from 'models/ErrorReporting'
import { SolanaWalletAddress } from 'models/Wallet'
import {
withdrawUSDCActions,
withdrawUSDCSelectors,
ErrorLevel,
SolanaWalletAddress,
getTokenAccountInfo,
isValidSolDestinationAddress
} from 'services/audius-backend/solana'
import { getUSDCUserBank } from 'store/buy-usdc/utils'
import { getContext } from 'store/effects'
isSolWallet,
isValidSolDestinationAddress,
getUSDCUserBank,
getContext
} from '@audius/common'
import { PublicKey } from '@solana/web3.js'
import BN from 'bn.js'
import { takeLatest } from 'redux-saga/effects'
import { call, put, select } from 'typed-redux-saga'

import {
const {
beginWithdrawUSDC,
setAmount,
setAmountFailed,
setAmountSucceeded,
setDestinationAddress,
setDestinationAddressFailed,
setDestinationAddressSucceeded
} from './slice'
setDestinationAddressSucceeded,
withdrawUSDCFailed
} = withdrawUSDCActions
const { getWithdrawDestinationAddress } = withdrawUSDCSelectors

function* doSetAmount({ payload: { amount } }: ReturnType<typeof setAmount>) {
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
Expand Down Expand Up @@ -83,6 +89,46 @@ function* doSetDestinationAddress({
}
}

function* doWithdrawUSDC({ payload }: ReturnType<typeof beginWithdrawUSDC>) {
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
try {
// Assume destinationAddress and amount have already been validated
const destinationAddress = yield* select(getWithdrawDestinationAddress)
if (!destinationAddress) {
throw new Error('Please enter a destination address')
}
// const amount = yield* select(getWithdrawAmount)
const isDestinationSolAddress = yield* call(
isSolWallet,
audiusBackendInstance,
destinationAddress as SolanaWalletAddress
)
const destinationPubkey = new PublicKey(destinationAddress)
// Destination is an ATA
if (!isDestinationSolAddress) {
const destinationAccountInfo = yield* call(
getTokenAccountInfo,
audiusBackendInstance,
{
mint: 'usdc',
tokenAccount: destinationPubkey
}
)
// Destination account does not exist - create and fund
if (!destinationAccountInfo) {
// TODO
}
}
} catch (e: unknown) {
const reportToSentry = yield* getContext('reportToSentry')
reportToSentry({
level: ErrorLevel.Error,
error: e as Error
})
yield* put(withdrawUSDCFailed({ error: e as Error }))
}
}

function* watchSetAmount() {
yield takeLatest(setAmount, doSetAmount)
}
Expand All @@ -91,6 +137,10 @@ function* watchSetDestinationAddress() {
yield takeLatest(setDestinationAddress, doSetDestinationAddress)
}

function* watchBeginWithdrawUSDC() {
yield takeLatest(beginWithdrawUSDC, doWithdrawUSDC)
}

export default function sagas() {
return [watchSetAmount, watchSetDestinationAddress]
return [watchSetAmount, watchSetDestinationAddress, watchBeginWithdrawUSDC]
}
2 changes: 1 addition & 1 deletion apps/audius-client/packages/web/src/store/sagas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
chatSagas,
premiumContentSagas,
purchaseContentSagas,
withdrawUSDCSagas,
remoteConfigSagas,
deletePlaylistConfirmationModalUISagas as deletePlaylistConfirmationModalSagas,
duplicateAddConfirmationModalUISagas as duplicateAddConfirmationModalSagas,
Expand Down Expand Up @@ -90,6 +89,7 @@ import scrollLockSagas from 'store/application/ui/scrollLock/sagas'
import stemUploadSagas from 'store/application/ui/stemsUpload/sagas'
import themeSagas from 'store/application/ui/theme/sagas'
import userListModalSagas from 'store/application/ui/userListModal/sagas'
import withdrawUSDCSagas from 'store/application/ui/withdraw-usdc/sagas'
import errorSagas from 'store/errors/sagas'
import reachabilitySagas from 'store/reachability/sagas'
import routingSagas from 'store/routing/sagas'
Expand Down

0 comments on commit 415eb1e

Please sign in to comment.