仅在Solana的交易数据中获取NFT转移
给定一个钱包地址,我想找到发送给它的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你快到了!如果要过滤NFT,则需要检查已转移的令牌的薄荷,并检查薄荷的电源是否为
1
,例如:这很笨拙,因为它只是它检查某些帐户之前和之后有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.: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.