Skip to main content
Glama

ONEDeFi MCP Server

by JMadhan1
yield_farming.py•17.2 kB
import os import logging from blockchain.ethereum import EthereumClient from blockchain.polygon import PolygonClient from blockchain.solana import SolanaClient logger = logging.getLogger(__name__) class YieldFarmingOperations: """Yield farming and liquidity provision operations""" def __init__(self): self.ethereum_client = EthereumClient() self.polygon_client = PolygonClient() self.solana_client = SolanaClient() # Protocol contract addresses self.protocols = { 'ethereum': { 'uniswap_v2': '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f', 'uniswap_v3': '0x1F98431c8aD98523631AE4a59f267346ea31F984', 'sushiswap': '0xC0AEe478e3658e2610c5F7A4A2E1777cE9e4f2Ac' }, 'polygon': { 'quickswap': '0x5757371414417b8C6CAad45bAeF941aBc7d3Ab32', 'sushiswap': '0xc35DADB65012eC5796536bD9864eD8773aBc74C4' } } def add_liquidity(self, blockchain, protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b): """Add liquidity to a farming pool""" try: if blockchain.lower() == 'ethereum': return self._add_liquidity_ethereum(protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b) elif blockchain.lower() == 'polygon': return self._add_liquidity_polygon(protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b) elif blockchain.lower() == 'solana': return self._add_liquidity_solana(protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b) else: return {"success": False, "error": f"Unsupported blockchain: {blockchain}"} except Exception as e: logger.error(f"Add liquidity operation failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_ethereum(self, protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b): """Add liquidity on Ethereum""" try: if protocol.lower() == 'uniswap': return self._add_liquidity_uniswap_v2(wallet_address, token_a, token_b, amount_a, amount_b) elif protocol.lower() == 'sushiswap': return self._add_liquidity_sushiswap(wallet_address, token_a, token_b, amount_a, amount_b) else: return {"success": False, "error": f"Unsupported protocol: {protocol}"} except Exception as e: logger.error(f"Ethereum liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_uniswap_v2(self, wallet_address, token_a, token_b, amount_a, amount_b): """Add liquidity to Uniswap V2""" try: router_address = "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D" # Uniswap V2 Router # ABI for addLiquidity function add_liquidity_abi = [{ "inputs": [ {"name": "tokenA", "type": "address"}, {"name": "tokenB", "type": "address"}, {"name": "amountADesired", "type": "uint256"}, {"name": "amountBDesired", "type": "uint256"}, {"name": "amountAMin", "type": "uint256"}, {"name": "amountBMin", "type": "uint256"}, {"name": "to", "type": "address"}, {"name": "deadline", "type": "uint256"} ], "name": "addLiquidity", "type": "function" }] # Calculate minimum amounts (95% of desired amounts for slippage protection) amount_a_min = int(int(amount_a) * 0.95) amount_b_min = int(int(amount_b) * 0.95) deadline = int(self.ethereum_client.get_current_timestamp()) + 3600 # 1 hour from now # Encode function call function_data = self.ethereum_client.encode_function_call( abi=add_liquidity_abi[0], args=[token_a, token_b, int(amount_a), int(amount_b), amount_a_min, amount_b_min, wallet_address, deadline] ) # Execute transaction tx_hash = self.ethereum_client.send_transaction( wallet_address=wallet_address, to_address=router_address, data=function_data, value="0" ) if tx_hash: # Calculate LP tokens received (this would be fetched from transaction receipt) lp_tokens = self._calculate_lp_tokens(token_a, token_b, amount_a, amount_b) return { "success": True, "tx_hash": tx_hash, "protocol": "uniswap_v2", "operation": "add_liquidity", "token_a": token_a, "token_b": token_b, "amount_a": amount_a, "amount_b": amount_b, "lp_tokens": lp_tokens, "pool_address": self._get_pair_address(token_a, token_b), "metadata": { "router": router_address, "deadline": deadline } } else: return {"success": False, "error": "Transaction failed"} except Exception as e: logger.error(f"Uniswap V2 liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_sushiswap(self, wallet_address, token_a, token_b, amount_a, amount_b): """Add liquidity to SushiSwap""" try: router_address = "0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F" # SushiSwap Router # Similar to Uniswap V2, using SushiSwap router add_liquidity_abi = [{ "inputs": [ {"name": "tokenA", "type": "address"}, {"name": "tokenB", "type": "address"}, {"name": "amountADesired", "type": "uint256"}, {"name": "amountBDesired", "type": "uint256"}, {"name": "amountAMin", "type": "uint256"}, {"name": "amountBMin", "type": "uint256"}, {"name": "to", "type": "address"}, {"name": "deadline", "type": "uint256"} ], "name": "addLiquidity", "type": "function" }] amount_a_min = int(int(amount_a) * 0.95) amount_b_min = int(int(amount_b) * 0.95) deadline = int(self.ethereum_client.get_current_timestamp()) + 3600 function_data = self.ethereum_client.encode_function_call( abi=add_liquidity_abi[0], args=[token_a, token_b, int(amount_a), int(amount_b), amount_a_min, amount_b_min, wallet_address, deadline] ) tx_hash = self.ethereum_client.send_transaction( wallet_address=wallet_address, to_address=router_address, data=function_data, value="0" ) if tx_hash: lp_tokens = self._calculate_lp_tokens(token_a, token_b, amount_a, amount_b) return { "success": True, "tx_hash": tx_hash, "protocol": "sushiswap", "operation": "add_liquidity", "token_a": token_a, "token_b": token_b, "amount_a": amount_a, "amount_b": amount_b, "lp_tokens": lp_tokens, "metadata": { "router": router_address } } else: return {"success": False, "error": "Transaction failed"} except Exception as e: logger.error(f"SushiSwap liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_polygon(self, protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b): """Add liquidity on Polygon""" try: if protocol.lower() == 'quickswap': return self._add_liquidity_quickswap(wallet_address, token_a, token_b, amount_a, amount_b) else: return {"success": False, "error": f"Unsupported protocol: {protocol}"} except Exception as e: logger.error(f"Polygon liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_quickswap(self, wallet_address, token_a, token_b, amount_a, amount_b): """Add liquidity to QuickSwap""" try: router_address = "0xa5E0829CaCEd8fFDD4De3c43696c57F7D7A678ff" # QuickSwap Router add_liquidity_abi = [{ "inputs": [ {"name": "tokenA", "type": "address"}, {"name": "tokenB", "type": "address"}, {"name": "amountADesired", "type": "uint256"}, {"name": "amountBDesired", "type": "uint256"}, {"name": "amountAMin", "type": "uint256"}, {"name": "amountBMin", "type": "uint256"}, {"name": "to", "type": "address"}, {"name": "deadline", "type": "uint256"} ], "name": "addLiquidity", "type": "function" }] amount_a_min = int(int(amount_a) * 0.95) amount_b_min = int(int(amount_b) * 0.95) deadline = int(self.polygon_client.get_current_timestamp()) + 3600 function_data = self.polygon_client.encode_function_call( abi=add_liquidity_abi[0], args=[token_a, token_b, int(amount_a), int(amount_b), amount_a_min, amount_b_min, wallet_address, deadline] ) tx_hash = self.polygon_client.send_transaction( wallet_address=wallet_address, to_address=router_address, data=function_data, value="0" ) if tx_hash: return { "success": True, "tx_hash": tx_hash, "protocol": "quickswap", "operation": "add_liquidity", "token_a": token_a, "token_b": token_b, "amount_a": amount_a, "amount_b": amount_b, "blockchain": "polygon", "metadata": { "router": router_address } } else: return {"success": False, "error": "Transaction failed"} except Exception as e: logger.error(f"QuickSwap liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_solana(self, protocol, wallet_address, pool_id, token_a, token_b, amount_a, amount_b): """Add liquidity on Solana""" try: if protocol.lower() == 'raydium': return self._add_liquidity_raydium(wallet_address, pool_id, token_a, token_b, amount_a, amount_b) elif protocol.lower() == 'orca': return self._add_liquidity_orca(wallet_address, pool_id, token_a, token_b, amount_a, amount_b) else: return {"success": False, "error": f"Unsupported protocol: {protocol}"} except Exception as e: logger.error(f"Solana liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def _add_liquidity_raydium(self, wallet_address, pool_id, token_a, token_b, amount_a, amount_b): """Add liquidity to Raydium""" try: # This would use Raydium SDK or direct instruction building instruction = self.solana_client.build_raydium_add_liquidity_instruction( wallet_address=wallet_address, pool_id=pool_id, token_a=token_a, token_b=token_b, amount_a=amount_a, amount_b=amount_b ) tx_hash = self.solana_client.send_transaction( wallet_address=wallet_address, instructions=[instruction] ) if tx_hash: return { "success": True, "tx_hash": tx_hash, "protocol": "raydium", "operation": "add_liquidity", "pool_id": pool_id, "token_a": token_a, "token_b": token_b, "amount_a": amount_a, "amount_b": amount_b, "blockchain": "solana" } else: return {"success": False, "error": "Transaction failed"} except Exception as e: logger.error(f"Raydium liquidity addition failed: {str(e)}") return {"success": False, "error": str(e)} def remove_liquidity(self, blockchain, protocol, wallet_address, pool_id, lp_tokens): """Remove liquidity from a farming pool""" try: if blockchain.lower() == 'ethereum': return self._remove_liquidity_ethereum(protocol, wallet_address, pool_id, lp_tokens) elif blockchain.lower() == 'polygon': return self._remove_liquidity_polygon(protocol, wallet_address, pool_id, lp_tokens) elif blockchain.lower() == 'solana': return self._remove_liquidity_solana(protocol, wallet_address, pool_id, lp_tokens) else: return {"success": False, "error": f"Unsupported blockchain: {blockchain}"} except Exception as e: logger.error(f"Remove liquidity operation failed: {str(e)}") return {"success": False, "error": str(e)} def get_farming_positions(self, blockchain, wallet_address): """Get farming positions for a wallet""" try: positions = [] if blockchain.lower() == 'ethereum': uniswap_positions = self._get_uniswap_positions(wallet_address) positions.extend(uniswap_positions) sushiswap_positions = self._get_sushiswap_positions(wallet_address) positions.extend(sushiswap_positions) elif blockchain.lower() == 'polygon': quickswap_positions = self._get_quickswap_positions(wallet_address) positions.extend(quickswap_positions) elif blockchain.lower() == 'solana': raydium_positions = self._get_raydium_positions(wallet_address) positions.extend(raydium_positions) orca_positions = self._get_orca_positions(wallet_address) positions.extend(orca_positions) return {"success": True, "positions": positions} except Exception as e: logger.error(f"Failed to get farming positions: {str(e)}") return {"success": False, "error": str(e)} def _calculate_lp_tokens(self, token_a, token_b, amount_a, amount_b): """Calculate LP tokens received (simplified calculation)""" # This would use actual pool reserves to calculate precise LP tokens return str(int(amount_a) + int(amount_b)) # Simplified calculation def _get_pair_address(self, token_a, token_b): """Get pair contract address for tokens""" # This would calculate or fetch the actual pair address return "0x" + "0" * 40 # Placeholder def _get_uniswap_positions(self, wallet_address): """Get Uniswap positions""" # This would query Uniswap subgraph or contracts return [] def _get_sushiswap_positions(self, wallet_address): """Get SushiSwap positions""" # This would query SushiSwap subgraph or contracts return [] def _get_quickswap_positions(self, wallet_address): """Get QuickSwap positions""" # This would query QuickSwap subgraph or contracts return [] def _get_raydium_positions(self, wallet_address): """Get Raydium positions""" # This would query Raydium API or on-chain data return [] def _get_orca_positions(self, wallet_address): """Get Orca positions""" # This would query Orca API or on-chain data return []

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JMadhan1/OneDefi-MCP'

If you have feedback or need assistance with the MCP directory API, please join our Discord server