-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refactor): create pool accounts to hooks (#1913)
- Loading branch information
Ross Bulat
authored
Jan 31, 2024
1 parent
c453dd1
commit 42c0638
Showing
14 changed files
with
144 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type { ReactNode } from 'react'; | ||
import { createContext, useContext, useState } from 'react'; | ||
import type { FavoritePoolsContextState } from './types'; | ||
import { useNetwork } from 'contexts/Network'; | ||
import { defaultFavoritePoolsContext } from './defaults'; | ||
|
||
export const FavoritePoolsContext = createContext<FavoritePoolsContextState>( | ||
defaultFavoritePoolsContext | ||
); | ||
|
||
export const useFavoritePools = () => useContext(FavoritePoolsContext); | ||
|
||
export const FavoritePoolsProvider = ({ | ||
children, | ||
}: { | ||
children: ReactNode; | ||
}) => { | ||
const { network } = useNetwork(); | ||
|
||
// Get favorite pools from local storage. | ||
const getLocalFavorites = () => { | ||
const localFavorites = localStorage.getItem(`${network}_favorite_pools`); | ||
return localFavorites !== null ? JSON.parse(localFavorites) : []; | ||
}; | ||
|
||
// Stores the user's favorite pools. | ||
const [favorites, setFavorites] = useState<string[]>(getLocalFavorites()); | ||
|
||
// Adds a favorite validator. | ||
const addFavorite = (address: string) => { | ||
const newFavorites = Object.assign(favorites); | ||
if (!newFavorites.includes(address)) { | ||
newFavorites.push(address); | ||
} | ||
|
||
localStorage.setItem( | ||
`${network}_favorite_pools`, | ||
JSON.stringify(newFavorites) | ||
); | ||
setFavorites([...newFavorites]); | ||
}; | ||
|
||
// Removes a favorite pool if they exist. | ||
const removeFavorite = (address: string) => { | ||
const newFavorites = Object.assign(favorites).filter( | ||
(validator: string) => validator !== address | ||
); | ||
localStorage.setItem( | ||
`${network}_favorite_pools`, | ||
JSON.stringify(newFavorites) | ||
); | ||
setFavorites([...newFavorites]); | ||
}; | ||
|
||
return ( | ||
<FavoritePoolsContext.Provider | ||
value={{ | ||
favorites, | ||
addFavorite, | ||
removeFavorite, | ||
}} | ||
> | ||
{children} | ||
</FavoritePoolsContext.Provider> | ||
); | ||
}; |
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,8 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
export interface FavoritePoolsContextState { | ||
favorites: string[]; | ||
addFavorite: (address: string) => void; | ||
removeFavorite: (address: string) => void; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,42 @@ | ||
// Copyright 2023 @paritytech/polkadot-staking-dashboard authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { bnToU8a, u8aConcat } from '@polkadot/util'; | ||
import BigNumber from 'bignumber.js'; | ||
import { BN } from 'bn.js'; | ||
import { EmptyH256, ModPrefix, U32Opts } from 'consts'; | ||
import { useApi } from 'contexts/Api'; | ||
|
||
export const useCreatePoolAccounts = () => { | ||
const { api, consts } = useApi(); | ||
const { poolsPalletId } = consts; | ||
|
||
// Generates pool stash and reward accounts. Assumes `poolsPalletId` is synced. | ||
const createPoolAccounts = (poolId: number) => { | ||
const poolIdBigNumber = new BigNumber(poolId); | ||
return { | ||
stash: createAccount(poolIdBigNumber, 0), | ||
reward: createAccount(poolIdBigNumber, 1), | ||
}; | ||
}; | ||
|
||
const createAccount = (poolId: BigNumber, index: number): string => { | ||
if (!api) { | ||
return ''; | ||
} | ||
return api.registry | ||
.createType( | ||
'AccountId32', | ||
u8aConcat( | ||
ModPrefix, | ||
poolsPalletId, | ||
new Uint8Array([index]), | ||
bnToU8a(new BN(poolId.toString()), U32Opts), | ||
EmptyH256 | ||
) | ||
) | ||
.toString(); | ||
}; | ||
|
||
return createPoolAccounts; | ||
}; |
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
Oops, something went wrong.