Skip to content

Instantly share code, notes, and snippets.

@TABASCOatw
Created October 24, 2023 08:37
Show Gist options
  • Save TABASCOatw/71e93d6b48bd07c23d449e133b0f7263 to your computer and use it in GitHub Desktop.
Save TABASCOatw/71e93d6b48bd07c23d449e133b0f7263 to your computer and use it in GitHub Desktop.
Particle Native SmartAccount implementation (v1, pre-SA upgrade)
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, uiMode: 'dark' },
});
const smartAccount = new SmartAccount(new ParticleProvider(particle.auth), {
...config,
networkConfig: [
{ dappAPIKey: process.env.REACT_APP_BICONOMY_KEY, chainId: EthereumGoerli.id }
],
});
const customProvider = new ethers.providers.Web3Provider(new AAWrapProvider(smartAccount, SendTransactionMode.Gasless), "any");
particle.setERC4337(true);
let userInfo = null;
let ethBalance = null;
const fetchEthBalance = async () => {
const address = await smartAccount.getAddress();
const balance = await customProvider.getBalance(address);
ethBalance = ethers.utils.formatEther(balance);
};
const handleLogin = async (preferredAuthType) => {
userInfo = !particle.auth.isLogin() ? await particle.auth.login({preferredAuthType}) : particle.auth.getUserInfo();
if (userInfo) {
await fetchEthBalance();
}
}
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);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment