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

Commit

Permalink
[C-1553] Refactor token-dashboard sagas (#2340)
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanjeffers authored Dec 3, 2022
1 parent 4553262 commit d64e1a9
Show file tree
Hide file tree
Showing 17 changed files with 570 additions and 574 deletions.
95 changes: 54 additions & 41 deletions packages/common/src/store/pages/token-dashboard/slice.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// @ts-nocheck
import { createSlice, PayloadAction } from '@reduxjs/toolkit'

import { Nullable } from 'utils/typeUtils'
Expand All @@ -10,22 +9,27 @@ import {
AssociatedWallets,
CanReceiveWAudio,
ConfirmRemoveWalletAction,
InputSendDataAction,
TokenDashboardPageModalState,
TokenDashboardState
} from './types'

const initialConfirmingWallet = {
wallet: null,
chain: null,
balance: null,
collectibleCount: null,
signature: null
}

const initialState: TokenDashboardState = {
modalState: null,
modalVisible: false,
discordCode: null,
associatedWallets: {
status: null,
connectedEthWallets: null,
confirmingWallet: {
wallet: null,
chain: null,
balance: null,
collectibleCount: null
},
confirmingWallet: initialConfirmingWallet,
connectedSolWallets: null,
errorMessage: null,
removeWallet: {
Expand Down Expand Up @@ -141,12 +145,7 @@ const slice = createSlice({
} else if (chain === Chain.Eth) {
state.associatedWallets.connectedEthWallets = associatedWallets
}
state.associatedWallets.confirmingWallet = {
wallet: null,
chain: null,
balance: null,
collectibleCount: null
}
state.associatedWallets.confirmingWallet = initialConfirmingWallet
state.associatedWallets.status = null
},
pressConnectWallets: (state) => {
Expand All @@ -158,10 +157,21 @@ const slice = createSlice({
state.associatedWallets.removeWallet.wallet = null
state.associatedWallets.errorMessage = null
},
addWallet: (state) => {
state.associatedWallets.status = 'Connecting'
state.associatedWallets.errorMessage = null
},
connectNewWallet: (state) => {
state.associatedWallets.status = 'Connecting'
state.associatedWallets.errorMessage = null
},
addConnectedWallet: (
state,
action: PayloadAction<{ signature: string; publicKey: string }>
) => {
state.associatedWallets.confirmingWallet.signature =
action.payload.signature
},
setIsConnectingWallet: (
state,
{
Expand All @@ -182,30 +192,37 @@ const slice = createSlice({
},
setWalletAddedConfirmed: (
state,
{
payload: { wallet, balance, chain, collectibleCount }
}: PayloadAction<{
wallet: string
balance: BNWei
chain: Chain
collectibleCount: number
}>
action: PayloadAction<
Partial<{
wallet: string
balance: BNWei
chain: Chain
collectibleCount: number
}>
>
) => {
if (chain === Chain.Sol) {
state.associatedWallets.connectedSolWallets = (
state.associatedWallets.connectedSolWallets || []
).concat({ address: wallet, balance, collectibleCount })
} else if (chain === Chain.Eth) {
state.associatedWallets.connectedEthWallets = (
state.associatedWallets.connectedEthWallets || []
).concat({ address: wallet, balance, collectibleCount })
}
state.associatedWallets.confirmingWallet = {
wallet: null,
chain: null,
balance: null,
collectibleCount: null
const confirmingWallet = state.associatedWallets.confirmingWallet
const newWallet = { ...confirmingWallet, ...action.payload }
const { chain, wallet, balance, collectibleCount } = newWallet
switch (chain) {
case Chain.Sol: {
state.associatedWallets.connectedSolWallets?.push({
address: wallet!,
balance,
collectibleCount: collectibleCount ?? 0
})
break
}
case Chain.Eth: {
state.associatedWallets.connectedEthWallets?.push({
address: wallet!,
balance,
collectibleCount: collectibleCount ?? 0
})
}
}

state.associatedWallets.confirmingWallet = initialConfirmingWallet
state.associatedWallets.status = 'Confirmed'
},
requestRemoveWallet: (
Expand Down Expand Up @@ -260,12 +277,7 @@ const slice = createSlice({
state.associatedWallets.removeWallet.status = null
state.associatedWallets.removeWallet.wallet = null
state.associatedWallets.removeWallet.chain = null
state.associatedWallets.confirmingWallet = {
wallet: null,
chain: null,
balance: null,
collectibleCount: null
}
state.associatedWallets.confirmingWallet = initialConfirmingWallet
state.associatedWallets.status = null
},
preloadWalletProviders: (_state) => {},
Expand All @@ -276,6 +288,7 @@ const slice = createSlice({
})

export const {
addWallet,
setModalState,
setModalVisibility,
pressReceive,
Expand Down
2 changes: 1 addition & 1 deletion packages/common/src/store/pages/token-dashboard/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export type InputSendDataAction = PayloadAction<{
}>

export type AssociatedWalletsState = {
status: Nullable<'Connecting' | 'Confirming' | 'Confirmed'>
status: Nullable<'Connecting' | 'Connected' | 'Confirming' | 'Confirmed'>
connectedEthWallets: Nullable<AssociatedWallets>
connectedSolWallets: Nullable<AssociatedWallets>
confirmingWallet: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ const NavigationContainer = (props: NavigationContainerProps) => {
WalletConnect: {
initialRouteName: 'Wallets',
screens: {
Wallets: 'wallets',
ConfirmWalletConnection: 'wallet-connect',
ConfirmSignMessage: 'wallet-sign-message'
Wallets: 'wallets'
}
},
Feed: 'feed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ const useStyles = makeStyles(({ spacing, palette }) => ({
linkedWalletLogo: {
marginRight: spacing(2)
},
linkedWalletActions: {
width: spacing(7)
},
chainIcon: {
borderWidth: 1,
borderColor: palette.neutralLight7,
Expand Down Expand Up @@ -263,7 +260,6 @@ export const LinkedWallets = () => {
>
{messages.audio}
</Text>
<View />
</View>
<FlatList
renderItem={({ item }) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
fetchSolanaCollectiblesForWallets
} from 'common/store/profile/sagas'
import { waitForBackendAndAccount } from 'utils/sagaHelpers'

const { fetchAssociatedWallets, setAssociatedWallets } =
tokenDashboardPageActions

Expand Down
93 changes: 93 additions & 0 deletions packages/web/src/store/token-dashboard/addWalletToUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
accountSelectors,
cacheActions,
getContext,
Kind,
tokenDashboardPageActions,
User,
walletActions
} from '@audius/common'
import { call, put, select } from 'typed-redux-saga'

import { requestConfirmation } from 'common/store/confirmer/actions'
import { confirmTransaction } from 'common/store/confirmer/sagas'

import { getAccountMetadataCID } from './getAccountMetadataCID'
const { getUserId } = accountSelectors
const { getBalance } = walletActions
const { setWalletAddedConfirmed, updateWalletError } = tokenDashboardPageActions

const CONNECT_WALLET_CONFIRMATION_UID = 'CONNECT_WALLET'

export function* addWalletToUser(
updatedMetadata: User,
disconnect: () => Generator
) {
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
const accountUserId = yield* select(getUserId)
if (!accountUserId) return

function* transactMetadata() {
const result = yield* call(
audiusBackendInstance.updateCreator,
updatedMetadata,
accountUserId!
)
if (!result) {
throw new Error(
`Could not confirm connect wallet for account user id ${accountUserId}`
)
}
const { blockHash, blockNumber } = result

const confirmed = yield* call(confirmTransaction, blockHash, blockNumber)
if (!confirmed) {
throw new Error(
`Could not confirm connect wallet for account user id ${accountUserId}`
)
}
}

function* onSuccess() {
// Update the user's balance w/ the new wallet
yield* put(getBalance())

yield* put(setWalletAddedConfirmed({}))

const updatedCID = yield* call(getAccountMetadataCID)

if (updatedCID) {
yield* put(
cacheActions.update(Kind.USERS, [
{
id: accountUserId,
metadata: { metadata_multihash: updatedCID }
}
])
)
}

// Disconnect the web3 instance because after we've linked, we no longer need it
yield* call(disconnect)
}

function* onError() {
yield* put(
updateWalletError({
errorMessage:
'An error occured while connecting a wallet with your account.'
})
)
// Disconnect the web3 instance in the event of an error, we no longer need it
yield* call(disconnect)
}

yield* put(
requestConfirmation(
CONNECT_WALLET_CONFIRMATION_UID,
transactMetadata,
onSuccess,
onError
)
)
}
61 changes: 61 additions & 0 deletions packages/web/src/store/token-dashboard/associateNewWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
accountSelectors,
Chain,
getContext,
newUserMetadata,
tokenDashboardPageActions,
tokenDashboardPageSelectors
} from '@audius/common'
import { call, put, select } from 'typed-redux-saga'

import { upgradeToCreator } from 'common/store/cache/users/sagas'
import { fetchServices } from 'common/store/service-selection/slice'
const { getAccountUser } = accountSelectors
const { updateWalletError } = tokenDashboardPageActions
const { getConfirmingWallet } = tokenDashboardPageSelectors

export function* associateNewWallet(signature: string) {
const { wallet, chain } = yield* select(getConfirmingWallet)
if (!wallet || !chain) return
const audiusBackendInstance = yield* getContext('audiusBackendInstance')
const userMetadata = yield* select(getAccountUser)
let updatedMetadata = newUserMetadata({ ...userMetadata })

if (
!updatedMetadata.creator_node_endpoint ||
!updatedMetadata.metadata_multihash
) {
yield* put(fetchServices())
const upgradedToCreator = yield* call(upgradeToCreator)
if (!upgradedToCreator) {
yield* put(
updateWalletError({
errorMessage:
'An error occured while connecting a wallet with your account.'
})
)
return
}
const updatedUserMetadata = yield* select(getAccountUser)
updatedMetadata = newUserMetadata({ ...updatedUserMetadata })
}

const currentWalletSignatures = yield* call(
chain === Chain.Eth
? audiusBackendInstance.fetchUserAssociatedEthWallets
: audiusBackendInstance.fetchUserAssociatedSolWallets,
updatedMetadata
)

const associatedWallets = {
...currentWalletSignatures,
[wallet]: { signature }
}

const associatedWalletsKey =
chain === Chain.Eth ? 'associated_wallets' : 'associated_sol_wallets'

updatedMetadata[associatedWalletsKey] = associatedWallets

return updatedMetadata
}
Loading

0 comments on commit d64e1a9

Please sign in to comment.