|
@@ -147,8 +147,8 @@ class EthClient:
|
|
|
"""估算交易的 gas limit"""
|
|
"""估算交易的 gas limit"""
|
|
|
return self.w3.eth.estimate_gas(tx)
|
|
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:
|
|
try:
|
|
|
# 填充 gas 和 nonce (如果未提供)
|
|
# 填充 gas 和 nonce (如果未提供)
|
|
|
if 'nonce' not in tx:
|
|
if 'nonce' not in tx:
|
|
@@ -175,6 +175,15 @@ class EthClient:
|
|
|
tx['gas'] = int(int(tx['gas']) * gas_limit_multiplier)
|
|
tx['gas'] = int(int(tx['gas']) * gas_limit_multiplier)
|
|
|
|
|
|
|
|
signed_tx = self.w3.eth.account.sign_transaction(tx, self.account.key)
|
|
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)
|
|
tx_hash = self.w3.eth.send_raw_transaction(signed_tx.raw_transaction)
|
|
|
return self.w3.to_hex(tx_hash)
|
|
return self.w3.to_hex(tx_hash)
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
@@ -351,18 +360,24 @@ class EthClient:
|
|
|
return self.w3.from_wei(balance_wei, 'ether')
|
|
return self.w3.from_wei(balance_wei, 'ether')
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
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()
|
|
client = EthClient()
|
|
|
|
|
|
|
|
- # CHAIN_ID = 1
|
|
|
|
|
|
|
+ CHAIN_ID = 1
|
|
|
# IN_AMOUNT_TO_QUERY = decimal.Decimal('1')
|
|
# IN_AMOUNT_TO_QUERY = decimal.Decimal('1')
|
|
|
# IN_TOKEN_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7' # USDT on Ethereum
|
|
# IN_TOKEN_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7' # USDT on Ethereum
|
|
|
# IN_TOKEN_DECIMALS = decimal.Decimal(6)
|
|
# IN_TOKEN_DECIMALS = decimal.Decimal(6)
|
|
|
# OUT_TOKEN_ADDRESS = '0xf816507E690f5Aa4E29d164885EB5fa7a5627860' # RATO on Ethereum
|
|
# OUT_TOKEN_ADDRESS = '0xf816507E690f5Aa4E29d164885EB5fa7a5627860' # RATO on Ethereum
|
|
|
- USER_WALLET = ''
|
|
|
|
|
|
|
+ USER_WALLET = wallet['user_wallet']
|
|
|
# SLIPPAGE = 1
|
|
# SLIPPAGE = 1
|
|
|
# USER_EXCHANGE_WALLET = '0xc71835a042F4d870B0F4296cc89cAeb921a9f3DA'
|
|
# USER_EXCHANGE_WALLET = '0xc71835a042F4d870B0F4296cc89cAeb921a9f3DA'
|
|
|
|
|
|
|
@@ -388,19 +403,39 @@ if __name__ == "__main__":
|
|
|
tx['maxPriorityFeePerGas'] = int(tx['maxPriorityFeePerGas'])
|
|
tx['maxPriorityFeePerGas'] = int(tx['maxPriorityFeePerGas'])
|
|
|
tx['maxFeePerGas'] = int(int(latest_block['baseFeePerGas']) * 2 + 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)
|
|
# tx_hash = client._sign_and_send_transaction(tx)
|
|
|
# receipt = client.wait_for_transaction_receipt(tx_hash)
|
|
# receipt = client.wait_for_transaction_receipt(tx_hash)
|
|
|
# logging.info(f"{tx_hash} 交易已确认! Status: {'Success' if receipt.status == 1 else 'Failed'}")
|
|
# 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:
|
|
except Exception as e:
|
|
|
- print(f"Gas 估算失败: {e}")
|
|
|
|
|
|
|
+ print(f"測試失败: {e}")
|
|
|
|
|
+
|
|
|
|
|
+ traceback.print_exc()
|
|
|
|
|
|
|
|
# # --- 使用示例 ---
|
|
# # --- 使用示例 ---
|
|
|
# # 确保你的 .env 文件配置正确
|
|
# # 确保你的 .env 文件配置正确
|