Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags; edited tags
Link
Rob Hitchens
  • 55.6k
  • 11
  • 92
  • 147
Source Link
Sergey
  • 131
  • 3

Cost-effective way to calculate final user balance based on variable events

Apologies for the title. I'm not sure what would be a more descriptive way to name this question.

Assume there is a contract which has 3 potential parties. Buyers, sellers and shareholders. Buyers/sellers trade with the contract and shareholders earn 1% fees on such trades.

Let's say I bought 100 shares in the contract which then sells $1000 worth of tokens to someone. I now have a claim to $10 (1000*0.01) which were accounted for as fees.

Sound simple. But the problem is that anyone after me can purchase shares as well which will dilute my ownership.

Given the following scenario:

  • If I buy 100 shares in the contract
  • The contract sells to someone tokens for $1,000
  • I now have a claim to $10 (1000*0.01)
  • Someone else buys an additional 100 shares, diluting my ownership to just 50%
  • The contract sells more tokens for another $1,000. But out of this sale, I only have a claim to $5.
  • My final claim is now $10 + $5.

So the question is: since my ownership percentage can be different on each trade, how can the contract calculate my final balance after say 10,000 different trades in all of which I could have had a different ownership percentage?

There could be tens of thousands of shareholders and hundreds of thousands of trades. So updating balances for each shareholder on each trade event would be too costly.

Is there a cost-effective way to calculate my final balance? Or is the entire logic flawed and something like this is not possible?