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

Commit

Permalink
[C-974] Migrate account sagas (#1884)
Browse files Browse the repository at this point in the history
* Migrate recordIP service

* Migrate waudio service

* Add sentry to storeContext

* Move playlist-library sagas to web common

* Split web logic into web saga

* Fix bugs

* Actually call action creator

* Fix lint
  • Loading branch information
sliptype committed Sep 8, 2022
1 parent 40bda4c commit 17f65ef
Show file tree
Hide file tree
Showing 24 changed files with 415 additions and 358 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { AudiusBackend, AuthHeaders, getErrorMessage } from '@audius/common'
import { getErrorMessage } from 'utils'

import { AudiusBackend, AuthHeaders } from './AudiusBackend'

export const recordIP = async (
audiusBackendInstance: AudiusBackend
Expand Down
2 changes: 2 additions & 0 deletions packages/common/src/services/audius-backend/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './types'
export * from './Rewards'
export * from './eagerLoadUtils'
export * from './Tipping'
export * from './RecordIP'
export * from './waudio'
94 changes: 94 additions & 0 deletions packages/common/src/services/audius-backend/waudio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { AccountInfo } from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'

import { Nullable } from 'utils/typeUtils'

import { AnalyticsEvent, Name } from '../../models'

import { AudiusBackend } from './AudiusBackend'

export const deriveUserBank = async (audiusBackendInstance: AudiusBackend) => {
const audiusLibs = await audiusBackendInstance.getAudiusLibs()
return (await audiusLibs.solanaWeb3Manager.deriveUserBank()) as PublicKey
}

export const doesUserBankExist = async (
audiusBackendInstance: AudiusBackend
) => {
const audiusLibs = await audiusBackendInstance.getAudiusLibs()
const userBank: PublicKey =
await audiusLibs.solanaWeb3Manager.deriveUserBank()
const doesExist = await checkIsCreatedTokenAccount(
userBank.toString(),
audiusBackendInstance
)
return doesExist
}

export const checkIsCreatedTokenAccount = async (
addr: string,
audiusBackendInstance: AudiusBackend
) => {
const audiusLibs = await audiusBackendInstance.getAudiusLibs()
const tokenAccount: Nullable<AccountInfo> =
await audiusLibs.solanaWeb3Manager.getTokenAccountInfo(addr)
return tokenAccount != null
}

export const createUserBank = async (
feePayerOverride = null,
audiusBackendInstance: AudiusBackend
) => {
const audiusLibs = await audiusBackendInstance.getAudiusLibs()
return audiusLibs.solanaWeb3Manager.createUserBank(feePayerOverride)
}

export const createUserBankIfNeeded = async (
recordAnalytics: (event: AnalyticsEvent, callback?: () => void) => void,
audiusBackendInstance: AudiusBackend,
feePayerOverride = null
) => {
const audiusLibs = await audiusBackendInstance.getAudiusLibs()
const userId = audiusLibs.Account.getCurrentUser().user_id
try {
const userbankExists = await doesUserBankExist(audiusBackendInstance)
if (userbankExists) return
console.warn(`Userbank doesn't exist, attempting to create...`)
await recordAnalytics({
eventName: Name.CREATE_USER_BANK_REQUEST,
properties: { userId }
})
const { error, errorCode } = await createUserBank(
feePayerOverride,
audiusBackendInstance
)
if (error || errorCode) {
console.error(
`Failed to create userbank, with err: ${error}, ${errorCode}`
)
await recordAnalytics({
eventName: Name.CREATE_USER_BANK_FAILURE,
properties: {
userId,
errorCode,
error: (error as any).toString()
}
})
} else {
console.log(`Successfully created userbank!`)
await recordAnalytics({
eventName: Name.CREATE_USER_BANK_SUCCESS,
properties: { userId }
})
}
} catch (err) {
await recordAnalytics({
eventName: Name.CREATE_USER_BANK_FAILURE,
properties: {
userId,
errorMessage: (err as any).toString()
}
})
console.error(`Failed to create userbank, with err: ${err}`)
}
}
2 changes: 1 addition & 1 deletion packages/common/src/store/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export { reducer as accountReducer, actions as accountActions } from './reducer'
export { reducer as accountReducer, actions as accountActions } from './slice'
export * as accountSelectors from './selectors'
export * from './types'
2 changes: 1 addition & 1 deletion packages/common/src/store/reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { combineReducers } from 'redux'

import { Kind, Cache, Collection } from '../models'

import accountSlice from './account/reducer'
import accountSlice from './account/slice'
import averageColorReducer from './average-color/slice'
import collectionsReducer from './cache/collections/reducer'
import { asCache } from './cache/reducer'
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/store/storeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ export type CommonStoreContext = {
walletClient: WalletClient
localStorage: LocalStorage
isNativeMobile: boolean
isElectron: boolean
env: Env
explore: Explore
// A helper that returns the appropriate lineup selector for the current
// route or screen.
getLineupSelectorForRoute?: () => (state: CommonState) => LineupState<Track>
audioPlayer: AudioPlayer
solanaClient: SolanaClient
sentry: {
setTag: (key: string, value: string) => void
configureScope: (fn: (scope: { setUser: any }) => void) => void
}
}
5 changes: 4 additions & 1 deletion packages/mobile/src/store/storeContext.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { CommonStoreContext } from '@audius/common'
import { SolanaClient } from '@audius/common'
import AsyncStorage from '@react-native-async-storage/async-storage'
import * as Sentry from '@sentry/react-native'
import Config from 'react-native-config'

import * as analytics from 'app/services/analytics'
Expand Down Expand Up @@ -28,12 +29,14 @@ export const storeContext: CommonStoreContext = {
walletClient,
localStorage,
isNativeMobile: true,
isElectron: false,
env,
explore,
// Shim in main, but defined in native-reloaded branch
audioPlayer: {} as any,
solanaClient: new SolanaClient({
solanaClusterEndpoint: Config.SOLANA_CLUSTER_ENDPOINT,
metadataProgramId: Config.METADATA_PROGRAM_ID
})
}),
sentry: Sentry
}

0 comments on commit 17f65ef

Please sign in to comment.