当我在nodeJS中的某个帐户上接收令牌时,我想接收回调。此代码工作正常,但仅适用于SOL。如何为索拉纳的USDT或任何代币做同样的事情?
const web3 = require("@solana/web3.js");
(async () => {
const publicKey = new web3.PublicKey(wallet);
const solanaConnection = new web3.Connection(httpsNode, {
wsEndpoint: wssNode,
});
solanaConnection.onLogs(
publicKey,
(logs, context) => console.log("Updated account info: ", logs),
"confirmed"
);
})();
您可以做基本上相同的事情,但不应该使用publicKey
,而是应该使用该钱包的USDT相关代币账户地址,例如
const web3 = require("@solana/web3.js");
const splToken = require("@solana/spl-token");
(async () => {
const walletPublicKey = new web3.PublicKey(wallet);
const usdtMint = new web3.PublicKey("Es9vMFrzaCERmJfrF4H2FYD4KCoNkY11McCe8BenwNYB");
const publicKey = splToken.getAssociatedTokenAddressSync(usdtMint, walletPublicKey);
const solanaConnection = new web3.Connection(httpsNode, {
wsEndpoint: wssNode,
});
solanaConnection.onLogs(
publicKey,
(logs, context) => console.log("Updated account info: ", logs),
"confirmed"
);
})();