Skip to content

Commit

Permalink
refreshUserReward in lock/unlock + increase precision
Browse files Browse the repository at this point in the history
  • Loading branch information
ridev6 committed Aug 7, 2023
1 parent 1778379 commit b1b596c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
21 changes: 14 additions & 7 deletions ride/boosting_v2.ride
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ let SCALE8 = 8
let MULT8 = 100000000
let POOLWEIGHTMULT = MULT8
let contractFilename = "boosting.ride"
let SCALE18 = 18
let MULT18 = 1_000_000_000_000_000_000
let MULT18BI = MULT18.toBigInt()

# 24 * 60
let blocksInDay = 1440
Expand Down Expand Up @@ -606,6 +609,8 @@ func lockActions(i: Invocation, durationMonths: Int) = {

let userGwxAmountTotal = getUserGwxAmountTotal(userAddress)

strict gwxRewardInv = gwxRewardContract.invoke("refreshUserReward", [userAddress.bytes], [])

let arr = if (userIsExisting) then [] else [
IntegerEntry(nextUserNumKEY, userNum + 1),
StringEntry(keyUser2NumMapping(userAddressStr), userNumStr),
Expand Down Expand Up @@ -685,18 +690,18 @@ func unlock(userAddressStr: String, txIdStr: String, amount: Int) = {
let t = (height - lockStart) / blocksInDay

let exponent = fraction(
t,
8 * blocksInDay * MULT8,
lockDuration
t.toBigInt(),
{ 8 * blocksInDay }.toBigInt() * MULT18BI,
lockDuration.toBigInt()
)
let wxWithdrawable = if (height > lockEnd) then {
userAmount - wxClaimed
} else {
fraction(
userAmount,
MULT8 - pow(5, 1, exponent, SCALE8, SCALE8, DOWN),
MULT8
)
userAmount.toBigInt(),
MULT18BI - pow(5.toBigInt(), 1, exponent, SCALE18, SCALE18, DOWN),
MULT18BI
).toInt()
}

if (amount > wxWithdrawable)
Expand All @@ -719,6 +724,8 @@ func unlock(userAddressStr: String, txIdStr: String, amount: Int) = {
let gwxAmountTotal = getGwxAmountTotal()
let userGwxAmountTotal = getUserGwxAmountTotal(userAddress)

strict gwxRewardInv = gwxRewardContract.invoke("refreshUserReward", [userAddress.bytes], [])

LockParamsEntry(userAddress, txId, userAmount, lockStart, lockDuration, gwxRemaining, wxClaimed + amount)
++ StatsEntry(-userAmount, 0, 0, -1) # fixme: -1 only if single lock from user existed
:+ HistoryEntry("unlock", userAddressStr, userAmount, lockStart, lockDuration, gwxBurned, i)
Expand Down
13 changes: 10 additions & 3 deletions ride/gwx_reward_v2.ride
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func getTradingReward(userAddress: String) = {
func keyRewardPerGwxIntegral() = ["%s", "rewardPerGwxIntegral"].makeString(SEP)

# call when reward or total gwx amount are changed
func refreshRewardPerGwxIntegral() = {
func _refreshRewardPerGwxIntegral() = {
# rewardPerGwx = dh * emissionPerBlock / totalGwxAmount + rewardPerGwxPrevious
let rewardPerGwxIntegralPrevious = {
match this.getString(keyRewardPerGwxIntegral()) {
Expand Down Expand Up @@ -260,7 +260,7 @@ func refreshRewardPerGwxIntegral() = {
func keyRewardPerGwxIntegralUserLast(userAddress: Address) = ["%s%s", "rewardPerGwxIntegralUserLast", userAddress.toString()].makeString(SEP)

# call when user total gwx amount is changed
func refreshUserReward(userAddress: Address) = {
func _refreshUserReward(userAddress: Address) = {
let rewardPerGwxIntegralUserLast = {
match this.getString(keyRewardPerGwxIntegralUserLast(userAddress)) {
case s: String => s.parseBigInt()
Expand All @@ -274,7 +274,7 @@ func refreshUserReward(userAddress: Address) = {
)
let userUnclaimed = userIdxOrFail.keyUserUnclaimed().getInteger().valueOrElse(0)

let (rewardPerGwxIntegralActions, rewardPerGwxIntegral) = refreshRewardPerGwxIntegral()
let (rewardPerGwxIntegralActions, rewardPerGwxIntegral) = _refreshRewardPerGwxIntegral()

# userReward = userGwxAmount * (rewardPerGwxIntegral - rewardPerGwxIntegralUserLast) + userRewardPrevious - userRewardClaimed
let userGwxAmount = boostingContractOrFail().invoke("getUserGwxAmount", [userAddress.toString()], []).exactAs[Int]
Expand All @@ -291,6 +291,13 @@ func refreshUserReward(userAddress: Address) = {
)
}

@Callable(i)
func refreshUserReward(userAddressBytes: ByteVector) = {
strict checkCaller = i.caller == boostingContractOrFail() || throwErr("permission denied")

_refreshUserReward(Address(userAddressBytes))
}

@Callable(i)
func tradeRewardInternal(
paymentAmountLeftOver: Int,
Expand Down

0 comments on commit b1b596c

Please sign in to comment.