仅在Solana的交易数据中获取NFT转移

发布于 2025-02-13 16:38:19 字数 1558 浏览 0 评论 0原文

给定一个钱包地址,我想找到发送给它的NFT的所有交易。我有一些代码,但不能100%确定它只是在做NFT。确定交易或解析指令是否与NFT相关的标识符是什么(SPL-TOKEN?)

import {
    clusterApiUrl,
    ConfirmedSignaturesForAddress2Options,
    Connection,
    PublicKey,
} from '@solana/web3.js';
import fs from 'fs';
import path from 'path';

const WALLET = "<WALLET>"; // receiving NFTs
const LIMIT = 100;

(async () => {
    // Connect to the Solana cluster
    const c = new Connection(clusterApiUrl('mainnet-beta'));

    // The wallet receiving NFTs
    const p = new PublicKey(WALLET);

    // Query for signitures
    const opts: ConfirmedSignaturesForAddress2Options = {
        limit: 10,
    }
    const cs = await c.getConfirmedSignaturesForAddress2(p, opts);

    // Parse the signiture transaction data
    const td = [];
    for (let i = 0; i < cs.length; i++) {

        const s = cs[i];
        const d = await c.getParsedTransaction(s.signature, 'finalized');

        if (!d) continue;
        td.push(d);

    }

    // Build a map of mint source and destinations
    const tm: any = {};
    for (let i = 0; i < td.length; i++) {
        const d = td[i];

        d.meta?.postTokenBalances?.forEach(({ mint, owner }) => {

            if (!tm[mint]) tm[mint] = { destination: "", source: "" };
            if (owner === p.toString()) tm[mint].destination = owner;
            else tm[mint].source = owner;

        });
    }

    fs.writeFileSync(path.join(process.cwd(), 'nft-transfers.json'), JSON.stringify(tm, null, 2));

})();

Given a wallet address I want to find all transactions for NFTs that were sent to it. I have some code but not 100% certain its just doing NFTs. What is the identifier to determine if a transaction or parsed instruction is NFT related (SPL-Token??)

import {
    clusterApiUrl,
    ConfirmedSignaturesForAddress2Options,
    Connection,
    PublicKey,
} from '@solana/web3.js';
import fs from 'fs';
import path from 'path';

const WALLET = "<WALLET>"; // receiving NFTs
const LIMIT = 100;

(async () => {
    // Connect to the Solana cluster
    const c = new Connection(clusterApiUrl('mainnet-beta'));

    // The wallet receiving NFTs
    const p = new PublicKey(WALLET);

    // Query for signitures
    const opts: ConfirmedSignaturesForAddress2Options = {
        limit: 10,
    }
    const cs = await c.getConfirmedSignaturesForAddress2(p, opts);

    // Parse the signiture transaction data
    const td = [];
    for (let i = 0; i < cs.length; i++) {

        const s = cs[i];
        const d = await c.getParsedTransaction(s.signature, 'finalized');

        if (!d) continue;
        td.push(d);

    }

    // Build a map of mint source and destinations
    const tm: any = {};
    for (let i = 0; i < td.length; i++) {
        const d = td[i];

        d.meta?.postTokenBalances?.forEach(({ mint, owner }) => {

            if (!tm[mint]) tm[mint] = { destination: "", source: "" };
            if (owner === p.toString()) tm[mint].destination = owner;
            else tm[mint].source = owner;

        });
    }

    fs.writeFileSync(path.join(process.cwd(), 'nft-transfers.json'), JSON.stringify(tm, null, 2));

})();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生百味 2025-02-20 16:38:19

你快到了!如果要过滤NFT,则需要检查已转移的令牌的薄荷,并检查薄荷的电源是否为1,例如:

// Parse the signiture transaction data
    const td = [];
    for (let i = 0; i < cs.length; i++) {

        const s = cs[i];
        const d = await c.getParsedTransaction(s.signature, 'finalized');

        if (!d) continue;
        if (d.meta.preTokenBalances.includes(1) &&
            d.meta.preTokenBalances.includes(0) &&
            d.meta.postTokenBalances.includes(1) &&
            d.meta.postTokenBalances.includes(0)) {
            td.push(d);
        }
    }

这很笨拙,因为它只是它检查某些帐户之前和之后有1和0令牌。您可能可以使用查找更聪明地做一些更智能的事情,以确保更改了1和0的帐户索引。

You're almost there! If you want to filter for NFTs, you need to check against the mint for the token that was transferred, and check that the supply for the mint is 1, e.g.:

// Parse the signiture transaction data
    const td = [];
    for (let i = 0; i < cs.length; i++) {

        const s = cs[i];
        const d = await c.getParsedTransaction(s.signature, 'finalized');

        if (!d) continue;
        if (d.meta.preTokenBalances.includes(1) &&
            d.meta.preTokenBalances.includes(0) &&
            d.meta.postTokenBalances.includes(1) &&
            d.meta.postTokenBalances.includes(0)) {
            td.push(d);
        }
    }

This is pretty janky, since it's just checking some accounts had 1 and 0 tokens, before and after. You can probably do something smarter with find to make sure that the account index with 1 and 0 changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文