使用Python建立到DYDX的客户端连接的步骤是什么?

vlf7wbxs  于 2023-05-05  发布在  Python
关注(0)|答案(1)|浏览(127)

我得到这个错误,当我尝试使客户端连接到DYDX。我已经包含了所有必要的API_keys和主机。
Error connecting to client: 'Eth' object has no attribute 'defaultAccount'
下面是创建到Dydx的客户端连接的代码

from dydx3 import Client
from web3 import Web3

from constants import (
    HOST,
    DYDX_API_KEY,
    DYDX_API_SECRET,
    DYDX_API_PASSPHRASE,
    ETH_PRIVATE_KEY,
    STARK_PRIVATE_KEY,
    HTTP_PROVIDER
)

def connect_dydx():

    # Create client connection
    client = Client(
        host=HOST,
        api_key_credentials={
            "key": DYDX_API_KEY,
            "secret": DYDX_API_SECRET,
            "passphrase": DYDX_API_PASSPHRASE
        },
        stark_private_key=STARK_PRIVATE_KEY,
        eth_private_key=ETH_PRIVATE_KEY,
        web3=Web3(Web3.HTTPProvider(HTTP_PROVIDER)),
        
    )

    # Confirm Client
    account = client._private.get_account()
    account_id = account.data["account"]["id"]
    quote_balance = account.data["account"]["quoteBalance"]

    print("Connection Successful")
    print("Account ID: ", account_id)
    print("Quote Balance: ", quote_balance)

    return client

这是针对main.py

from connection import connect_dydx

if __name__ == "__main__":
    
    # Connect to client
    try:
        client = connect_dydx()
    except Exception as e:
        print(e)
        print("Error connecting to client: ", e)
        exit(1)

我已经尝试更新包和库,但还没有任何工作。

jgovgodb

jgovgodb1#

你有没有试过默认的例子:https://github.com/dydxprotocol/dydx-v3-python/blob/master/examples/onboard.py

'''Example for onboarding an account and accessing private endpoints.

Usage: python -m examples.onboard
'''

from dydx3 import Client
from dydx3.constants import API_HOST_GOERLI
from dydx3.constants import NETWORK_ID_GOERLI
from web3 import Web3

# Ganache test address.
ETHEREUM_ADDRESS = '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b'

# Ganache node.
WEB_PROVIDER_URL = 'http://localhost:8545'

client = Client(
    network_id=NETWORK_ID_GOERLI,
    host=API_HOST_GOERLI,
    default_ethereum_address=ETHEREUM_ADDRESS,
    web3=Web3(Web3.HTTPProvider(WEB_PROVIDER_URL)),
)

# Set STARK key.
stark_key_pair_with_y_coordinate = client.onboarding.derive_stark_key()
client.stark_private_key = stark_key_pair_with_y_coordinate['private_key']
(public_x, public_y) = (
    stark_key_pair_with_y_coordinate['public_key'],
    stark_key_pair_with_y_coordinate['public_key_y_coordinate'],
)

# Onboard the account.
onboarding_response = client.onboarding.create_user(
    stark_public_key=public_x,
    stark_public_key_y_coordinate=public_y,
)
print('onboarding_response', onboarding_response)

# Query a private endpoint.
accounts_response = client.private.get_accounts()
print('accounts_response', accounts_response)

相关问题