Consider a contract which manages an unlimited amount of ERC20 tokens. A user can create a token via this contract and give it a name. The contract also acts as exchange for buying and selling tokens it created for Dai:

contract TokenManager {

    function createNewToken(string name) external {
        // Deploy a new ERC20 token with this name

    function buy(string name, uint amount) external {
        // Mint tokens to user, take Dai

    function sell(string name, uint amount) external {
        // Burn tokens from user, send Dai

The exact price model of the tokens is not important for this question. The key point is that the price of a token increases the more Dai were spent for it and vice versa.

Now to the question: The Dai users pay for the tokens can be invested into Compound or similar to generate interest. I want to spread this generated interest across a given amount of the highest ranking tokens. For example the top 100 tokens receive all of the generated interest according to their share of Dai in the top 100 tokens. By receiving the interest their price rises further.

The problem is keeping track which of the tokens are in the top 100. Theoretically it would be possible with a linked-list which contains all tokens ordered by their market cap. However in practice the gas costs would be too high for an unlimited amount of tokens. Is there a way to do this with constant gas?

  • Finding the top 100 would require (partial) sorting, which is not gas-friendly. You may be able to simplify by giving interest to all tokens, proportional to their market-share. Then again, this can be expensive if the number of tokens are significantly greater than 100. – hrkrshnn Jul 12 '20 at 16:18

Your Answer

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

Browse other questions tagged or ask your own question.