0%

ok | ok api 平台对接

ok api 其实分为两种

  • oklink
  • okapi
    • 这篇文章主要说这个
    • 这里的 api 指的是平台 api,但是,平台 api 又分为两种
    • cex
    • 链上

这里主要贴一下 okpi 的认证。

这里主要是抓取了 ok 平台的链上挂单。

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
import base64
import hmac
import json
import urllib.parse
from datetime import datetime, timezone
from typing import Dict

import requests


class OkApiService:
key = ''
secret = ""

def pre_hash(self, timestamp, method, request_path, params=None):
# 根据字符串和参数创建预签名
query_string = ''
if method == 'GET' and params:
query_string = '?' + urllib.parse.urlencode(params).replace("%2520", "%20")
if method == 'POST' and params:
query_string = json.dumps(params)
return f"{timestamp}{method}{request_path}{query_string}"

def create_signature(self, method, request_path, params=None):
# 获取 ISO 8601 格式时间戳
timestamp = datetime.now(timezone.utc).isoformat()[:-9] + 'Z'
# 生成签名
message = self.pre_hash(timestamp, method, request_path, params)
signature = hmac.new(bytes(self.secret, encoding='utf8'), bytes(message, encoding='utf-8'), digestmod='sha256')
signature = base64.b64encode(signature.digest())
return signature, timestamp

def get_market_list(self, token_info: Dict) -> None:

url = f"https://www.okx.com/api/v5/mktplace/nft/markets/listings?chain={token_info.get('chain')}&collectionAddress={token_info.get('collectionAddress')}&limit={100}&sort=price_asc"

params = {
"chain": token_info.get('chain'),
"collectionAddress": token_info.get('collectionAddress'),
"limit": 100,
"sort": "price_asc"
}
signature, timestamp = self.create_signature("GET", "/api/v5/mktplace/nft/markets/listings", params)
headers = {
"Content-Type": 'application/json',
'OK-ACCESS-KEY': self.key,
'OK-ACCESS-SIGN': signature,
'OK-ACCESS-TIMESTAMP': timestamp,
'OK-ACCESS-PASSPHRASE': '',
}

response = requests.request("GET", url, headers=headers)
print(len(response.json().get("data").get("data")))
for d in response.json().get("data").get("data"):
print(f"{d.get('tokenId')}")


if __name__ == '__main__':
params = {
"chain": "Lumi%20Layer3",
"collectionAddress": "0x3fa10a9d43113687f2fd143fca7445d8ef8e334a"
}
OkApiService().get_market_list(params)
#
params = {
"chain": "eth",
"collectionAddress": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d"
}
OkApiService().get_market_list(params)

这里面有几个点要注意

  • lumi 的网络本来是
    • Lumi Layer3,中间有个空格,但是,空格可以用%20代替
    • 但是 %20 会被 urllib.parse.urlencode 变成 %2520,所以,加了一个 replace("%2520", "%20")
    • 这个地方可以再优化
  • 尽管 OK 平台的公开信息并不支持 Lumi Layer3,但是尝试后发现可以

请我喝杯咖啡吧~