|
@@ -1,6 +1,6 @@
|
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
|
import { Connection, PublicKey, Keypair, VersionedTransaction, Transaction } from '@solana/web3.js';
|
|
import { Connection, PublicKey, Keypair, VersionedTransaction, Transaction } from '@solana/web3.js';
|
|
|
-import { getAssociatedTokenAddress, createAssociatedTokenAccountInstruction } from '@solana/spl-token';
|
|
|
|
|
|
|
+import { getAssociatedTokenAddress, createAssociatedTokenAccountInstruction, TOKEN_PROGRAM_ID, TOKEN_2022_PROGRAM_ID } from '@solana/spl-token';
|
|
|
import bs58 from 'bs58';
|
|
import bs58 from 'bs58';
|
|
|
import { CONFIG } from '../config/index.js';
|
|
import { CONFIG } from '../config/index.js';
|
|
|
import { logger, sleep } from '../utils/index.js';
|
|
import { logger, sleep } from '../utils/index.js';
|
|
@@ -36,6 +36,24 @@ export class JupiterSwapper {
|
|
|
return headers;
|
|
return headers;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询 mint 账户的 owner 判断是标准 SPL Token 还是 Token-2022
|
|
|
|
|
+ */
|
|
|
|
|
+ async getTokenProgramId(mintAddress) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const mintAccountInfo = await this.connection.getAccountInfo(new PublicKey(mintAddress));
|
|
|
|
|
+ if (mintAccountInfo) {
|
|
|
|
|
+ const owner = mintAccountInfo.owner.toString();
|
|
|
|
|
+ if (owner === TOKEN_2022_PROGRAM_ID.toString()) {
|
|
|
|
|
+ return TOKEN_2022_PROGRAM_ID;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return TOKEN_PROGRAM_ID;
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ return TOKEN_PROGRAM_ID;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async getTokenBalance(mintAddress) {
|
|
async getTokenBalance(mintAddress) {
|
|
|
try {
|
|
try {
|
|
|
if (mintAddress === 'So11111111111111111111111111111111111111112') {
|
|
if (mintAddress === 'So11111111111111111111111111111111111111112') {
|
|
@@ -43,9 +61,12 @@ export class JupiterSwapper {
|
|
|
return balance / 1e9;
|
|
return balance / 1e9;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ const programId = await this.getTokenProgramId(mintAddress);
|
|
|
const tokenAccount = await getAssociatedTokenAddress(
|
|
const tokenAccount = await getAssociatedTokenAddress(
|
|
|
new PublicKey(mintAddress),
|
|
new PublicKey(mintAddress),
|
|
|
- this.keypair.publicKey
|
|
|
|
|
|
|
+ this.keypair.publicKey,
|
|
|
|
|
+ false,
|
|
|
|
|
+ programId
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
@@ -289,22 +310,27 @@ export class JupiterSwapper {
|
|
|
|
|
|
|
|
async ensureTokenAccount(mintAddress) {
|
|
async ensureTokenAccount(mintAddress) {
|
|
|
try {
|
|
try {
|
|
|
|
|
+ const mintPubkey = new PublicKey(mintAddress);
|
|
|
|
|
+ const programId = await this.getTokenProgramId(mintAddress);
|
|
|
const tokenAccount = await getAssociatedTokenAddress(
|
|
const tokenAccount = await getAssociatedTokenAddress(
|
|
|
- new PublicKey(mintAddress),
|
|
|
|
|
- this.keypair.publicKey
|
|
|
|
|
|
|
+ mintPubkey,
|
|
|
|
|
+ this.keypair.publicKey,
|
|
|
|
|
+ false,
|
|
|
|
|
+ programId
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
await this.connection.getAccountInfo(tokenAccount);
|
|
await this.connection.getAccountInfo(tokenAccount);
|
|
|
return tokenAccount;
|
|
return tokenAccount;
|
|
|
} catch (e) {
|
|
} catch (e) {
|
|
|
- logger.info(`Creating token account for ${mintAddress}...`);
|
|
|
|
|
|
|
+ logger.info(`Creating token account for ${mintAddress} (program: ${programId.toString().slice(0, 8)}...)...`);
|
|
|
const transaction = new Transaction().add(
|
|
const transaction = new Transaction().add(
|
|
|
createAssociatedTokenAccountInstruction(
|
|
createAssociatedTokenAccountInstruction(
|
|
|
this.keypair.publicKey,
|
|
this.keypair.publicKey,
|
|
|
tokenAccount,
|
|
tokenAccount,
|
|
|
this.keypair.publicKey,
|
|
this.keypair.publicKey,
|
|
|
- new PublicKey(mintAddress)
|
|
|
|
|
|
|
+ mintPubkey,
|
|
|
|
|
+ programId
|
|
|
)
|
|
)
|
|
|
);
|
|
);
|
|
|
|
|
|