diff --git a/packages/plugin-starknet/package.json b/packages/plugin-starknet/package.json index 363b9645..9cdc6b18 100644 --- a/packages/plugin-starknet/package.json +++ b/packages/plugin-starknet/package.json @@ -6,6 +6,7 @@ "types": "dist/index.d.ts", "dependencies": { "@ai16z/eliza": "workspace:*", + "@ai16z/plugin-trustdb": "workspace:*", "@avnu/avnu-sdk": "^2.1.1", "starknet": "^6.11.0", "tsup": "^8.3.5", diff --git a/packages/plugin-starknet/src/actions/swap.ts b/packages/plugin-starknet/src/actions/swap.ts index 9d4b350d..55dc779b 100644 --- a/packages/plugin-starknet/src/actions/swap.ts +++ b/packages/plugin-starknet/src/actions/swap.ts @@ -14,8 +14,8 @@ import { fetchQuotes, QuoteRequest, } from "@avnu/avnu-sdk"; -import { getStarknetAccount } from "../providers/wallet.ts"; -import { validateSettings } from "../utils/index.ts"; + +import { getStarknetAccount, validateSettings } from "../utils/index.ts"; const swapTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined. diff --git a/packages/plugin-starknet/src/providers/token.ts b/packages/plugin-starknet/src/providers/token.ts new file mode 100644 index 00000000..fb6a952c --- /dev/null +++ b/packages/plugin-starknet/src/providers/token.ts @@ -0,0 +1,1064 @@ +import { settings } from "@ai16z/eliza"; +import { IAgentRuntime, Memory, Provider, State } from "@ai16z/eliza"; +import { + DexScreenerData, + DexScreenerPair, + HolderData, + ProcessedTokenData, + TokenSecurityData, + TokenTradeData, + CalculatedBuyAmounts, + Prices, +} from "../types/trustDB.ts"; +import * as fs from "fs"; +import NodeCache from "node-cache"; +import * as path from "path"; +import { WalletProvider, Item } from "./walletProvider.ts"; +import { num } from "starknet"; +import { + analyzeHighSupplyHolders, + evaluateTokenTrading, + TokenMetrics, +} from "./utils.ts"; +// import { Connection, PublicKey } from "@solana/web3.js"; + +const PROVIDER_CONFIG = { + BIRDEYE_API: "https://public-api.birdeye.so", + MAX_RETRIES: 3, + RETRY_DELAY: 2000, + DEFAULT_RPC: "https://api.mainnet-beta.solana.com", + TOKEN_ADDRESSES: { + SOL: "So11111111111111111111111111111111111111112", + BTC: "qfnqNqs3nCAHjnyCgLRDbBtq4p2MtHZxw8YjSyYhPoL", + ETH: "7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs", + Example: "2weMjPLLybRMMva1fM3U31goWWrCpF59CHWNhnCJ9Vyh", + }, + TOKEN_SECURITY_ENDPOINT: "/defi/token_security?address=", + TOKEN_TRADE_DATA_ENDPOINT: "/defi/v3/token/trade-data/single?address=", + DEX_SCREENER_API: "https://api.dexscreener.com/latest/dex/tokens/", + MAIN_WALLET: "", +}; + +export class TokenProvider { + private cache: NodeCache; + private cacheDir: string; + + constructor( + // private connection: Connection, + private tokenAddress: string, + private walletProvider: WalletProvider + ) { + this.cache = new NodeCache({ stdTTL: 300 }); // 5 minutes cache + const __dirname = path.resolve(); + + // Find the 'eliza' folder in the filepath and adjust the cache directory path + const elizaIndex = __dirname.indexOf("eliza"); + if (elizaIndex !== -1) { + const pathToEliza = __dirname.slice(0, elizaIndex + 5); // include 'eliza' + this.cacheDir = path.join(pathToEliza, "cache"); + } else { + this.cacheDir = path.join(__dirname, "cache"); + } + + this.cacheDir = path.join(__dirname, "cache"); + if (!fs.existsSync(this.cacheDir)) { + fs.mkdirSync(this.cacheDir); + } + } + + private readCacheFromFile(cacheKey: string): T | null { + const filePath = path.join(this.cacheDir, `${cacheKey}.json`); + console.log({ filePath }); + if (fs.existsSync(filePath)) { + const fileContent = fs.readFileSync(filePath, "utf-8"); + const parsed = JSON.parse(fileContent); + const now = Date.now(); + if (now < parsed.expiry) { + console.log( + `Reading cached data from file for key: ${cacheKey}` + ); + return parsed.data as T; + } else { + console.log( + `Cache expired for key: ${cacheKey}. Deleting file.` + ); + fs.unlinkSync(filePath); + } + } + return null; + } + + private writeCacheToFile(cacheKey: string, data: T): void { + const filePath = path.join(this.cacheDir, `${cacheKey}.json`); + const cacheData = { + data: data, + expiry: Date.now() + 300000, // 5 minutes in milliseconds + }; + fs.writeFileSync(filePath, JSON.stringify(cacheData), "utf-8"); + console.log(`Cached data written to file for key: ${cacheKey}`); + } + + private getCachedData(cacheKey: string): T | null { + // Check in-memory cache first + const cachedData = this.cache.get(cacheKey); + if (cachedData) { + return cachedData; + } + + // Check file-based cache + const fileCachedData = this.readCacheFromFile(cacheKey); + if (fileCachedData) { + // Populate in-memory cache + this.cache.set(cacheKey, fileCachedData); + return fileCachedData; + } + + return null; + } + + private setCachedData(cacheKey: string, data: T): void { + // Set in-memory cache + this.cache.set(cacheKey, data); + + // Write to file-based cache + this.writeCacheToFile(cacheKey, data); + } + + // TODO: remove this + private async fetchWithRetry( + url: string, + options: RequestInit = {} + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ): Promise { + let lastError: Error; + + for (let i = 0; i < PROVIDER_CONFIG.MAX_RETRIES; i++) { + try { + const response = await fetch(url, { + ...options, + headers: { + Accept: "application/json", + "x-chain": "solana", + "X-API-KEY": settings.BIRDEYE_API_KEY || "", + ...options.headers, + }, + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `HTTP error! status: ${response.status}, message: ${errorText}` + ); + } + + const data = await response.json(); + return data; + } catch (error) { + console.error(`Attempt ${i + 1} failed:`, error); + lastError = error as Error; + if (i < PROVIDER_CONFIG.MAX_RETRIES - 1) { + const delay = PROVIDER_CONFIG.RETRY_DELAY * Math.pow(2, i); + console.log(`Waiting ${delay}ms before retrying...`); + await new Promise((resolve) => setTimeout(resolve, delay)); + continue; + } + } + } + + console.error( + "All attempts failed. Throwing the last error:", + lastError + ); + throw lastError; + } + + // TODO: Update to Starknet + async getTokensInWallet(runtime: IAgentRuntime): Promise { + const walletInfo = + await this.walletProvider.fetchPortfolioValue(runtime); + const items = walletInfo.items; + return items; + } + + // check if the token symbol is in the wallet + async getTokenFromWallet(runtime: IAgentRuntime, tokenSymbol: string) { + try { + const items = await this.getTokensInWallet(runtime); + const token = items.find((item) => item.symbol === tokenSymbol); + + if (token) { + return token.address; + } else { + return null; + } + } catch (error) { + console.error("Error checking token in wallet:", error); + return null; + } + } + + // TODO: Update to Starknet + async fetchPrices(): Promise { + try { + const cacheKey = "prices"; + const cachedData = this.getCachedData(cacheKey); + if (cachedData) { + console.log("Returning cached prices."); + return cachedData; + } + const { SOL, BTC, ETH } = PROVIDER_CONFIG.TOKEN_ADDRESSES; + const tokens = [SOL, BTC, ETH]; + const prices: Prices = { + solana: { usd: "0" }, + bitcoin: { usd: "0" }, + ethereum: { usd: "0" }, + }; + + for (const token of tokens) { + const response = await this.fetchWithRetry( + `${PROVIDER_CONFIG.BIRDEYE_API}/defi/price?address=${token}`, + { + headers: { + "x-chain": "solana", + }, + } + ); + + if (response?.data?.value) { + const price = response.data.value.toString(); + prices[ + token === SOL + ? "solana" + : token === BTC + ? "bitcoin" + : "ethereum" + ].usd = price; + } else { + console.warn(`No price data available for token: ${token}`); + } + } + this.setCachedData(cacheKey, prices); + return prices; + } catch (error) { + console.error("Error fetching prices:", error); + throw error; + } + } + + // TODO: change to starknet + async calculateBuyAmounts(): Promise { + const dexScreenerData = await this.fetchDexScreenerData(); + const prices = await this.fetchPrices(); + const solPrice = num.toBigInt(prices.solana.usd); + + if (!dexScreenerData || dexScreenerData.pairs.length === 0) { + return { none: 0, low: 0, medium: 0, high: 0 }; + } + + // Get the first pair + const pair = dexScreenerData.pairs[0]; + const { liquidity, marketCap } = pair; + if (!liquidity || !marketCap) { + return { none: 0, low: 0, medium: 0, high: 0 }; + } + + if (liquidity.usd === 0) { + return { none: 0, low: 0, medium: 0, high: 0 }; + } + if (marketCap < 100000) { + return { none: 0, low: 0, medium: 0, high: 0 }; + } + + // impact percentages based on liquidity + const impactPercentages = { + LOW: 0.01, // 1% of liquidity + MEDIUM: 0.05, // 5% of liquidity + HIGH: 0.1, // 10% of liquidity + }; + + // Calculate buy amounts in USD + const lowBuyAmountUSD = liquidity.usd * impactPercentages.LOW; + const mediumBuyAmountUSD = liquidity.usd * impactPercentages.MEDIUM; + const highBuyAmountUSD = liquidity.usd * impactPercentages.HIGH; + + // Convert each buy amount to SOL + const lowBuyAmountSOL = num.toBigInt(lowBuyAmountUSD) / solPrice; + const mediumBuyAmountSOL = num.toBigInt(mediumBuyAmountUSD) / solPrice; + const highBuyAmountSOL = num.toBigInt(highBuyAmountUSD) / solPrice; + + return { + none: 0, + low: Number(lowBuyAmountSOL), + medium: Number(mediumBuyAmountSOL), + high: Number(highBuyAmountSOL), + }; + } + + // TODO: Update to Starknet + async fetchTokenSecurity(): Promise { + const cacheKey = `tokenSecurity_${this.tokenAddress}`; + const cachedData = this.getCachedData(cacheKey); + if (cachedData) { + console.log( + `Returning cached token security data for ${this.tokenAddress}.` + ); + return cachedData; + } + const url = `${PROVIDER_CONFIG.BIRDEYE_API}${PROVIDER_CONFIG.TOKEN_SECURITY_ENDPOINT}${this.tokenAddress}`; + const data = await this.fetchWithRetry(url); + + if (!data?.success || !data?.data) { + throw new Error("No token security data available"); + } + + const security: TokenSecurityData = { + ownerBalance: data.data.ownerBalance, + creatorBalance: data.data.creatorBalance, + ownerPercentage: data.data.ownerPercentage, + creatorPercentage: data.data.creatorPercentage, + top10HolderBalance: data.data.top10HolderBalance, + top10HolderPercent: data.data.top10HolderPercent, + }; + this.setCachedData(cacheKey, security); + console.log(`Token security data cached for ${this.tokenAddress}.`); + + return security; + } + + // TODO: Update to Starknet + async fetchTokenTradeData(): Promise { + const cacheKey = `tokenTradeData_${this.tokenAddress}`; + const cachedData = this.getCachedData(cacheKey); + if (cachedData) { + console.log( + `Returning cached token trade data for ${this.tokenAddress}.` + ); + return cachedData; + } + + const url = `${PROVIDER_CONFIG.BIRDEYE_API}${PROVIDER_CONFIG.TOKEN_TRADE_DATA_ENDPOINT}${this.tokenAddress}`; + const options = { + method: "GET", + headers: { + accept: "application/json", + "X-API-KEY": settings.BIRDEYE_API_KEY || "", + }, + }; + + const data = await fetch(url, options) + .then((res) => res.json()) + .catch((err) => console.error(err)); + + if (!data?.success || !data?.data) { + throw new Error("No token trade data available"); + } + + const tradeData: TokenTradeData = { + address: data.data.address, + holder: data.data.holder, + market: data.data.market, + last_trade_unix_time: data.data.last_trade_unix_time, + last_trade_human_time: data.data.last_trade_human_time, + price: data.data.price, + history_30m_price: data.data.history_30m_price, + price_change_30m_percent: data.data.price_change_30m_percent, + history_1h_price: data.data.history_1h_price, + price_change_1h_percent: data.data.price_change_1h_percent, + history_2h_price: data.data.history_2h_price, + price_change_2h_percent: data.data.price_change_2h_percent, + history_4h_price: data.data.history_4h_price, + price_change_4h_percent: data.data.price_change_4h_percent, + history_6h_price: data.data.history_6h_price, + price_change_6h_percent: data.data.price_change_6h_percent, + history_8h_price: data.data.history_8h_price, + price_change_8h_percent: data.data.price_change_8h_percent, + history_12h_price: data.data.history_12h_price, + price_change_12h_percent: data.data.price_change_12h_percent, + history_24h_price: data.data.history_24h_price, + price_change_24h_percent: data.data.price_change_24h_percent, + unique_wallet_30m: data.data.unique_wallet_30m, + unique_wallet_history_30m: data.data.unique_wallet_history_30m, + unique_wallet_30m_change_percent: + data.data.unique_wallet_30m_change_percent, + unique_wallet_1h: data.data.unique_wallet_1h, + unique_wallet_history_1h: data.data.unique_wallet_history_1h, + unique_wallet_1h_change_percent: + data.data.unique_wallet_1h_change_percent, + unique_wallet_2h: data.data.unique_wallet_2h, + unique_wallet_history_2h: data.data.unique_wallet_history_2h, + unique_wallet_2h_change_percent: + data.data.unique_wallet_2h_change_percent, + unique_wallet_4h: data.data.unique_wallet_4h, + unique_wallet_history_4h: data.data.unique_wallet_history_4h, + unique_wallet_4h_change_percent: + data.data.unique_wallet_4h_change_percent, + unique_wallet_8h: data.data.unique_wallet_8h, + unique_wallet_history_8h: data.data.unique_wallet_history_8h, + unique_wallet_8h_change_percent: + data.data.unique_wallet_8h_change_percent, + unique_wallet_24h: data.data.unique_wallet_24h, + unique_wallet_history_24h: data.data.unique_wallet_history_24h, + unique_wallet_24h_change_percent: + data.data.unique_wallet_24h_change_percent, + trade_30m: data.data.trade_30m, + trade_history_30m: data.data.trade_history_30m, + trade_30m_change_percent: data.data.trade_30m_change_percent, + sell_30m: data.data.sell_30m, + sell_history_30m: data.data.sell_history_30m, + sell_30m_change_percent: data.data.sell_30m_change_percent, + buy_30m: data.data.buy_30m, + buy_history_30m: data.data.buy_history_30m, + buy_30m_change_percent: data.data.buy_30m_change_percent, + volume_30m: data.data.volume_30m, + volume_30m_usd: data.data.volume_30m_usd, + volume_history_30m: data.data.volume_history_30m, + volume_history_30m_usd: data.data.volume_history_30m_usd, + volume_30m_change_percent: data.data.volume_30m_change_percent, + volume_buy_30m: data.data.volume_buy_30m, + volume_buy_30m_usd: data.data.volume_buy_30m_usd, + volume_buy_history_30m: data.data.volume_buy_history_30m, + volume_buy_history_30m_usd: data.data.volume_buy_history_30m_usd, + volume_buy_30m_change_percent: + data.data.volume_buy_30m_change_percent, + volume_sell_30m: data.data.volume_sell_30m, + volume_sell_30m_usd: data.data.volume_sell_30m_usd, + volume_sell_history_30m: data.data.volume_sell_history_30m, + volume_sell_history_30m_usd: data.data.volume_sell_history_30m_usd, + volume_sell_30m_change_percent: + data.data.volume_sell_30m_change_percent, + trade_1h: data.data.trade_1h, + trade_history_1h: data.data.trade_history_1h, + trade_1h_change_percent: data.data.trade_1h_change_percent, + sell_1h: data.data.sell_1h, + sell_history_1h: data.data.sell_history_1h, + sell_1h_change_percent: data.data.sell_1h_change_percent, + buy_1h: data.data.buy_1h, + buy_history_1h: data.data.buy_history_1h, + buy_1h_change_percent: data.data.buy_1h_change_percent, + volume_1h: data.data.volume_1h, + volume_1h_usd: data.data.volume_1h_usd, + volume_history_1h: data.data.volume_history_1h, + volume_history_1h_usd: data.data.volume_history_1h_usd, + volume_1h_change_percent: data.data.volume_1h_change_percent, + volume_buy_1h: data.data.volume_buy_1h, + volume_buy_1h_usd: data.data.volume_buy_1h_usd, + volume_buy_history_1h: data.data.volume_buy_history_1h, + volume_buy_history_1h_usd: data.data.volume_buy_history_1h_usd, + volume_buy_1h_change_percent: + data.data.volume_buy_1h_change_percent, + volume_sell_1h: data.data.volume_sell_1h, + volume_sell_1h_usd: data.data.volume_sell_1h_usd, + volume_sell_history_1h: data.data.volume_sell_history_1h, + volume_sell_history_1h_usd: data.data.volume_sell_history_1h_usd, + volume_sell_1h_change_percent: + data.data.volume_sell_1h_change_percent, + trade_2h: data.data.trade_2h, + trade_history_2h: data.data.trade_history_2h, + trade_2h_change_percent: data.data.trade_2h_change_percent, + sell_2h: data.data.sell_2h, + sell_history_2h: data.data.sell_history_2h, + sell_2h_change_percent: data.data.sell_2h_change_percent, + buy_2h: data.data.buy_2h, + buy_history_2h: data.data.buy_history_2h, + buy_2h_change_percent: data.data.buy_2h_change_percent, + volume_2h: data.data.volume_2h, + volume_2h_usd: data.data.volume_2h_usd, + volume_history_2h: data.data.volume_history_2h, + volume_history_2h_usd: data.data.volume_history_2h_usd, + volume_2h_change_percent: data.data.volume_2h_change_percent, + volume_buy_2h: data.data.volume_buy_2h, + volume_buy_2h_usd: data.data.volume_buy_2h_usd, + volume_buy_history_2h: data.data.volume_buy_history_2h, + volume_buy_history_2h_usd: data.data.volume_buy_history_2h_usd, + volume_buy_2h_change_percent: + data.data.volume_buy_2h_change_percent, + volume_sell_2h: data.data.volume_sell_2h, + volume_sell_2h_usd: data.data.volume_sell_2h_usd, + volume_sell_history_2h: data.data.volume_sell_history_2h, + volume_sell_history_2h_usd: data.data.volume_sell_history_2h_usd, + volume_sell_2h_change_percent: + data.data.volume_sell_2h_change_percent, + trade_4h: data.data.trade_4h, + trade_history_4h: data.data.trade_history_4h, + trade_4h_change_percent: data.data.trade_4h_change_percent, + sell_4h: data.data.sell_4h, + sell_history_4h: data.data.sell_history_4h, + sell_4h_change_percent: data.data.sell_4h_change_percent, + buy_4h: data.data.buy_4h, + buy_history_4h: data.data.buy_history_4h, + buy_4h_change_percent: data.data.buy_4h_change_percent, + volume_4h: data.data.volume_4h, + volume_4h_usd: data.data.volume_4h_usd, + volume_history_4h: data.data.volume_history_4h, + volume_history_4h_usd: data.data.volume_history_4h_usd, + volume_4h_change_percent: data.data.volume_4h_change_percent, + volume_buy_4h: data.data.volume_buy_4h, + volume_buy_4h_usd: data.data.volume_buy_4h_usd, + volume_buy_history_4h: data.data.volume_buy_history_4h, + volume_buy_history_4h_usd: data.data.volume_buy_history_4h_usd, + volume_buy_4h_change_percent: + data.data.volume_buy_4h_change_percent, + volume_sell_4h: data.data.volume_sell_4h, + volume_sell_4h_usd: data.data.volume_sell_4h_usd, + volume_sell_history_4h: data.data.volume_sell_history_4h, + volume_sell_history_4h_usd: data.data.volume_sell_history_4h_usd, + volume_sell_4h_change_percent: + data.data.volume_sell_4h_change_percent, + trade_8h: data.data.trade_8h, + trade_history_8h: data.data.trade_history_8h, + trade_8h_change_percent: data.data.trade_8h_change_percent, + sell_8h: data.data.sell_8h, + sell_history_8h: data.data.sell_history_8h, + sell_8h_change_percent: data.data.sell_8h_change_percent, + buy_8h: data.data.buy_8h, + buy_history_8h: data.data.buy_history_8h, + buy_8h_change_percent: data.data.buy_8h_change_percent, + volume_8h: data.data.volume_8h, + volume_8h_usd: data.data.volume_8h_usd, + volume_history_8h: data.data.volume_history_8h, + volume_history_8h_usd: data.data.volume_history_8h_usd, + volume_8h_change_percent: data.data.volume_8h_change_percent, + volume_buy_8h: data.data.volume_buy_8h, + volume_buy_8h_usd: data.data.volume_buy_8h_usd, + volume_buy_history_8h: data.data.volume_buy_history_8h, + volume_buy_history_8h_usd: data.data.volume_buy_history_8h_usd, + volume_buy_8h_change_percent: + data.data.volume_buy_8h_change_percent, + volume_sell_8h: data.data.volume_sell_8h, + volume_sell_8h_usd: data.data.volume_sell_8h_usd, + volume_sell_history_8h: data.data.volume_sell_history_8h, + volume_sell_history_8h_usd: data.data.volume_sell_history_8h_usd, + volume_sell_8h_change_percent: + data.data.volume_sell_8h_change_percent, + trade_24h: data.data.trade_24h, + trade_history_24h: data.data.trade_history_24h, + trade_24h_change_percent: data.data.trade_24h_change_percent, + sell_24h: data.data.sell_24h, + sell_history_24h: data.data.sell_history_24h, + sell_24h_change_percent: data.data.sell_24h_change_percent, + buy_24h: data.data.buy_24h, + buy_history_24h: data.data.buy_history_24h, + buy_24h_change_percent: data.data.buy_24h_change_percent, + volume_24h: data.data.volume_24h, + volume_24h_usd: data.data.volume_24h_usd, + volume_history_24h: data.data.volume_history_24h, + volume_history_24h_usd: data.data.volume_history_24h_usd, + volume_24h_change_percent: data.data.volume_24h_change_percent, + volume_buy_24h: data.data.volume_buy_24h, + volume_buy_24h_usd: data.data.volume_buy_24h_usd, + volume_buy_history_24h: data.data.volume_buy_history_24h, + volume_buy_history_24h_usd: data.data.volume_buy_history_24h_usd, + volume_buy_24h_change_percent: + data.data.volume_buy_24h_change_percent, + volume_sell_24h: data.data.volume_sell_24h, + volume_sell_24h_usd: data.data.volume_sell_24h_usd, + volume_sell_history_24h: data.data.volume_sell_history_24h, + volume_sell_history_24h_usd: data.data.volume_sell_history_24h_usd, + volume_sell_24h_change_percent: + data.data.volume_sell_24h_change_percent, + }; + this.setCachedData(cacheKey, tradeData); + return tradeData; + } + + async fetchDexScreenerData(): Promise { + const cacheKey = `dexScreenerData_${this.tokenAddress}`; + const cachedData = this.getCachedData(cacheKey); + if (cachedData) { + console.log("Returning cached DexScreener data."); + return cachedData; + } + + const url = `https://api.dexscreener.com/latest/dex/search?q=${this.tokenAddress}`; + try { + console.log( + `Fetching DexScreener data for token: ${this.tokenAddress}` + ); + const data = await fetch(url) + .then((res) => res.json()) + .catch((err) => { + console.error(err); + }); + + if (!data || !data.pairs) { + throw new Error("No DexScreener data available"); + } + + const dexData: DexScreenerData = { + schemaVersion: data.schemaVersion, + pairs: data.pairs, + }; + + // Cache the result + this.setCachedData(cacheKey, dexData); + + return dexData; + } catch (error) { + console.error(`Error fetching DexScreener data:`, error); + return { + schemaVersion: "1.0.0", + pairs: [], + }; + } + } + + async searchDexScreenerData( + symbol: string + ): Promise { + const cacheKey = `dexScreenerData_search_${symbol}`; + const cachedData = this.getCachedData(cacheKey); + if (cachedData) { + console.log("Returning cached search DexScreener data."); + return this.getHighestLiquidityPair(cachedData); + } + + const url = `https://api.dexscreener.com/latest/dex/search?q=${symbol}`; + try { + console.log(`Fetching DexScreener data for symbol: ${symbol}`); + const data = await fetch(url) + .then((res) => res.json()) + .catch((err) => { + console.error(err); + return null; + }); + + if (!data || !data.pairs || data.pairs.length === 0) { + throw new Error("No DexScreener data available"); + } + + const dexData: DexScreenerData = { + schemaVersion: data.schemaVersion, + pairs: data.pairs, + }; + + // Cache the result + this.setCachedData(cacheKey, dexData); + + // Return the pair with the highest liquidity and market cap + return this.getHighestLiquidityPair(dexData); + } catch (error) { + console.error(`Error fetching DexScreener data:`, error); + return null; + } + } + getHighestLiquidityPair(dexData: DexScreenerData): DexScreenerPair | null { + if (dexData.pairs.length === 0) { + return null; + } + + // Sort pairs by both liquidity and market cap to get the highest one + return dexData.pairs.reduce((highestPair, currentPair) => { + const currentLiquidity = currentPair.liquidity.usd; + const currentMarketCap = currentPair.marketCap; + const highestLiquidity = highestPair.liquidity.usd; + const highestMarketCap = highestPair.marketCap; + + if ( + currentLiquidity > highestLiquidity || + (currentLiquidity === highestLiquidity && + currentMarketCap > highestMarketCap) + ) { + return currentPair; + } + return highestPair; + }); + } + + async analyzeHolderDistribution( + tradeData: TokenTradeData + ): Promise { + // Define the time intervals to consider (e.g., 30m, 1h, 2h) + const intervals = [ + { + period: "30m", + change: tradeData.unique_wallet_30m_change_percent, + }, + { period: "1h", change: tradeData.unique_wallet_1h_change_percent }, + { period: "2h", change: tradeData.unique_wallet_2h_change_percent }, + { period: "4h", change: tradeData.unique_wallet_4h_change_percent }, + { period: "8h", change: tradeData.unique_wallet_8h_change_percent }, + { + period: "24h", + change: tradeData.unique_wallet_24h_change_percent, + }, + ]; + + // Calculate the average change percentage + const validChanges = intervals + .map((interval) => interval.change) + .filter( + (change) => change !== null && change !== undefined + ) as number[]; + + if (validChanges.length === 0) { + return "stable"; + } + + const averageChange = + validChanges.reduce((acc, curr) => acc + curr, 0) / + validChanges.length; + + const increaseThreshold = 10; // e.g., average change > 10% + const decreaseThreshold = -10; // e.g., average change < -10% + + if (averageChange > increaseThreshold) { + return "increasing"; + } else if (averageChange < decreaseThreshold) { + return "decreasing"; + } else { + return "stable"; + } + } + + // TODO: Update to Starknet + async fetchHolderList(): Promise { + const cacheKey = `holderList_${this.tokenAddress}`; + const cachedData = this.getCachedData(cacheKey); + if (cachedData) { + console.log("Returning cached holder list."); + return cachedData; + } + + const allHoldersMap = new Map(); + let page = 1; + const limit = 1000; + let cursor; + //HELIOUS_API_KEY needs to be added + const url = `https://mainnet.helius-rpc.com/?api-key=${settings.HELIUS_API_KEY || ""}`; + console.log({ url }); + + try { + // eslint-disable-next-line no-constant-condition + while (true) { + const params = { + limit: limit, + displayOptions: {}, + mint: this.tokenAddress, + cursor: cursor, + }; + if (cursor != undefined) { + params.cursor = cursor; + } + console.log(`Fetching holders - Page ${page}`); + if (page > 2) { + break; + } + const response = await fetch(url, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + jsonrpc: "2.0", + id: "helius-test", + method: "getTokenAccounts", + params: params, + }), + }); + + const data = await response.json(); + + if ( + !data || + !data.result || + !data.result.token_accounts || + data.result.token_accounts.length === 0 + ) { + console.log( + `No more holders found. Total pages fetched: ${page - 1}` + ); + break; + } + + console.log( + `Processing ${data.result.token_accounts.length} holders from page ${page}` + ); + + // eslint-disable-next-line @typescript-eslint/no-explicit-any + data.result.token_accounts.forEach((account: any) => { + const owner = account.owner; + const balance = parseFloat(account.amount); + + if (allHoldersMap.has(owner)) { + allHoldersMap.set( + owner, + allHoldersMap.get(owner)! + balance + ); + } else { + allHoldersMap.set(owner, balance); + } + }); + cursor = data.result.cursor; + page++; + } + + const holders: HolderData[] = Array.from( + allHoldersMap.entries() + ).map(([address, balance]) => ({ + address, + balance: balance.toString(), + })); + + console.log(`Total unique holders fetched: ${holders.length}`); + + // Cache the result + this.setCachedData(cacheKey, holders); + + return holders; + } catch (error) { + console.error("Error fetching holder list from Helius:", error); + throw new Error("Failed to fetch holder list from Helius."); + } + } + + async filterHighValueHolders( + tradeData: TokenTradeData + ): Promise> { + const holdersData = await this.fetchHolderList(); + + const tokenPriceUsd = num.toBigInt(tradeData.price); + + const highValueHolders = holdersData + .filter((holder) => { + const balanceUsd = num.toBigInt(holder.balance) * tokenPriceUsd; + return balanceUsd > 5; + }) + .map((holder) => ({ + holderAddress: holder.address, + balanceUsd: ( + num.toBigInt(holder.balance) * tokenPriceUsd + ).toString(), + })); + + return highValueHolders; + } + + async checkRecentTrades(volume24hUsd: bigint): Promise { + return volume24hUsd > 0; + } + + async countHighSupplyHolders( + securityData: TokenSecurityData + ): Promise { + try { + const holders = await this.fetchHolderList(); + const result = analyzeHighSupplyHolders({ + holders, + ownerBalance: securityData.ownerBalance, + creatorBalance: securityData.creatorBalance, + }); + + return result.count; + } catch (error) { + console.error("Error counting high supply holders:", error); + return 0; + } + } + + async getProcessedTokenData(): Promise { + try { + console.log( + `Fetching security data for token: ${this.tokenAddress}` + ); + const security = await this.fetchTokenSecurity(); + + console.log(`Fetching trade data for token: ${this.tokenAddress}`); + const tradeData = await this.fetchTokenTradeData(); + + console.log( + `Fetching DexScreener data for token: ${this.tokenAddress}` + ); + const dexData = await this.fetchDexScreenerData(); + + console.log( + `Analyzing holder distribution for token: ${this.tokenAddress}` + ); + const holderDistributionTrend = + await this.analyzeHolderDistribution(tradeData); + + console.log( + `Filtering high-value holders for token: ${this.tokenAddress}` + ); + const highValueHolders = + await this.filterHighValueHolders(tradeData); + + console.log( + `Checking recent trades for token: ${this.tokenAddress}` + ); + const recentTrades = await this.checkRecentTrades( + num.toBigInt(tradeData.volume_24h_usd) + ); + + console.log( + `Counting high-supply holders for token: ${this.tokenAddress}` + ); + const highSupplyHoldersCount = + await this.countHighSupplyHolders(security); + + console.log( + `Determining DexScreener listing status for token: ${this.tokenAddress}` + ); + const isDexScreenerListed = dexData.pairs.length > 0; + const isDexScreenerPaid = dexData.pairs.some( + (pair) => pair.boosts && pair.boosts.active > 0 + ); + + const processedData: ProcessedTokenData = { + security, + tradeData, + holderDistributionTrend, + highValueHolders, + recentTrades, + highSupplyHoldersCount, + dexScreenerData: dexData, + isDexScreenerListed, + isDexScreenerPaid, + }; + + // console.log("Processed token data:", processedData); + return processedData; + } catch (error) { + console.error("Error processing token data:", error); + throw error; + } + } + + async shouldTradeToken(): Promise { + try { + const tokenData = await this.getProcessedTokenData(); + const { tradeData, security, dexScreenerData } = tokenData; + const { ownerBalance, creatorBalance } = security; + const { liquidity, marketCap } = dexScreenerData.pairs[0]; + + const totalSupply = + num.toBigInt(ownerBalance) + num.toBigInt(creatorBalance); + + const metrics: TokenMetrics = { + liquidityUsd: num.toBigInt(liquidity.usd), + marketCapUsd: num.toBigInt(marketCap), + totalSupply, + ownerPercentage: + Number(num.toBigInt(ownerBalance)) / Number(totalSupply), + creatorPercentage: + Number(num.toBigInt(creatorBalance)) / Number(totalSupply), + top10HolderPercent: + Number(num.toBigInt(tradeData.volume_24h_usd)) / + Number(totalSupply), + priceChange24hPercent: Number( + num.toBigInt(tradeData.price_change_24h_percent) + ), + priceChange12hPercent: Number( + num.toBigInt(tradeData.price_change_12h_percent) + ), + uniqueWallet24h: tradeData.unique_wallet_24h, + volume24hUsd: num.toBigInt(tradeData.volume_24h_usd), + }; + + const { shouldTrade } = evaluateTokenTrading(metrics); + return shouldTrade; + } catch (error) { + console.error("Error processing token data:", error); + throw error; + } + } + + formatTokenData(data: ProcessedTokenData): string { + let output = `**Token Security and Trade Report**\n`; + output += `Token Address: ${this.tokenAddress}\n\n`; + + // Security Data + output += `**Ownership Distribution:**\n`; + output += `- Owner Balance: ${data.security.ownerBalance}\n`; + output += `- Creator Balance: ${data.security.creatorBalance}\n`; + output += `- Owner Percentage: ${data.security.ownerPercentage}%\n`; + output += `- Creator Percentage: ${data.security.creatorPercentage}%\n`; + output += `- Top 10 Holders Balance: ${data.security.top10HolderBalance}\n`; + output += `- Top 10 Holders Percentage: ${data.security.top10HolderPercent}%\n\n`; + + // Trade Data + output += `**Trade Data:**\n`; + output += `- Holders: ${data.tradeData.holder}\n`; + output += `- Unique Wallets (24h): ${data.tradeData.unique_wallet_24h}\n`; + output += `- Price Change (24h): ${data.tradeData.price_change_24h_percent}%\n`; + output += `- Price Change (12h): ${data.tradeData.price_change_12h_percent}%\n`; + output += `- Volume (24h USD): $${num + .toBigInt(data.tradeData.volume_24h_usd) + .toString()}\n`; + output += `- Current Price: $${num.toBigInt(data.tradeData.price).toString()}\n\n`; + + // Holder Distribution Trend + output += `**Holder Distribution Trend:** ${data.holderDistributionTrend}\n\n`; + + // High-Value Holders + output += `**High-Value Holders (>$5 USD):**\n`; + if (data.highValueHolders.length === 0) { + output += `- No high-value holders found or data not available.\n`; + } else { + data.highValueHolders.forEach((holder) => { + output += `- ${holder.holderAddress}: $${holder.balanceUsd}\n`; + }); + } + output += `\n`; + + // Recent Trades + output += `**Recent Trades (Last 24h):** ${data.recentTrades ? "Yes" : "No"}\n\n`; + + // High-Supply Holders + output += `**Holders with >2% Supply:** ${data.highSupplyHoldersCount}\n\n`; + + // DexScreener Status + output += `**DexScreener Listing:** ${data.isDexScreenerListed ? "Yes" : "No"}\n`; + if (data.isDexScreenerListed) { + output += `- Listing Type: ${data.isDexScreenerPaid ? "Paid" : "Free"}\n`; + output += `- Number of DexPairs: ${data.dexScreenerData.pairs.length}\n\n`; + output += `**DexScreener Pairs:**\n`; + data.dexScreenerData.pairs.forEach((pair, index) => { + output += `\n**Pair ${index + 1}:**\n`; + output += `- DEX: ${pair.dexId}\n`; + output += `- URL: ${pair.url}\n`; + output += `- Price USD: $${num.toBigInt(pair.priceUsd).toString()}\n`; + output += `- Volume (24h USD): $${num.toBigInt(pair.volume.h24).toString()}\n`; + output += `- Boosts Active: ${pair.boosts && pair.boosts.active}\n`; + output += `- Liquidity USD: $${num.toBigInt(pair.liquidity.usd).toString()}\n`; + }); + } + output += `\n`; + + console.log("Formatted token data:", output); + return output; + } + + async getFormattedTokenReport(): Promise { + try { + console.log("Generating formatted token report..."); + const processedData = await this.getProcessedTokenData(); + return this.formatTokenData(processedData); + } catch (error) { + console.error("Error generating token report:", error); + return "Unable to fetch token information. Please try again later."; + } + } +} + +// TODO: Check + +const tokenProvider: Provider = { + get: async ( + runtime: IAgentRuntime, + _message: Memory, + _state?: State, + tokenAddress?: string + ): Promise => { + try { + const walletProvider = new WalletProvider(runtime); + const provider = new TokenProvider(tokenAddress, walletProvider); + + return provider.getFormattedTokenReport(); + } catch (error) { + console.error("Error fetching token data:", error); + return "Unable to fetch token information. Please try again later."; + } + }, +}; + +export { tokenProvider }; diff --git a/packages/plugin-starknet/src/providers/trustScoreProvider.ts b/packages/plugin-starknet/src/providers/trustScoreProvider.ts new file mode 100644 index 00000000..809d8e92 --- /dev/null +++ b/packages/plugin-starknet/src/providers/trustScoreProvider.ts @@ -0,0 +1,646 @@ +import { + ProcessedTokenData, + TokenSecurityData, + // TokenTradeData, + // DexScreenerData, + // DexScreenerPair, + // HolderData, +} from "../types/trustDB.ts"; +// import { Connection, PublicKey } from "@solana/web3.js"; +// import { getAssociatedTokenAddress } from "@solana/spl-token"; +// import { TokenProvider } from "./token.ts"; +import { WalletProvider } from "./walletProvider.ts"; +import { + TrustScoreDatabase, + RecommenderMetrics, + TokenPerformance, + TradePerformance, + TokenRecommendation, +} from "@ai16z/plugin-trustdb"; +import { settings } from "@ai16z/eliza"; +import { IAgentRuntime, Memory, Provider, State } from "@ai16z/eliza"; +import { getTokenBalance } from "../utils/index.ts"; +import { walletProvider } from "./walletProvider.ts"; +import { TokenProvider } from "./token.ts"; + +const Wallet = settings.MAIN_WALLET_ADDRESS; +interface TradeData { + buy_amount: number; + is_simulation: boolean; +} +interface sellDetails { + sell_amount: number; + sell_recommender_id: string | null; +} +interface RecommendationGroup { + recommendation: any; + trustScore: number; +} + +interface RecommenderData { + recommenderId: string; + trustScore: number; + riskScore: number; + consistencyScore: number; + recommenderMetrics: RecommenderMetrics; +} + +interface TokenRecommendationSummary { + tokenAddress: string; + averageTrustScore: number; + averageRiskScore: number; + averageConsistencyScore: number; + recommenders: RecommenderData[]; +} +export class TrustScoreManager { + private tokenProvider: TokenProvider; + private trustScoreDb: TrustScoreDatabase; + private DECAY_RATE = 0.95; + private MAX_DECAY_DAYS = 30; + private backend; + private backendToken; + private runtime: IAgentRuntime; + constructor( + runtime: IAgentRuntime, + tokenProvider: TokenProvider, + trustScoreDb: TrustScoreDatabase + ) { + this.tokenProvider = tokenProvider; + this.trustScoreDb = trustScoreDb; + + // TODO: change to starknet + this.backend = runtime.getSetting("BACKEND_URL"); + + // TODO: change to starknet + this.backendToken = runtime.getSetting("BACKEND_TOKEN"); + + this.runtime = runtime; + } + + // Get Recommender Balance + async getRecommenderBalance(recommenderWallet: string): Promise { + try { + const tokenBalance = await getTokenBalance( + this.runtime, + recommenderWallet + ); + const balance = parseFloat(tokenBalance); + return balance; + } catch (error) { + console.error("Error fetching balance", error); + return 0; + } + } + + /** + * Generates and saves trust score based on processed token data and user recommendations. + * @param tokenAddress The address of the token to analyze. + * @param recommenderId The UUID of the recommender. + * @returns An object containing TokenPerformance and RecommenderMetrics. + */ + async generateTrustScore( + tokenAddress: string, + recommenderId: string, + recommenderWallet: string + ): Promise<{ + tokenPerformance: TokenPerformance; + recommenderMetrics: RecommenderMetrics; + }> { + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + console.log(`Fetched processed token data for token: ${tokenAddress}`); + + const recommenderMetrics = + await this.trustScoreDb.getRecommenderMetrics(recommenderId); + + const isRapidDump = await this.isRapidDump(tokenAddress); + const sustainedGrowth = await this.sustainedGrowth(tokenAddress); + const suspiciousVolume = await this.suspiciousVolume(tokenAddress); + const balance = await this.getRecommenderBalance(recommenderWallet); + const virtualConfidence = balance / 1000000; // TODO: create formula to calculate virtual confidence based on user balance + const lastActive = recommenderMetrics.lastActiveDate; + const now = new Date(); + const inactiveDays = Math.floor( + (now.getTime() - lastActive.getTime()) / (1000 * 60 * 60 * 24) + ); + const decayFactor = Math.pow( + this.DECAY_RATE, + Math.min(inactiveDays, this.MAX_DECAY_DAYS) + ); + const decayedScore = recommenderMetrics.trustScore * decayFactor; + const validationTrustScore = + this.trustScoreDb.calculateValidationTrust(tokenAddress); + + return { + tokenPerformance: { + tokenAddress: + processedData.dexScreenerData.pairs[0]?.baseToken.address || + "", + priceChange24h: + processedData.tradeData.price_change_24h_percent, + volumeChange24h: processedData.tradeData.volume_24h, + trade_24h_change: + processedData.tradeData.trade_24h_change_percent, + liquidity: + processedData.dexScreenerData.pairs[0]?.liquidity.usd || 0, + liquidityChange24h: 0, + holderChange24h: + processedData.tradeData.unique_wallet_24h_change_percent, + rugPull: false, // TODO: Implement rug pull detection + isScam: false, // TODO: Implement scam detection + marketCapChange24h: 0, // TODO: Implement market cap change + sustainedGrowth: sustainedGrowth, + rapidDump: isRapidDump, + suspiciousVolume: suspiciousVolume, + validationTrust: validationTrustScore, + lastUpdated: new Date(), + }, + recommenderMetrics: { + recommenderId: recommenderId, + trustScore: recommenderMetrics.trustScore, + totalRecommendations: recommenderMetrics.totalRecommendations, + successfulRecs: recommenderMetrics.successfulRecs, + avgTokenPerformance: recommenderMetrics.avgTokenPerformance, + riskScore: recommenderMetrics.riskScore, + consistencyScore: recommenderMetrics.consistencyScore, + virtualConfidence: virtualConfidence, + lastActiveDate: now, + trustDecay: decayedScore, + lastUpdated: new Date(), + }, + }; + } + + async updateRecommenderMetrics( + recommenderId: string, + tokenPerformance: TokenPerformance, + recommenderWallet: string + ): Promise { + const recommenderMetrics = + await this.trustScoreDb.getRecommenderMetrics(recommenderId); + + const totalRecommendations = + recommenderMetrics.totalRecommendations + 1; + const successfulRecs = tokenPerformance.rugPull + ? recommenderMetrics.successfulRecs + : recommenderMetrics.successfulRecs + 1; + const avgTokenPerformance = + (recommenderMetrics.avgTokenPerformance * + recommenderMetrics.totalRecommendations + + tokenPerformance.priceChange24h) / + totalRecommendations; + + const overallTrustScore = this.calculateTrustScore( + tokenPerformance, + recommenderMetrics + ); + const riskScore = this.calculateOverallRiskScore( + tokenPerformance, + recommenderMetrics + ); + const consistencyScore = this.calculateConsistencyScore( + tokenPerformance, + recommenderMetrics + ); + + const balance = await this.getRecommenderBalance(recommenderWallet); + const virtualConfidence = balance / 1000000; // TODO: create formula to calculate virtual confidence based on user balance + const lastActive = recommenderMetrics.lastActiveDate; + const now = new Date(); + const inactiveDays = Math.floor( + (now.getTime() - lastActive.getTime()) / (1000 * 60 * 60 * 24) + ); + const decayFactor = Math.pow( + this.DECAY_RATE, + Math.min(inactiveDays, this.MAX_DECAY_DAYS) + ); + const decayedScore = recommenderMetrics.trustScore * decayFactor; + + const newRecommenderMetrics: RecommenderMetrics = { + recommenderId: recommenderId, + trustScore: overallTrustScore, + totalRecommendations: totalRecommendations, + successfulRecs: successfulRecs, + avgTokenPerformance: avgTokenPerformance, + riskScore: riskScore, + consistencyScore: consistencyScore, + virtualConfidence: virtualConfidence, + lastActiveDate: new Date(), + trustDecay: decayedScore, + lastUpdated: new Date(), + }; + + await this.trustScoreDb.updateRecommenderMetrics(newRecommenderMetrics); + } + + calculateTrustScore( + tokenPerformance: TokenPerformance, + recommenderMetrics: RecommenderMetrics + ): number { + const riskScore = this.calculateRiskScore(tokenPerformance); + const consistencyScore = this.calculateConsistencyScore( + tokenPerformance, + recommenderMetrics + ); + + return (riskScore + consistencyScore) / 2; + } + + calculateOverallRiskScore( + tokenPerformance: TokenPerformance, + recommenderMetrics: RecommenderMetrics + ) { + const riskScore = this.calculateRiskScore(tokenPerformance); + const consistencyScore = this.calculateConsistencyScore( + tokenPerformance, + recommenderMetrics + ); + + return (riskScore + consistencyScore) / 2; + } + + calculateRiskScore(tokenPerformance: TokenPerformance): number { + let riskScore = 0; + if (tokenPerformance.rugPull) { + riskScore += 10; + } + if (tokenPerformance.isScam) { + riskScore += 10; + } + if (tokenPerformance.rapidDump) { + riskScore += 5; + } + if (tokenPerformance.suspiciousVolume) { + riskScore += 5; + } + return riskScore; + } + + calculateConsistencyScore( + tokenPerformance: TokenPerformance, + recommenderMetrics: RecommenderMetrics + ): number { + const avgTokenPerformance = recommenderMetrics.avgTokenPerformance; + const priceChange24h = tokenPerformance.priceChange24h; + + return Math.abs(priceChange24h - avgTokenPerformance); + } + + async suspiciousVolume(tokenAddress: string): Promise { + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + const unique_wallet_24h = processedData.tradeData.unique_wallet_24h; + const volume_24h = processedData.tradeData.volume_24h; + const suspiciousVolume = unique_wallet_24h / volume_24h > 0.5; + console.log(`Fetched processed token data for token: ${tokenAddress}`); + return suspiciousVolume; + } + + async sustainedGrowth(tokenAddress: string): Promise { + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + console.log(`Fetched processed token data for token: ${tokenAddress}`); + + return processedData.tradeData.volume_24h_change_percent > 50; + } + + async isRapidDump(tokenAddress: string): Promise { + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + console.log(`Fetched processed token data for token: ${tokenAddress}`); + + return processedData.tradeData.trade_24h_change_percent < -50; + } + + async checkTrustScore(tokenAddress: string): Promise { + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + console.log(`Fetched processed token data for token: ${tokenAddress}`); + + return { + ownerBalance: processedData.security.ownerBalance, + creatorBalance: processedData.security.creatorBalance, + ownerPercentage: processedData.security.ownerPercentage, + creatorPercentage: processedData.security.creatorPercentage, + top10HolderBalance: processedData.security.top10HolderBalance, + top10HolderPercent: processedData.security.top10HolderPercent, + }; + } + + /** + * Creates a TradePerformance object based on token data and recommender. + * @param tokenAddress The address of the token. + * @param recommenderId The UUID of the recommender. + * @param data ProcessedTokenData. + * @returns TradePerformance object. + */ + async createTradePerformance( + runtime: IAgentRuntime, + tokenAddress: string, + recommenderId: string, + data: TradeData + ): Promise { + const recommender = + await this.trustScoreDb.getOrCreateRecommenderWithTelegramId( + recommenderId + ); + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + + // TODO: change to starknet + const wallet = new WalletProvider(runtime); + + const prices = await wallet.fetchPrices(runtime); + const solPrice = prices.solana.usd; + const buySol = data.buy_amount / parseFloat(solPrice); + const buy_value_usd = data.buy_amount * processedData.tradeData.price; + + const creationData = { + token_address: tokenAddress, + recommender_id: recommender.id, + buy_price: processedData.tradeData.price, + sell_price: 0, + buy_timeStamp: new Date().toISOString(), + sell_timeStamp: "", + buy_amount: data.buy_amount, + sell_amount: 0, + buy_sol: buySol, + received_sol: 0, + buy_value_usd: buy_value_usd, + sell_value_usd: 0, + profit_usd: 0, + profit_percent: 0, + buy_market_cap: + processedData.dexScreenerData.pairs[0]?.marketCap || 0, + sell_market_cap: 0, + market_cap_change: 0, + buy_liquidity: + processedData.dexScreenerData.pairs[0]?.liquidity.usd || 0, + sell_liquidity: 0, + liquidity_change: 0, + last_updated: new Date().toISOString(), + rapidDump: false, + }; + this.trustScoreDb.addTradePerformance(creationData, data.is_simulation); + // api call to update trade performance + this.createTradeInBe(tokenAddress, recommenderId, data); + return creationData; + } + + async delay(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms)); + } + + // TODO: change to starknet + async createTradeInBe( + tokenAddress: string, + recommenderId: string, + data: TradeData, + retries = 3, + delayMs = 2000 + ) { + for (let attempt = 1; attempt <= retries; attempt++) { + try { + await fetch( + `${this.backend}/api/updaters/createTradePerformance`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${this.backendToken}`, + }, + body: JSON.stringify({ + tokenAddress: tokenAddress, + tradeData: data, + recommenderId: recommenderId, + }), + } + ); + // If the request is successful, exit the loop + return; + } catch (error) { + console.error( + `Attempt ${attempt} failed: Error creating trade in backend`, + error + ); + if (attempt < retries) { + console.log(`Retrying in ${delayMs} ms...`); + await this.delay(delayMs); // Wait for the specified delay before retrying + } else { + console.error("All attempts failed."); + } + } + } + } + + /** + * Updates a trade with sell details. + * @param tokenAddress The address of the token. + * @param recommenderId The UUID of the recommender. + * @param buyTimeStamp The timestamp when the buy occurred. + * @param sellDetails An object containing sell-related details. + * @param isSimulation Whether the trade is a simulation. If true, updates in simulation_trade; otherwise, in trade. + * @returns boolean indicating success. + */ + + async updateSellDetails( + runtime: IAgentRuntime, + tokenAddress: string, + recommenderId: string, + sellTimeStamp: string, + sellDetails: sellDetails, + isSimulation: boolean + ) { + const recommender = + await this.trustScoreDb.getOrCreateRecommenderWithTelegramId( + recommenderId + ); + const processedData: ProcessedTokenData = + await this.tokenProvider.getProcessedTokenData(); + + // TODO: + const wallet = new WalletProvider(this.runtime); + + const prices = await wallet.fetchPrices(runtime); + const solPrice = prices.solana.usd; + const sellSol = sellDetails.sell_amount / parseFloat(solPrice); + const sell_value_usd = + sellDetails.sell_amount * processedData.tradeData.price; + const trade = await this.trustScoreDb.getLatestTradePerformance( + tokenAddress, + recommender.id, + isSimulation + ); + const buyTimeStamp = trade.buy_timeStamp; + const marketCap = + processedData.dexScreenerData.pairs[0]?.marketCap || 0; + const liquidity = + processedData.dexScreenerData.pairs[0]?.liquidity.usd || 0; + const sell_price = processedData.tradeData.price; + const profit_usd = sell_value_usd - trade.buy_value_usd; + const profit_percent = (profit_usd / trade.buy_value_usd) * 100; + + const market_cap_change = marketCap - trade.buy_market_cap; + const liquidity_change = liquidity - trade.buy_liquidity; + + const isRapidDump = await this.isRapidDump(tokenAddress); + + const sellDetailsData = { + sell_price: sell_price, + sell_timeStamp: sellTimeStamp, + sell_amount: sellDetails.sell_amount, + received_sol: sellSol, + sell_value_usd: sell_value_usd, + profit_usd: profit_usd, + profit_percent: profit_percent, + sell_market_cap: marketCap, + market_cap_change: market_cap_change, + sell_liquidity: liquidity, + liquidity_change: liquidity_change, + rapidDump: isRapidDump, + sell_recommender_id: sellDetails.sell_recommender_id || null, + }; + this.trustScoreDb.updateTradePerformanceOnSell( + tokenAddress, + recommender.id, + buyTimeStamp, + sellDetailsData, + isSimulation + ); + return sellDetailsData; + } + + // get all recommendations + async getRecommendations( + startDate: Date, + endDate: Date + ): Promise> { + const recommendations = this.trustScoreDb.getRecommendationsByDateRange( + startDate, + endDate + ); + + // Group recommendations by tokenAddress + const groupedRecommendations = recommendations.reduce( + (acc, recommendation) => { + const { tokenAddress } = recommendation; + if (!acc[tokenAddress]) acc[tokenAddress] = []; + acc[tokenAddress].push(recommendation); + return acc; + }, + {} as Record> + ); + + const result = Object.keys(groupedRecommendations).map( + (tokenAddress) => { + const tokenRecommendations = + groupedRecommendations[tokenAddress]; + + // Initialize variables to compute averages + let totalTrustScore = 0; + let totalRiskScore = 0; + let totalConsistencyScore = 0; + const recommenderData = []; + + tokenRecommendations.forEach((recommendation) => { + const tokenPerformance = + this.trustScoreDb.getTokenPerformance( + recommendation.tokenAddress + ); + const recommenderMetrics = + this.trustScoreDb.getRecommenderMetrics( + recommendation.recommenderId + ); + + const trustScore = this.calculateTrustScore( + tokenPerformance, + recommenderMetrics + ); + const consistencyScore = this.calculateConsistencyScore( + tokenPerformance, + recommenderMetrics + ); + const riskScore = this.calculateRiskScore(tokenPerformance); + + // Accumulate scores for averaging + totalTrustScore += trustScore; + totalRiskScore += riskScore; + totalConsistencyScore += consistencyScore; + + recommenderData.push({ + recommenderId: recommendation.recommenderId, + trustScore, + riskScore, + consistencyScore, + recommenderMetrics, + }); + }); + + // Calculate averages for this token + const averageTrustScore = + totalTrustScore / tokenRecommendations.length; + const averageRiskScore = + totalRiskScore / tokenRecommendations.length; + const averageConsistencyScore = + totalConsistencyScore / tokenRecommendations.length; + + return { + tokenAddress, + averageTrustScore, + averageRiskScore, + averageConsistencyScore, + recommenders: recommenderData, + }; + } + ); + + // Sort recommendations by the highest average trust score + result.sort((a, b) => b.averageTrustScore - a.averageTrustScore); + + return result; + } +} + +export const trustScoreProvider: Provider = { + async get( + runtime: IAgentRuntime, + message: Memory, + state?: State + ): Promise { + try { + const trustScoreDb = new TrustScoreDatabase( + runtime.databaseAdapter.db + ); + + // Get the user ID from the message + const userId = message.userId; + + if (!userId) { + console.error("User ID is missing from the message"); + return ""; + } + + // Get the recommender metrics for the user + const recommenderMetrics = + await trustScoreDb.getRecommenderMetrics(userId); + + if (!recommenderMetrics) { + console.error("No recommender metrics found for user:", userId); + return ""; + } + + // Compute the trust score + const trustScore = recommenderMetrics.trustScore; + + const user = await runtime.databaseAdapter.getAccountById(userId); + + // Format the trust score string + const trustScoreString = `${user.name}'s trust score: ${trustScore.toFixed(2)}`; + + return trustScoreString; + } catch (error) { + console.error("Error in trust score provider:", error.message); + return `Failed to fetch trust score: ${error instanceof Error ? error.message : "Unknown error"}`; + } + }, +}; diff --git a/packages/plugin-starknet/src/providers/utils.ts b/packages/plugin-starknet/src/providers/utils.ts new file mode 100644 index 00000000..afa5b9b2 --- /dev/null +++ b/packages/plugin-starknet/src/providers/utils.ts @@ -0,0 +1,133 @@ +import { num } from "starknet"; +import { HolderData } from "../types/trustDB"; + +export interface TokenMetrics { + liquidityUsd: bigint; + marketCapUsd: bigint; + totalSupply: bigint; + ownerPercentage: number; + creatorPercentage: number; + top10HolderPercent: number; + priceChange24hPercent: number; + priceChange12hPercent: number; + uniqueWallet24h: number; + volume24hUsd: bigint; +} + +export interface TradingThresholds { + volume24hUsdThreshold?: number; + priceChange24hPercentThreshold?: number; + priceChange12hPercentThreshold?: number; + top10HolderPercentThreshold?: number; + uniqueWallet24hThreshold?: number; + minimumLiquidityUsd?: number; + minimumMarketCapUsd?: number; +} + +export function evaluateTokenTrading( + metrics: TokenMetrics, + thresholds: TradingThresholds = {} +): { shouldTrade: boolean; reasons: string[] } { + // Default thresholds + const { + volume24hUsdThreshold = 1000, + priceChange24hPercentThreshold = 10, + priceChange12hPercentThreshold = 5, + top10HolderPercentThreshold = 0.05, + uniqueWallet24hThreshold = 100, + minimumLiquidityUsd = 1000, + minimumMarketCapUsd = 100000, + } = thresholds; + + const reasons: string[] = []; + + // Evaluate each condition + if (metrics.top10HolderPercent >= top10HolderPercentThreshold) { + reasons.push("High concentration in top 10 holders"); + } + + if (metrics.volume24hUsd >= BigInt(volume24hUsdThreshold)) { + reasons.push("High 24h trading volume"); + } + + if (metrics.priceChange24hPercent >= priceChange24hPercentThreshold) { + reasons.push("Significant 24h price change"); + } + + if (metrics.priceChange12hPercent >= priceChange12hPercentThreshold) { + reasons.push("Significant 12h price change"); + } + + if (metrics.uniqueWallet24h >= uniqueWallet24hThreshold) { + reasons.push("High number of unique wallets"); + } + + if (metrics.liquidityUsd < BigInt(minimumLiquidityUsd)) { + reasons.push("Low liquidity"); + } + + if (metrics.marketCapUsd < BigInt(minimumMarketCapUsd)) { + reasons.push("Low market cap"); + } + + return { + shouldTrade: reasons.length > 0, + reasons, + }; +} + +export interface HolderAnalysisParams { + holders: HolderData[]; + ownerBalance: string; + creatorBalance: string; + thresholdPercentage?: number; +} + +export interface HolderAnalysisResult { + count: number; + holders: Array<{ + address: string; + percentage: number; + }>; + totalSupply: bigint; +} + +export function analyzeHighSupplyHolders( + params: HolderAnalysisParams +): HolderAnalysisResult { + try { + const { + holders, + ownerBalance, + creatorBalance, + thresholdPercentage = 0.02, // Default threshold of 2% + } = params; + + const ownerBalanceBigInt = num.toBigInt(ownerBalance); + const totalSupply = ownerBalanceBigInt + num.toBigInt(creatorBalance); + + const highSupplyHolders = holders + .map((holder) => { + const balance = num.toBigInt(holder.balance); + const percentage = Number(balance) / Number(totalSupply); + return { + address: holder.address, + percentage, + }; + }) + .filter((holder) => holder.percentage > thresholdPercentage); + + return { + count: highSupplyHolders.length, + holders: highSupplyHolders, + totalSupply, + }; + } catch (error) { + console.error("Error analyzing high supply holders:", error); + return { + count: 0, + holders: [], + totalSupply: BigInt(0), + }; + } +} diff --git a/packages/plugin-starknet/src/providers/wallet.ts b/packages/plugin-starknet/src/providers/wallet.ts deleted file mode 100644 index 2d76e96e..00000000 --- a/packages/plugin-starknet/src/providers/wallet.ts +++ /dev/null @@ -1,37 +0,0 @@ -// TODO: Implement this for Starknet. -// It should return the formatted token balances and the portfolio value. - -import { IAgentRuntime, Memory, State } from "@ai16z/eliza"; -import { Account, RpcProvider } from "starknet"; - -export const getStarknetProvider = (runtime: IAgentRuntime) => { - return new RpcProvider({ - nodeUrl: runtime.getSetting("STARKNET_RPC_URL"), - }); -}; - -export const getStarknetAccount = (runtime: IAgentRuntime) => { - return new Account( - getStarknetProvider(runtime), - runtime.getSetting("STARKNET_ADDRESS"), - runtime.getSetting("STARKNET_PRIVATE_KEY") - ); -}; - -export class WalletProvider { - private account: Account; - private provider: RpcProvider; - - constructor(runtime: IAgentRuntime) { - this.account = getStarknetAccount(runtime); - this.provider = getStarknetProvider(runtime); - } - - async getFormattedTokenBalances(): Promise { - return ""; - } - - async getPortfolioValue(): Promise { - return ""; - } -} diff --git a/packages/plugin-starknet/src/providers/walletProvider.ts b/packages/plugin-starknet/src/providers/walletProvider.ts new file mode 100644 index 00000000..91d90b35 --- /dev/null +++ b/packages/plugin-starknet/src/providers/walletProvider.ts @@ -0,0 +1,294 @@ +import { IAgentRuntime, Memory, Provider, State } from "@ai16z/eliza"; +import { Connection, PublicKey } from "@solana/web3.js"; +import BigNumber from "bignumber.js"; +import NodeCache from "node-cache"; +import { validateSettings } from "../utils"; +// Provider configuration +const PROVIDER_CONFIG = { + BIRDEYE_API: "https://public-api.birdeye.so", + MAX_RETRIES: 3, + RETRY_DELAY: 2000, + DEFAULT_RPC: "https://api.mainnet-beta.solana.com", + TOKEN_ADDRESSES: { + SOL: "So11111111111111111111111111111111111111112", + BTC: "qfnqNqs3nCAHjnyCgLRDbBtq4p2MtHZxw8YjSyYhPoL", + ETH: "7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs", + }, +}; + +export interface Item { + name: string; + address: string; + symbol: string; + decimals: number; + balance: string; + uiAmount: string; + priceUsd: string; + valueUsd: string; + valueSol?: string; +} + +interface WalletPortfolio { + totalUsd: string; + totalSol?: string; + items: Array; +} + +interface BirdEyePriceData { + data: { + [key: string]: { + price: number; + priceChange24h: number; + }; + }; +} + +interface Prices { + solana: { usd: string }; + bitcoin: { usd: string }; + ethereum: { usd: string }; +} + +export class WalletProvider { + private cache: NodeCache; + private runtime: IAgentRuntime; + + constructor(runtime: IAgentRuntime) { + this.cache = new NodeCache({ stdTTL: 300 }); // Cache TTL set to 5 minutes + this.runtime = runtime; + } + + // This should be ETH/STK/BTC + private async fetchWithRetry( + url: string, + options: RequestInit = {} + ): Promise { + let lastError: Error; + + for (let i = 0; i < PROVIDER_CONFIG.MAX_RETRIES; i++) { + try { + const response = await fetch(url, { + ...options, + headers: { + Accept: "application/json", + "x-chain": "solana", + "X-API-KEY": + // TODO: change to starknet + this.runtime.getSetting("BIRDEYE_API_KEY") || "", + ...options.headers, + }, + }); + + if (!response.ok) { + const errorText = await response.text(); + throw new Error( + `HTTP error! status: ${response.status}, message: ${errorText}` + ); + } + + const data = await response.json(); + return data; + } catch (error) { + console.error(`Attempt ${i + 1} failed:`, error); + lastError = error; + if (i < PROVIDER_CONFIG.MAX_RETRIES - 1) { + const delay = PROVIDER_CONFIG.RETRY_DELAY * Math.pow(2, i); + await new Promise((resolve) => setTimeout(resolve, delay)); + continue; + } + } + } + + console.error( + "All attempts failed. Throwing the last error:", + lastError + ); + throw lastError; + } + + async fetchPortfolioValue(runtime): Promise { + try { + const cacheKey = `portfolio-${this.runtime.getSetting("STARKNET_WALLET_ADDRESS")}`; + const cachedValue = this.cache.get(cacheKey); + + if (cachedValue) { + console.log("Cache hit for fetchPortfolioValue"); + return cachedValue; + } + console.log("Cache miss for fetchPortfolioValue"); + + // TODO: change to starknet + + // const walletData = await this.fetchWithRetry( + // runtime, + // `${PROVIDER_CONFIG.BIRDEYE_API}/v1/wallet/token_list?wallet=${this.walletPublicKey.toBase58()}` + // ); + + // if (!walletData?.success || !walletData?.data) { + // console.error("No portfolio data available", walletData); + // throw new Error("No portfolio data available"); + // } + + // const data = walletData.data; + // const totalUsd = new BigNumber(data.totalUsd.toString()); + // const prices = await this.fetchPrices(runtime); + // const solPriceInUSD = new BigNumber(prices.solana.usd.toString()); + + // const items = data.items.map((item: any) => ({ + // ...item, + // valueSol: new BigNumber(item.valueUsd || 0) + // .div(solPriceInUSD) + // .toFixed(6), + // name: item.name || "Unknown", + // symbol: item.symbol || "Unknown", + // priceUsd: item.priceUsd || "0", + // valueUsd: item.valueUsd || "0", + // })); + + // const totalSol = totalUsd.div(solPriceInUSD); + // const portfolio = { + // totalUsd: totalUsd.toString(), + // totalSol: totalSol.toFixed(6), + // items: items.sort((a, b) => + // new BigNumber(b.valueUsd) + // .minus(new BigNumber(a.valueUsd)) + // .toNumber() + // ), + // }; + // this.cache.set(cacheKey, portfolio); + // return portfolio; + } catch (error) { + console.error("Error fetching portfolio:", error); + throw error; + } + } + + async fetchPrices(runtime): Promise { + try { + const cacheKey = "prices"; + const cachedValue = this.cache.get(cacheKey); + + if (cachedValue) { + console.log("Cache hit for fetchPrices"); + return cachedValue; + } + console.log("Cache miss for fetchPrices"); + + const { SOL, BTC, ETH } = PROVIDER_CONFIG.TOKEN_ADDRESSES; + const tokens = [SOL, BTC, ETH]; + const prices: Prices = { + solana: { usd: "0" }, + bitcoin: { usd: "0" }, + ethereum: { usd: "0" }, + }; + + // TODO: change to starknet + + // for (const token of tokens) { + // const response = await this.fetchWithRetry( + // runtime, + // `${PROVIDER_CONFIG.BIRDEYE_API}/defi/price?address=${token}`, + // { + // headers: { + // "x-chain": "solana", + // }, + // } + // ); + + // if (response?.data?.value) { + // const price = response.data.value.toString(); + // prices[ + // token === SOL + // ? "solana" + // : token === BTC + // ? "bitcoin" + // : "ethereum" + // ].usd = price; + // } else { + // console.warn(`No price data available for token: ${token}`); + // } + // } + + this.cache.set(cacheKey, prices); + return prices; + } catch (error) { + console.error("Error fetching prices:", error); + throw error; + } + } + + formatPortfolio( + runtime, + portfolio: WalletPortfolio, + prices: Prices + ): string { + let output = `${runtime.character.description}\n`; + output += `Wallet Address: ${this.runtime.getSetting("STARKNET_WALLET_ADDRESS")}\n\n`; + + const totalUsdFormatted = new BigNumber(portfolio.totalUsd).toFixed(2); + const totalSolFormatted = portfolio.totalSol; + + output += `Total Value: $${totalUsdFormatted} (${totalSolFormatted} SOL)\n\n`; + output += "Token Balances:\n"; + + const nonZeroItems = portfolio.items.filter((item) => + new BigNumber(item.uiAmount).isGreaterThan(0) + ); + + if (nonZeroItems.length === 0) { + output += "No tokens found with non-zero balance\n"; + } else { + for (const item of nonZeroItems) { + const valueUsd = new BigNumber(item.valueUsd).toFixed(2); + output += `${item.name} (${item.symbol}): ${new BigNumber( + item.uiAmount + ).toFixed(6)} ($${valueUsd} | ${item.valueSol} SOL)\n`; + } + } + + output += "\nMarket Prices:\n"; + output += `SOL: $${new BigNumber(prices.solana.usd).toFixed(2)}\n`; + output += `BTC: $${new BigNumber(prices.bitcoin.usd).toFixed(2)}\n`; + output += `ETH: $${new BigNumber(prices.ethereum.usd).toFixed(2)}\n`; + + return output; + } + + async getFormattedPortfolio(runtime): Promise { + try { + const [portfolio, prices] = await Promise.all([ + this.fetchPortfolioValue(runtime), + this.fetchPrices(runtime), + ]); + + return this.formatPortfolio(runtime, portfolio, prices); + } catch (error) { + console.error("Error generating portfolio report:", error); + return "Unable to fetch wallet information. Please try again later."; + } + } +} + +const walletProvider: Provider = { + get: async ( + runtime: IAgentRuntime, + _message: Memory, + _state?: State + ): Promise => { + try { + if (!validateSettings(runtime)) { + return ""; + } + + const provider = new WalletProvider(runtime); + + return await provider.getFormattedPortfolio(runtime); + } catch (error) { + console.error("Error in wallet provider:", error.message); + return `Failed to fetch wallet information: ${error instanceof Error ? error.message : "Unknown error"}`; + } + }, +}; + +// Module exports +export { walletProvider }; diff --git a/packages/plugin-starknet/src/types/trustDB.ts b/packages/plugin-starknet/src/types/trustDB.ts new file mode 100644 index 00000000..4f8d3e35 --- /dev/null +++ b/packages/plugin-starknet/src/types/trustDB.ts @@ -0,0 +1,287 @@ +export interface TokenSecurityData { + ownerBalance: string; + creatorBalance: string; + ownerPercentage: number; + creatorPercentage: number; + top10HolderBalance: string; + top10HolderPercent: number; +} + +export interface TokenTradeData { + address: string; + holder: number; + market: number; + last_trade_unix_time: number; + last_trade_human_time: string; + price: number; + history_30m_price: number; + price_change_30m_percent: number; + history_1h_price: number; + price_change_1h_percent: number; + history_2h_price: number; + price_change_2h_percent: number; + history_4h_price: number; + price_change_4h_percent: number; + history_6h_price: number; + price_change_6h_percent: number; + history_8h_price: number; + price_change_8h_percent: number; + history_12h_price: number; + price_change_12h_percent: number; + history_24h_price: number; + price_change_24h_percent: number; + unique_wallet_30m: number; + unique_wallet_history_30m: number; + unique_wallet_30m_change_percent: number; + unique_wallet_1h: number; + unique_wallet_history_1h: number; + unique_wallet_1h_change_percent: number; + unique_wallet_2h: number; + unique_wallet_history_2h: number; + unique_wallet_2h_change_percent: number; + unique_wallet_4h: number; + unique_wallet_history_4h: number; + unique_wallet_4h_change_percent: number; + unique_wallet_8h: number; + unique_wallet_history_8h: number | null; + unique_wallet_8h_change_percent: number | null; + unique_wallet_24h: number; + unique_wallet_history_24h: number | null; + unique_wallet_24h_change_percent: number | null; + trade_30m: number; + trade_history_30m: number; + trade_30m_change_percent: number; + sell_30m: number; + sell_history_30m: number; + sell_30m_change_percent: number; + buy_30m: number; + buy_history_30m: number; + buy_30m_change_percent: number; + volume_30m: number; + volume_30m_usd: number; + volume_history_30m: number; + volume_history_30m_usd: number; + volume_30m_change_percent: number; + volume_buy_30m: number; + volume_buy_30m_usd: number; + volume_buy_history_30m: number; + volume_buy_history_30m_usd: number; + volume_buy_30m_change_percent: number; + volume_sell_30m: number; + volume_sell_30m_usd: number; + volume_sell_history_30m: number; + volume_sell_history_30m_usd: number; + volume_sell_30m_change_percent: number; + trade_1h: number; + trade_history_1h: number; + trade_1h_change_percent: number; + sell_1h: number; + sell_history_1h: number; + sell_1h_change_percent: number; + buy_1h: number; + buy_history_1h: number; + buy_1h_change_percent: number; + volume_1h: number; + volume_1h_usd: number; + volume_history_1h: number; + volume_history_1h_usd: number; + volume_1h_change_percent: number; + volume_buy_1h: number; + volume_buy_1h_usd: number; + volume_buy_history_1h: number; + volume_buy_history_1h_usd: number; + volume_buy_1h_change_percent: number; + volume_sell_1h: number; + volume_sell_1h_usd: number; + volume_sell_history_1h: number; + volume_sell_history_1h_usd: number; + volume_sell_1h_change_percent: number; + trade_2h: number; + trade_history_2h: number; + trade_2h_change_percent: number; + sell_2h: number; + sell_history_2h: number; + sell_2h_change_percent: number; + buy_2h: number; + buy_history_2h: number; + buy_2h_change_percent: number; + volume_2h: number; + volume_2h_usd: number; + volume_history_2h: number; + volume_history_2h_usd: number; + volume_2h_change_percent: number; + volume_buy_2h: number; + volume_buy_2h_usd: number; + volume_buy_history_2h: number; + volume_buy_history_2h_usd: number; + volume_buy_2h_change_percent: number; + volume_sell_2h: number; + volume_sell_2h_usd: number; + volume_sell_history_2h: number; + volume_sell_history_2h_usd: number; + volume_sell_2h_change_percent: number; + trade_4h: number; + trade_history_4h: number; + trade_4h_change_percent: number; + sell_4h: number; + sell_history_4h: number; + sell_4h_change_percent: number; + buy_4h: number; + buy_history_4h: number; + buy_4h_change_percent: number; + volume_4h: number; + volume_4h_usd: number; + volume_history_4h: number; + volume_history_4h_usd: number; + volume_4h_change_percent: number; + volume_buy_4h: number; + volume_buy_4h_usd: number; + volume_buy_history_4h: number; + volume_buy_history_4h_usd: number; + volume_buy_4h_change_percent: number; + volume_sell_4h: number; + volume_sell_4h_usd: number; + volume_sell_history_4h: number; + volume_sell_history_4h_usd: number; + volume_sell_4h_change_percent: number; + trade_8h: number; + trade_history_8h: number | null; + trade_8h_change_percent: number | null; + sell_8h: number; + sell_history_8h: number | null; + sell_8h_change_percent: number | null; + buy_8h: number; + buy_history_8h: number | null; + buy_8h_change_percent: number | null; + volume_8h: number; + volume_8h_usd: number; + volume_history_8h: number; + volume_history_8h_usd: number; + volume_8h_change_percent: number | null; + volume_buy_8h: number; + volume_buy_8h_usd: number; + volume_buy_history_8h: number; + volume_buy_history_8h_usd: number; + volume_buy_8h_change_percent: number | null; + volume_sell_8h: number; + volume_sell_8h_usd: number; + volume_sell_history_8h: number; + volume_sell_history_8h_usd: number; + volume_sell_8h_change_percent: number | null; + trade_24h: number; + trade_history_24h: number; + trade_24h_change_percent: number | null; + sell_24h: number; + sell_history_24h: number; + sell_24h_change_percent: number | null; + buy_24h: number; + buy_history_24h: number; + buy_24h_change_percent: number | null; + volume_24h: number; + volume_24h_usd: number; + volume_history_24h: number; + volume_history_24h_usd: number; + volume_24h_change_percent: number | null; + volume_buy_24h: number; + volume_buy_24h_usd: number; + volume_buy_history_24h: number; + volume_buy_history_24h_usd: number; + volume_buy_24h_change_percent: number | null; + volume_sell_24h: number; + volume_sell_24h_usd: number; + volume_sell_history_24h: number; + volume_sell_history_24h_usd: number; + volume_sell_24h_change_percent: number | null; +} + +export interface HolderData { + address: string; + balance: string; +} + +export interface ProcessedTokenData { + security: TokenSecurityData; + tradeData: TokenTradeData; + holderDistributionTrend: string; // 'increasing' | 'decreasing' | 'stable' + highValueHolders: Array<{ + holderAddress: string; + balanceUsd: string; + }>; + recentTrades: boolean; + highSupplyHoldersCount: number; + dexScreenerData: DexScreenerData; + + isDexScreenerListed: boolean; + isDexScreenerPaid: boolean; +} + +export interface DexScreenerPair { + chainId: string; + dexId: string; + url: string; + pairAddress: string; + baseToken: { + address: string; + name: string; + symbol: string; + }; + quoteToken: { + address: string; + name: string; + symbol: string; + }; + priceNative: string; + priceUsd: string; + txns: { + m5: { buys: number; sells: number }; + h1: { buys: number; sells: number }; + h6: { buys: number; sells: number }; + h24: { buys: number; sells: number }; + }; + volume: { + h24: number; + h6: number; + h1: number; + m5: number; + }; + priceChange: { + m5: number; + h1: number; + h6: number; + h24: number; + }; + liquidity: { + usd: number; + base: number; + quote: number; + }; + fdv: number; + marketCap: number; + pairCreatedAt: number; + info: { + imageUrl: string; + websites: { label: string; url: string }[]; + socials: { type: string; url: string }[]; + }; + boosts: { + active: number; + }; +} + +export interface DexScreenerData { + schemaVersion: string; + pairs: DexScreenerPair[]; +} + +export interface Prices { + solana: { usd: string }; + bitcoin: { usd: string }; + ethereum: { usd: string }; +} + +export interface CalculatedBuyAmounts { + none: 0; + low: number; + medium: number; + high: number; +} diff --git a/packages/plugin-starknet/src/utils/erc20.json b/packages/plugin-starknet/src/utils/erc20.json new file mode 100644 index 00000000..e69de29b diff --git a/packages/plugin-starknet/src/utils/index.ts b/packages/plugin-starknet/src/utils/index.ts index 3588ede1..00931b64 100644 --- a/packages/plugin-starknet/src/utils/index.ts +++ b/packages/plugin-starknet/src/utils/index.ts @@ -1,5 +1,7 @@ import { IAgentRuntime } from "@ai16z/eliza"; +import { Account, Contract, RpcProvider } from "starknet"; + export const validateSettings = (runtime: IAgentRuntime) => { const requiredSettings = [ "STARKNET_ADDRESS", @@ -15,3 +17,35 @@ export const validateSettings = (runtime: IAgentRuntime) => { return true; }; + +export const getTokenBalance = async ( + runtime: IAgentRuntime, + tokenAddress: string +) => { + const provider = getStarknetProvider(runtime); + + const { abi: tokenAbi } = await provider.getClassAt(tokenAddress); + if (tokenAbi === undefined) { + throw new Error("no abi."); + } + + const tokenContract = new Contract(tokenAbi, tokenAddress, provider); + + tokenContract.connect(getStarknetAccount(runtime)); + + return await tokenContract.balanceOf(tokenAddress); +}; + +export const getStarknetProvider = (runtime: IAgentRuntime) => { + return new RpcProvider({ + nodeUrl: runtime.getSetting("STARKNET_RPC_URL"), + }); +}; + +export const getStarknetAccount = (runtime: IAgentRuntime) => { + return new Account( + getStarknetProvider(runtime), + runtime.getSetting("STARKNET_ADDRESS"), + runtime.getSetting("STARKNET_PRIVATE_KEY") + ); +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ec6f5d8d..742b735d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -775,6 +775,9 @@ importers: '@ai16z/eliza': specifier: workspace:* version: link:../core + '@ai16z/plugin-trustdb': + specifier: workspace:* + version: link:../plugin-trustdb '@avnu/avnu-sdk': specifier: ^2.1.1 version: 2.1.1(ethers@6.13.4(bufferutil@4.0.8)(utf-8-validate@5.0.10))(qs@6.13.0)(starknet@6.11.0(encoding@0.1.13))