Browse Source

一些不影響的更新。

skyfffire 5 months ago
parent
commit
eb87f8ec89
2 changed files with 94 additions and 14 deletions
  1. 45 0
      checker/ok_chain_client.py
  2. 49 14
      web3_py_client.py

+ 45 - 0
checker/ok_chain_client.py

@@ -150,6 +150,51 @@ def approve(chain_id, token_contract_address, approve_amount):
 
     return send_get_request(get_request_path, get_params)
 
+def gas_limit(chain_id, from_addr, to_addr, value, call_data=None):
+    post_request_path = '/api/v5/dex/pre-transaction/gas-limit'
+    post_params = {
+        'chainIndex': chain_id,
+        'fromAddress': from_addr,
+        'toAddress': to_addr,
+        'txAmount': str(value)
+    }
+
+    if call_data is not None:
+        post_params['extJson'] = call_data
+
+    return send_post_request(post_request_path, post_params)
+
+def broadcast(chain_id, address, signed_tx):
+    post_request_path = '/api/v5/dex/pre-transaction/broadcast-transaction'
+    post_params = {
+        'chainIndex': chain_id,
+        'address': address,
+        'signedTx': signed_tx
+    }
+
+    return send_post_request(post_request_path, post_params)
+
+def orders(chain_id, address, tx_status=None, order_id=None):
+    get_request_path = '/api/v5/dex/post-transaction/orders'
+    get_params = {
+        'chainIndex': chain_id,
+        'address': address
+    }
+
+    '''
+    交易状态:
+        1: 排队中
+        2: 成功
+        3: 失败
+    '''
+    if tx_status in [1, 2, 3]:
+        get_params['txStatus'] = txStatus
+
+    if order_id is not None:
+        get_params['orderId'] = order_id
+
+    return send_get_request(get_request_path, get_params)
+
 if __name__ == "__main__":
     import decimal
     import pprint

+ 49 - 14
web3_py_client.py

@@ -147,8 +147,8 @@ class EthClient:
         """估算交易的 gas limit"""
         return self.w3.eth.estimate_gas(tx)
 
-    def _sign_and_send_transaction(self, tx: dict, gas_limit_multiplier: float = 1.2) -> str:
-        """签署并发送交易,返回交易哈希"""
+    def _sign(self, tx: dict, gas_limit_multiplier: float = 1.2) -> dict:
+        """签署并返回簽名結果"""
         try:
             # 填充 gas 和 nonce (如果未提供)
             if 'nonce' not in tx:
@@ -175,6 +175,15 @@ class EthClient:
             tx['gas'] = int(int(tx['gas']) * gas_limit_multiplier)
 
             signed_tx = self.w3.eth.account.sign_transaction(tx, self.account.key)
+
+            return signed_tx
+        except Exception as e:
+            logging.info(f"Error signing transaction: {e}")
+
+    def _sign_and_send_transaction(self, tx: dict, gas_limit_multiplier: float = 1.2) -> str:
+        """签署并发送交易,返回交易哈希"""
+        try:
+            signed_tx = self._sign(tx, gas_limit_multiplier)
             tx_hash = self.w3.eth.send_raw_transaction(signed_tx.raw_transaction)
             return self.w3.to_hex(tx_hash)
         except Exception as e:
@@ -351,18 +360,24 @@ class EthClient:
         return self.w3.from_wei(balance_wei, 'ether')
 
 if __name__ == "__main__":
-    from ok_chain_client import swap
-    import decimal
-    import pprint
+    import traceback
+    import time
+    from pprint import pprint
+    from checker import ok_chain_client
+    # from ok_chain_client import swap, broadcast, orders
+    from decimal import Decimal
+    from config import wallet
+
+    pprint(ok_chain_client.api_config)
 
     client = EthClient()
 
-    # CHAIN_ID = 1
+    CHAIN_ID = 1
     # IN_AMOUNT_TO_QUERY = decimal.Decimal('1')
     # IN_TOKEN_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7'     # USDT on Ethereum
     # IN_TOKEN_DECIMALS = decimal.Decimal(6)
     # OUT_TOKEN_ADDRESS = '0xf816507E690f5Aa4E29d164885EB5fa7a5627860'    # RATO on Ethereum
-    USER_WALLET = ''
+    USER_WALLET = wallet['user_wallet']
     # SLIPPAGE = 1
     # USER_EXCHANGE_WALLET = '0xc71835a042F4d870B0F4296cc89cAeb921a9f3DA'
 
@@ -388,19 +403,39 @@ if __name__ == "__main__":
         tx['maxPriorityFeePerGas'] = int(tx['maxPriorityFeePerGas'])
         tx['maxFeePerGas'] = int(int(latest_block['baseFeePerGas']) * 2 + tx['maxPriorityFeePerGas'])
 
-        pprint.pprint(tx)
-        estimated_gas = client.w3.eth.estimate_gas(tx)
-        estimated_wei = estimated_gas * (tx['maxPriorityFeePerGas'] + tx['maxFeePerGas'])
-        estimated_eth = estimated_wei / (10 ** 18)
-        logging.info(f"估算的燃气量: {estimated_gas}, eth消耗: {estimated_eth}")
+        # pprint(ok_chain_client.gas_limit(CHAIN_ID, tx['from'], tx['to'], tx['value']))
 
-        logging.info(f"餘額:{client.w3.eth.get_balance(USER_WALLET)}")
+        # pprint(tx)
+        # estimated_gas = client.w3.eth.estimate_gas(tx)
+        # estimated_wei = estimated_gas * (tx['maxPriorityFeePerGas'] + tx['maxFeePerGas'])
+        # estimated_eth = estimated_wei / (10 ** 18)
+        # logging.info(f"估算的燃气量: {estimated_gas}, eth消耗: {estimated_eth}")
+
+        # logging.info(f"餘額:{client.w3.eth.get_balance(USER_WALLET) / Decimal('1e18')}")
 
         # tx_hash = client._sign_and_send_transaction(tx)
         # receipt = client.wait_for_transaction_receipt(tx_hash)
         # logging.info(f"{tx_hash} 交易已确认! Status: {'Success' if receipt.status == 1 else 'Failed'}")
+
+
+        # # ok api發交易測試
+        # signed_tx = client._sign(tx)
+        # raw_transaction = client.w3.to_hex(signed_tx.raw_transaction)
+        # broadcast_rst = ok_chain_client.broadcast(CHAIN_ID, USER_WALLET, raw_transaction)
+
+        # pprint(broadcast_rst)
+
+        # order_id = broadcast_rst['data'][0]['orderId']
+
+        # while True:
+        #     wallet_orders = ok_chain_client.orders(CHAIN_ID, USER_WALLET)
+        #     pprint(wallet_orders)
+
+        #     time.sleep(1)
     except Exception as e:
-        print(f"Gas 估算失败: {e}")
+        print(f"測試失败: {e}")
+
+        traceback.print_exc()
     
     # # --- 使用示例 ---
     # # 确保你的 .env 文件配置正确