UNPKG

genius-bridge-sdk

Version:
204 lines 8.84 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GeniusBridgeSdk = void 0; const axios_1 = __importDefault(require("axios")); const enums_1 = require("./types/enums"); const logger_1 = require("./utils/logger"); const check_vm_1 = require("./utils/check-vm"); const address_validation_1 = require("./utils/address-validation"); const constants_1 = require("./utils/constants"); const is_native_1 = require("./utils/is-native"); let logger; class GeniusBridgeSdk { constructor(config) { this.chains = [ enums_1.ChainIdEnum.ETHEREUM, enums_1.ChainIdEnum.ARBITRUM, enums_1.ChainIdEnum.OPTIMISM, enums_1.ChainIdEnum.POLYGON, enums_1.ChainIdEnum.BSC, enums_1.ChainIdEnum.AVALANCHE, enums_1.ChainIdEnum.BASE, enums_1.ChainIdEnum.SOLANA, enums_1.ChainIdEnum.SONIC, // Add other supported chains ]; if (config?.debug) { logger_1.LoggerFactory.configure(logger_1.LoggerFactory.createConsoleLogger({ level: logger_1.LogLevelEnum.DEBUG })); } // Use custom logger if provided else if (config?.logger) { logger_1.LoggerFactory.configure(config.logger); } logger = logger_1.LoggerFactory.getLogger(); // Apply configuration with defaults this.baseUrl = config?.geniusBridgeBaseUrl || 'https://bridge-api.tradegeniuses.net'; this.priceEndpoint = '/quoting/price'; this.quoteEndpoint = '/quoting/quote'; } isCorrectConfig(_config) { // GeniusBridge has no required config fields, all are optional return true; } async fetchPrice(params) { try { this.validatePriceParams(params); const priceEndpoint = `${this.baseUrl}${this.priceEndpoint}`; const response = await axios_1.default.post(priceEndpoint, params); return response.data; } catch (error) { logger.error(`Failed to fetch price`, error instanceof Error ? error : undefined); throw new Error(`Failed to fetch GeniusBridge price, error: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async fetchQuote(params) { try { this.validateQuoteParams(params); const transformedParams = this.transformQuoteParams(params); const quoteUri = `${this.baseUrl}${this.quoteEndpoint}`; const response = await axios_1.default.post(quoteUri, transformedParams); return response.data; } catch (error) { logger.error(`Failed to fetch quote`, error instanceof Error ? error : undefined); throw new Error(`Failed to fetch GeniusBridge quote, error: ${error instanceof Error ? error.message : 'Unknown error'}`); } } validatePriceParams(params) { const { networkIn, networkOut, tokenIn, tokenOut, amountIn } = params; if (networkIn === networkOut) { logger.error('Single chain swaps are not supported by GeniusBridge'); throw new Error('Single chain swaps are not supported by GeniusBridge'); } if (!this.chains.includes(networkIn)) { logger.error(`Network ${networkIn} not supported by GeniusBridge`); throw new Error(`Network ${networkIn} not supported by GeniusBridge`); } if (!this.chains.includes(networkOut)) { logger.error(`Network ${networkOut} not supported by GeniusBridge`); throw new Error(`Network ${networkOut} not supported by GeniusBridge`); } if (amountIn === '0') { logger.error('Amount in must be greater than 0'); throw new Error('Amount in must be greater than 0'); } // Validate token addresses based on network type if ((0, check_vm_1.isSolanaNetwork)(networkIn) && !(0, is_native_1.isNative)(tokenIn)) { try { (0, address_validation_1.validateSolanaAddress)(tokenIn); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { logger.error(`Invalid Solana token address: ${tokenIn}`); throw new Error(`Invalid Solana token address: ${tokenIn}`); } } else if ((0, check_vm_1.isEVMNetwork)(networkIn) && !(0, is_native_1.isNative)(tokenIn)) { try { (0, address_validation_1.validateAndChecksumEvmAddress)(tokenIn); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { logger.error(`Invalid EVM token address: ${tokenIn}`); throw new Error(`Invalid EVM token address: ${tokenIn}`); } } if ((0, check_vm_1.isSolanaNetwork)(networkOut) && !(0, is_native_1.isNative)(tokenOut)) { try { (0, address_validation_1.validateSolanaAddress)(tokenOut); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { logger.error(`Invalid Solana token address: ${tokenOut}`); throw new Error(`Invalid Solana token address: ${tokenOut}`); } } else if ((0, check_vm_1.isEVMNetwork)(networkOut) && !(0, is_native_1.isNative)(tokenOut)) { try { (0, address_validation_1.validateAndChecksumEvmAddress)(tokenOut); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { logger.error(`Invalid EVM token address: ${tokenOut}`); throw new Error(`Invalid EVM token address: ${tokenOut}`); } } } validateQuoteParams(params) { this.validatePriceParams(params); if (!params.from) { logger.error('From address is required for quote'); throw new Error('From address is required for quote'); } // Verify 'to' address if provided if (params.to) { if ((0, check_vm_1.isSolanaNetwork)(params.networkOut)) { try { (0, address_validation_1.validateSolanaAddress)(params.to); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { logger.error(`Invalid Solana receiver address: ${params.to}`); throw new Error(`Invalid Solana receiver address: ${params.to}`); } } else if ((0, check_vm_1.isEVMNetwork)(params.networkOut)) { try { (0, address_validation_1.validateAndChecksumEvmAddress)(params.to); // eslint-disable-next-line @typescript-eslint/no-unused-vars } catch (error) { logger.error(`Invalid EVM receiver address: ${params.to}`); throw new Error(`Invalid EVM receiver address: ${params.to}`); } } } } transformPriceParams(params) { let { networkIn, networkOut, tokenIn, tokenOut } = params; const { amountIn, slippage, from } = params; // Handle token address transformation if ((0, is_native_1.isNative)(tokenIn)) { if ((0, check_vm_1.isEVMNetwork)(networkIn)) { tokenIn = constants_1.NATIVE_ADDRESS; } else if ((0, check_vm_1.isSolanaNetwork)(networkIn)) { tokenIn = constants_1.SOL_NATIVE_ADDRESS; } } if ((0, is_native_1.isNative)(tokenOut)) { if ((0, check_vm_1.isEVMNetwork)(networkOut)) { tokenOut = constants_1.NATIVE_ADDRESS; } else if ((0, check_vm_1.isSolanaNetwork)(networkOut)) { tokenOut = constants_1.SOL_NATIVE_ADDRESS; } } return { networkIn, networkOut, tokenIn, tokenOut, amountIn, slippage, from, }; } transformQuoteParams(params) { const transformedPriceParams = this.transformPriceParams(params); return { ...transformedPriceParams, to: params.to || params.from, authority: { networkInAddress: params.from, networkOutAddress: params.to || params.from, }, }; } } exports.GeniusBridgeSdk = GeniusBridgeSdk; //# sourceMappingURL=genius-bridge.js.map