Skip to content

Commit

Permalink
Update l2mp_leasing.ride
Browse files Browse the repository at this point in the history
  • Loading branch information
JunkiJay authored Oct 15, 2024
1 parent 9a6494f commit 2674cee
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions ride/l2mp_leasing.ride
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

let contractFile = "l2mp_leasing.ride"
let SEP = "__"
let BLOCKS_IN_INTERVAL = 1000
let scale8 = 1_0000_0000

func throwErr(msg: String) = {
throw(contractFile + ": " + msg)
Expand Down Expand Up @@ -110,6 +112,10 @@ func genVotesKeys(keyPrefix: String) = {
result
}

func keyClaimedBlock(userAddress: String) =
["%s", userAddress, "claimedBlock"].makeString(SEP)


# Count all votes for Prefix
func countVotesHelper(result: Int, voteKey: String) = {
result + getInteger(voteKey).valueOrElse(0)
Expand Down Expand Up @@ -340,6 +346,26 @@ func getStakeForActions(userAddress: String, i: Invocation) = {
]
}

func calculateClaim(userAddress: String) = {
let claimedBlockKey = keyClaimedBlock(userAddress)
let claimedBlock = this.getInteger(claimedBlockKey).valueOrElse(height)

let blocksPassed = height - claimedBlock
let amountStaked = getUserTotalLocked(userAddress)

let toClaimNow = blocksPassed * (amountStaked * scale8) / BLOCKS_IN_INTERVAL

toClaimNow
}

func setUser(userAddress: String, claimedBlock: Int) = {
let claimedBlockKey = keyClaimedBlock(userAddress)

[
IntegerEntry(claimedBlockKey, claimedBlock)
]
}

func getStakeActions(
nodeAddress: String,
userAddress: String,
Expand Down Expand Up @@ -427,6 +453,21 @@ func setNewPeriodLength(newPeriodLength: Int) = {
getSetNewPeriodLengthActions(newPeriodLength)
}

@Callable(i)
func claimUnit() = {
let userAddress = i.caller.toString()

let toClaimNow = calculateClaim(userAddress)

let claimedBlockKey = keyClaimedBlock(userAddress)
let actions = [
IntegerEntry(claimedBlockKey, height)
]
let transferUnits = ScriptTransfer(i.caller, toClaimNow, unit)

actions ++ [transferUnits]
}

@Callable(i)
func setForceStopFlag(stop: Boolean) = {
strict check = [
Expand Down

0 comments on commit 2674cee

Please sign in to comment.