Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
197 changes: 134 additions & 63 deletions ride/ido.ride
Original file line number Diff line number Diff line change
Expand Up @@ -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[IdxInvClaimedPriceAssetAmount].parseIntValue()
let totalsClaimedIdoAssetAmount = totals[IdxInvClaimedIdoAssetAmount].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(),
Expand All @@ -567,49 +606,10 @@ 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()),
IntegerEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount),
IntegerEntry(keyTotalRemainingAmount(), newTotalRemainingAmount),
TotalsEntry(keyInvestor(userAddress58), newInvestArray, totalsDiff, newClaimPeriodHeight, 0),
TotalsEntry(keyTotals(), newTotalArray, totalsDiff, newClaimPeriodHeight, 0)
]
Expand Down Expand Up @@ -656,21 +656,36 @@ 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)
]
}

(
[
ScriptTransfer(i.caller, outAmount + priceAssetBalanceIdoDecimals, outAssetId),
IntegerEntry(keyPriceAssetBalance(userAddress58), 0),
TotalsEntry(keyInvestor(userAddress58), origInvestArray, totalsDiff, newClaimPeriodHeight, priceAssetBalance),
TotalsEntry(keyTotals(), readTotalsArrayOrDefault(), totalsDiff, newClaimPeriodHeight, priceAssetBalance),

# TODO consider to add more data into history: operationPrice, realPrice
ClaimOperationHistoryEntry(
userAddress58,
claimedPriceAmountFromDiff,
claimedIdoAssetAmountFromDiff + priceAssetBalanceIdoDecimals,
i.transactionId
)
],
] ++ entries,
unit
)
}
Expand Down Expand Up @@ -724,6 +739,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

[
IntegerEntry(keyInvestorRemainingAmount(userAddress58), investorRemainingAmount),
IntegerEntry(keyTotalRemainingAmount(), newTotalRemainingAmount),
StringEntry(keyInvestor(userAddress58), newInvestor),
StringEntry(keyTotals(), newTotals)
]
}

@Callable(i)
func claimREADONLY(claimedAssetId58: String, userAddress58: String) = {
let claimResultTuple = internalClaim(claimedAssetId58, userAddress58.addressFromStringValue(), "".fromBase58String())
Expand Down
18 changes: 9 additions & 9 deletions test/components/ido/claim.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('ido: claim.mjs', /** @this {MochaSuiteModified} */() => {

expect(
await checkStateChanges(
stateChanges.invokes[0].stateChanges,
stateChanges.invokes[1].stateChanges,
0,
0,
0,
Expand All @@ -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,
Expand All @@ -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,
}, {
Expand Down