-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
195 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useCoretimeApi } from '@/contexts/apis'; | ||
import { ApiState } from '@/contexts/apis/types'; | ||
import { useToast } from '@/contexts/toast'; | ||
import { useInkathon } from '@scio-labs/use-inkathon'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
// React hook for fetching balance. | ||
const useBalance = () => { | ||
const { | ||
state: { api, apiState, symbol }, | ||
} = useCoretimeApi(); | ||
const { activeAccount } = useInkathon(); | ||
|
||
const [balance, setBalance] = useState(0); | ||
|
||
const { toastWarning } = useToast(); | ||
|
||
const fetchBalance = useCallback(async () => { | ||
if (api && apiState == ApiState.READY && activeAccount) { | ||
const accountData: any = ( | ||
await api.query.system.account(activeAccount.address) | ||
).toHuman(); | ||
const balance = parseFloat(accountData.data.free.toString()); | ||
setBalance(balance); | ||
|
||
if (balance === 0) { | ||
toastWarning( | ||
`The selected account does not have any ${symbol} tokens on the Coretime chain.` | ||
); | ||
} | ||
} | ||
}, [api, activeAccount, toastWarning, symbol]); | ||
|
||
useEffect(() => { | ||
fetchBalance(); | ||
}, [fetchBalance]); | ||
|
||
return balance; | ||
}; | ||
|
||
export default useBalance; |
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,102 @@ | ||
import { SalePhase } from '@/models'; | ||
import { getBlockTimestamp, parseHNString } from '@/utils/functions'; | ||
import { | ||
getCurrentPhase, | ||
getSaleEndInBlocks, | ||
getSaleProgress, | ||
getSaleStartInBlocks, | ||
} from '@/utils/sale/utils'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { Section } from '../Elements'; | ||
import { useRouter } from 'next/router'; | ||
import { ApiState } from '@/contexts/apis/types'; | ||
import { useSaleInfo } from '@/contexts/sales'; | ||
import { useCoretimeApi } from '@/contexts/apis'; | ||
|
||
// Custom hook for fetching current phase | ||
const useSalePhase = () => { | ||
const { | ||
state: { api, apiState }, | ||
} = useCoretimeApi(); | ||
const { saleInfo, config } = useSaleInfo(); | ||
|
||
const [currentPhase, setCurrentPhase] = useState<SalePhase | null>(null); | ||
|
||
const [saleEnd, setSaleEnd] = useState<number | null>(null); | ||
const [saleEndTimestamp, setSaleEndTimestamp] = useState(0); | ||
|
||
const [progress, setProgress] = useState<number | null>(0); | ||
const [saleSections, setSaleSections] = useState<Section[]>([]); | ||
|
||
const router = useRouter(); | ||
const { network } = router.query; | ||
|
||
const fetchCurrentPhase = useCallback( | ||
async (api: ApiPromise) => { | ||
const blockNumber = (await api.query.system.number()).toJSON() as number; | ||
const lastCommittedTimeslice = parseHNString( | ||
( | ||
(await api.query.broker.status()).toHuman() as any | ||
).lastCommittedTimeslice.toString() | ||
); | ||
|
||
const _saleStart = getSaleStartInBlocks(saleInfo, config); | ||
const _saleEnd = getSaleEndInBlocks( | ||
saleInfo, | ||
blockNumber, | ||
lastCommittedTimeslice, | ||
network | ||
); | ||
|
||
setSaleEnd(_saleEnd); | ||
getBlockTimestamp(api, _saleEnd).then((value: number) => | ||
setSaleEndTimestamp(value) | ||
); | ||
|
||
const progress = getSaleProgress( | ||
saleInfo, | ||
config, | ||
blockNumber, | ||
lastCommittedTimeslice, | ||
network | ||
); | ||
setProgress(progress); | ||
|
||
setCurrentPhase(getCurrentPhase(saleInfo, blockNumber)); | ||
|
||
const saleDuration = _saleEnd - _saleStart; | ||
|
||
setSaleSections([ | ||
{ name: 'Interlude', value: 0 }, | ||
{ | ||
name: 'Leadin phase', | ||
value: (config.interludeLength / saleDuration) * 100, | ||
}, | ||
{ | ||
name: 'Fixed price phase', | ||
value: | ||
((config.interludeLength + config.leadinLength) / saleDuration) * | ||
100, | ||
}, | ||
]); | ||
}, | ||
[saleInfo, config, network] | ||
); | ||
|
||
useEffect(() => { | ||
if (!api || apiState !== ApiState.READY) return; | ||
|
||
fetchCurrentPhase(api); | ||
}, [fetchCurrentPhase, api, apiState]); | ||
|
||
return { | ||
currentPhase, | ||
saleEnd, | ||
saleEndTimestamp, | ||
progress, | ||
saleSections, | ||
}; | ||
}; | ||
|
||
export default useSalePhase; |
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,30 @@ | ||
import { useCoretimeApi } from '@/contexts/apis'; | ||
import { ApiState } from '@/contexts/apis/types'; | ||
import { useSaleInfo } from '@/contexts/sales'; | ||
import { getCurrentPrice } from '@/utils/sale/utils'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
const useSalePrice = () => { | ||
const { | ||
state: { api, apiState }, | ||
} = useCoretimeApi(); | ||
const { saleInfo } = useSaleInfo(); | ||
|
||
const [currentPrice, setCurrentPrice] = useState(0); | ||
|
||
const fetchCurrentPrice = useCallback(async () => { | ||
if (api && apiState == ApiState.READY) { | ||
const blockNumber = (await api.query.system.number()).toJSON() as number; | ||
const price = getCurrentPrice(saleInfo, blockNumber); | ||
setCurrentPrice(price); | ||
} | ||
}, [api, saleInfo]); | ||
|
||
useEffect(() => { | ||
fetchCurrentPrice(); | ||
}, [fetchCurrentPrice]); | ||
|
||
return currentPrice; | ||
}; | ||
|
||
export default useSalePrice; |
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.