Deprecating This Week: Get ERC20 token allowance

Hello,

Could you please confirm if the deprecation of this endpoint will affect dApps that are using the Moralis Self-Hosted server?

Thank you.

Hi @zrco

The selfhosted server even use the same APIs in the server code, so yes they will be affected.:pray:

Hi @johnversus

Is there a workaround for this issue?

The suggested alternate endpoint is to use wallet token approval endpoint. Please try using it to get all the allowed approval data

Is this endpoint supported in Moralis v1? Do I need to update the Self-Hosted Server in order to enable Wallet Token Approvals?

It is not available in v1 sdk. Maybe you need to create a new cloud function with this endpoint and then call the cloud function from your client to use it with the required params.

I was able to replace the endpoint with the following workaround.

function getProviderAndSigner() {
    if (typeof window.ethereum !== 'undefined') {
      const provider = new ethers.providers.Web3Provider(window.ethereum);
      await provider.send('eth_requestAccounts', []); 
      const signer = provider.getSigner();
      return { provider, signer };
    } else {
      throw new Error('No Web3 provider found. Please install MetaMask.');
    }
  }
export async function getTokenAllowance(token_address, chainId) {
  try {
    // Get the signer from the provider
    const { signer } = await getProviderAndSigner();
    const tokenABI = [
      "function approve(address spender, uint256 amount) public returns (bool)",
      "function allowance(address owner, address spender) public view returns (uint256)"
    ];
    const tokenContract = new ethers.Contract(token_address, tokenABI, signer);
    const ownerAddress = await signer.getAddress();
    const spenderAddress = dappContractAddress(chainId); 
    const allowance = await tokenContract.allowance(ownerAddress, spenderAddress);
    return allowance.toString();
  } catch (error) {
    console.error("Error fetching token allowance:", error);
    throw new Error(`Failed to get token allowance: ${error.message}`);
  }
}