UNPKG

@suiware/ai-tools

Version:

Pluggable tools for Vercel AI SDK which allow AI assistants to interact with Sui Network and perform various actions.

192 lines (189 loc) 6.03 kB
import { getSetting, __async, SuiService, __pow, formatBalance } from './chunk-HFORSGSH.mjs'; import { SuinsClient } from '@mysten/suins'; import { SuiClient, getFullnodeUrl } from '@mysten/sui/client'; import { NAVISDKClient, NAVX, Sui, vSui, USDT, WETH, CETUS, haSui, WBTC, AUSD, wUSDC, nUSDC, ETH, USDY, NS, LorenzoBTC, DEEP, FDUSD, BLUE, BUCK, suiUSDT, stSUI, suiBTC, WSOL, LBTC, WAL } from 'navi-sdk'; var SuinsService = class { constructor(suiClient) { this.suiClient = suiClient; this.suinsClient = new SuinsClient({ client: this.suiClient, network: getSetting("SUI_NETWORK") }); } resolveSuinsName(name) { return __async(this, null, function* () { const nameRecord = yield this.suinsClient.getNameRecord(name); return (nameRecord == null ? void 0 : nameRecord.targetAddress) || null; }); } static isValidSuinsName(name) { return name.endsWith(".sui") || name.startsWith("@"); } }; var SUPPORTED_COINS = [ NAVX, Sui, vSui, USDT, WETH, CETUS, haSui, WBTC, AUSD, wUSDC, nUSDC, ETH, USDY, NS, LorenzoBTC, DEEP, FDUSD, BLUE, BUCK, suiUSDT, stSUI, suiBTC, WSOL, LBTC, WAL ]; // src/services/NaviService.ts var NaviService = class _NaviService { constructor() { const privateKey = getSetting("SUI_PRIVATE_KEY"); if (!SuiService.isValidPrivateKey(privateKey)) { throw new Error("Invalid SUI_PRIVATE_KEY in the config"); } const suiNetwork = getSetting("SUI_NETWORK"); this.naviClient = new NAVISDKClient({ privateKeyList: [privateKey], networkType: suiNetwork }); this.account = this.naviClient.accounts[0]; this.suiClient = new SuiClient({ url: getFullnodeUrl(suiNetwork) }); } getSuiClient() { return this.suiClient; } getAddress() { return this.account.address; } getAllBalances() { return __async(this, null, function* () { const allBalances = yield this.naviClient.getAllBalances(); return _NaviService.getSupportedCoins().map((x) => ({ [_NaviService.naviUsdcToUsdc(x.symbol)]: allBalances[x.address] })); }); } swap(sourceToken, targetToken, amount) { return __async(this, null, function* () { if (this.naviClient.networkType !== "mainnet") { throw new Error("Only mainnet is supported"); } const sourceTokenMetadata = _NaviService.getSupportedCoinBySymbol(sourceToken); const targetTokenMetadata = _NaviService.getSupportedCoinBySymbol(targetToken); const amountIn = (typeof amount === "string" ? parseFloat(amount) : amount) * __pow(10, sourceTokenMetadata.decimal); return yield this.account.swap( sourceTokenMetadata.address, targetTokenMetadata.address, amountIn, 0 ); }); } transfer(token, address, amount) { return __async(this, null, function* () { let resolvedAddress = address; if (SuinsService.isValidSuinsName(address)) { const suinsService = new SuinsService(this.getSuiClient()); resolvedAddress = yield suinsService.resolveSuinsName(address); if (!resolvedAddress) { throw new Error(`Suins name ${address} not found`); } } if (resolvedAddress == this.getAddress()) { throw new Error("You cannot transfer to your own address"); } const sourceTokenMetadata = _NaviService.getSupportedCoinBySymbol(token); const amountIn = (typeof amount === "string" ? parseFloat(amount) : amount) * __pow(10, sourceTokenMetadata.decimal); return yield this.account.sendCoin( sourceTokenMetadata.address, resolvedAddress, amountIn ); }); } getWalletNonZeroCoins() { return __async(this, null, function* () { const allBalances = yield this.suiClient.getAllBalances({ owner: this.account.address }); const coinBalances = {}; for (const { coinType, totalBalance } of allBalances) { if (parseFloat(totalBalance) == 0) { continue; } const coinInfo = this.getSupportedCoinByAddress(coinType); if (coinInfo) { coinBalances[coinInfo.symbol] = formatBalance( totalBalance, coinInfo.decimal ); continue; } const coinMetadata = yield this.fetchCoinMetadata(coinType); if (coinMetadata) { coinBalances[coinMetadata.symbol] = formatBalance( totalBalance, coinMetadata.decimals ); continue; } const decimal = yield this.account.getCoinDecimal(coinType); coinBalances[coinType] = formatBalance(totalBalance, decimal); } return coinBalances; }); } getWalletBalance() { return __async(this, null, function* () { return yield this.naviClient.accounts[0].getWalletBalance(); }); } static getSupportedCoins() { return SUPPORTED_COINS; } static isSupportedCoin(symbol) { return this.getSupportedCoins().some( (coin) => this.naviUsdcToUsdc(coin.symbol).toUpperCase() === symbol.toUpperCase() ); } static getSupportedCoinBySymbol(symbol) { return this.getSupportedCoins().find( (coin) => this.naviUsdcToUsdc(coin.symbol).toUpperCase() === symbol.toUpperCase() ); } static getMissingSupportedCoin(symbol) { return this.getSupportedCoins().find( (coin) => this.naviUsdcToUsdc(coin.symbol).toUpperCase() !== symbol.toUpperCase() ); } getSupportedCoinByAddress(address) { const naviCoins = _NaviService.getSupportedCoins(); return naviCoins.find((x) => x.address === address); } fetchCoinMetadata(coinType) { return __async(this, null, function* () { return this.suiClient.getCoinMetadata({ coinType }); }); } static naviUsdcToUsdc(symbol) { return symbol === "nUSDC" ? "USDC" : symbol; } }; export { NaviService, SuinsService }; //# sourceMappingURL=chunk-OZX7KUX2.mjs.map //# sourceMappingURL=chunk-OZX7KUX2.mjs.map