New to the Defi space and I may be overcomplicating this...

I'm trying to find a programmatic way to calculate the total p&l for ERC20 tokens I've staked. I understand that in general to get the current value of my assets staked in a token the calculation would be: [token_current_price * num_of_tokens]. (And then for net profit in ETH, go through my previous transactions and keep subtracting (cost_of_eth_at_transaction_time * num_tokens_staked)).

What is confusing to me though, is that the value of ETH itself fluctuates, which makes it nearly impossible to calculate my real return in USD (unless I'm misunderstanding)? What if the ETH used to buy the tokens was valued lowered when initially traded (USD -> ETH). Wouldn't this mean that instead of using cost_of_eth_at_transaction_time, I would have to use cost_of_eth_at_time_eth_was_traded_for_usd?

Is there an API that abstracts this information, where I can provide my address and see how much I've earned or lost in ETH or USD?


Here's how I understood the underlying questions:

  1. "How to calculate total profit/loss on a single investment?" (Meaning by "investment" a single purchase of some amount of ERC20 token. The sum of your separate p/l's will yield your net p/l.)
  2. "Is there an API out there that does this for me?"

I would argue that the latter is unnecessary, because the calculation is straightforward.

The first and most important thing is to decide on relative to what you're calculating.

P/L relative to ETH:

Assuming you have some amount of ERC20 token, and you know how to retrieve its current price in ETH, and you have a record of how much ETH you initially spent on purchasing it:

num_of_tokens * token_current_price_in_eth - eth_spent_at_purchase

Will give you the net ETH profit on that particular investment.

In the event that your num_of_tokens is an aggregate of purchases you made at various points in time, all you need to do is ensure that eth_spent_at_purchase is the sum of all ETH you ever spent on purchasing this particular ERC20 token.

P/L relative to USD:

If you would like to do all of your calculations on the basis of data from the Ethereum blockchain, I would recommend to use stablecoins such as DAI, USDC, or USDT to measure your USD income.

Assuming you have some amount of ERC20 token, you know how to retrieve its current price in ETH, you know how to retrieve the current price of ETH in a stablecoin, and you have a record of how much ETH you initially spent on purchasing it together with the stablecoin price of ETH at that time:

num_of_tokens * token_current_price_in_eth * eth_current_price_in_stablecoin - eth_spent_at_purchase * eth_stablecoin_price_at_purchase

Will give you the net USD income on that particular investment. This may have a tiny amount of imprecision as stablecoins only approximate the real-world price of USD, but as they do a very good job of it, the imprecision will not be significant enough to distort your measurements.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.