Check if wallet is approved to spend token
Asked Answered
D

1

5

I was wondering if there's a way to check if a token is already approved for spending. 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 each time it buys and sells the same token. Thanks for all your help anticipation.

Dozer answered 23/8, 2021 at 21:52 Comment(0)
D
6

I got help elsewhere. Anyway here's an example for anyone looking for a solution:

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)

Dozer answered 27/8, 2021 at 22:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.