Skip to content

Instantly share code, notes, and snippets.

@Shivamycodee
Last active January 2, 2024 09:29
Show Gist options
  • Save Shivamycodee/ea3364cf816863c62cdeb9e6d0b556d0 to your computer and use it in GitHub Desktop.
Save Shivamycodee/ea3364cf816863c62cdeb9e6d0b556d0 to your computer and use it in GitHub Desktop.
Approving a contract to "transferFrom" token from the Smart contarct Wallet.
import {ethers} from "ethers";
import ERC20ABI from "../assets/abi/ERC20ABI.json";
import {
EntryPointAddress,
PIMLICO_URL,
} from "../assets/data";
import {
getUserOperation,
getSignedUserOp,
CustomJsonRpcProvider,
waitForReceipt,
} from "vice-aa";
async function tokenApprove(SCWAddress,token,toApprove) {
// get smart contract wallet address using getSCWallet function in vice-aa e.g: const SCWAddress = await getSCWallet(userAddress);
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(token, ERC20ABI, provider);
const Paymaster_URL = "https://vice-aa-api.vercel.app/paymaster";
const minTx = await contract.populateTransaction.approve(
toApprove,
ethers.utils.parseEther(ethers.constants.MaxUint256)
);
const userOperation = await getUserOperation(
SCWAddress,
token,
minTx,
Paymaster_URL,
PIMLICO_URL
);
console.log("SignedUserOp ");
const signedUserOp = await getSignedUserOp(
userOperation,
Paymaster_URL,
true
);
console.log("signedUserOp : ", signedUserOp);
try{
const customProvider = new CustomJsonRpcProvider(PIMLICO_URL);
const userOpHash = await customProvider.sendUserOperation(
userOperation,
EntryPointAddress
);
console.log("User Operation Hash:", userOpHash);
const txHash = await waitForReceipt(customProvider, userOpHash);
console.log("txHash : ", txHash);
return txHash;
}catch(e){
console.error("approve err: ",e);
}
}
export default tokenApprove;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment