This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: restructure App * chore: suffix page components with "Page" * chore: add Recoil * chore: rename WalletContext to AppContext and add useContract * chore: lazy load pages * chore: upgrade React to 18.1.0
- Loading branch information
Showing
18 changed files
with
257 additions
and
254 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,19 @@ | ||
import "./index.css"; | ||
import { Routes as Router, Route, Navigate } from "react-router-dom"; | ||
import { Setup } from "src/pages/Setup"; | ||
import { Swap } from "src/pages/Swap"; | ||
import { Assets } from "src/pages/Assets"; | ||
import { Pages } from "src/types/pages"; | ||
import { Pool } from "src/pages/Pool"; | ||
import { MintToken } from "src/pages/MintToken"; | ||
import { RequireWallet } from "./components/RequireWallet"; | ||
import { MainLayout } from "./layouts/MainLayout"; | ||
import { RemoveLiquidity } from "./pages/RemoveLiquidity"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
import { RecoilRoot } from "recoil"; | ||
import { AppContextProvider } from "src/context/AppContext"; | ||
|
||
function App() { | ||
import AppRoutes from "./AppRoutes"; | ||
|
||
const { PUBLIC_URL } = process.env; | ||
|
||
export default function App() { | ||
return ( | ||
<Router> | ||
<Route element={<MainLayout />}> | ||
<Route | ||
path="*" | ||
element={<RequireWallet children={<Navigate to={Pages.assets} />} />} | ||
/> | ||
<Route path={Pages.setup} element={<Setup />} /> | ||
<Route | ||
path={Pages.assets} | ||
element={<RequireWallet children={<Assets />} />} | ||
/> | ||
<Route | ||
path={Pages.swap} | ||
element={<RequireWallet children={<Swap />} />} | ||
/> | ||
<Route | ||
path={Pages.pool} | ||
element={<RequireWallet children={<Pool />} />} | ||
/> | ||
<Route | ||
path={Pages.mintToken} | ||
element={<RequireWallet children={<MintToken />} />} | ||
/> | ||
<Route | ||
path={Pages.removeLiquidity} | ||
element={<RequireWallet children={<RemoveLiquidity />} />} | ||
/> | ||
</Route> | ||
</Router> | ||
<RecoilRoot> | ||
<BrowserRouter basename={PUBLIC_URL}> | ||
<AppContextProvider> | ||
<AppRoutes /> | ||
</AppContextProvider> | ||
</BrowserRouter> | ||
</RecoilRoot> | ||
); | ||
} | ||
|
||
export default App; |
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,46 @@ | ||
import { lazy } from "react"; | ||
import { Routes, Route, Navigate } from "react-router-dom"; | ||
import { RequireWallet } from "./components/RequireWallet"; | ||
import { MainLayout } from "./layouts/MainLayout"; | ||
import { Pages } from "./types/pages"; | ||
|
||
const SetupPage = lazy(() => import("src/pages/SetupPage")); | ||
const SwapPage = lazy(() => import("src/pages/SwapPage")); | ||
const AssetsPage = lazy(() => import("src/pages/AssetsPage")); | ||
const PoolPage = lazy(() => import("src/pages/PoolPage")); | ||
const MintTokenPage = lazy(() => import("src/pages/MintTokenPage")); | ||
const RemoveLiquidityPage = lazy(() => import("./pages/RemoveLiquidityPage")); | ||
|
||
export default function AppRoutes() { | ||
return ( | ||
<Routes> | ||
<Route element={<MainLayout />}> | ||
<Route | ||
path="*" | ||
element={<RequireWallet children={<Navigate to={Pages.assets} />} />} | ||
/> | ||
<Route path={Pages.setup} element={<SetupPage />} /> | ||
<Route | ||
path={Pages.assets} | ||
element={<RequireWallet children={<AssetsPage />} />} | ||
/> | ||
<Route | ||
path={Pages.swap} | ||
element={<RequireWallet children={<SwapPage />} />} | ||
/> | ||
<Route | ||
path={Pages.pool} | ||
element={<RequireWallet children={<PoolPage />} />} | ||
/> | ||
<Route | ||
path={Pages.mintToken} | ||
element={<RequireWallet children={<MintTokenPage />} />} | ||
/> | ||
<Route | ||
path={Pages.removeLiquidity} | ||
element={<RequireWallet children={<RemoveLiquidityPage />} />} | ||
/> | ||
</Route> | ||
</Routes> | ||
); | ||
} |
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,94 @@ | ||
import React, { PropsWithChildren, useContext, useMemo } from "react"; | ||
import { Wallet, ScriptTransactionRequest, TransactionResult } from "fuels"; | ||
import { CoinETH } from "src/lib/constants"; | ||
import { randomBytes } from "ethers/lib/utils"; | ||
import { CONTRACT_ID, FAUCET_AMOUNT, FUEL_PROVIDER_URL } from "src/config"; | ||
import { atom, useRecoilState } from "recoil"; | ||
import { persistEffect } from "src/lib/recoilEffects"; | ||
import { | ||
SwayswapContractAbi, | ||
SwayswapContractAbi__factory, | ||
} from "src/types/contracts"; | ||
|
||
interface AppContextValue { | ||
wallet: Wallet | null; | ||
contract: SwayswapContractAbi | null; | ||
createWallet: () => void; | ||
faucet: () => Promise<TransactionResult>; | ||
} | ||
|
||
const walletPrivateKeyState = atom<string | null>({ | ||
key: "privateKey", | ||
default: null, | ||
effects_UNSTABLE: [persistEffect], | ||
}); | ||
|
||
export const AppContext = React.createContext<AppContextValue | null>(null); | ||
|
||
export const useAppContext = () => useContext(AppContext)!; | ||
|
||
export const useWallet = () => { | ||
const { wallet } = useContext(AppContext)!; | ||
return wallet; | ||
}; | ||
|
||
export const useContract = () => { | ||
const { contract } = useContext(AppContext)!; | ||
return contract; | ||
}; | ||
|
||
export const AppContextProvider = ({ children }: PropsWithChildren<{}>) => { | ||
const [privateKey, setPrivateKey] = useRecoilState(walletPrivateKeyState); | ||
|
||
const wallet = useMemo(() => { | ||
if (!privateKey) return null; | ||
return new Wallet(privateKey, FUEL_PROVIDER_URL); | ||
}, [privateKey]); | ||
|
||
const contract = useMemo(() => { | ||
if (!wallet) return null; | ||
return SwayswapContractAbi__factory.connect(CONTRACT_ID, wallet); | ||
}, [wallet]); | ||
|
||
return ( | ||
<AppContext.Provider | ||
value={{ | ||
wallet, | ||
contract, | ||
createWallet: () => { | ||
const wallet = Wallet.generate({ | ||
provider: FUEL_PROVIDER_URL, | ||
}); | ||
setPrivateKey(wallet.privateKey); | ||
return wallet; | ||
}, | ||
faucet: async () => { | ||
const transactionRequest = new ScriptTransactionRequest({ | ||
gasPrice: 0, | ||
gasLimit: "0x0F4240", | ||
script: "0x24400000", | ||
scriptData: randomBytes(32), | ||
}); | ||
// @ts-ignore | ||
transactionRequest.addCoin({ | ||
id: "0x000000000000000000000000000000000000000000000000000000000000000000", | ||
assetId: CoinETH, | ||
amount: FAUCET_AMOUNT, | ||
owner: | ||
"0xf1e92c42b90934aa6372e30bc568a326f6e66a1a0288595e6e3fbd392a4f3e6e", | ||
}); | ||
transactionRequest.addCoinOutput( | ||
wallet!.address, | ||
FAUCET_AMOUNT, | ||
CoinETH | ||
); | ||
const submit = await wallet!.sendTransaction(transactionRequest); | ||
|
||
return submit.wait(); | ||
}, | ||
}} | ||
> | ||
{children} | ||
</AppContext.Provider> | ||
); | ||
}; |
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
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,7 @@ | ||
import { recoilPersist } from 'recoil-persist'; | ||
import { LocalStorageKey } from './constants'; | ||
|
||
export const persistEffect = recoilPersist({ | ||
key: `${LocalStorageKey}-state`, | ||
storage: localStorage, | ||
}).persistAtom; |
Oops, something went wrong.