This repository has been archived by the owner on Oct 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[C-1553] Refactor token-dashboard sagas (#2340)
- Loading branch information
1 parent
4553262
commit d64e1a9
Showing
17 changed files
with
570 additions
and
574 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
packages/web/src/store/token-dashboard/associateNewWallet.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.