Skip to content

Commit

Permalink
Merge pull request #394 from waves-exchange/l2mp-available-fix
Browse files Browse the repository at this point in the history
Withdraw no less
  • Loading branch information
JunkiJay authored Aug 1, 2023
2 parents 57801ff + c2ce0eb commit 19aff56
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 36 deletions.
4 changes: 2 additions & 2 deletions ride/l2mp_staking.ride
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func formatHistory(
totalAssetAmount: Int,
totalLpAmount: Int
) = [
"%d%d",
"%d%d%d%d",
totalProfit.toString(),
price.toString(),
totalAssetAmount.toString(),
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions test/components/l2mp_staking/getAssetsInfo.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
},
Expand All @@ -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}` },
},
});
},
Expand Down
22 changes: 14 additions & 8 deletions test/components/l2mp_staking/stake.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,50 @@ 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,
call: {
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',
Expand Down
22 changes: 14 additions & 8 deletions test/components/l2mp_staking/stakeFor.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
20 changes: 12 additions & 8 deletions test/components/l2mp_staking/withdraw.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 () {
Expand Down Expand Up @@ -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({
Expand All @@ -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([
{
Expand All @@ -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',
Expand Down
14 changes: 7 additions & 7 deletions test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions test/utils/api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 19aff56

Please sign in to comment.