Ethereum: Update JSON portfolio not receiving data

Ethereum: Wallet JSON update not fetching data

As an experienced developer, it’s frustrating when a simple script doesn’t work as expected. In this article, we’ll solve the problem of saving Ethereum wallet data to a JSON file.

Ethereum: Update Portfolio JSON not getting data

Problem

The code you provided above:

import json

from datetime import datetime

import requests

binance.client import client






Initialize Binance API credentials

client = Client("YOUR_BTC_API_Key", "YOUR_BTC_API_SECRET")

symbol = "ETH"

exchange = "binance"

def update_portfolio():

portfolio = {}


Get current portfolio data

response = client.get_exchange_data(exchange=exchange, symbol=symbol)

data = json.loads(response.text)


Parse JSON data to update wallet dictionary

in data["balances"]:

if asset["symbol"] == symbol:

asset_amount = float(asset["amount"])

coin_name = asset["asset"]["name"]

portfolio[coin_name] = asset_amount


Save the updated wallet data to a JSON file

open("portfolio.json", "w") as a file:

json.dump(portfolio, file)

update_portfolio()

After adding this code, a portfolio.json file is created in the same directory. However, nothing seems to happen when you try to update it with json.load() or json.dump().

Solution

The problem lies in the way we parse the JSON data from the Binance API. We need to use specific fields in each asset object that correspond to the currency we want to track. Here is an updated version of the code:

import json

from datetime import datetime

import requests

binance.client import client


Initialize Binance API credentials

client = Client("YOUR_BTC_API_Key", "YOUR_BTC_API_SECRET")

symbol = "ETH"

exchange = "binance"

def update_portfolio():

portfolio = {}


Get current portfolio data

response = client.get_exchange_data(exchange=exchange, symbol=symbol)

data = json.loads(response.text)


Parse JSON data to update wallet dictionary

in data["balances"]:

if asset["symbol"] == symbol:

asset_amount = float(asset["amount"])

coin_name = asset["asset"]["name"]

portfolio[coin_name] = asset_amount


Save the updated portfolio data to a JSON file

open("portfolio.json", "w") as a file:

json.dump(portfolio, file)

update_portfolio()

In this revised version:

  • We use the json.load() variable directly instead of using the data variable.
  • We loop through each asset object in the response and use its attributes (e.g. asset["symbol"], asset["amount"]) to determine which currency we want to track.

Use case example

To test this updated code, create a new file named “main.py” with the following content:

import json

from datetime import datetime

import requests

binance.client import client


Initialize Binance API credentials

client = Client("YOUR_BTC_API_Key", "YOUR_BTC_API_SECRET")

symbol = "BTC/USDT"

exchange = "binance"

def main():

portfolio = {}


Get current portfolio data

response = client.get_exchange_data(exchange=exchange, symbol=symbol)

data = json.loads(response.text)


Parse the JSON data to update the wallet dictionary

in data["balances"]:

if asset["symbol"] == symbol:

asset_amount = float(asset["amount"])

coin_name = asset["asset"]["name"]

portfolio[coin_name] = asset_amount


Save the updated wallet data to a JSON file

open("portfolio.json", "w") as a file:

json.dump(portfolio, file)

main()

Run the script using python main.py . This should create a new portfolio.json file in the same directory.

Predicts Predicts Market Crypto News

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top