178 lines
3.7 KiB
JavaScript
178 lines
3.7 KiB
JavaScript
![]() |
import { Connection, PublicKey } from "@solana/web3.js"
|
||
|
import {
|
||
|
closeEmptyTokenAccounts,
|
||
|
create_TipLink,
|
||
|
create_gibwork_task,
|
||
|
fetchPrice,
|
||
|
fetchTokenDetailedReport,
|
||
|
fetchTokenReportSummary,
|
||
|
getAssetsByOwner,
|
||
|
getTPS,
|
||
|
get_balance,
|
||
|
get_balance_other,
|
||
|
launchPumpFunToken,
|
||
|
parseTransaction,
|
||
|
resolveSolDomain,
|
||
|
stakeWithJup,
|
||
|
trade,
|
||
|
transfer,
|
||
|
create_image,
|
||
|
get_wallet_address
|
||
|
} from "../tools"
|
||
|
|
||
|
import { DEFAULT_OPTIONS } from "../constants"
|
||
|
|
||
|
/**
|
||
|
* Main class for interacting with Solana blockchain
|
||
|
* Provides a unified interface for token operations, NFT management, trading and more
|
||
|
*
|
||
|
* @class SolanaAgentKit
|
||
|
* @property {Connection} connection - Solana RPC connection
|
||
|
* @property {WalletAdapter} wallet - Wallet that implements WalletAdapter for signing transactions
|
||
|
* @property {PublicKey} wallet_address - Public key of the wallet
|
||
|
* @property {Config} config - Configuration object
|
||
|
*/
|
||
|
export class SolanaAgentKit {
|
||
|
constructor(wallet, rpc_url, configOrKey) {
|
||
|
this.connection = new Connection(
|
||
|
rpc_url || "https://api.mainnet-beta.solana.com"
|
||
|
)
|
||
|
this.wallet = wallet
|
||
|
this.wallet_address = this.wallet.publicKey
|
||
|
|
||
|
// Handle both old and new patterns
|
||
|
if (typeof configOrKey === "string" || configOrKey === null) {
|
||
|
this.config = { OPENAI_API_KEY: configOrKey || "" }
|
||
|
} else {
|
||
|
this.config = configOrKey
|
||
|
}
|
||
|
}
|
||
|
|
||
|
getAnchorWallet() {
|
||
|
const adapter = this.wallet
|
||
|
return {
|
||
|
publicKey: adapter.publicKey,
|
||
|
signTransaction: adapter.signTransaction.bind(adapter),
|
||
|
signAllTransactions: adapter.signAllTransactions.bind(adapter),
|
||
|
payer: adapter
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
async getBalance(token_address) {
|
||
|
return get_balance(this, token_address)
|
||
|
}
|
||
|
|
||
|
async getBalanceOther(walletAddress, tokenAddress) {
|
||
|
return get_balance_other(this, walletAddress, tokenAddress)
|
||
|
}
|
||
|
|
||
|
async createImage(prompt) {
|
||
|
return create_image(this, prompt)
|
||
|
}
|
||
|
|
||
|
async getWalletAddress() {
|
||
|
return get_wallet_address(this)
|
||
|
}
|
||
|
|
||
|
async transfer(to, amount, mint) {
|
||
|
return transfer(this, to, amount, mint)
|
||
|
}
|
||
|
|
||
|
async resolveSolDomain(domain) {
|
||
|
return resolveSolDomain(this, domain)
|
||
|
}
|
||
|
|
||
|
async getPrimaryDomain(account) {
|
||
|
return getPrimaryDomain(this, account)
|
||
|
}
|
||
|
|
||
|
async trade(
|
||
|
outputMint,
|
||
|
inputAmount,
|
||
|
inputMint,
|
||
|
slippageBps = DEFAULT_OPTIONS.SLIPPAGE_BPS
|
||
|
) {
|
||
|
return trade(this, outputMint, inputAmount, inputMint, slippageBps)
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
async getTPS() {
|
||
|
return getTPS(this)
|
||
|
}
|
||
|
|
||
|
async fetchTokenPrice(mint) {
|
||
|
return fetchPrice(new PublicKey(mint))
|
||
|
}
|
||
|
|
||
|
async launchPumpFunToken(
|
||
|
tokenName,
|
||
|
tokenTicker,
|
||
|
description,
|
||
|
imageUrl,
|
||
|
options
|
||
|
) {
|
||
|
return launchPumpFunToken(
|
||
|
this,
|
||
|
tokenName,
|
||
|
tokenTicker,
|
||
|
description,
|
||
|
imageUrl,
|
||
|
options
|
||
|
)
|
||
|
}
|
||
|
|
||
|
async stake(amount) {
|
||
|
return stakeWithJup(this, amount)
|
||
|
}
|
||
|
|
||
|
|
||
|
async createGibworkTask(
|
||
|
title,
|
||
|
content,
|
||
|
requirements,
|
||
|
tags,
|
||
|
tokenMintAddress,
|
||
|
tokenAmount,
|
||
|
payer
|
||
|
) {
|
||
|
return create_gibwork_task(
|
||
|
this,
|
||
|
title,
|
||
|
content,
|
||
|
requirements,
|
||
|
tags,
|
||
|
new PublicKey(tokenMintAddress),
|
||
|
tokenAmount,
|
||
|
payer ? new PublicKey(payer) : undefined
|
||
|
)
|
||
|
}
|
||
|
|
||
|
async createTiplink(amount, splmintAddress) {
|
||
|
return create_TipLink(this, amount, splmintAddress)
|
||
|
}
|
||
|
|
||
|
|
||
|
async closeEmptyTokenAccounts() {
|
||
|
return closeEmptyTokenAccounts(this)
|
||
|
}
|
||
|
|
||
|
async fetchTokenReportSummary(mint) {
|
||
|
return fetchTokenReportSummary(mint)
|
||
|
}
|
||
|
|
||
|
async fetchTokenDetailedReport(mint) {
|
||
|
return fetchTokenDetailedReport(mint)
|
||
|
}
|
||
|
|
||
|
async heliusParseTransactions(transactionId) {
|
||
|
return parseTransaction(this, transactionId)
|
||
|
}
|
||
|
async getAllAssetsbyOwner(owner, limit) {
|
||
|
return getAssetsByOwner(this, owner, limit)
|
||
|
}
|
||
|
|
||
|
}
|