-
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
23 changed files
with
217 additions
and
45 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,4 @@ | ||
import { useDispatch } from "react-redux" | ||
import { AppDispatch } from "#/types" | ||
|
||
export const useAppDispatch = useDispatch.withTypes<AppDispatch>() |
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,4 @@ | ||
import { useSelector } from "react-redux" | ||
import { RootState } from "#/types" | ||
|
||
export const useAppSelector = useSelector.withTypes<RootState>() |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { combineReducers } from "@reduxjs/toolkit" | ||
import * as combinedSlices from "./slices" | ||
|
||
export default combineReducers({ | ||
export const reducer = combineReducers({ | ||
...combinedSlices, | ||
}) |
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,3 @@ | ||
import { RootState } from "#/types" | ||
|
||
export const selectBtcUsdPrice = (state: RootState) => state.btcReducer.usdPrice |
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 @@ | ||
export * from "./btc.selector" |
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,27 @@ | ||
import { createSlice } from "@reduxjs/toolkit" | ||
import { BtcState } from "#/types" | ||
import { fetchBTCPriceUSD } from "../thunks" | ||
|
||
const initialState: BtcState = { | ||
isLoadingPriceUSD: false, | ||
usdPrice: 0, | ||
} | ||
|
||
// Store Bitcoin data such as balance, balance in usd and other related data to Bitcoin chain. | ||
const btcSlice = createSlice({ | ||
name: "btc", | ||
initialState, | ||
reducers: {}, | ||
extraReducers: (builder) => { | ||
builder.addCase(fetchBTCPriceUSD.pending, (state) => { | ||
state.isLoadingPriceUSD = true | ||
}) | ||
builder.addCase(fetchBTCPriceUSD.fulfilled, (state, action) => { | ||
state.isLoadingPriceUSD = false | ||
state.usdPrice = action.payload | ||
}) | ||
}, | ||
}) | ||
|
||
export const btcActions = btcSlice.actions | ||
export const btcReducer = btcSlice.reducer |
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 |
---|---|---|
@@ -1 +1 @@ | ||
export * from "./todo.slice" | ||
export { btcReducer } from "./btc.slice" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,10 @@ | ||
import { configureStore } from "@reduxjs/toolkit" | ||
import { preloadedState } from "./state" | ||
import combinedReducer from "./reducer" | ||
import combinedMiddleware from "./middleware" | ||
import { reducer } from "./reducer" | ||
|
||
export const reduxStore = configureStore({ | ||
reducer: combinedReducer, | ||
export const store = configureStore({ | ||
reducer, | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware(combinedMiddleware), | ||
preloadedState, | ||
devTools: process.env.NODE_ENV !== "production", | ||
devTools: true | ||
}) |
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 @@ | ||
import { BITCOIN } from "#/constants"; | ||
import { fetchCryptoCurrencyPriceUSD } from "#/utils"; | ||
import { createAsyncThunk } from "@reduxjs/toolkit"; | ||
|
||
export const fetchBTCPriceUSD = createAsyncThunk( | ||
"btc/fetchBTCPriceUSD", | ||
async () => await fetchCryptoCurrencyPriceUSD(BITCOIN) | ||
) |
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 @@ | ||
export * from "./btc.thunk" |
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,4 @@ | ||
export interface BtcState { | ||
isLoadingPriceUSD: boolean | ||
usdPrice: number | ||
} |
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,4 @@ | ||
import { store } from "#/redux/store" | ||
|
||
export type RootState = ReturnType<typeof store.getState> | ||
export type AppDispatch = typeof store.dispatch |
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,81 @@ | ||
// export const toUsdBalance = (token: TokenState) => { | ||
// const amount = BigNumber.from(10) | ||
// .pow(18 - (token.decimals ?? 18)) // cast all to 18 decimals | ||
// .mul(BigNumber.from(token.balance)) | ||
// .toString() | ||
|
||
// return getUsdBalance(amount, token.usdConversion) | ||
// } | ||
// import { FixedNumber } from "@ethersproject/bignumber" | ||
|
||
// export const toUsdBalance = ( | ||
// balance: string | number, | ||
// usdConversion: number | ||
// ): FixedNumber => { | ||
// return FixedNumber.fromString(usdConversion.toString()).mulUnsafe( | ||
// FixedNumber.fromString(balance.toString()) | ||
// ) | ||
// } | ||
|
||
|
||
// import { BigNumber } from "ethers" | ||
|
||
// const toUsdBalance = (token: TokenState) => { | ||
// const amount = BigNumber.from(10) | ||
// .pow(18 - (token.decimals ?? 18)) // cast all to 18 decimals | ||
// .mul(BigNumber.from(token.balance)) | ||
// .toString() | ||
|
||
// return getUsdBalance(amount, token.usdConversion) | ||
// } | ||
|
||
// import { FixedNumber } from "@ethersproject/bignumber" | ||
|
||
// export const toUsdBalance = ( | ||
// balance: string | number, | ||
// usdConversion: number | ||
// ): FixedNumber => { | ||
// return FixedNumber.fromString(usdConversion.toString()).mulUnsafe( | ||
// FixedNumber.fromString(balance.toString()) | ||
// ) | ||
// } | ||
|
||
import { FixedNumber } from "@ethersproject/bignumber" | ||
import { formatUnits } from "@ethersproject/units" | ||
import numeral from "numeral" | ||
|
||
export const formatNumeral = (number: string | number, format = "0,00.00") => { | ||
return numeral(number).format(format) | ||
} | ||
|
||
export const formatFiatCurrencyAmount = ( | ||
amount: number | string, | ||
format = "0,00", | ||
currencySymbol = "$" | ||
) => { | ||
return formatNumeral(amount, `${currencySymbol}${format}`) | ||
} | ||
|
||
|
||
export const toUsdBalance = ( | ||
balance: string | number, | ||
usdConversion: number | ||
): FixedNumber => { | ||
console.log('### balance', FixedNumber.fromString(usdConversion.toString()).mulUnsafe( | ||
FixedNumber.fromString(balance.toString()) | ||
)) | ||
return FixedNumber.fromString(usdConversion.toString()).mulUnsafe( | ||
FixedNumber.fromString(balance.toString()) | ||
).floor() | ||
} | ||
|
||
|
||
export const getUsdBalance = ( | ||
balance: string | number, | ||
usdConversion: number | ||
): string => { | ||
console.log('### toUsdBalance(formatUnits(balance), usdConversion).toString()', toUsdBalance(formatUnits(balance), usdConversion).toString()) | ||
return formatFiatCurrencyAmount( | ||
toUsdBalance(formatUnits(balance), usdConversion).toString() | ||
) | ||
} |
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,13 @@ | ||
import axios from "axios" | ||
import { Currency } from "#/types" | ||
|
||
const coingeckoApiURL = "https://api.coingecko.com/api/v3" | ||
|
||
export const fetchCryptoCurrencyPriceUSD = async ({ | ||
name | ||
}: Currency) => { | ||
const response = await axios.get( | ||
`${coingeckoApiURL}/simple/price?ids=${name}&vs_currencies=usd` | ||
) | ||
return response.data[name.toLowerCase()].usd; | ||
} |
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.