From c96feb1730f5d496f0b8f5a29fe631f07a533ff2 Mon Sep 17 00:00:00 2001 From: Epic waves rider <108883923+epicwavesrider@users.noreply.github.com> Date: Fri, 2 Dec 2022 17:25:46 +0400 Subject: [PATCH 1/3] fix: remainings in ido asset claim --- ride/ido.ride | 176 ++++++++++++++++++++++------------ test/components/ido/claim.mjs | 18 ++-- 2 files changed, 126 insertions(+), 68 deletions(-) diff --git a/ride/ido.ride b/ride/ido.ride index 1f8684abe..fedbf5b28 100644 --- a/ride/ido.ride +++ b/ride/ido.ride @@ -531,25 +531,64 @@ func claim(claimedAssetId58: String, userAddress58: String) = { let priceAssetDecimals = priceAssetId58.fromBase58String().assetInfo().value().decimals - let entries = if claimedAssetId58 == priceAssetId58 then { - let ( - periodMinAvailableToClaim, - periodTotalAvailableToClaim, - periodUserAvailableToClaim, - totalUserAvailableToClaim, - usdtPriceAssetAllowableRatio, - currentUsdtPriceAssetRatio, - endPeriodBlocksLeft, - updatedCurrentPeriod, - periodStart, - periodEnd - ) = internalClaimV2( - priceAssetId58, - userAddress58, - outAmount, - totalsDiff[IdxDiffClaimedPriceAmountIncrement] - ) + let ( + periodMinAvailableToClaim, + periodTotalAvailableToClaim, + periodUserAvailableToClaim, + totalUserAvailableToClaim, + usdtPriceAssetAllowableRatio, + currentUsdtPriceAssetRatio, + endPeriodBlocksLeft, + updatedCurrentPeriod, + periodStart, + periodEnd + ) = internalClaimV2( + priceAssetId58, + userAddress58, + outAmount, + totalsDiff[IdxDiffClaimedPriceAmountIncrement] + ) + + strict evaluateResult = this.invoke( + "claimREADONLY", [priceAssetId58, userAddress58], [] + ).asString().split(SEP) + + let availablePriceAmountToClaim = evaluateResult[3].parseIntValue() + let investorTotalAmount = origInvestArray[IdxInvTotalAmount].parseIntValue() + let investorRemainingAmount = origInvestArray[IdxInvRemainingAmount].parseIntValue() + let investorPriceAssetBalance = origInvestArray[IdxInvClaimedPriceAssetAmount].parseIntValue() + let investorIdoAssetBalance = origInvestArray[IdxInvClaimedIdoAssetAmount].parseIntValue() + let investorLastClaimedHeight = origInvestArray[IdxInvLastClaimedHeight].parseIntValue() + + let newInvestArray = [ + investorTotalAmount.toString(), + "0", + investorPriceAssetBalance.toString(), + investorIdoAssetBalance.toString(), + investorLastClaimedHeight.toString() + ] + + let totals = readTotalsArrayOrDefault() + let totalsTotalAmount = totals[IdxInvTotalAmount].parseIntValue() + let totalsRemainingAmount = totals[IdxInvRemainingAmount].parseIntValue() + let totalsClaimedPriceAssetAmount = totals[IdxInvRemainingAmount].parseIntValue() + let totalsClaimedIdoAssetAmount = totals[IdxInvClaimedPriceAssetAmount].parseIntValue() + let totalsLastClaimedHeight = totals[IdxInvLastClaimedHeight].parseIntValue() + + let newTotalsRemainingAmount = totalsRemainingAmount - investorRemainingAmount + + let newTotalArray = [ + totalsTotalAmount.toString(), + newTotalsRemainingAmount.toString(), + totalsClaimedPriceAssetAmount.toString(), + totalsClaimedIdoAssetAmount.toString(), + totalsLastClaimedHeight.toString() + ] + + let newTotalRemainingAmount = keyTotalRemainingAmount().getInteger().valueOrElse(0) + investorRemainingAmount + + let entries = if claimedAssetId58 == priceAssetId58 then { strict checks = [ periodUserAvailableToClaim > 0 || "unavailable to claim because user period allowance reached".throwErr(), periodTotalAvailableToClaim > 0 || "unavailable to claim because total period allowance reached".throwErr(), @@ -567,46 +606,7 @@ func claim(claimedAssetId58: String, userAddress58: String) = { IntegerEntry(keyPriceAssetBalance(userAddress58), priceAssetBalance - periodMinAvailableToClaim) ] } else { - strict evaluateResult = this.invoke( - "claimREADONLY", [priceAssetId58, userAddress58], [] - ).asString().split(SEP) - - let availablePriceAmountToClaim = evaluateResult[3].parseIntValue() - let lastClaimEntries = if availablePriceAmountToClaim == periodMinAvailableToClaim then { - let investorTotalAmount = origInvestArray[IdxInvTotalAmount].parseIntValue() - let investorRemainingAmount = origInvestArray[IdxInvRemainingAmount].parseIntValue() - let investorPriceAssetBalance = origInvestArray[IdxInvClaimedPriceAssetAmount].parseIntValue() - let investorIdoAssetBalance = origInvestArray[IdxInvClaimedIdoAssetAmount].parseIntValue() - let investorLastClaimedHeight = origInvestArray[IdxInvLastClaimedHeight].parseIntValue() - - let newInvestArray = [ - investorTotalAmount.toString(), - "0", - investorPriceAssetBalance.toString(), - investorIdoAssetBalance.toString(), - investorLastClaimedHeight.toString() - ] - - let totals = readTotalsArrayOrDefault() - let totalsTotalAmount = totals[IdxInvTotalAmount].parseIntValue() - let totalsRemainingAmount = totals[IdxInvRemainingAmount].parseIntValue() - let totalsClaimedPriceAssetAmount = totals[IdxInvRemainingAmount].parseIntValue() - let totalsClaimedIdoAssetAmount = totals[IdxInvClaimedPriceAssetAmount].parseIntValue() - let totalsLastClaimedHeight = totals[IdxInvLastClaimedHeight].parseIntValue() - - let newTotalsRemainingAmount = totalsRemainingAmount - investorRemainingAmount - - let newTotalArray = [ - totalsTotalAmount.toString(), - newTotalsRemainingAmount.toString(), - totalsClaimedPriceAssetAmount.toString(), - totalsClaimedIdoAssetAmount.toString(), - totalsLastClaimedHeight.toString() - ] - - let newTotalRemainingAmount = keyTotalRemainingAmount().getInteger().valueOrElse(0) + investorRemainingAmount - [ StringEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount.toString()), StringEntry(keyTotalRemainingAmount(), newTotalRemainingAmount.toString()), @@ -658,10 +658,12 @@ func claim(claimedAssetId58: String, userAddress58: String) = { ( [ + StringEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount.toString()), + StringEntry(keyTotalRemainingAmount(), newTotalRemainingAmount.toString()), ScriptTransfer(i.caller, outAmount + priceAssetBalanceIdoDecimals, outAssetId), IntegerEntry(keyPriceAssetBalance(userAddress58), 0), - TotalsEntry(keyInvestor(userAddress58), origInvestArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), - TotalsEntry(keyTotals(), readTotalsArrayOrDefault(), totalsDiff, newClaimPeriodHeight, priceAssetBalance), + TotalsEntry(keyInvestor(userAddress58), newInvestArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), + TotalsEntry(keyTotals(), newTotalArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), # TODO consider to add more data into history: operationPrice, realPrice ClaimOperationHistoryEntry( @@ -724,6 +726,62 @@ func claim(claimedAssetId58: String, userAddress58: String) = { # user spent remaining 6 price assets } +@Callable(i) +func cleanRemaingAmount(userAddress58: String) = { + let priceAssetId58 = readConfigArray()[IdxCfgPriceAssetId] + + strict evaluateResult = this.invoke( + "claimREADONLY", [priceAssetId58, userAddress58], [] + ).asString().split(SEP) + + let availablePriceAmountToClaim = evaluateResult[2].parseIntValue() + + strict checkUnclaimedAssets = availablePriceAmountToClaim == 0 || "user have unclaimed assets".throwErr() + + let investor = keyInvestor(userAddress58).getString().value().split(SEP) + let investorTotalAmount = investor[IdxInvTotalAmount].parseIntValue() + let investorRemainingAmount = investor[IdxInvRemainingAmount].parseIntValue() + let investorPriceAssetBalance = investor[IdxInvClaimedPriceAssetAmount].parseIntValue() + let investorIdoAssetBalance = investor[IdxInvClaimedIdoAssetAmount].parseIntValue() + let investorLastClaimedHeight = investor[IdxInvLastClaimedHeight].parseIntValue() + + strict investorRemainingAmountCheck = investorRemainingAmount != 0 || "investorRemainingAmount already zero".throwErr() + + let newInvestor = [ + "%d%d%d%d%d", + investorTotalAmount.toString(), + "0", + investorPriceAssetBalance.toString(), + investorIdoAssetBalance.toString(), + investorLastClaimedHeight.toString() + ].makeString(SEP) + + let totals = keyTotals().getString().value().split(SEP) + let totalsTotalAmount = totals[IdxInvTotalAmount].parseIntValue() + let totalsRemainingAmount = totals[IdxInvRemainingAmount].parseIntValue() + let totalsClaimedPriceAssetAmount = totals[IdxInvClaimedPriceAssetAmount].parseIntValue() + let totalsClaimedIdoAssetAmount = totals[IdxInvClaimedIdoAssetAmount].parseIntValue() + let totalsLastClaimedHeight = totals[IdxInvLastClaimedHeight].parseIntValue() + + let newTotals = [ + "%d%d%d%d%d", + totalsTotalAmount.toString(), + (totalsRemainingAmount - investorRemainingAmount).toString(), + totalsClaimedPriceAssetAmount.toString(), + totalsClaimedIdoAssetAmount.toString(), + totalsLastClaimedHeight.toString() + ].makeString(SEP) + + let newTotalRemainingAmount = keyTotalRemainingAmount().getInteger().valueOrElse(0) + investorRemainingAmount + + [ + StringEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount.toString()), + StringEntry(keyTotalRemainingAmount(), newTotalRemainingAmount.toString()), + StringEntry(keyInvestor(userAddress58), newInvestor), + StringEntry(keyTotals(), newTotals) + ] +} + @Callable(i) func claimREADONLY(claimedAssetId58: String, userAddress58: String) = { let claimResultTuple = internalClaim(claimedAssetId58, userAddress58.addressFromStringValue(), "".fromBase58String()) diff --git a/test/components/ido/claim.mjs b/test/components/ido/claim.mjs index 76c00c8ad..d02631aa5 100644 --- a/test/components/ido/claim.mjs +++ b/test/components/ido/claim.mjs @@ -116,7 +116,7 @@ describe('ido: claim.mjs', /** @this {MochaSuiteModified} */() => { expect( await checkStateChanges( - stateChanges.invokes[0].stateChanges, + stateChanges.invokes[1].stateChanges, 0, 0, 0, @@ -129,20 +129,20 @@ describe('ido: claim.mjs', /** @this {MochaSuiteModified} */() => { ), ).to.eql(true); - expect(stateChanges.invokes[0].dApp).to.eql(address(this.accounts.lpStable, chainId)); - expect(stateChanges.invokes[0].call.function).to.eql('putOneTknV2WithoutTakeFeeREADONLY'); - expect(stateChanges.invokes[0].call.args).to.eql([{ + expect(stateChanges.invokes[1].dApp).to.eql(address(this.accounts.lpStable, chainId)); + expect(stateChanges.invokes[1].call.function).to.eql('putOneTknV2WithoutTakeFeeREADONLY'); + expect(stateChanges.invokes[1].call.args).to.eql([{ type: 'Int', value: expectedPutOneTknV2PriceAssetAmount, }, { type: 'String', value: this.usdnAssetId, }]); - expect(stateChanges.invokes[0].payment).to.eql([]); + expect(stateChanges.invokes[1].payment).to.eql([]); expect( await checkStateChanges( - stateChanges.invokes[1].stateChanges, + stateChanges.invokes[2].stateChanges, 0, 0, 0, @@ -155,9 +155,9 @@ describe('ido: claim.mjs', /** @this {MochaSuiteModified} */() => { ), ).to.eql(true); - expect(stateChanges.invokes[1].dApp).to.eql(address(this.accounts.lpStable, chainId)); - expect(stateChanges.invokes[1].call.function).to.eql('getOneTknV2READONLY'); - expect(stateChanges.invokes[1].call.args).to.eql([{ + expect(stateChanges.invokes[2].dApp).to.eql(address(this.accounts.lpStable, chainId)); + expect(stateChanges.invokes[2].call.function).to.eql('getOneTknV2READONLY'); + expect(stateChanges.invokes[2].call.args).to.eql([{ type: 'String', value: this.usdtAssetId, }, { From a467b2c8676c8f1cf2f403ce3cd646f2d1fd2036 Mon Sep 17 00:00:00 2001 From: Epic waves rider <108883923+epicwavesrider@users.noreply.github.com> Date: Fri, 2 Dec 2022 19:30:20 +0400 Subject: [PATCH 2/3] fix: remainings in ido asset claim --- ride/ido.ride | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/ride/ido.ride b/ride/ido.ride index fedbf5b28..ab5b3de8a 100644 --- a/ride/ido.ride +++ b/ride/ido.ride @@ -608,8 +608,8 @@ func claim(claimedAssetId58: String, userAddress58: String) = { } else { let lastClaimEntries = if availablePriceAmountToClaim == periodMinAvailableToClaim then { [ - StringEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount.toString()), - StringEntry(keyTotalRemainingAmount(), newTotalRemainingAmount.toString()), + IntegerEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount), + IntegerEntry(keyTotalRemainingAmount(), newTotalRemainingAmount), TotalsEntry(keyInvestor(userAddress58), newInvestArray, totalsDiff, newClaimPeriodHeight, 0), TotalsEntry(keyTotals(), newTotalArray, totalsDiff, newClaimPeriodHeight, 0) ] @@ -656,15 +656,28 @@ func claim(claimedAssetId58: String, userAddress58: String) = { scale8, pow(10, 0, priceAssetDecimals, 0, 0, DOWN) ) + let claimStart = cfgArray[IdxCfgClaimStart].parseIntValue() + let claimDuration = cfgArray[IdxCfgClaimDuration].parseIntValue() + let claimEnd = claimStart + claimDuration + + let entries = if height > claimEnd then { + [ + IntegerEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount), + IntegerEntry(keyTotalRemainingAmount(), newTotalRemainingAmount), + TotalsEntry(keyInvestor(userAddress58), newInvestArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), + TotalsEntry(keyTotals(), newTotalArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance) + ] + } else { + [ + TotalsEntry(keyInvestor(userAddress58), origInvestArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), + TotalsEntry(keyTotals(), readTotalsArrayOrDefault(), totalsDiff, newClaimPeriodHeight, priceAssetBalance) + ] + } + ( [ - StringEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount.toString()), - StringEntry(keyTotalRemainingAmount(), newTotalRemainingAmount.toString()), ScriptTransfer(i.caller, outAmount + priceAssetBalanceIdoDecimals, outAssetId), IntegerEntry(keyPriceAssetBalance(userAddress58), 0), - TotalsEntry(keyInvestor(userAddress58), newInvestArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), - TotalsEntry(keyTotals(), newTotalArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance), - # TODO consider to add more data into history: operationPrice, realPrice ClaimOperationHistoryEntry( userAddress58, @@ -672,7 +685,7 @@ func claim(claimedAssetId58: String, userAddress58: String) = { claimedIdoAssetAmountFromDiff + priceAssetBalanceIdoDecimals, i.transactionId ) - ], + ] ++ entries, unit ) } @@ -775,8 +788,8 @@ func cleanRemaingAmount(userAddress58: String) = { let newTotalRemainingAmount = keyTotalRemainingAmount().getInteger().valueOrElse(0) + investorRemainingAmount [ - StringEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount.toString()), - StringEntry(keyTotalRemainingAmount(), newTotalRemainingAmount.toString()), + IntegerEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount), + IntegerEntry(keyTotalRemainingAmount(), newTotalRemainingAmount), StringEntry(keyInvestor(userAddress58), newInvestor), StringEntry(keyTotals(), newTotals) ] From c7a5ce6d3cb37f61327ac4d7e5097082a5a609b6 Mon Sep 17 00:00:00 2001 From: Epic waves rider <108883923+epicwavesrider@users.noreply.github.com> Date: Fri, 2 Dec 2022 19:55:29 +0400 Subject: [PATCH 3/3] fix: remainings in ido asset claim --- ride/ido.ride | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ride/ido.ride b/ride/ido.ride index ab5b3de8a..357a071f2 100644 --- a/ride/ido.ride +++ b/ride/ido.ride @@ -572,8 +572,8 @@ func claim(claimedAssetId58: String, userAddress58: String) = { let totals = readTotalsArrayOrDefault() let totalsTotalAmount = totals[IdxInvTotalAmount].parseIntValue() let totalsRemainingAmount = totals[IdxInvRemainingAmount].parseIntValue() - let totalsClaimedPriceAssetAmount = totals[IdxInvRemainingAmount].parseIntValue() - let totalsClaimedIdoAssetAmount = totals[IdxInvClaimedPriceAssetAmount].parseIntValue() + let totalsClaimedPriceAssetAmount = totals[IdxInvClaimedPriceAssetAmount].parseIntValue() + let totalsClaimedIdoAssetAmount = totals[IdxInvClaimedIdoAssetAmount].parseIntValue() let totalsLastClaimedHeight = totals[IdxInvLastClaimedHeight].parseIntValue() let newTotalsRemainingAmount = totalsRemainingAmount - investorRemainingAmount