Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update QueryApi nft-indexer #1605

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 27 additions & 42 deletions docs/bos/tutorial/indexer-tutorials/nft-indexer.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,62 +77,47 @@ The first part of the logic is to filter blockchain transactions for a specific
The `getBlock` function for this NFT indexer looks like this:

```js
import { Block } from "@near-lake/primitives";

async function getBlock(block: Block) {

const h = block.header().height;
const createdOn = block.streamerMessage.block.header.timestamp;

for (let r of block.receipts()) {
// check if we have events
if (!r.events.length) continue;
for (let ev of block.events()) {
const r = block.actionByReceiptId(ev.relatedReceiptId);
const createdOn = block.streamerMessage.block.header.timestamp;

for (let log of r.logs) {
// check if logs follow the Events format
if (!log.match(/^EVENT_JSON:(.*)$/)) continue;
try {
let event = ev.rawEvent;

try {
// parse the JSON Event
let event = JSON.parse(log.substring(log.indexOf(":") + 1));
if (event.standard === "nep171" && event.event === "nft_mint") {
console.log(event);

// check if it's a NEP-171 NFT minting
if (event.standard === "nep171" && event.event === "nft_mint") {
console.log(r.receiverId);
console.log(event);
let marketplace = "unknown";
if (r.receiverId.endsWith(".paras.near"))
marketplace = "Paras";
else if (r.receiverId.endsWith(".sharddog.near"))
marketplace = "ShardDog";
else if (r.receiverId.match(/\.mintbase\d+\.near$/))
marketplace = "Mintbase";

// identify the NFT marketplace
let marketplace = "unknown";
if (r.receiverId.endsWith(".paras.near"))
marketplace = "Paras";
else if (r.receiverId.endsWith(".sharddog.near"))
marketplace = "ShardDog";
else if (r.receiverId.match(/\.mintbase\d+\.near$/))
marketplace = "Mintbase";
const nftMintData = {
marketplace: marketplace,
block_height: block.header().height,
block_timestamp: createdOn,
receipt_id: r.receiptId,
receiver_id: r.receiverId,
nft_data: JSON.stringify(event.data),
};

const nftMintData = {
marketplace: marketplace,
block_height: h,
block_timestamp: createdOn,
receipt_id: r.receiptId,
receiver_id: r.receiverId,
nft_data: JSON.stringify(event.data),
};

// store result to the database
await context.db.Nfts.insert(nftMintData);
await context.db.Nfts.insert(nftMintData);

console.log(`NFT by ${r.receiptId} has been added to the database`);
}
} catch (e) {
console.log(e);
console.log(`NFT by ${r.receiptId} has been added to the database`);
}
} catch (e) {
console.log(e);
}
}
}
```

This filter selects receipts that have events of type `nft_mint` and standard `nep171`. In addition, it parses the JSON event data and identifies the NFT marketplace.
This indexer filters [Blocks](https://near.github.io/near-lake-framework-js/classes/block.Block.html) that have [Events](https://near.github.io/near-lake-framework-js/classes/events.Event.html) of type `nft_mint` and standard `nep171`. In addition, it stores the JSON event data and identifies the NFT marketplace.

### Saving the data to the Database

Expand Down
Loading