Examining Offers from an explorer perspective

This is a repost of a question that came up in the #chialisp channel on keybase:

Any one have good tutorial or video explaining offers in chia? Need help in parsing accepted offers to identify the CAT/NFT price! How do I match coins in spent block using announcements to identify the coins settled as part of the offer?

No tutorial or video, but let me giving you the basic algorithm from an explorer’s perspective:

  1. Look for every coin sent to this inner address chia-blockchain/settlement_payments.clvm.hex.sha256tree at main · Chia-Network/chia-blockchain · GitHub . It should be spent in the same block. If it’s not, just ignore it, it’s someone playing around.
  2. Look at the spend of the coin that was sent to that address (settlement coin). It will create some puzzle announcements.
  3. Calculate the announcement IDs (sha256(puzzle_hash + data being announced))
  4. Look at all of the announcements that were asserted in every other spend in the block. Find the coins that asserted an announcement of an ID that the settlement coin created.
  5. Finally look, at the outputs that the settlement coin created.

You now know that the coins who were asserting an announcement from the settlement coin were dependent on the creation of the outputs that the settlement coin created. Those coins may have been dependent on other settlement coins as well, so you may have to build a little bit of a web to figure out the whole offer.
Doing the above method will be able to tell you what quantity of assets was offered for what quantity of other assets in a specific block. If you want to know who offered what for what, you need to get a little fancier in step 3.
The solution to the settlement coin has a list of payments batches that look like this (nonce . ((puzhash amt memos) (puzhash amt memos) ...)). Each one of these batches gets turned into announcement data in this line: chia-blockchain/settlement_payments.clvm at 0fc63d62d22cd13dac653fe6370105c716cc5928 · Chia-Network/chia-blockchain · GitHub. So now you can figure out which coin asserted which announcement in the settlement coin and can therefore figure out the address that coin came from and how much they were requesting in return.