Created
June 8, 2025 07:41
-
-
Save Tecnocrat/5c5dea602495a8bf82488b90a8381020 to your computer and use it in GitHub Desktop.
Trading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // SPDX-License-Identifier: MIT | |
| pragma solidity ^0.8.0; | |
| contract CryptoTradingApp { | |
| struct Position { | |
| uint256 leverage; | |
| uint256 amount; | |
| } | |
| mapping(address => Position) public tradePositions; | |
| event PositionOpened(address indexed trader, uint256 leverage, uint256 amount); | |
| function openPosition(uint256 leverage, uint256 amount) external { | |
| require(leverage >= 1, "Invalid leverage"); | |
| require(amount > 0, "Amount must be greater than zero"); | |
| tradePositions[msg.sender] = Position(leverage, amount); | |
| emit PositionOpened(msg.sender, leverage, amount); | |
| } | |
| function getPosition(address trader) external view returns (uint256 leverage, uint256 amount) { | |
| Position memory position = tradePositions[trader]; | |
| return (position.leverage, position.amount); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from web3 import Web3 | |
| import requests | |
| class BNBChainConnector: | |
| def __init__(self, rpc_url): | |
| self.web3 = Web3(Web3.HTTPProvider(rpc_url)) | |
| if not self.web3.is_connected(): | |
| raise ConnectionError("Failed to connect to BNB Chain RPC") | |
| def is_connected(self): | |
| return self.web3.is_connected() | |
| class BinanceAPI: | |
| @staticmethod | |
| def get_price(symbol="BNBUSDT"): | |
| url = f"https://api.binance.com/api/v3/ticker/price?symbol={symbol}" | |
| try: | |
| response = requests.get(url) | |
| response.raise_for_status() | |
| return float(response.json()["price"]) | |
| except (requests.RequestException, KeyError) as e: | |
| raise ValueError(f"Failed to fetch price for {symbol}: {e}") | |
| if __name__ == "__main__": | |
| # Connect to BNB Chain | |
| bnb_rpc = "https://bsc-dataseed.binance.org/" | |
| connector = BNBChainConnector(bnb_rpc) | |
| print("Connected to BNB:", connector.is_connected()) | |
| # Fetch and print BNB price | |
| try: | |
| price = BinanceAPI.get_price() | |
| print("BNB Price:", price) | |
| except ValueError as e: | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment