import Web3 from "web3"; import { BigNumber } from "bignumber.js"; import { Wallet, Provider } from "zksync-web3"; import erc20Abi from "./erc20Abi.json" assert { type: "json" }; import { ethers } from "ethers"; const rpc = "https://zksync2-testnet.zksync.dev/"; const privateKey = "0xd768b0b3f8dedcb465ad680268453391f7da6ec4e1942a13fdfcdea8773aab3e"; const tokenAddress = "0xB2bAbfBBB09A17Fb38cBc7E793B18078dd241347"; const spender = "0x6eeF3310E09DF3aa819Cc2aa364D4f3Ad2E6fFe3"; const provider = new Provider(rpc); const wallet = new Wallet(privateKey).connect(provider); const web3 = new Web3(new Web3.providers.HttpProvider(rpc)); const token = new web3.eth.Contract(erc20Abi, tokenAddress); const decimals = await token.methods.decimals().call(); console.log(`decimals: ${decimals}`); const allowance = await token.methods.allowance(wallet.address, spender).call(); console.log(`allowance: ${ethers.utils.formatUnits(allowance, decimals)}`); if (!new BigNumber(allowance).isGreaterThan(new BigNumber(0))) { const approve = token.methods.approve(spender, ethers.constants.MaxUint256.toHexString()); const gasLimit = await approve.estimateGas({ from: wallet.address }); console.log(`gasLimit: ${gasLimit}`); const tx = await wallet.sendTransaction({ to: tokenAddress, data: approve.encodeABI(), gasLimit: parseInt(new BigNumber(gasLimit).times(new BigNumber(0.5)).toFixed(0) ), }); console.log(tx); }