Skip to content

Commit

Permalink
dev: removed zero-transfer rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
heswithme committed Sep 27, 2024
1 parent 4a320f2 commit 6e7a9d6
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions contracts/mocks/ERC20Rebasing.vy
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ def allowance(_owner: address, _spender: address) -> uint256:

@external
def transfer(_to: address, _value: uint256) -> bool:
self._rebase()
_shares: uint256 = self._get_shares_by_coins(_value)

if _shares > 0:
# only rebase on nonzero transfers
self._rebase()

self.shares[msg.sender] -= _shares
self.shares[_to] += _shares
log Transfer(msg.sender, _to, _value)
Expand All @@ -74,9 +77,13 @@ def transfer(_to: address, _value: uint256) -> bool:

@external
def transferFrom(_from: address, _to: address, _value: uint256) -> bool:
self._rebase()
_shares: uint256 = self._get_shares_by_coins(_value)
_shares = min(self.shares[_from], _shares)

if _shares > 0:
# only rebase on nonzero transfers
self._rebase()

# Value can be less than expected even if self.shares[_from] > _shares
_new_value: uint256 = self._get_coins_by_shares(_shares)

Expand All @@ -99,20 +106,20 @@ def approve(_spender: address, _value: uint256) -> bool:
@view
def _share_price() -> uint256:
if self.totalShares == 0:
return 10 ** self.decimals
return self.totalCoin * 10 ** self.decimals / self.totalShares
return 10**self.decimals
return self.totalCoin * 10**self.decimals / self.totalShares


@internal
@view
def _get_coins_by_shares(_shares: uint256) -> uint256:
return _shares * self._share_price() / 10 ** self.decimals
return _shares * self._share_price() / 10**self.decimals


@internal
@view
def _get_shares_by_coins(_coins: uint256) -> uint256:
return _coins * 10 ** self.decimals / self._share_price()
return _coins * 10**self.decimals / self._share_price()


@external
Expand Down

0 comments on commit 6e7a9d6

Please sign in to comment.