Last active
September 6, 2024 22:44
-
-
Save caffeinum/3d315291de914dd68c35d3ca045ba368 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import type { KernelAccountClient, KernelSmartAccount } from "@zerodev/sdk"; | |
import type { Account, Chain, Transport, WalletClient } from "viem"; | |
import { getPublicClient, type SupportedChainId } from "~/utils/publicClient"; | |
export function pretendWalletClient( | |
kernelAccountClient: KernelAccountClient< | |
"0x0000000071727De22E5E9d8BAf0edAc6f37da032", | |
Transport, | |
Chain, | |
KernelSmartAccount< | |
"0x0000000071727De22E5E9d8BAf0edAc6f37da032", | |
Transport, | |
Chain | |
> | |
>, | |
): WalletClient { | |
// this is just multichain wrapper on top of createPublicClient from viem | |
const publicClient = getPublicClient({ | |
chainId: kernelAccountClient.chain.id as SupportedChainId, | |
}); | |
const walletClient = publicClient as unknown as WalletClient; | |
walletClient.getAddresses = () => | |
Promise.resolve([kernelAccountClient.account.address]); | |
walletClient.account = kernelAccountClient.account as Account; | |
walletClient.sendTransaction = async (transaction) => { | |
// debugger; | |
if (!transaction.data) { | |
throw new Error("No data"); | |
} | |
if (!transaction.to) { | |
throw new Error("No to"); | |
} | |
if (transaction.value && transaction.value > 0n) { | |
throw new Error("No support for value, we cant sponsor msg.value"); | |
} | |
console.log("transaction", transaction); | |
const calldata = await kernelAccountClient.account.encodeCallData({ | |
to: transaction.to, | |
value: transaction.value ?? BigInt(0), | |
data: transaction.data, | |
}); | |
console.log("calldata", calldata); | |
const txHash = await kernelAccountClient.sendTransaction({ | |
to: transaction.to, | |
value: transaction.value ?? BigInt(0), | |
data: transaction.data, | |
}); | |
console.log("txHash", txHash); | |
return txHash; | |
}; | |
return walletClient; | |
} |
This file contains hidden or 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
import { createConfig, EVM, type SDKConfig } from "@lifi/sdk"; | |
import { useKernelAccountClient } from "../app/_components/Web3AuthProvider"; | |
import { pretendWalletClient } from "../pretendWalletClient"; | |
export function useLiFiSDK() { | |
const [lifiConfig, setLifiConfig] = useState<SDKConfig | null>(null); | |
const { kernelAccountClient } = useKernelAccountClient(); | |
const { switchChain } = useSwitchChain(); | |
useEffect(() => { | |
if (!kernelAccountClient) return; | |
const config = createConfig({ | |
integrator: "moai_cash", | |
providers: [ | |
EVM({ | |
getWalletClient: () => | |
Promise.resolve(pretendWalletClient(kernelAccountClient)), | |
switchChain: async (chainId) => { | |
// TODO: re-create kernelAccountClient with correct chain | |
return Promise.resolve(pretendWalletClient(kernelAccountClient)); | |
}, | |
}), | |
], | |
}); | |
setLifiConfig(config); | |
}, [kernelAccountClient, switchChain]); | |
return lifiConfig; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment