web3.py – How to Resolve Invalid or Unsigned Transaction Error from 0x Swap API Quote

0xweb3.py

I'm trying out the new 0x Fantom integration API, in order to automate some trading tasks.

According to this, using the Swap-API from 0x returns a transaction for a quote, which just needs to be signed & sent to the network: https://0x.org/docs/api#get-swapv1quote , quote:

Get an easy-to-consume quote for buying or selling any token. The
return format is a valid unsigned Ethereum transaction and can be
submitted directly to an Ethereum node to complete the swap.

But when I actually get the result from a (working) quote, the data I receive cannot be treated as unsigned transaction: signing via web3 (Python) complains that there's unrecognized fields.

Sample code:

get_quote_url = 'https://fantom.api.0x.org/swap/v1/quote?sellToken=DAI&buyToken=WETH&sellAmount=10000000000000000000'
response = requests.get(get_quote_url) 
quote = response.json()
my_sell_DAI_for_WETH_quote['nonce'] = w3.eth.get_transaction_count(my_address)
w3.eth.account.signTransaction(my_sell_DAI_for_WETH_quote, my_address_privkey)

This results in the error:

"TypeError: Transaction must not include unrecognized fields:
{'allowanceTarget', 'orders', 'estimatedGas', 'sources',
'minimumProtocolFee', 'protocolFee', 'sellTokenToEthRate',
'buyTokenToEthRate', 'sellAmount', 'buyAmount', 'guaranteedPrice',
'buyTokenAddress', 'price', 'sellTokenAddress'}"

What am I missing here?

Best Answer

It's valid for most javascript web3 libraries, which typically ignore unused fields. YMMV with Python. It looks like you might just need to prune those excess fields.

Related Topic