-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from InvArch:yaki-tnkr-claim-fix-020124
Yaki-tnkr-claim-fix-020124
- Loading branch information
Showing
11 changed files
with
136 additions
and
91 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
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 @@ | ||
import { createContext, useContext, useState, useEffect, useCallback, ReactNode } from 'react'; | ||
import BigNumber from 'bignumber.js'; | ||
import useApi from '../hooks/useApi'; | ||
import useAccount from '../stores/account'; | ||
|
||
interface BalanceContextType { | ||
availableBalance: BigNumber; | ||
reloadAccountInfo: () => void; | ||
} | ||
|
||
const BalanceContext = createContext<BalanceContextType>({ | ||
availableBalance: new BigNumber(0), | ||
reloadAccountInfo: () => { }, | ||
}); | ||
|
||
export const BalanceProvider = ({ children }: { children: ReactNode; }) => { | ||
const [availableBalance, setAvailableBalance] = useState(new BigNumber(0)); | ||
const api = useApi(); | ||
const { selectedAccount } = useAccount(); | ||
|
||
const loadAccountInfo = useCallback(async () => { | ||
if (!selectedAccount || !api) return; | ||
const account = await api.query.system.account(selectedAccount.address); | ||
const balance = account.toPrimitive() as { data: { free: string; }; }; | ||
const locked = (await api.query.ocifStaking.ledger(selectedAccount.address)).toPrimitive() as { locked: string; }; | ||
const currentBalance = new BigNumber(balance.data.free).minus(new BigNumber(locked.locked)); | ||
|
||
setAvailableBalance(currentBalance); | ||
}, [api, selectedAccount]); | ||
|
||
useEffect(() => { | ||
loadAccountInfo(); | ||
}, [selectedAccount, api, loadAccountInfo]); | ||
|
||
return ( | ||
<BalanceContext.Provider value={{ availableBalance, reloadAccountInfo: loadAccountInfo }}> | ||
{children} | ||
</BalanceContext.Provider> | ||
); | ||
}; | ||
|
||
export const useBalance = () => useContext(BalanceContext); |
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.