-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lucid refactor, small management components refactor, scaffolding for…
… utxo selection
- Loading branch information
Showing
21 changed files
with
649 additions
and
176 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useEffect, useState, createContext, useContext, useRef } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import { Emulator, Lucid, generateSeedPhrase } from 'lucid-cardano' | ||
import { RootState } from '../app/store' | ||
import { Wallet, addWallet, removeWallet } from '../features/management/managementSlice' | ||
|
||
interface LucidContextState { | ||
lucid: Lucid | null | ||
isLucidLoading: boolean | ||
} | ||
|
||
const LucidContext = createContext<LucidContextState>({ | ||
lucid: null, | ||
isLucidLoading: false, | ||
}) | ||
|
||
export const LucidProvider = ({ children }: { children: React.ReactNode }) => { | ||
const [lucid, setLucid] = useState<Lucid | null>(null) | ||
const [isLucidLoading, setIsLucidLoading] = useState(false) | ||
const [genesisWalletOrUndefined, setGenesisWallet] = useState<Wallet | undefined>(undefined) | ||
const lucidConfig = useSelector((state: RootState) => state.lucid) | ||
const wallets = useSelector((state: RootState) => state.management.wallets) | ||
const dispatch = useDispatch() | ||
|
||
const areSideEffectsSafe = useRef(true) // is this really the best way to prevent duplicate effect invocations? | ||
|
||
useEffect(() => { | ||
let genesisWallet = genesisWalletOrUndefined | ||
setIsLucidLoading(true) | ||
|
||
const initializeLucid = async () => { | ||
const network = lucidConfig.network === "Emulator" ? "Custom" : lucidConfig.network | ||
if (lucid && lucid.network === network) { | ||
// eventually we need to handle provider changes here | ||
return | ||
} | ||
|
||
const lucidInstance = await Lucid.new(undefined, network) | ||
|
||
let genesisSeed: string | undefined = undefined | ||
let genesisWalletAddress = '' | ||
if (lucidConfig.network === "Emulator" && wallets.length === 0) { | ||
// need to have SOME wallet to provide the emulator provider | ||
genesisSeed = generateSeedPhrase() | ||
lucidInstance.selectWalletFromSeed(genesisSeed) | ||
genesisWalletAddress = await lucidInstance.wallet.address() | ||
} else if (wallets.length > 0) { | ||
lucidInstance.selectWalletFromSeed(wallets[0].seed) | ||
} | ||
|
||
if (lucidConfig.network === "Emulator") { | ||
if (areSideEffectsSafe.current) { | ||
areSideEffectsSafe.current = false | ||
genesisWallet = { | ||
address: genesisWalletAddress, | ||
seed: genesisSeed!! | ||
} | ||
|
||
dispatch(addWallet(genesisWallet)) | ||
setGenesisWallet(genesisWallet) | ||
|
||
lucidInstance.provider = new Emulator([{ | ||
address: genesisWallet?.address || '', | ||
assets: { | ||
lovelace: 20000000000n | ||
} | ||
}]) | ||
|
||
setLucid(lucidInstance) | ||
setIsLucidLoading(false) | ||
} else { | ||
areSideEffectsSafe.current = true | ||
} | ||
} | ||
} | ||
|
||
initializeLucid() | ||
|
||
return | ||
}, [lucidConfig]) | ||
|
||
return ( | ||
<LucidContext.Provider value={{ lucid, isLucidLoading }}> | ||
{children} | ||
</LucidContext.Provider> | ||
) | ||
} | ||
|
||
export function useLucid() { | ||
return useContext(LucidContext) | ||
} |
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,32 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit' | ||
import { Network } from 'lucid-cardano' | ||
|
||
|
||
export interface LucidState { | ||
network: Network | "Emulator" | ||
isLoading: boolean | ||
} | ||
|
||
const initialState: LucidState = { | ||
network: "Emulator", | ||
isLoading: true | ||
} | ||
|
||
const lucidSlice = createSlice({ | ||
name: 'lucid', | ||
initialState, | ||
reducers: { | ||
setNetwork(state, action:PayloadAction<Network | "Emulator">) { | ||
state.network = action.payload | ||
}, | ||
setIsLucidLoading(state, action: PayloadAction<boolean>) { | ||
state.isLoading = action.payload | ||
} | ||
} | ||
}) | ||
|
||
export const { | ||
setNetwork, | ||
setIsLucidLoading, | ||
} = lucidSlice.actions | ||
export default lucidSlice.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
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
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.