From 05fadf87ac2ba4301c63dea37fc0cdf1e4137857 Mon Sep 17 00:00:00 2001 From: bra1nsurfer Date: Mon, 31 Jul 2023 16:40:28 +0300 Subject: [PATCH 1/5] Withdraw no less fix --- ride/l2mp_staking.ride | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ride/l2mp_staking.ride b/ride/l2mp_staking.ride index d41b8f645..5c302da78 100644 --- a/ride/l2mp_staking.ride +++ b/ride/l2mp_staking.ride @@ -308,7 +308,7 @@ func withdraw(withdrawAssetAmount: Int) = { withdrawAssetAmount >= minWithdrawAssetAmount || ("withdraw amount is too small. Min: (" + minWithdrawAssetAmount.toString() + ")").throwErr() ] - getWithdrawActions(i, lpAmountToWithdraw) + getWithdrawActions(i, min([userLpAmount, lpAmountToWithdraw + 1])) } # Return tuple: From bc2cb6b9d55aee8ae28d8e3bb68cd9a73c4d245f Mon Sep 17 00:00:00 2001 From: bra1nsurfer Date: Mon, 31 Jul 2023 17:54:29 +0300 Subject: [PATCH 2/5] Fix history string prefix --- ride/l2mp_staking.ride | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ride/l2mp_staking.ride b/ride/l2mp_staking.ride index 5c302da78..e120b6de9 100644 --- a/ride/l2mp_staking.ride +++ b/ride/l2mp_staking.ride @@ -39,7 +39,7 @@ func formatHistory( totalAssetAmount: Int, totalLpAmount: Int ) = [ - "%d%d", + "%d%d%d%d", totalProfit.toString(), price.toString(), totalAssetAmount.toString(), From a130afca3b91034b710810e533ee3fe6e4761ed6 Mon Sep 17 00:00:00 2001 From: bra1nsurfer Date: Mon, 31 Jul 2023 17:54:41 +0300 Subject: [PATCH 3/5] Update tests --- .../l2mp_staking/getAssetsInfo.spec.mjs | 4 ++-- test/components/l2mp_staking/stake.spec.mjs | 22 ++++++++++++------- .../components/l2mp_staking/stakeFor.spec.mjs | 22 ++++++++++++------- .../components/l2mp_staking/withdraw.spec.mjs | 11 ++++++++-- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/test/components/l2mp_staking/getAssetsInfo.spec.mjs b/test/components/l2mp_staking/getAssetsInfo.spec.mjs index b3e576ac8..062b79a93 100644 --- a/test/components/l2mp_staking/getAssetsInfo.spec.mjs +++ b/test/components/l2mp_staking/getAssetsInfo.spec.mjs @@ -58,7 +58,7 @@ describe('l2mp_staking: get staked info', /** @this {MochaSuiteModified} */() => value: { _1: { type: 'Int', value: stakeAmount1 }, _2: { type: 'Int', value: stakeAmount1 }, - _3: { type: 'Int', value: 100000000 }, + _3: { type: 'BigInt', value: `${10e17}` }, _4: { type: 'Int', value: stakeAmount1 }, _5: { type: 'Int', value: 0 }, }, @@ -81,7 +81,7 @@ describe('l2mp_staking: get staked info', /** @this {MochaSuiteModified} */() => value: { _1: { type: 'Int', value: stakeAmount1 + stakeAmount2 }, _2: { type: 'Int', value: stakeAmount1 + stakeAmount2 }, - _3: { type: 'Int', value: 100000000 }, + _3: { type: 'BigInt', value: `${10e17}` }, }, }); }, diff --git a/test/components/l2mp_staking/stake.spec.mjs b/test/components/l2mp_staking/stake.spec.mjs index 7d9445a19..88c715efc 100644 --- a/test/components/l2mp_staking/stake.spec.mjs +++ b/test/components/l2mp_staking/stake.spec.mjs @@ -12,9 +12,10 @@ describe('l2mp_staking: staking', /** @this {MochaSuiteModified} */() => { it( 'should be able to stake token', async function () { - const stakeAmount1 = 10e8; + const stakeAmount = 10e8; const price = 1; - const expectedLpAmount1 = stakeAmount1 * price; + const expectedPrice = price * 10e17; + const expectedLpAmount = stakeAmount * price; const stakeTx = invokeScript({ dApp: this.accounts.l2mpStaking.addr, @@ -22,34 +23,39 @@ describe('l2mp_staking: staking', /** @this {MochaSuiteModified} */() => { function: 'stake', }, payment: [ - { assetId: this.l2mpAssetId, amount: stakeAmount1 }, + { assetId: this.l2mpAssetId, amount: stakeAmount }, ], additionalFee: 4e5, chainId, }, this.accounts.user1.seed); - const { stateChanges, height } = await broadcastAndWait(stakeTx); + const { stateChanges, height, id } = await broadcastAndWait(stakeTx); expect(stateChanges.data).to.be.deep.equal([ + { + key: `%s%s%s__stake__${this.accounts.user1.addr}__${id}`, + type: 'string', + value: `%d%d%d%d__0__${expectedPrice}__0__0`, + }, { key: '%s__totalLpAmount', type: 'integer', - value: expectedLpAmount1, + value: expectedLpAmount, }, { key: '%s__totalAssetAmount', type: 'integer', - value: stakeAmount1, + value: stakeAmount, }, { key: `%s%s__userLpAmount__${this.accounts.user1.addr}`, type: 'integer', - value: expectedLpAmount1, + value: expectedLpAmount, }, { key: `%s%s__totalAssetStaked__${this.accounts.user1.addr}`, type: 'integer', - value: stakeAmount1, + value: stakeAmount, }, { key: '%s__startBlock', diff --git a/test/components/l2mp_staking/stakeFor.spec.mjs b/test/components/l2mp_staking/stakeFor.spec.mjs index da83e0c84..eaecda516 100644 --- a/test/components/l2mp_staking/stakeFor.spec.mjs +++ b/test/components/l2mp_staking/stakeFor.spec.mjs @@ -12,9 +12,10 @@ describe('l2mp_staking: staking for another user', /** @this {MochaSuiteModified it( 'should be able to stake token for another user', async function () { - const stakeAmount1 = 10e8; + const stakeAmount = 10e8; const price = 1; - const expectedLpAmount1 = stakeAmount1 * price; + const expectedPrice = price * 10e17; + const expectedLpAmount = stakeAmount * price; const stakeTx = invokeScript({ dApp: this.accounts.l2mpStaking.addr, @@ -26,34 +27,39 @@ describe('l2mp_staking: staking for another user', /** @this {MochaSuiteModified }], }, payment: [ - { assetId: this.l2mpAssetId, amount: stakeAmount1 }, + { assetId: this.l2mpAssetId, amount: stakeAmount }, ], additionalFee: 4e5, chainId, }, this.accounts.user1.seed); - const { stateChanges, height } = await broadcastAndWait(stakeTx); + const { stateChanges, height, id } = await broadcastAndWait(stakeTx); expect(stateChanges.data).to.be.deep.equal([ + { + key: `%s%s%s__stake__${this.accounts.user2.addr}__${id}`, + type: 'string', + value: `%d%d%d%d__0__${expectedPrice}__0__0`, + }, { key: '%s__totalLpAmount', type: 'integer', - value: expectedLpAmount1, + value: expectedLpAmount, }, { key: '%s__totalAssetAmount', type: 'integer', - value: stakeAmount1, + value: stakeAmount, }, { key: `%s%s__userLpAmount__${this.accounts.user2.addr}`, type: 'integer', - value: expectedLpAmount1, + value: expectedLpAmount, }, { key: `%s%s__totalAssetStaked__${this.accounts.user2.addr}`, type: 'integer', - value: stakeAmount1, + value: stakeAmount, }, { key: '%s__startBlock', diff --git a/test/components/l2mp_staking/withdraw.spec.mjs b/test/components/l2mp_staking/withdraw.spec.mjs index cb896b0ae..3a6047e2c 100644 --- a/test/components/l2mp_staking/withdraw.spec.mjs +++ b/test/components/l2mp_staking/withdraw.spec.mjs @@ -14,8 +14,10 @@ describe('l2mp_staking: withdraw tokens', /** @this {MochaSuiteModified} */() => const stakeAmount = 10e8; const expectedLpAmount = 10e8; const blocksCount = 2; + const totalProfit = emissionPerBlock * blocksCount; // TODO: sometimes contract returns 1004999999 instead of 1005000000 - const expectedWithdrawAmount = stakeAmount + ((emissionPerBlock / 2) * blocksCount); + const expectedWithdrawAmount = stakeAmount + (totalProfit / 2); + const expectedPrice = expectedWithdrawAmount * 10e8; before( async function () { @@ -87,7 +89,7 @@ describe('l2mp_staking: withdraw tokens', /** @this {MochaSuiteModified} */() => chainId, }, this.accounts.user1.seed); - const { stateChanges } = await broadcastAndWait(withdrawTx); + const { stateChanges, id } = await broadcastAndWait(withdrawTx); expect(stateChanges.transfers).to.be.deep.equal([ { @@ -98,6 +100,11 @@ describe('l2mp_staking: withdraw tokens', /** @this {MochaSuiteModified} */() => ]); expect(stateChanges.data).to.be.deep.equal([ + { + key: `%s%s%s__withdraw__${this.accounts.user1.addr}__${id}`, + type: 'string', + value: `%d%d%d%d__${totalProfit}__${expectedPrice}__${stakeAmount * 2}__${expectedLpAmount * 2}`, + }, { key: '%s__totalLpAmount', type: 'integer', From 95a12892486d08f783f6ede2c06dbe5e00ba2fb4 Mon Sep 17 00:00:00 2001 From: bra1nsurfer Date: Tue, 1 Aug 2023 09:26:06 +0300 Subject: [PATCH 4/5] Bump ride-js version --- test/package-lock.json | 14 +++++++------- test/package.json | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/test/package-lock.json b/test/package-lock.json index c39c87f70..f78cc6826 100644 --- a/test/package-lock.json +++ b/test/package-lock.json @@ -14,7 +14,7 @@ "@types/mocha": "^9.1.0", "@waves/bignumber": "^1.1.1", "@waves/node-api-js": "^1.3.0", - "@waves/ride-js": "^2.2.0", + "@waves/ride-js": "^2.2.7", "@waves/ts-lib-crypto": "^1.4.4-beta.1", "@waves/ts-types": "^1.1.0", "@waves/waves-transactions": "^4.2.5-beta.3", @@ -461,9 +461,9 @@ } }, "node_modules/@waves/ride-js": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@waves/ride-js/-/ride-js-2.2.0.tgz", - "integrity": "sha512-a46cZYAE9Cp2DjXPcqrx4CQRtTGLIr+gIvPomRCkDqZpgtd2DvSh6alLJryYMSeMHlgugmZv/qos5Z7OMAs4eQ==", + "version": "2.2.7", + "resolved": "https://registry.npmjs.org/@waves/ride-js/-/ride-js-2.2.7.tgz", + "integrity": "sha512-mL0BgWVMFnqvoL5d9XYbKJfAE6FXs3FXgrsQpRJj5RiOxtIplFWM2Q+EbiIvSmAZ6EUhP/5ufaIbbZeNkMMOIA==", "dependencies": { "@jest/globals": "^27.5.1", "@waves/ts-lib-crypto": "^1.4.3", @@ -4235,9 +4235,9 @@ } }, "@waves/ride-js": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/@waves/ride-js/-/ride-js-2.2.0.tgz", - "integrity": "sha512-a46cZYAE9Cp2DjXPcqrx4CQRtTGLIr+gIvPomRCkDqZpgtd2DvSh6alLJryYMSeMHlgugmZv/qos5Z7OMAs4eQ==", + "version": "2.2.7", + "resolved": "https://registry.npmjs.org/@waves/ride-js/-/ride-js-2.2.7.tgz", + "integrity": "sha512-mL0BgWVMFnqvoL5d9XYbKJfAE6FXs3FXgrsQpRJj5RiOxtIplFWM2Q+EbiIvSmAZ6EUhP/5ufaIbbZeNkMMOIA==", "requires": { "@jest/globals": "^27.5.1", "@waves/ts-lib-crypto": "^1.4.3", diff --git a/test/package.json b/test/package.json index f29616d53..1ade5db19 100644 --- a/test/package.json +++ b/test/package.json @@ -44,7 +44,7 @@ "@types/mocha": "^9.1.0", "@waves/bignumber": "^1.1.1", "@waves/node-api-js": "^1.3.0", - "@waves/ride-js": "^2.2.0", + "@waves/ride-js": "^2.2.7", "@waves/ts-lib-crypto": "^1.4.4-beta.1", "@waves/ts-types": "^1.1.0", "@waves/waves-transactions": "^4.2.5-beta.3", From c2ce0eb445b52db6594ea9e604eb574c763c57f0 Mon Sep 17 00:00:00 2001 From: bra1nsurfer Date: Tue, 1 Aug 2023 09:54:02 +0300 Subject: [PATCH 5/5] Fixed tests --- test/components/l2mp_staking/withdraw.spec.mjs | 9 +++------ test/utils/api.mjs | 2 ++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/test/components/l2mp_staking/withdraw.spec.mjs b/test/components/l2mp_staking/withdraw.spec.mjs index 3a6047e2c..30232b12a 100644 --- a/test/components/l2mp_staking/withdraw.spec.mjs +++ b/test/components/l2mp_staking/withdraw.spec.mjs @@ -2,7 +2,7 @@ import chai from 'chai'; import chaiAsPromised from 'chai-as-promised'; import { invokeScript } from '@waves/waves-transactions'; import { - chainId, waitForHeight, broadcastAndWait, + chainId, waitForHeight, broadcastAndWait, broadcast, } from '../../utils/api.mjs'; chai.use(chaiAsPromised); @@ -69,11 +69,8 @@ describe('l2mp_staking: withdraw tokens', /** @this {MochaSuiteModified} */() => chainId, }, this.accounts.user1.seed); - const [{ height: startHeight }] = await Promise.all([ - broadcastAndWait(stakeTx), - broadcastAndWait(stakeForTx), - ]); - + const { height: startHeight } = await broadcastAndWait(stakeTx); + await broadcast(stakeForTx); await waitForHeight(startHeight + blocksCount); const withdrawTx = invokeScript({ diff --git a/test/utils/api.mjs b/test/utils/api.mjs index 6610f0550..ec66c67b5 100644 --- a/test/utils/api.mjs +++ b/test/utils/api.mjs @@ -14,6 +14,8 @@ export const largeNumbeConvertHeader = { export const separator = '__'; +export const broadcast = async (tx) => api.transactions.broadcast(tx, {}); + export const broadcastAndWait = async (tx) => { await api.transactions.broadcast(tx, {}); await nodeInteraction.waitForTx(tx.id, { apiBase });