-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_gateio_borrow_isolated_margin_try_inf.py
91 lines (75 loc) · 2.52 KB
/
01_gateio_borrow_isolated_margin_try_inf.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import ccxt
import time
import requests
import hashlib
import hmac
# =====================================
# 입력
GATEIO_API = ""
GATEIO_SECRET = ""
BORROW_TICKER = "BTC" # 빌리고자 하는 코인
USDT_AMOUNT = 10 # 사용할 USDT 금액
# =====================================
exchange = ccxt.gate(
{
"apiKey": GATEIO_API,
"secret": GATEIO_SECRET,
}
)
def transfer_and_borrow(ticker):
# Spot에서 Margin으로 USDT 전송
try:
exchange.transfer(
code="USDT",
amount=USDT_AMOUNT,
fromAccount="spot",
toAccount="margin",
params={"currency_pair": f"{ticker}_USDT"},
)
print(f"성공적으로 {USDT_AMOUNT} USDT를 Margin으로 전송했습니다.")
except Exception as e:
print(f"전송 중 오류 발생: {e}")
return
# max borrowable 구하기
try:
borrowable = get_maximum_borrowable(ticker)
amount = float(borrowable["borrowable"])
print(f"최대 대출 가능 금액: {amount}")
except Exception as e:
print(f"최대 대출 가능 금액 조회 중 오류 발생: {e}")
return
# 대출 시도
while True:
try:
exchange.borrowIsolatedMargin(f"{ticker}/USDT", ticker, amount)
print(f"성공적으로 {amount} {ticker}를 대출했습니다.")
break
except Exception as e:
print(f"대출 중 오류 발생: {e}")
time.sleep(1)
def get_maximum_borrowable(ticker):
host = "https://api.gateio.ws"
prefix = "/api/v4"
url = "/margin/uni/borrowable"
headers = {"Accept": "application/json", "Content-Type": "application/json"}
query_param = f"currency={ticker}¤cy_pair={ticker}_USDT"
sign_headers = gen_sign("GET", prefix + url, query_param)
headers.update(sign_headers)
response = requests.request(
"GET", host + prefix + url + "?" + query_param, headers=headers
)
return response.json()
def gen_sign(method, url, query_string=None, payload_string=None):
key = GATEIO_API
secret = GATEIO_SECRET
t = time.time()
m = hashlib.sha512()
m.update((payload_string or "").encode("utf-8"))
hashed_payload = m.hexdigest()
s = "%s\n%s\n%s\n%s\n%s" % (method, url, query_string or "", hashed_payload, t)
sign = hmac.new(
secret.encode("utf-8"), s.encode("utf-8"), hashlib.sha512
).hexdigest()
return {"KEY": key, "Timestamp": str(t), "SIGN": sign}
# 사용 예시
transfer_and_borrow(BORROW_TICKER)