Created
October 25, 2023 04:28
-
-
Save TABASCOatw/9fe60ec2a68f712a807d5c0dff71638e to your computer and use it in GitHub Desktop.
Taiko Jolnir Simple smart account implementation with Smart WaaS (modular SA update)
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
import React, { useState, useEffect } from 'react'; | |
import { ParticleNetwork } from '@particle-network/auth'; | |
import { ParticleProvider } from '@particle-network/provider'; | |
import { TaikoJolnir } from '@particle-network/chains'; | |
import { AAWrapProvider, SmartAccount } from '@particle-network/aa'; | |
import { ethers } from 'ethers'; | |
const config = { | |
projectId: process.env.REACT_APP_PROJECT_ID, | |
clientKey: process.env.REACT_APP_CLIENT_KEY, | |
appId: process.env.REACT_APP_APP_ID, | |
}; | |
const particle = new ParticleNetwork({ | |
...config, | |
chainName: TaikoJolnir.name, | |
chainId: TaikoJolnir.id, | |
wallet: { displayWalletEntry: true } | |
}); | |
const smartAccount = new SmartAccount(new ParticleProvider(particle.auth), { | |
...config, | |
aaOptions: { | |
simple: [{ chainId: TaikoJolnir.id, version: '1.0.0' }] | |
} | |
}); | |
const customProvider = new ethers.providers.Web3Provider(new AAWrapProvider(smartAccount), "any"); | |
particle.setERC4337({ | |
name: 'SIMPLE', | |
version: '1.0.0' | |
}); | |
const App = () => { | |
const [userInfo, setUserInfo] = useState(null); | |
const [ethBalance, setEthBalance] = useState(null); | |
useEffect(() => { | |
if (userInfo) { | |
fetchEthBalance(); | |
} | |
}, [userInfo]); | |
const fetchEthBalance = async () => { | |
const address = await smartAccount.getAddress(); | |
const balance = await customProvider.getBalance(address); | |
setEthBalance(ethers.utils.formatEther(balance)); | |
}; | |
const handleLogin = async (preferredAuthType) => { | |
const user = !particle.auth.isLogin() ? await particle.auth.login({preferredAuthType}) : particle.auth.getUserInfo(); | |
setUserInfo(user); | |
} | |
const executeUserOp = async () => { | |
const signer = customProvider.getSigner(); | |
const tx = { | |
to: "0x000000000000000000000000000000000000dEaD", | |
value: ethers.utils.parseEther("0.001"), | |
}; | |
const txResponse = await signer.sendTransaction(tx); | |
const txReceipt = await txResponse.wait(); | |
console.log('Transaction hash:', txReceipt.transactionHash); | |
}; | |
return ( | |
<div className="App"> | |
{!userInfo ? ( | |
<div> | |
<button onClick={() => handleLogin('google')}>Sign in with Google</button> | |
<button onClick={() => handleLogin('twitter')}>Sign in with Twitter</button> | |
</div> | |
) : ( | |
<div> | |
<h2>{userInfo.name}</h2> | |
<div> | |
<small>{ethBalance} ETH</small> | |
<button onClick={executeUserOp}>Execute User Operation</button> | |
</div> | |
</div> | |
)} | |
</div> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment