liquidity-gauge-diff

Created Diff never expires
197 removals
Lines
Total
Removed
Words
Total
Removed
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
656 lines
55 additions
Lines
Total
Added
Words
Total
Added
To continue using this feature, upgrade to
Diffchecker logo
Diffchecker Pro
523 lines
# @version 0.2.16
# @version 0.3.10
"""
"""
@title Liquidity Gauge v4
@title Liquidity Gauge v4 XChain
@author StakeDAO Protocol
@author StakeDAO Protocol
@license MIT
@license MIT
"""
"""


# Original idea and credit:
# Original idea and credit:
# Curve Finance's veCRV
# Curve Finance's veCRV
# https://github.com/curvefi/curve-dao-contracts/blob/master/contracts/gauges/LiquidityGaugeV4.vy
# https://github.com/curvefi/curve-dao-contracts/blob/master/contracts/gauges/LiquidityGaugeV4.vy
# Mostly forked from Curve, except that now there is no direct link between the gauge controller
# Mostly forked from Curve, except that now there is no direct link between the gauge controller
# and the gauges. In this implementation, SDT rewards are like any other token rewards.
# and the gauges. In this implementation, SDT rewards are like any other token rewards.


from vyper.interfaces import ERC20
from vyper.interfaces import ERC20


implements: ERC20
implements: ERC20

interface VotingEscrow:
def user_point_epoch(addr: address) -> uint256: view
def user_point_history__ts(addr: address, epoch: uint256) -> uint256: view

interface VotingEscrowBoost:
def adjusted_balance_of(_account: address) -> uint256: view


interface ERC20Extended:
interface ERC20Extended:
def symbol() -> String[26]: view
def symbol() -> String[26]: view
def decimals() -> uint256: view
def decimals() -> uint256: view


Text moved from lines 45-49
event CommitOwnership:
admin: address

event ApplyOwnership:
admin: address


event Deposit:
event Deposit:
provider: indexed(address)
provider: indexed(address)
value: uint256
value: uint256


event Withdraw:
event Withdraw:
provider: indexed(address)
provider: indexed(address)
value: uint256
value: uint256


event UpdateLiquidityLimit:
user: address
original_balance: uint256
original_supply: uint256
working_balance: uint256
working_supply: uint256

Text moved to lines 22-26
event CommitOwnership:
admin: address

event ApplyOwnership:
admin: address

event Transfer:
event Transfer:
_from: indexed(address)
_from: indexed(address)
_to: indexed(address)
_to: indexed(address)
_value: uint256
_value: uint256


event Approval:
event Approval:
_owner: indexed(address)
_owner: indexed(address)
_spender: indexed(address)
_spender: indexed(address)
_value: uint256
_value: uint256


event RewardDataUpdate:
event RewardDataUpdate:
_token: indexed(address)
_token: indexed(address)
_amount: uint256
_amount: uint256


struct Reward:
struct Reward:
token: address
token: address
distributor: address
distributor: address
period_finish: uint256
period_finish: uint256
rate: uint256
rate: uint256
last_update: uint256
last_update: uint256
integral: uint256
integral: uint256



MAX_REWARDS: constant(uint256) = 10
MAX_REWARDS: constant(uint256) = 8
TOKENLESS_PRODUCTION: constant(uint256) = 40
TOKENLESS_PRODUCTION: constant(uint256) = 40
WEEK: constant(uint256) = 604800
WEEK: constant(uint256) = 604800


SDT: public(address)
voting_escrow: public(address)
veBoost_proxy: public(address)

staking_token: public(address)
staking_token: public(address)
decimal_staking_token: public(uint256)
decimal_staking_token: public(uint256)


balanceOf: public(HashMap[address, uint256])
balanceOf: public(HashMap[address, uint256])
totalSupply: public(uint256)
totalSupply: public(uint256)
allowance: public(HashMap[address, HashMap[address, uint256]])
allowance: public(HashMap[address, HashMap[address, uint256]])


name: public(String[64])
name: public(String[64])
symbol: public(String[32])
symbol: public(String[32])


working_balances: public(HashMap[address, uint256])
working_supply: public(uint256)

integrate_checkpoint_of: public(HashMap[address, uint256])
integrate_checkpoint_of: public(HashMap[address, uint256])


# For tracking external rewards
# For tracking external rewards
reward_count: public(uint256)
reward_count: public(uint256)
reward_tokens: public(address[MAX_REWARDS])
reward_tokens: public(address[MAX_REWARDS])


reward_data: public(HashMap[address, Reward])
reward_data: public(HashMap[address, Reward])


# claimant -> default reward receiver
# claimant -> default reward receiver
rewards_receiver: public(HashMap[address, address])
rewards_receiver: public(HashMap[address, address])


# reward token -> claiming address -> integral
# reward token -> claiming address -> integral
reward_integral_for: public(HashMap[address, HashMap[address, uint256]])
reward_integral_for: public(HashMap[address, HashMap[address, uint256]])


# user -> [uint128 claimable amount][uint128 claimed amount]
# user -> [uint128 claimable amount][uint128 claimed amount]
claim_data: HashMap[address, HashMap[address, uint256]]
claim_data: HashMap[address, HashMap[address, uint256]]


admin: public(address)
admin: public(address)
future_admin: public(address)
future_admin: public(address)

claimer: public(address)
claimer: public(address)


initialized: public(bool)


@external
def __init__():
"""
@notice Contract constructor
@dev The contract has an initializer to prevent the take over of the implementation
"""
assert self.initialized == False #dev: contract is already initialized
self.initialized = True

@external
@external
def initialize(_staking_token: address, _admin: address, _SDT: address, _voting_escrow: address, _veBoost_proxy: address, _distributor: address):
def __init__(_staking_token: address, _admin: address):
"""
"""
@notice Contract initializer
@notice Contract initializer
@param _staking_token Liquidity Pool contract address
@param _staking_token Liquidity Pool contract address
@param _admin Admin who can kill the gauge
@param _reward_token
@param _SDT Address of the SDT token
@param _distributor
@param _voting_escrow Address of the veSDT contract
@param _veBoost_proxy Address of the proxy contract used to query veSDT balances and taking into account potential delegations
@param _distributor Address of the contract responsible for distributing SDT tokens to this gauge
"""
"""
assert self.initialized == False #dev: contract is already initialized
assert _staking_token != empty(address)
self.initialized = True
assert _admin != empty(address)

assert _admin != ZERO_ADDRESS
assert _SDT != ZERO_ADDRESS
assert _voting_escrow != ZERO_ADDRESS
assert _veBoost_proxy != ZERO_ADDRESS
assert _distributor != ZERO_ADDRESS


self.admin = _admin
self.admin = _admin
self.staking_token = _staking_token
self.staking_token = _staking_token
self.decimal_staking_token = ERC20Extended(_staking_token).decimals()
self.decimal_staking_token = ERC20Extended(_staking_token).decimals()


symbol: String[26] = ERC20Extended(_staking_token).symbol()
symbol: String[26] = ERC20Extended(_staking_token).symbol()
self.name = concat("Stake DAO ", symbol, " Gauge")
self.name = concat("Stake DAO ", symbol, " Gauge")
self.symbol = concat(symbol, "-gauge")
self.symbol = concat(symbol, "-gauge")
self.SDT = _SDT
self.voting_escrow = _voting_escrow
self.veBoost_proxy = _veBoost_proxy

# add in all liquidityGauge the SDT reward - the distribution could be null though
self.reward_data[_SDT].distributor = _distributor
self.reward_tokens[0] = _SDT
self.reward_count = 1


@view
@view
@external
@external
def decimals() -> uint256:
def decimals() -> uint256:
"""
"""
@notice Get the number of decimals for this token
@notice Get the number of decimals for this token
@dev Implemented as a view method to reduce gas costs
@dev Implemented as a view method to reduce gas costs
@return uint256 decimal places
@return uint256 decimal places
"""
"""
return self.decimal_staking_token
return self.decimal_staking_token



@internal
def _update_liquidity_limit(addr: address, l: uint256, L: uint256):
"""
@notice Calculate limits which depend on the amount of SDT token per-user.
Effectively it calculates working balances to apply amplification
of SDT production by SDT
@param addr User address
@param l User's amount of liquidity (LP tokens)
@param L Total amount of liquidity (LP tokens)
"""
# To be called after totalSupply is updated
voting_balance: uint256 = VotingEscrowBoost(self.veBoost_proxy).adjusted_balance_of(addr)
voting_total: uint256 = ERC20(self.voting_escrow).totalSupply()

lim: uint256 = l * TOKENLESS_PRODUCTION / 100
if voting_total > 0:
lim += L * voting_balance / voting_total * (100 - TOKENLESS_PRODUCTION) / 100

lim = min(l, lim)
old_bal: uint256 = self.working_balances[addr]
self.working_balances[addr] = lim
_working_supply: uint256 = self.working_supply + lim - old_bal
self.working_supply = _working_supply

log UpdateLiquidityLimit(addr, l, L, lim, _working_supply)


@internal
@internal
def _checkpoint_reward(_user: address, token: address, _total_supply: uint256, _user_balance: uint256, _claim: bool, receiver: address):
def _checkpoint_reward(_user: address, token: address, _total_supply: uint256, _user_balance: uint256, _claim: bool, receiver: address):
"""
"""
@notice Claim pending rewards and checkpoint rewards for a user
@notice Claim pending rewards and checkpoint rewards for a user
"""
"""
total_supply: uint256 = _total_supply
total_supply: uint256 = _total_supply
user_balance: uint256 = _user_balance
user_balance: uint256 = _user_balance
if token == self.SDT :
total_supply = self.working_supply
user_balance = self.working_balances[_user]


integral: uint256 = self.reward_data[token].integral
integral: uint256 = self.reward_data[token].integral
last_update: uint256 = min(block.timestamp, self.reward_data[token].period_finish)
last_update: uint256 = min(block.timestamp, self.reward_data[token].period_finish)
duration: uint256 = last_update - self.reward_data[token].last_update
duration: uint256 = last_update - self.reward_data[token].last_update
if duration != 0:
if duration != 0:
self.reward_data[token].last_update = last_update
self.reward_data[token].last_update = last_update
if total_supply != 0:
if total_supply != 0:
integral += duration * self.reward_data[token].rate * 10**18 / total_supply
integral += duration * self.reward_data[token].rate * 10**18 / total_supply
self.reward_data[token].integral = integral
self.reward_data[token].integral = integral


if _user != ZERO_ADDRESS:
if _user != empty(address):
integral_for: uint256 = self.reward_integral_for[token][_user]
integral_for: uint256 = self.reward_integral_for[token][_user]
new_claimable: uint256 = 0
new_claimable: uint256 = 0


if integral_for < integral:
if integral_for < integral:
self.reward_integral_for[token][_user] = integral
self.reward_integral_for[token][_user] = integral
new_claimable = user_balance * (integral - integral_for) / 10**18
new_claimable = user_balance * (integral - integral_for) / 10**18


claim_data: uint256 = self.claim_data[_user][token]
claim_data: uint256 = self.claim_data[_user][token]
total_claimable: uint256 = shift(claim_data, -128) + new_claimable
total_claimable: uint256 = (claim_data >> 128) + new_claimable
if total_claimable > 0:
if total_claimable > 0:
total_claimed: uint256 = claim_data % 2**128
total_claimed: uint256 = claim_data % 2**128
if _claim:
if _claim:
response: Bytes[32] = raw_call(
response: Bytes[32] = raw_call(
token,
token,
concat(
concat(
method_id("transfer(address,uint256)"),
method_id("transfer(address,uint256)"),
convert(receiver, bytes32),
convert(receiver, bytes32),
convert(total_claimable, bytes32),
convert(total_claimable, bytes32),
),
),
max_outsize=32,
max_outsize=32,
)
)
if len(response) != 0:
if len(response) != 0:
assert convert(response, bool)
assert convert(response, bool)
self.claim_data[_user][token] = total_claimed + total_claimable
self.claim_data[_user][token] = total_claimed + total_claimable
elif new_claimable > 0:
elif new_claimable > 0:
self.claim_data[_user][token] = total_claimed + shift(total_claimable, 128)
self.claim_data[_user][token] = total_claimed + (total_claimable << 128)
if token == self.SDT :
self.integrate_checkpoint_of[_user] = block.timestamp
@internal
@internal
def _checkpoint_rewards(_user: address, _total_supply: uint256, _claim: bool, _receiver: address, _only_checkpoint:bool = False):
def _checkpoint_rewards(_user: address, _total_supply: uint256, _claim: bool, _receiver: address):
"""
"""
@notice Claim pending rewards and checkpoint rewards for a user
@notice Claim pending rewards and checkpoint rewards for a user
"""
"""


receiver: address = _receiver
receiver: address = _receiver
user_balance: uint256 = 0
user_balance: uint256 = 0
if _user != ZERO_ADDRESS:
if _user != empty(address):
user_balance = self.balanceOf[_user]
user_balance = self.balanceOf[_user]
if _claim and _receiver == ZERO_ADDRESS:
if _claim and _receiver == empty(address):
# if receiver is not explicitly declared, check if a default receiver is set
# if receiver is not explicitly declared, check if a default receiver is set
receiver = self.rewards_receiver[_user]
receiver = self.rewards_receiver[_user]
if receiver == ZERO_ADDRESS:
if receiver == empty(address):
# if no default receiver is set, direct claims to the user
# if no default receiver is set, direct claims to the user
receiver = _user
receiver = _user


if _only_checkpoint:
reward_count: uint256 = self.reward_count
self._checkpoint_reward(_user, self.SDT, _total_supply, user_balance, False, receiver)
for i in range(MAX_REWARDS):
else:
if i == reward_count:
reward_count: uint256 = self.reward_count
break
for i in range(MAX_REWARDS):
token: address = self.reward_tokens[i]
if i == reward_count:
self._checkpoint_reward(_user, token, _total_supply, user_balance, _claim, receiver)
break
token: address = self.reward_tokens[i]
self._checkpoint_reward(_user, token, _total_supply, user_balance, _claim, receiver)

@external
def user_checkpoint(addr: address) -> bool:
"""
@notice Record a checkpoint for `addr`
@param addr User address
@return bool success
"""
assert msg.sender == addr # dev: unauthorized
total_supply: uint256 = self.totalSupply
self._checkpoint_rewards(addr, total_supply, False, ZERO_ADDRESS, True)
self._update_liquidity_limit(addr, self.balanceOf[addr], total_supply)
return True


@view
@view
@external
@external
def claimed_reward(_addr: address, _token: address) -> uint256:
def claimed_reward(_addr: address, _token: address) -> uint256:
"""
"""
@notice Get the number of already-claimed reward tokens for a user
@notice Get the number of already-claimed reward tokens for a user
@param _addr Account to get reward amount for
@param _addr Account to get reward amount for
@param _token Token to get reward amount for
@param _token Token to get reward amount for
@return uint256 Total amount of `_token` already claimed by `_addr`
@return uint256 Total amount of `_token` already claimed by `_addr`
"""
"""
return self.claim_data[_addr][_token] % 2**128
return self.claim_data[_addr][_token] % 2**128




@view
@view
@external
@external
def claimable_reward(_user: address, _reward_token: address) -> uint256:
def claimable_reward(_user: address, _reward_token: address) -> uint256:
"""
"""
@notice Get the number of claimable reward tokens for a user
@notice Get the number of claimable reward tokens for a user
@param _user Account to get reward amount for
@param _user Account to get reward amount for
@param _reward_token Token to get reward amount for
@param _reward_token Token to get reward amount for
@return uint256 Claimable reward token amount
@return uint256 Claimable reward token amount
"""
"""
integral: uint256 = self.reward_data[_reward_token].integral
integral: uint256 = self.reward_data[_reward_token].integral
total_supply: uint256 = self.totalSupply
total_supply: uint256 = self.totalSupply
user_balance: uint256 = self.balanceOf[_user]
user_balance: uint256 = self.balanceOf[_user]
if _reward_token == self.SDT:
total_supply = self.working_supply
user_balance = self.working_balances[_user]
if total_supply != 0:
if total_supply != 0:
last_update: uint256 = min(block.timestamp, self.reward_data[_reward_token].period_finish)
last_update: uint256 = min(block.timestamp, self.reward_data[_reward_token].period_finish)
duration: uint256 = last_update - self.reward_data[_reward_token].last_update
duration: uint256 = last_update - self.reward_data[_reward_token].last_update
integral += (duration * self.reward_data[_reward_token].rate * 10**18 / total_supply)
integral += (duration * self.reward_data[_reward_token].rate * 10**18 / total_supply)


integral_for: uint256 = self.reward_integral_for[_reward_token][_user]
integral_for: uint256 = self.reward_integral_for[_reward_token][_user]
new_claimable: uint256 = user_balance * (integral - integral_for) / 10**18
new_claimable: uint256 = user_balance * (integral - integral_for) / 10**18


return shift(self.claim_data[_user][_reward_token], -128) + new_claimable
return (self.claim_data[_user][_reward_token] >> 128) + new_claimable




@external
@external
def set_rewards_receiver(_receiver: address):
def set_rewards_receiver(_receiver: address):
"""
"""
@notice Set the default reward receiver for the caller.
@notice Set the default reward receiver for the caller.
@dev When set to ZERO_ADDRESS, rewards are sent to the caller
@dev When set to empty(address), rewards are sent to the caller
@param _receiver Receiver address for any rewards claimed via `claim_rewards`
@param _receiver Receiver address for any rewards claimed via `claim_rewards`
"""
"""
self.rewards_receiver[msg.sender] = _receiver
self.rewards_receiver[msg.sender] = _receiver



@external
@external
@nonreentrant('lock')
@nonreentrant('lock')
def claim_rewards(_addr: address = msg.sender, _receiver: address = ZERO_ADDRESS):
def claim_rewards(_addr: address = msg.sender, _receiver: address = empty(address)):
"""
"""
@notice Claim available reward tokens for `_addr`
@notice Claim available reward tokens for `_addr`
@param _addr Address to claim for
@param _addr Address to claim for
@param _receiver Address to transfer rewards to - if set to
@param _receiver Address to transfer rewards to - if set to
ZERO_ADDRESS, uses the default reward receiver
empty(address), uses the default reward receiver
for the caller
for the caller
"""
"""
if _receiver != ZERO_ADDRESS:
if _receiver != empty(address):
assert _addr == msg.sender # dev: cannot redirect when claiming for another user
assert _addr == msg.sender # dev: cannot redirect when claiming for another user
self._checkpoint_rewards(_addr, self.totalSupply, True, _receiver)
self._checkpoint_rewards(_addr, self.totalSupply, True, _receiver)


@external
@external
@nonreentrant('lock')
@nonreentrant('lock')
def claim_rewards_for(_addr: address, _receiver: address):
def claim_rewards_for(_addr: address, _receiver: address):
"""
"""
@notice Claim available reward tokens for `_addr`
@notice Claim available reward tokens for `_addr`
@param _addr Address to claim for
@param _addr Address to claim for
@param _receiver Address to transfer rewards to - if set to
@param _receiver Address to transfer rewards to - if set to
ZERO_ADDRESS, uses the default reward receiver
empty(address), uses the default reward receiver
for the caller
for the caller
"""
"""
assert self.claimer == msg.sender # dev: only the claim contract can claim for other
assert self.claimer == msg.sender # dev: only the claim contract can claim for other
if _receiver != _addr:
if _receiver != _addr:
assert _receiver == self.claimer # dev: if the receiver is not the user it needs to be the claimer
assert _receiver == self.claimer # dev: if the receiver is not the user it needs to be the claimer
self._checkpoint_rewards(_addr, self.totalSupply, True, _receiver)
self._checkpoint_rewards(_addr, self.totalSupply, True, _receiver)



@external
def kick(addr: address):
"""
@notice Kick `addr` for abusing their boost
@dev Only if either they had another voting event, or their voting escrow lock expired
@param addr Address to kick
"""
t_last: uint256 = self.integrate_checkpoint_of[addr]
t_ve: uint256 = VotingEscrow(self.voting_escrow).user_point_history__ts(
addr, VotingEscrow(self.voting_escrow).user_point_epoch(addr)
)
_balance: uint256 = self.balanceOf[addr]

assert ERC20(self.voting_escrow).balanceOf(addr) == 0 or t_ve > t_last # dev: kick not allowed
assert self.working_balances[addr] > _balance * TOKENLESS_PRODUCTION / 100 # dev: kick not needed

total_supply: uint256 = self.totalSupply
self._checkpoint_rewards(addr, total_supply, False, ZERO_ADDRESS, True)

self._update_liquidity_limit(addr, self.balanceOf[addr], total_supply)


@external
@external
@nonreentrant('lock')
@nonreentrant('lock')
def deposit(_value: uint256, _addr: address = msg.sender, _claim_rewards: bool = False):
def deposit(_value: uint256, _addr: address = msg.sender, _claim_rewards: bool = False):
"""
"""
@notice Deposit `_value` LP tokens
@notice Deposit `_value` LP tokens
@dev Depositting also claims pending reward tokens
@dev Depositting also claims pending reward tokens
@param _value Number of tokens to deposit
@param _value Number of tokens to deposit
@param _addr Address to deposit for
@param _addr Address to deposit for
"""
"""
total_supply: uint256 = self.totalSupply
total_supply: uint256 = self.totalSupply


if _value != 0:
if _value != 0:
is_rewards: bool = self.reward_count != 0
is_rewards: bool = self.reward_count != 0
if is_rewards:
if is_rewards:
self._checkpoint_rewards(_addr, total_supply, _claim_rewards, ZERO_ADDRESS)
self._checkpoint_rewards(_addr, total_supply, _claim_rewards, empty(address))


total_supply += _value
total_supply += _value
new_balance: uint256 = self.balanceOf[_addr] + _value
new_balance: uint256 = self.balanceOf[_addr] + _value
self.balanceOf[_addr] = new_balance
self.balanceOf[_addr] = new_balance
self.totalSupply = total_supply
self.totalSupply = total_supply


self._update_liquidity_limit(_addr, new_balance, total_supply)

ERC20(self.staking_token).transferFrom(msg.sender, self, _value)
ERC20(self.staking_token).transferFrom(msg.sender, self, _value)
else:
else:
self._checkpoint_rewards(_addr, total_supply, False, ZERO_ADDRESS, True)
self._checkpoint_rewards(_addr, total_supply, False, empty(address))


log Deposit(_addr, _value)
log Deposit(_addr, _value)
log Transfer(ZERO_ADDRESS, _addr, _value)
log Transfer(empty(address), _addr, _value)




@external
@external
@nonreentrant('lock')
@nonreentrant('lock')
def withdraw(_value: uint256, _claim_rewards: bool = False):
def withdraw(_value: uint256, _claim_rewards: bool = False):
"""
"""
@notice Withdraw `_value` LP tokens
@notice Withdraw `_value` LP tokens
@dev Withdrawing also claims pending reward tokens
@dev Withdrawing also claims pending reward tokens
@param _value Number of tokens to withdraw
@param _value Number of tokens to withdraw
"""
"""
total_supply: uint256 = self.totalSupply
total_supply: uint256 = self.totalSupply


if _value != 0:
if _value != 0:
is_rewards: bool = self.reward_count != 0
is_rewards: bool = self.reward_count != 0
if is_rewards:
if is_rewards:
self._checkpoint_rewards(msg.sender, total_supply, _claim_rewards, ZERO_ADDRESS)
self._checkpoint_rewards(msg.sender, total_supply, _claim_rewards, empty(address))


total_supply -= _value
total_supply -= _value
new_balance: uint256 = self.balanceOf[msg.sender] - _value
new_balance: uint256 = self.balanceOf[msg.sender] - _value
self.balanceOf[msg.sender] = new_balance
self.balanceOf[msg.sender] = new_balance
self.totalSupply = total_supply
self.totalSupply = total_supply


self._update_liquidity_limit(msg.sender, new_balance, total_supply)

ERC20(self.staking_token).transfer(msg.sender, _value)
ERC20(self.staking_token).transfer(msg.sender, _value)
else:
else:
self._checkpoint_rewards(msg.sender, total_supply, False, ZERO_ADDRESS, True)
self._checkpoint_rewards(msg.sender, total_supply, False, empty(address))


log Withdraw(msg.sender, _value)
log Withdraw(msg.sender, _value)
log Transfer(msg.sender, ZERO_ADDRESS, _value)
log Transfer(msg.sender, empty(address), _value)




@internal
@internal
def _transfer(_from: address, _to: address, _value: uint256):
def _transfer(_from: address, _to: address, _value: uint256):
total_supply: uint256 = self.totalSupply
total_supply: uint256 = self.totalSupply


if _value != 0:
if _value != 0:
is_rewards: bool = self.reward_count != 0
is_rewards: bool = self.reward_count != 0
if is_rewards:
if is_rewards:
self._checkpoint_rewards(_from, total_supply, False, ZERO_ADDRESS)
self._checkpoint_rewards(_from, total_supply, False, empty(address))
new_balance: uint256 = self.balanceOf[_from] - _value
new_balance: uint256 = self.balanceOf[_from] - _value
self.balanceOf[_from] = new_balance
self.balanceOf[_from] = new_balance
self._update_liquidity_limit(_from, new_balance, total_supply)


if is_rewards:
if is_rewards:
self._checkpoint_rewards(_to, total_supply, False, ZERO_ADDRESS)
self._checkpoint_rewards(_to, total_supply, False, empty(address))
new_balance = self.balanceOf[_to] + _value
new_balance = self.balanceOf[_to] + _value
self.balanceOf[_to] = new_balance
self.balanceOf[_to] = new_balance
self._update_liquidity_limit(_to, new_balance, total_supply)
else:
else:
self._checkpoint_rewards(_from, total_supply, False, ZERO_ADDRESS, True)
self._checkpoint_rewards(_from, total_supply, False, empty(address))
self._checkpoint_rewards(_to, total_supply, False, ZERO_ADDRESS, True)
self._checkpoint_rewards(_to, total_supply, False, empty(address))


log Transfer(_from, _to, _value)
log Transfer(_from, _to, _value)




@external
@external
@nonreentrant('lock')
@nonreentrant('lock')
def transfer(_to : address, _value : uint256) -> bool:
def transfer(_to : address, _value : uint256) -> bool:
"""
"""
@notice Transfer token for a specified address
@notice Transfer token for a specified address
@dev Transferring claims pending reward tokens for the sender and receiver
@dev Transferring claims pending reward tokens for the sender and receiver
@param _to The address to transfer to.
@param _to The address to transfer to.
@param _value The amount to be transferred.
@param _value The amount to be transferred.
"""
"""
self._transfer(msg.sender, _to, _value)
self._transfer(msg.sender, _to, _value)


return True
return True




@external
@external
@nonreentrant('lock')
@nonreentrant('lock')
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
"""
"""
@notice Transfer tokens from one address to another.
@notice Transfer tokens from one address to another.
@dev Transferring claims pending reward tokens for the sender and receiver
@dev Transferring claims pending reward tokens for the sender and receiver
@param _from address The address which you want to send tokens from
@param _from address The address which you want to send tokens from
@param _to address The address which you want to transfer to
@param _to address The address which you want to transfer to
@param _value uint256 the amount of tokens to be transferred
@param _value uint256 the amount of tokens to be transferred
"""
"""
_allowance: uint256 = self.allowance[_from][msg.sender]
_allowance: uint256 = self.allowance[_from][msg.sender]
if _allowance != MAX_UINT256:
if _allowance != max_value(uint256):
self.allowance[_from][msg.sender] = _allowance - _value
self.allowance[_from][msg.sender] = _allowance - _value


self._transfer(_from, _to, _value)
self._transfer(_from, _to, _value)


return True
return True




@external
@external
def approve(_spender : address, _value : uint256) -> bool:
def approve(_spender : address, _value : uint256) -> bool:
"""
"""
@notice Approve the passed address to transfer the specified amount of
@notice Approve the passed address to transfer the specified amount of
tokens on behalf of msg.sender
tokens on behalf of msg.sender
@dev Beware that changing an allowance via this method brings the risk
@dev Beware that changing an allowance via this method brings the risk
that someone may use both the old and new allowance by unfortunate
that someone may use both the old and new allowance by unfortunate
transaction ordering. This may be mitigated with the use of
transaction ordering. This may be mitigated with the use of
{incraseAllowance} and {decreaseAllowance}.
{incraseAllowance} and {decreaseAllowance}.
https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
@param _spender The address which will transfer the funds
@param _spender The address which will transfer the funds
@param _value The amount of tokens that may be transferred
@param _value The amount of tokens that may be transferred
@return bool success
@return bool success
"""
"""
self.allowance[msg.sender][_spender] = _value
self.allowance[msg.sender][_spender] = _value
log Approval(msg.sender, _spender, _value)
log Approval(msg.sender, _spender, _value)


return True
return True




@external
@external
def increaseAllowance(_spender: address, _added_value: uint256) -> bool:
def increaseAllowance(_spender: address, _added_value: uint256) -> bool:
"""
"""
@notice Increase the allowance granted to `_spender` by the caller
@notice Increase the allowance granted to `_spender` by the caller
@dev This is alternative to {approve} that can be used as a mitigation for
@dev This is alternative to {approve} that can be used as a mitigation for
the potential race condition
the potential race condition
@param _spender The address which will transfer the funds
@param _spender The address which will transfer the funds
@param _added_value The amount of to increase the allowance
@param _added_value The amount of to increase the allowance
@return bool success
@return bool success
"""
"""
allowance: uint256 = self.allowance[msg.sender][_spender] + _added_value
allowance: uint256 = self.allowance[msg.sender][_spender] + _added_value
self.allowance[msg.sender][_spender] = allowance
self.allowance[msg.sender][_spender] = allowance


log Approval(msg.sender, _spender, allowance)
log Approval(msg.sender, _spender, allowance)


return True
return True




@external
@external
def decreaseAllowance(_spender: address, _subtracted_value: uint256) -> bool:
def decreaseAllowance(_spender: address, _subtracted_value: uint256) -> bool:
"""
"""
@notice Decrease the allowance granted to `_spender` by the caller
@notice Decrease the allowance granted to `_spender` by the caller
@dev This is alternative to {approve} that can be used as a mitigation for
@dev This is alternative to {approve} that can be used as a mitigation for
the potential race condition
the potential race condition
@param _spender The address which will transfer the funds
@param _spender The address which will transfer the funds
@param _subtracted_value The amount of to decrease the allowance
@param _subtracted_value The amount of to decrease the allowance
@return bool success
@return bool success
"""
"""
allowance: uint256 = self.allowance[msg.sender][_spender] - _subtracted_value
allowance: uint256 = self.allowance[msg.sender][_spender] - _subtracted_value
self.allowance[msg.sender][_spender] = allowance
self.allowance[msg.sender][_spender] = allowance


log Approval(msg.sender, _spender, allowance)
log Approval(msg.sender, _spender, allowance)


return True
return True


@external
@external
def add_reward(_reward_token: address, _distributor: address):
def add_reward(_reward_token: address, _distributor: address):
"""
"""
@notice Set the active reward contract
@notice Set the active reward contract
"""
"""
assert msg.sender == self.admin # dev: only owner
admin: address = self.admin
assert _reward_token != ZERO_ADDRESS
assert msg.sender == admin # dev: only owner
assert _reward_token != empty(address)


reward_count: uint256 = self.reward_count
reward_count: uint256 = self.reward_count
assert reward_count < MAX_REWARDS
assert reward_count < MAX_REWARDS
assert self.reward_data[_reward_token].distributor == ZERO_ADDRESS
assert self.reward_data[_reward_token].distributor == empty(address)


self.reward_data[_reward_token].distributor = _distributor
self.reward_data[_reward_token].distributor = _distributor
self.reward_tokens[reward_count] = _reward_token
self.reward_tokens[reward_count] = _reward_token
self.reward_count = reward_count + 1
self.reward_count = reward_count + 1


@external
@external
def set_reward_distributor(_reward_token: address, _distributor: address):
def set_reward_distributor(_reward_token: address, _distributor: address):
current_distributor: address = self.reward_data[_reward_token].distributor
current_distributor: address = self.reward_data[_reward_token].distributor

admin: address = self.admin
assert msg.sender == current_distributor or msg.sender == self.admin
assert msg.sender == current_distributor or msg.sender == admin
assert current_distributor != ZERO_ADDRESS
assert current_distributor != empty(address)
assert _distributor != ZERO_ADDRESS
assert _distributor != empty(address)


self.reward_data[_reward_token].distributor = _distributor
self.reward_data[_reward_token].distributor = _distributor


@external
@external
def set_claimer(_claimer: address):
def set_claimer(_claimer: address):
assert msg.sender == self.admin
admin: address = self.admin
assert _claimer != ZERO_ADDRESS
assert msg.sender == admin
assert _claimer != empty(address)


self.claimer = _claimer
self.claimer = _claimer


@external
@external
@nonreentrant("lock")
@nonreentrant("lock")
def deposit_reward_token(_reward_token: address, _amount: uint256):
def deposit_reward_token(_reward_token: address, _amount: uint256):
assert msg.sender == self.reward_data[_reward_token].distributor
assert msg.sender == self.reward_data[_reward_token].distributor


self._checkpoint_rewards(ZERO_ADDRESS, self.totalSupply, False, ZERO_ADDRESS)
self._checkpoint_rewards(empty(address), self.totalSupply, False, empty(address))


response: Bytes[32] = raw_call(
response: Bytes[32] = raw_call(
_reward_token,
_reward_token,
concat(
concat(
method_id("transferFrom(address,address,uint256)"),
method_id("transferFrom(address,address,uint256)"),
convert(msg.sender, bytes32),
convert(msg.sender, bytes32),
convert(self, bytes32),
convert(self, bytes32),
convert(_amount, bytes32),
convert(_amount, bytes32),
),
),
max_outsize=32,
max_outsize=32,
)
)
if len(response) != 0:
if len(response) != 0:
assert convert(response, bool)
assert convert(response, bool)


period_finish: uint256 = self.reward_data[_reward_token].period_finish
period_finish: uint256 = self.reward_data[_reward_token].period_finish
if block.timestamp >= period_finish:
if block.timestamp >= period_finish:
self.reward_data[_reward_token].rate = _amount / WEEK
self.reward_data[_reward_token].rate = _amount / WEEK
else:
else:
remaining: uint256 = period_finish - block.timestamp
remaining: uint256 = period_finish - block.timestamp
leftover: uint256 = remaining * self.reward_data[_reward_token].rate
leftover: uint256 = remaining * self.reward_data[_reward_token].rate
self.reward_data[_reward_token].rate = (_amount + leftover) / WEEK
self.reward_data[_reward_token].rate = (_amount + leftover) / WEEK


self.reward_data[_reward_token].last_update = block.timestamp
self.reward_data[_reward_token].last_update = block.timestamp
self.reward_data[_reward_token].period_finish = block.timestamp + WEEK
self.reward_data[_reward_token].period_finish = block.timestamp + WEEK


log RewardDataUpdate(_reward_token,_amount)
log RewardDataUpdate(_reward_token,_amount)


@external
@external
def commit_transfer_ownership(addr: address):
def commit_transfer_ownership(addr: address):
"""
"""
@notice Transfer ownership of Gauge to `addr`
@notice Transfer ownership of Gauge to `addr`
@param addr Address to have ownership transferred to
@param addr Address to have ownership transferred to
"""
"""
assert msg.sender == self.admin # dev: admin only
assert msg.sender == self.admin # dev: admin only
assert addr != ZERO_ADDRESS # dev: future admin cannot be the 0 address
assert addr != empty(address) # dev: future admin cannot be the 0 address


self.future_admin = addr
self.future_admin = addr
log CommitOwnership(addr)
log CommitOwnership(addr)




@external
@external
def accept_transfer_ownership():
def accept_transfer_ownership():
"""
"""
@notice Accept a pending ownership transfer
@notice Accept a pending ownership transfer
"""
"""
_admin: address = self.future_admin
_admin: address = self.future_admin
assert msg.sender == _admin # dev: future admin only
assert msg.sender == _admin # dev: future admin only


self.admin = _admin
self.admin = _admin
log ApplyOwnership(_admin)
log ApplyOwnership(_admin)