Gods Unchained Marketplace API - how to use with examples in Python. Part. 2

in Gods On Chain2 years ago

As the previous post on Immutable X api turned out fairly succesfull (you can check it out here: Gods Unchained Marketplace API - how to use with examples in Python. Part. 1. It covers the way to fetch listed orders).
Today I am going to tell you about another endpoint that lets you get list of actually completed transactions. The endpoint you should ping in order to get it is the following:
"https://api.x.immutable.com/v1/trades"
(you can actually click at the link to see the type of response we get.)
You will see something like this.

Capture.PNG

At the top level we have 2 fields: result - which contains list of transactions, and cursor which works for pagination, similarly to the order endpoint (more on this later). Each transaction contains status, transaction_id, timestamp (date of transaction) and 2 transacted items "a" and "b".
The first transaction item "a" is usually a cryptocurrency paid for asset - it is marked by token_type ERC20. Token_adress tells you what crypto is being traded (table that lets you look it up below). Field "sold" obviously contains an amount of crypto sold (you need to normalize it in order to get actual amount of crypto. For GODS and ETH just multiply by 10^-18).

adresscrypto
0x9ab7bb7fdc60f4357ecfef43986818a2a3569c62GOG
0xf57e7e7c23978c3caec3c3548e3d615c346e79ffIMX
0xccc8cb5229b0ac8069c51fd58367fd1e622afd97GODS
0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48USD
0xed35af169af46a02ee13b9d79eb57d6d68c1749eOMI
0xacb3c6a43d15b907e8433077b6d38ae40936fe2cGU

The other item, hold in "b" is usually is an NFT - marked with ERC721. A tricky field is token_address. As immutable X api lists all immutable X transactions, it does not limit itself to GU cards only. If you are only interested in GU you should limit transaction by filtering to only tokens with token_address "0xacb3c6a43d15b907e8433077b6d38ae40936fe2c". On the other hand, to determine specific card you just need to look at token_id. With token id you can find which card it is, by going to https://market.immutable.com/assets/0xacb3c6a43d15b907e8433077b6d38ae40936fe2c/{token_id} or automatically (I will discuss how to do it with python in part 3 of the tutorial).

Example of python code that lets you fetch all of the trades from last 24 hours:

import requests
import json
import datetime

url = "https://api.x.immutable.com/v1/trades?page_size=200&status=success"

all_active_trades = []
headers = {"Accept": "*/*"}

latest = datetime.datetime.now()
while latest > datetime.datetime.now() - datetime.timedelta(days=1):
    response = requests.request("GET", url, headers=headers)
    response = json.loads(response.content)

    all_active_trades.extend(response['result'])
    cursor = response['cursor']
    url = f"https://api.x.immutable.com/v1/trades?page_size=200&status=success&cursor={cursor}"
    latest = datetime.datetime.strptime(response['result'][-1]['timestamp'][:-1], "%Y-%m-%dT%H:%M:%S.%f")
    print(latest)

You can fetch more by modifying the days parameter in while condition.

Sort:  

Congratulations @matimath! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You distributed more than 100 upvotes.
Your next target is to reach 200 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Check out the last post from @hivebuzz:

Feedback from the March 1st Hive Power Up Day
Our Hive Power Delegations to the February PUM Winners
Support the HiveBuzz project. Vote for our proposal!