diff --git a/ride/l2mp_staking.ride b/ride/l2mp_staking.ride index d41b8f645..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(), @@ -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: 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..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); @@ -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 () { @@ -67,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({ @@ -87,7 +86,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 +97,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', 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 7cae94801..6e90adc7d 100644 --- a/test/package.json +++ b/test/package.json @@ -45,7 +45,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", 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 });