Check if wallet is approved to spend token

Hi there,

I was wondering if there’s a way to check if a token is already approved for spending via Moralis API. I’m not asking how to approve the token for spending, I’ve figured that part out via reference: How to approve a token for spending on (Uniswap router contract). I’d like my script to check first before selling a token and then decide, based on the result, to either run approval first or skip it. That way it’s not approving a token that’s already approved every time it sells the same token. Thanks for your help.

Hi,
It looks like this is what you need: https://deep-index.moralis.io/api-docs/#/token/getTokenAllowance

An example of requested url:
https://deep-index.moralis.io/api/v2/erc20/0x057Ec652A4F150f7FF94f089A38008f49a0DF88e/allowance?chain=eth&owner_address=0xd4a3BebD824189481FC45363602b83C9c7e9cbDf&spender_address=0x62AED87d21Ad0F3cdE4D147Fdcc9245401Af0044

Nice! thanks man. So if allowance returns 0 it’s not been approved yet right?

That should be the case, but you should test with a case where you know there is an approval.

I see, ok so I’m getting the following error msg. Any ideas?..

chain = "bsc"
token = Web3.toChecksumAddress("0x40619dc9f00ea34e51d96b6ec5d8a6ad75457434")

contract = web3.eth.contract(token, abi=config.pancakeswapTokenAbi)
owner_address = contract.functions.owner().call()
apprvData = 'https://deep-index.moralis.io/api/v2/erc20/' + token + '/allowance?chain=' + chain + '&owner_address=' + owner_address + '&spender_address=' + config.walletAddress3 
apprvStatusReq = requests.request("GET", apprvData, headers=config.moralisAPI)
apprvStatus = apprvStatusReq.json()['allowance']
print(apprvStatus)

Error:

Traceback (most recent call last):
  File "c:/Users/Owner/Documents/BlockchainPy/MyCodes/scratchTest.py", line 28, in <module>
    apprvStatus = apprvStatusReq.json()['allowance']
KeyError: 'allowance'

What you see if you print what is in apprvStatusReq.json()?

Both
print(apprvStatusReq) and
print(apprvStatus) returns the same error as above

That would be impossible for both of them to show the same error.

What does this code do?

print(apprvData)
apprvStatusReq = requests.request("GET", apprvData, headers=config.moralisAPI)
import json
print(json.dumps(apprvStatusReq.json(), indent=4)

Pardon me, sir you’re right. To answer your previous question…it returned { } ( empty curly)

print(json.dumps(apprvStatusReq.json(), indent=4) also returned { }

1 Like

I need an example of a contract, _owner and, _spender that has allowance in order to get you a working code example. You can also use directly web3 call to the contract to the allowance method in order to get that information.

I got this working example for web3:

const CONTRACT_ADDRESS = CONTRACT_ADDRESS!!!!;

const abi = [{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},]

let _owner = OWNER!!!!
let _spender = SPENDER!!!!
window.web3 = await Moralis.Web3.enable();
let contract_instance = new web3.eth.Contract(abi, CONTRACT_ADDRESS);
let x = await contract_instance.methods.allowance(_owner, _spender).call();
console.log(x);


Sure, for example:

contract = 0x40619dc9F00ea34e51D96b6EC5d8a6aD75457434
owner = 0x29cdf45A1cda9Fd722108b05BaaA5785DF5E98aF
spender = 0xb5C2c0A73f59508731b915c646615089e1517628

Direct Web3 call in python would be ideal.

python web3 example:

import json
import sys
from web3 import Web3

bsc = 'https://bsc-dataseed.binance.org/'
web3 = Web3(Web3.HTTPProvider(bsc))
print(web3.isConnected())


contract = '0x40619dc9F00ea34e51D96b6EC5d8a6aD75457434'
contract = web3.toChecksumAddress(contract)
abi = [{"constant":True,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":False,"stateMutability":"view","type":"function"},]
contract = web3.eth.contract(address=contract, abi=abi)

_owner = web3.toChecksumAddress("0x29cdf45A1cda9Fd722108b05BaaA5785DF5E98aF")
_spender = web3.toChecksumAddress("0xb5C2c0A73f59508731b915c646615089e1517628")

x = contract.functions.allowance(_owner, _spender).call();
print(x)

1 Like

Great! it works. However, I noticed that it returns 0 (zero) for both approved tokens and non-approved. I used pancakewap to confirm which tokens were approved or not. Any thoughts?

For me it worked fine with approved tokens, as in returning a number and not 0.

I copied and pasted the code, changed _owner to 3 other accounts that all belong to me, none of which returned anything besides a zero.

I also bought a new token and ran it before and after approving and it still returned a zero for both times. I don’t know anymore bro…

send a DM in discord and I’ll give you a working example

Ok what’s your handle on discord? Mine is BJ #6448

Mine is Crypto Kid#9642

You can find link for Moralis discord channel in https://admin.moralis.io/servers on the top of the page, in case that you are not already there.