Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround: Bybit の Derivatives v3 Contract API に対応する #219

Merged
merged 2 commits into from Jan 17, 2023

Conversation

cococig
Copy link
Contributor

@cococig cococig commented Jan 10, 2023

#184 の修正

  • WebSocketでBybitのprivate channelの認証が通るようにしました
    • 手元のDerivatives V3 Contract API Testnetで動作を確認しました
  • 合わせてtest_ws.pyにBybit用のテストケースを追加しました
  • REST APIでの認証については現状のままでもDerivatives V3 Contract APIでは問題なく動作しているため、変更を加えていません

[Author edited]

Bybit Derivatives v3 Contract API の対応

Derivatives v3 Contract API 1 の認証が有効になるよう認証ロジックを追加した。

REST API は従来の認証ロジックで認証可能だったが、WebSocket は新たに認証メッセージの送信が必要であるためその実装を追加している。

Derivatives v3 Unifired Margin API 2 の REST / WebSocket 認証については別途検証及び対応が必要であるため今回の PR では実装しない。

# request code
async def main():
    async with pybotters.Client() as client:
        ws = await client.ws_connect(
            "wss://stream.bybit.com/contract/private/v3",
            send_json={
                "op": "subscribe",
                "req_id": "10110001",
                "args": ["user.wallet.contractAccount"],
            },
        )
        await ws

# output
{'req_id': '10110001', 'success': True, 'ret_msg': '', 'op': 'subscribe', 'conn_id': 'cev1kaj49coq5truuq70-1u13'}

Footnotes

  1. https://bybit-exchange.github.io/docs/derivativesV3/contract

  2. https://bybit-exchange.github.io/docs/derivativesV3/unified_margin

@MtkN1
Copy link
Member

MtkN1 commented Jan 11, 2023

PR ありがとうございます!

REST API が現状の認証で通るというのは認識してませんでした。 であれば WebSocket の変更だけでよさそうですね。
こちらでも動作確認してみます。

Copy link
Member

@MtkN1 MtkN1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

確認致しました!

✅ Derivatives v3 Contract API に関しては動作確認しました。

しかしながら実際にリクエストして検証してると一部問題点があります。

問題点

  1. Derivatives v3 Contract API は認証が通るが Derivatives v3 Unifired Margin API は認証は通らない

例えば Unifired Margin の https://api.bybit.com/unified/v3/private/account/wallet/balance は認証が通らないようです。

👉 これ自体は対応不要で、この PR は Derivatives v3 Contract API のみ暫定対応する要件の PR に変更させてください。 Unifired Margin のテストは USD デリバティブアカウントにアップグレードが必要ですし、問題点が色々とみえたのでより包括的に v3 API に対応できるような実装を自分の方で対応したいと思います。

  1. WebSocket の認証メッセージ送信が Bybit の他の契約タイプの処理に影響を与えており、例えば Invserse 契約でも認証メッセージが送信されてしまいサーバー側に切断される
# 確認コード
async def main():
    async with pybotters.Client() as client:
        ws = await client.ws_connect(
            "wss://stream.bybit.com/realtime",
            send_json={"op": "subscribe", "args": ["wallet"]},
        )
        await ws

main ブランチであれば {'success': True, 'ret_msg': '', 'conn_id': '182b5330-c603-47dd-b59f-4cde5cb3da8c', 'request': {'op': 'subscribe', 'args': ['wallet']}} と print されますが PR ブランチであると何も出力されなくなります。

👉 一先ずバグを無くすため WS エンドポイントパスを /contract/private/v3 から始まる場合認証を送るよう suggestion のように修正お願いしてよろしいでしょうか?

pybotters/ws.py Outdated Show resolved Hide resolved
Commit suggestion

Co-authored-by: MtkN1 <51289448+MtkN1@users.noreply.github.com>
Copy link
Member

@MtkN1 MtkN1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

暫定対応として Merge させて頂きます。 Contribute ありがとうございました!

@MtkN1 MtkN1 changed the title Bybit の Derivatives v3 API に対応する Workaround: Bybit の Derivatives v3 Contract API に対応する Jan 17, 2023
@MtkN1 MtkN1 merged commit 155efe4 into pybotters:main Jan 17, 2023
@MtkN1 MtkN1 mentioned this pull request Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants