Rebase function in BEP20 token contract

1.5k Views Asked by At

Can anyone please explain to me the rebase function of this solidity code? I am building a rebase token like Titano.

function rebase(uint256 epoch, int256 supplyDelta)
        external
        onlyOwner
        returns (uint256)
    {
        require(!inSwap, "Try again");
        if (supplyDelta == 0) {
            emit LogRebase(epoch, _totalSupply);
            return _totalSupply;
        }
        if (supplyDelta < 0) {
            _totalSupply = _totalSupply.sub(uint256(-supplyDelta));
        } else {
            _totalSupply = _totalSupply.add(uint256(supplyDelta));
        }
        if (_totalSupply > MAX_SUPPLY) {
            _totalSupply = MAX_SUPPLY;
        }
        _gonsPerFragment = TOTAL_GONS.div(_totalSupply);
        pairContract.sync();
        emit LogRebase(epoch, _totalSupply);
        return _totalSupply;
    }

You can see the full code of the contract here.

1

There are 1 best solutions below

1
On BEST ANSWER

It changes the total supply by the supplyDelta param value.

A usual approach with rebase tokens is to store each holders stake percentage instead of their absolute token amount. Their actual token amount is then calculated by multiplying the stake percentage by a variable - in this case the _gonsPerFragment value.

Example:

Total supply 100, Alice owns 80% and Bob owns 20% of the tokens. This makes Alice owner of 80 tokens and Bob owner of 20 tokens.

Now lets start a new epoch and rebase the total supply by +200, making it total of 300. Alice now owns 240 tokens (still 80%) and Bob now owns 60 tokens (still 20%).