| 12345678910111213141516171819202122232425262728293031 |
- 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 = "0x0faF6df7054946141266420b43783387A78d82A9";
- const spender = "0x96c2Cf9edbEA24ce659EfBC9a6e3942b7895b5e8";
- 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);
- }
|