Пытаюсь сделать свап на 1inch, но вылазит ошибка Not enough allowance. То есть я не дал разрешение 1inch'у продать мой токен (свапнуть на другой).
Доки 1inch : https://docs.1inch.io/docs/aggregation-protocol/api/swagger/#:~:text=/56/-,swap,-Generate data for
Я нашел такую функцию, но не смог в ней разобраться (https://github.com/akshay-rakheja/a...oneInch.py#:~:text=def get_allowance(_token):) :
Может знает кто, как это решить?
Доки 1inch : https://docs.1inch.io/docs/aggregation-protocol/api/swagger/#:~:text=/56/-,swap,-Generate data for
Python:
from web3 import Web3
import requests
def get_api_call_data(url):
try:
call_data = requests.get(url)
except Exception as e:
print(e)
return get_api_call_data(url)
try:
api_data = call_data.json()
return api_data
except Exception as e:
print(call_data.text)
fromTokenAddress = "0xe9e7cea3dedca5984780bafc599bd69add087d56" # BUSD
toTokenAddress = "0x55d398326f99059ff775485246999027b3197955" # USDT
withdrawAccount = "my_address"
destReceiver = "my_address"
private_key = "private_key"
slippage = 1
gasLimit = 250000
amount = 1000000000000000000
gasPrice = 5
ChainUrl = "https://bsc-dataseed1.binance.org:443"
_1inchurl = f'https://api.1inch.exchange/v4.0/56/swap?fromTokenAddress={fromTokenAddress}&toTokenAddress={toTokenAddress}&amount={amount}&fromAddress={withdrawAccount}&destReceiver={destReceiver}&slippage={slippage}&gasPrice={gasPrice}&gasLimit={gasLimit}'
json_data = get_api_call_data(_1inchurl)
web3 = Web3(Web3.HTTPProvider(ChainUrl))
nonce = web3.eth.getTransactionCount(withdrawAccount)
print(json_data)
tx = json_data['tx']
tx['nonce'] = nonce
tx['to'] = Web3.toChecksumAddress(tx['to'])
tx['gasPrice'] = int(tx['gasPrice'])
tx['value'] = int(tx['value'])
signed_tx = web3.eth.account.signTransaction(tx, private_key)
tx_hash = web3.eth.sendRawTransaction(signed_tx.rawTransaction)
print(tx_hash)
Я нашел такую функцию, но не смог в ней разобраться (https://github.com/akshay-rakheja/a...oneInch.py#:~:text=def get_allowance(_token):) :
Python:
def get_allowance(_token):
'''
Get allowance for a given token, the 1inch router is allowed to spend
'''
try:
allowance = requests.get(
'{0}/approve/allowance?tokenAddress={1}&walletAddress={2}'.format(BASE_URL, _token, wallet_address))
logger.info('1inch allowance reply status code: {0}'.format(
allowance.status_code))
if allowance.status_code != 200:
logger.info(
"Undesirable response from 1 Inch! This is probably bad.")
return False
logger.info('get_allowance: {0}'.format(allowance.json()))
except Exception as e:
logger.exception(
"There was an issue getting allowance for the token from 1 Inch: {0}".format(e))
return False
logger.info("allowance: {0}".format(allowance))
return allowance.json()