139 lines
5.0 KiB
Python
139 lines
5.0 KiB
Python
import base64
|
|
import hashlib
|
|
import hmac
|
|
import time
|
|
import requests
|
|
|
|
|
|
class BitgetAPI:
|
|
def __init__(self, api_key, secret_key, passphrase, proxies=None):
|
|
self.api_key = api_key
|
|
self.secret_key = secret_key
|
|
self.passphrase = passphrase
|
|
self.base_url = 'https://api.bitget.com'
|
|
self.session = requests.Session()
|
|
|
|
if proxies:
|
|
self.session.proxies.update(proxies)
|
|
|
|
self.session.headers.update({
|
|
'ACCESS-KEY': self.api_key,
|
|
'ACCESS-PASSPHRASE': self.passphrase,
|
|
'Content-Type': 'application/json',
|
|
'locale': 'zh-CN'
|
|
})
|
|
|
|
def _sign(self, params, request_path):
|
|
timestamp = str(int(time.time() * 1000))
|
|
query_string = '&'.join([f"{key}={value}" for key, value in params.items()])
|
|
|
|
if params:
|
|
prehash_string = timestamp + 'GET' + request_path + '?' + query_string
|
|
else:
|
|
prehash_string = timestamp + 'GET' + request_path
|
|
|
|
signature = hmac.new(self.secret_key.encode('utf-8'), prehash_string.encode('utf-8'), hashlib.sha256).digest()
|
|
signature_base64 = base64.b64encode(signature).decode('utf-8')
|
|
return timestamp, signature_base64, query_string
|
|
|
|
def _send_request(self, method, request_path, params=None):
|
|
if not params:
|
|
params = {}
|
|
|
|
timestamp, signature_base64, query_string = self._sign(params, request_path)
|
|
|
|
self.session.headers["ACCESS-SIGN"] = signature_base64
|
|
self.session.headers["ACCESS-TIMESTAMP"] = timestamp
|
|
|
|
url = f"{self.base_url}{request_path}?{query_string}"
|
|
response = self.session.request(method, url)
|
|
try:
|
|
return response.json()
|
|
except ValueError:
|
|
print(f"请求失败,返回内容非 JSON 格式: {response.text}")
|
|
return None
|
|
|
|
def get_virtual_subaccount_list(self, limit=20):
|
|
request_path = '/api/v2/user/virtual-subaccount-list'
|
|
params = {
|
|
"limit": str(limit),
|
|
}
|
|
return self._send_request('GET', request_path, params)
|
|
|
|
def get_subaccount_deposit_address(self, coin, chain, sub_uid):
|
|
request_path = '/api/v2/spot/wallet/subaccount-deposit-address'
|
|
params = {
|
|
"coin": coin,
|
|
"chain": chain,
|
|
"subUid": sub_uid,
|
|
}
|
|
return self._send_request('GET', request_path, params)
|
|
|
|
def get_account_info(self):
|
|
request_path = '/api/v2/spot/account/info'
|
|
params = {}
|
|
return self._send_request('GET', request_path, params)
|
|
|
|
def get_subaccount_user_id(self):
|
|
request_path = '/api/v2/spot/account/info'
|
|
params = {}
|
|
response = self._send_request('GET', request_path, params)
|
|
if response and 'data' in response and 'userId' in response['data']:
|
|
return response['data']['userId']
|
|
return None
|
|
|
|
def get_subaccount_address(self, coin, chain):
|
|
request_path = '/api/v2/spot/wallet/deposit-address'
|
|
params = {
|
|
"coin": coin,
|
|
"chain": chain,
|
|
}
|
|
response = self._send_request('GET', request_path, params)
|
|
if response and 'data' in response and 'address' in response['data']:
|
|
return response['data']['address']
|
|
return None
|
|
|
|
def get_userid_and_address(self, coin, chain):
|
|
userid_and_address = {}
|
|
# 获取主账户的信息
|
|
main_user_id = self.get_subaccount_user_id()
|
|
if main_user_id:
|
|
main_address = self.get_subaccount_address(coin, chain)
|
|
if main_address:
|
|
userid_and_address[main_user_id] = main_address
|
|
|
|
# 获取虚拟子账户列表
|
|
subaccounts_response = self.get_virtual_subaccount_list()
|
|
if subaccounts_response and 'data' in subaccounts_response and 'subAccountList' in subaccounts_response['data']:
|
|
subaccounts = subaccounts_response['data']['subAccountList']
|
|
for subaccount in subaccounts:
|
|
sub_uid = subaccount['subAccountUid']
|
|
deposit_address_response = self.get_subaccount_deposit_address(coin, chain, sub_uid)
|
|
if deposit_address_response and 'data' in deposit_address_response and 'address' in \
|
|
deposit_address_response['data']:
|
|
userid_and_address[sub_uid] = deposit_address_response['data']['address']
|
|
|
|
return userid_and_address
|
|
|
|
|
|
if __name__ == '__main__':
|
|
# 替换为您的 API 密钥信息
|
|
api_key = 'bg_de5d1fee9b211c0da479e7061aa5d45a'
|
|
secret_key = 'b94d9d470338ca84045ab1f6222d15954e7422de9e3a83a020f1b31446571b93'
|
|
passphrase = '040828cjj'
|
|
|
|
proxies = {
|
|
'http': f'socks5h://L2924:B928192@31.57.104.140:30000',
|
|
'https': f'socks5h://L2924:B928192@31.57.104.140:30000'
|
|
}
|
|
|
|
params = {
|
|
"coin": "SOL",
|
|
"chain": "sol",
|
|
}
|
|
|
|
bitget_api = BitgetAPI(api_key, secret_key, passphrase, proxies)
|
|
userid_and_address = bitget_api.get_userid_and_address(params["coin"], params["chain"])
|
|
print(userid_and_address)
|
|
|