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

in Gods On Chain3 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:

(you can actually click at the link to see the type of response we get.)
You will see something like this. "https://api.x.immutable.com/v1/trades"

<p dir="auto"><img src="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/matimath/23t756Zdy4VpCFD5ruFFoxfYgsFCF6JnPq7bsPkJ7Jp1G8EQaT66mKafv2Mgukwamc5m3.PNG" alt="Capture.PNG" srcset="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/matimath/23t756Zdy4VpCFD5ruFFoxfYgsFCF6JnPq7bsPkJ7Jp1G8EQaT66mKafv2Mgukwamc5m3.PNG 1x, https://images.hive.blog/1536x0/https://files.peakd.com/file/peakd-hive/matimath/23t756Zdy4VpCFD5ruFFoxfYgsFCF6JnPq7bsPkJ7Jp1G8EQaT66mKafv2Mgukwamc5m3.PNG 2x" /> <p dir="auto">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".<br /> 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). <div class="table-responsive"><table> <thead> <tr><th>adress<th>crypto <tbody> <tr><td>0x9ab7bb7fdc60f4357ecfef43986818a2a3569c62<td>GOG <tr><td>0xf57e7e7c23978c3caec3c3548e3d615c346e79ff<td>IMX <tr><td>0xccc8cb5229b0ac8069c51fd58367fd1e622afd97<td>GODS <tr><td>0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48<td>USD <tr><td>0xed35af169af46a02ee13b9d79eb57d6d68c1749e<td>OMI <tr><td>0xacb3c6a43d15b907e8433077b6d38ae40936fe2c<td>GU <p dir="auto"><span>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 <a href="https://market.immutable.com/assets/0xacb3c6a43d15b907e8433077b6d38ae40936fe2c/{token_id}" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">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). <p dir="auto">Example of python code that lets you fetch all of the trades from last 24 hours: <pre><code>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) <p dir="auto">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):

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@matimath/upvotes.png?202203072114" /><td>You distributed more than 100 upvotes.<br />Your next target is to reach 200 upvotes. <p dir="auto"><sub><em>You can view your badges on <a href="https://hivebuzz.me/@matimath" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">your board and compare yourself to others in the <a href="https://hivebuzz.me/ranking" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Ranking<br /> <sub><em>If you no longer want to receive notifications, reply to this comment with the word <code>STOP <p dir="auto">To support your work, I also upvoted your post! <p dir="auto"><strong><span>Check out the last post from <a href="/@hivebuzz">@hivebuzz: <table><tr><td><a href="/hive-122221/@hivebuzz/pud-202203-feedback"><img src="https://images.hive.blog/64x128/https://i.imgur.com/zHjYI1k.jpg" /><td><a href="/hive-122221/@hivebuzz/pud-202203-feedback">Feedback from the March 1st Hive Power Up Day<tr><td><a href="/hive-122221/@hivebuzz/pum-202202-delegations"><img src="https://images.hive.blog/64x128/https://i.imgur.com/CJfvUMP.png" /><td><a href="/hive-122221/@hivebuzz/pum-202202-delegations">Our Hive Power Delegations to the February PUM Winners <h6>Support the HiveBuzz project. <a href="https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22199%22%5D&approve=true" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Vote for <a href="https://peakd.com/me/proposals/199" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">our proposal!