<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eric Ren on Medium]]></title>
        <description><![CDATA[Stories by Eric Ren on Medium]]></description>
        <link>https://medium.com/@eric.ren_51534?source=rss-82d891ec2102------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*T2SeFEVdbQ0jwmVhL0ZO2w.png</url>
            <title>Stories by Eric Ren on Medium</title>
            <link>https://medium.com/@eric.ren_51534?source=rss-82d891ec2102------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 21:19:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eric.ren_51534/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Polling NBA Top Shot P2P Market Purchase Events from Flow Blockchain using flow-go-sdk]]></title>
            <link>https://medium.com/@eric.ren_51534/polling-nba-top-shot-p2p-market-purchase-events-from-flow-blockchain-using-flow-go-sdk-3ec80119e75f?source=rss-82d891ec2102------2</link>
            <guid isPermaLink="false">https://medium.com/p/3ec80119e75f</guid>
            <category><![CDATA[nba-top-shot]]></category>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[blockchain]]></category>
            <dc:creator><![CDATA[Eric Ren]]></dc:creator>
            <pubDate>Tue, 29 Sep 2020 04:26:10 GMT</pubDate>
            <atom:updated>2021-01-27T22:06:14.365Z</atom:updated>
            <content:encoded><![CDATA[<h3>Polling NBA Top Shot Purchase Events from Flow Blockchain with flow-go-sdk</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YKXfrH7h5Q1TPIqPItQmjA.png" /></figure><p><a href="https://www.onflow.org/primer">Flow</a> is a fast, decentralized, and developer-friendly blockchain built by<br>Dapper Labs. Smart contracts on Flow are written in Cadence which is a resource-oriented programming language that comes with native safe and easy to use resource support on the blockchain.</p><p><a href="https://www.nbatopshot.com/">NBA Top Shot</a> is one of the first and most prominent DApps currently built on Flow. It allows users to own, share, and trade their favourite unique NFT NBA player moments. Such as this one (it’s one of my most precious moments):</p><p><a href="https://www.nbatopshot.com/moment/rrrkren+4af59222-e92b-49c5-92eb-45778024f57c">NBA Top Shot: Aaron Gordon - Owned by rrrkren</a></p><p>In this post we will dig deep into the p2p market purchase events of NBA Top Shot using publicly available API and connect directly with the Flow blockchain.</p><p>This post assumes familiarity with <a href="https://docs.onflow.org/cadence/language/">Cadence</a> and I strongly recommend reading through the <a href="https://github.com/dapperlabs/nba-smart-contracts/tree/master/contracts">NBA Top Shot smart contracts</a>. We are going to be using <a href="https://github.com/onflow/flow-go-sdk">flow-go-sdk v0.10.0</a>. The completed project can be found at <a href="https://github.com/rrrkren/topshot-sales">https://github.com/rrrkren/topshot-sales</a>.</p><p>Let’s get started by setting up our main func and connect to flow.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf9465aa986cbaac2d8b721d3f97d553/href">https://medium.com/media/bf9465aa986cbaac2d8b721d3f97d553/href</a></iframe><p>Now we have a good connection with flow, let’s get the latest block height</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a0d57ef60f445d3b968048f969a2c83/href">https://medium.com/media/8a0d57ef60f445d3b968048f969a2c83/href</a></iframe><p><a href="https://docs.onflow.org/cadence/language/events/">Events</a> in Cadence are similar to the ones from Ethereum, they are pre-defined in the smart contract and can be emitted during transactions. For our use case we will be polling Flow for the p2p market moment purchased events. The Top Shot p2p market smart contract can be found <a href="https://github.com/dapperlabs/nba-smart-contracts/blob/master/contracts/MarketTopShot.cdc">here</a>.</p><p>Suppose I’m interested in purchase events in the last 1000 blocks. Block time is about 1/s so that translates to roughly the last 16 minutes. We will use the method GetEventsForHeightRange to poll Market.MomentPurchased events.</p><p>The Type argument of GetEventsForHeightRange follows the format of A.{AccountAddress}.{ContractName}.{EventName}</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2455f9f515af272e2643341267c814c9/href">https://medium.com/media/2455f9f515af272e2643341267c814c9/href</a></iframe><p>Now we have a slice of 500 blockEvents that could contain aMarket.MomentPurchased event. Let’s iterate through them and print each one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fef2a291529383b9515f115e03b38dd0/href">https://medium.com/media/fef2a291529383b9515f115e03b38dd0/href</a></iframe><p>this is our print out:</p><pre>A.c1e4f4f4c4257510.Market.MomentPurchased: a2c3e058b4cbdf7a04e34dc51f5477a5487716b915936346137fa91c69e55853<br>{0xc00029ac30 [317574 1200000000 {1eabb8a3b542ba1b}]}<br>A.c1e4f4f4c4257510.Market.MomentPurchased: aca72c1cd9960cd357de63302c6bddcd99a48de44ed8c429a3b0037ec23459cb<br>{0xc00029ac80 [317565 1600000000 {90c3ace7af7645d4}]}</pre><p>What are these mysterious things that we printed? It seems flow.Events implements a String method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b45140f46c4c71cd373e5d02be34b48b/href">https://medium.com/media/b45140f46c4c71cd373e5d02be34b48b/href</a></iframe><p>And that’s our first line, the second line is a print out of the cadence.Event struct that contains an EventType and an array of values. Let’s try to decode that event from our event definition in the smart contract defined <a href="https://github.com/dapperlabs/nba-smart-contracts/blob/e6cd787cebb1eb50d04dccdc27844d3bff35511b/contracts/MarketTopShot.cdc#L52">here</a> :</p><p>Decoding cadence.Event is pretty simple, you find the event definition from the smart contract, then cast each of the cadence.Value in the Fields in order into their corresponding cadence type, such as cadence.UInt64, which are mostly wrapper types on top of primitive go types.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef9bb04058f8fec7a8711883216779e8/href">https://medium.com/media/ef9bb04058f8fec7a8711883216779e8/href</a></iframe><p>and to actually use our new decoded event:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1793bf1a6cc53bcfe7d5507eedac64dd/href">https://medium.com/media/1793bf1a6cc53bcfe7d5507eedac64dd/href</a></iframe><p>Because we implemented that nice little String method, our console log is looking much better now:</p><pre>moment purchased: momentid: 317574, price: 12.000000, seller: 1eabb8a3b542ba1b<br>moment purchased: momentid: 317565, price: 12.000000, seller: 90c3ace7af7645d4</pre><p>We now have the moment flowID, sale price, and seller account address of the purchase. It would be great to also able to fetch the serialNumber, setID, and playID to pin point the full metadata of the moment itself. The simplest way would have been polling for Moment.Minted events from flow the same way we polled these purchase events, however since flow currently periodically archives historical events/transaction results, it’s not always possible to fetch the minted event of a moment. In fact <em>candidate9</em> — the current iteration of the flow network only contains data from blocks 6483246 and above.</p><p>So, are we busted? No!</p><p>Since we know the seller, and that we know the seller is almost guaranteed to have the moment listed in their sale collection in the previous block (unless they listed and sold the moment in the same block, which is highly unlikely the way Top Shot currently works), we can use this nifty method called ExecuteScriptAtBlockHeight to get that information. Thanks to blockchain!</p><p>(The method ExecuteScriptAtBlockHeight has a caveat: flow execution nodes currently only keeps the last 1000 blocks that have made a change, thus it’s not always possible to execute script at arbitrary block heights. But for our current use case where we poll the last 1000 blocks, it should work.)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/019c705d1a023f2e1118de178e2a1fd6/href">https://medium.com/media/019c705d1a023f2e1118de178e2a1fd6/href</a></iframe><p>Woah what happened there? That was a lot of code! Don’t worry it’s quite<br>straightforward. We prepared a script that fetches a reference to a<br>Topshot.NFT moment and its price from the seller’s sale collection, and<br>storing it into a custom struct called SaleMoment. Then we decode the returned cadence.Value from the ExecuteScriptAtBlockHeight method and cast it into a cadence.Struct. Decoding the cadence.Struct is similar to decoding a cadence.Event, we parse the fields in their declared order and cast each field into their corresponding type.</p><p>And now back to our loop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/901361ff6d810696aa8d1c3b1ee10d2b/href">https://medium.com/media/901361ff6d810696aa8d1c3b1ee10d2b/href</a></iframe><p>Note the blockEvent.<em>Height</em>-1, we actually want the script to be executed at the block prior to the purchase tx block in order to get the moment from the seller’s sale collection before it was sold.</p><p>Et Voila! We now have a much better history item for this sale event:</p><pre>moment purchased: momentid: 310063, price: 12.000000, seller: 9cf0e3443ebc8bf9<br>saleMoment: serialNumber: 118, setID: 2, setName: Base Set, playID: 213, playerName: Giannis Antetokounmpo<br>transactionID: 54e881e34678ecd64edf14efebe06dfd94d4d00b8b47de4bcd0dcf417380d4fc, block height: 6633032</pre><pre>moment purchased: momentid: 113499, price: 4.000000, seller: 9cf0e3443ebc8bf9<br>saleMoment: serialNumber: 509, setID: 2, setName: Base Set, playID: 144, playerName: Jimmy Butler<br>transactionID: 5143ea740f7bb6e76c3f9298aa577b0df43a9619987ef4324b7a8e6f8c508633, block height: 6633113</pre><p>In order to make this a useful program that keeps historical purchase data, we can probably use a database to keep track of the block height cursor and the data we scraped from the blockchain. Since that doesn’t have much to do with the flow interaction itself, I’ll leave that to the reader.</p><p>Helpful resources:</p><ul><li><a href="https://docs.onflow.org/cadence/language/">Cadence Reference</a></li><li><a href="https://github.com/dapperlabs/nba-smart-contracts/tree/master/contracts">Top Shot smart contracts</a></li><li><a href="https://github.com/onflow/flow-go-sdk">flow-go-sdk</a></li><li><a href="https://github.com/rrrkren/topshot-sales">https://github.com/rrrkren/topshot-sales</a></li><li>If you are more into javascript and would like to build a DAPP, this is a good place to start: <a href="https://github.com/onflow/flow-js-sdk/tree/master/packages/fcl#flow-app-quickstart">https://github.com/onflow/flow-js-sdk/tree/master/packages/fcl#flow-app-quickstart</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3ec80119e75f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>