Created
October 28, 2023 15:44
-
-
Save TABASCOatw/3812c9a29c9fc0d5472da62f08721312 to your computer and use it in GitHub Desktop.
Ethereum Goerli Particle Smart WaaS SimpleAccount
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 { EthereumGoerli } from '@particle-network/chains'; | |
import { AAWrapProvider, SmartAccount, SendTransactionMode } 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: EthereumGoerli.name, | |
chainId: EthereumGoerli.id, | |
wallet: { displayWalletEntry: true } | |
}); | |
const smartAccount = new SmartAccount(new ParticleProvider(particle.auth), { | |
...config, | |
aaOptions: { | |
simple: [{ chainId: 5, version: '1.0.0' }] | |
} | |
}); | |
const customProvider = new ethers.providers.Web3Provider(new AAWrapProvider(smartAccount, SendTransactionMode.Gasless), "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