From 2674cee4df5a02c3ad80a06fc9d85f78630db66f Mon Sep 17 00:00:00 2001 From: Nikita <128384873+JunkiJay@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:19:50 +0300 Subject: [PATCH] Update l2mp_leasing.ride --- ride/l2mp_leasing.ride | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ride/l2mp_leasing.ride b/ride/l2mp_leasing.ride index daac54c3..3c604bf1 100644 --- a/ride/l2mp_leasing.ride +++ b/ride/l2mp_leasing.ride @@ -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) @@ -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) @@ -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, @@ -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 = [