Skip to content

Commit

Permalink
tests update
Browse files Browse the repository at this point in the history
  • Loading branch information
ridev6 committed Aug 22, 2023
1 parent f62f340 commit e43f0c9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 33 deletions.
5 changes: 4 additions & 1 deletion test/components/boosting/_hooks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ export const mochaHooks = {

this.epochLength = 7;

boosting.seed = this.accounts.boosting.seed;
boosting.maxLockDuration = this.maxLockDuration;
boosting.blocksInPeriod = this.blocksInPeriod;

await Promise.all([
managerVault.init({
caller: this.accounts.managerVault.seed,
Expand All @@ -132,7 +136,6 @@ export const mochaHooks = {
}),

boosting.init({
caller: this.accounts.boosting.seed,
factoryAddress: this.accounts.factory.addr,
referralsAddress: this.accounts.referral.addr,
votingEmissionAddress: this.accounts.votingEmission.addr,
Expand Down
74 changes: 57 additions & 17 deletions test/components/boosting/contract/boosting.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { data, invokeScript } from '@waves/waves-transactions';
import wc from '@waves/ts-lib-crypto';
import { broadcastAndWait, chainId, separator } from '../../../utils/api.mjs';

export const keyLock = (userAddress, txId) => ['%s%s%s', 'lock', userAddress, txId].join(separator);
Expand Down Expand Up @@ -26,9 +27,46 @@ export const parseLockParams = (s) => {
};
};

export const boosting = {
init: async ({
caller,
const decayConstant = 8;
class Boosting {
maxLockDuration = 0;

blocksInPeriod = 0;

seed = '';

address() {
return wc.address(this.seed, chainId);
}

calcGwxAmountStart({ wxAmount, duration }) {
return Math.floor((wxAmount * duration) / this.maxLockDuration);
}

calcWxWithdrawable({
lockWxAmount, lockDuration, passedPeriods,
}) {
const exponent = (passedPeriods * decayConstant * this.blocksInPeriod) / lockDuration;
// TODO: if height > lockEnd then userAmount
const wxWithdrawable = Math.floor(lockWxAmount * (1 - 0.5 ** exponent));

return wxWithdrawable;
}

calcGwxAmountBurned({
gwxAmountStart, gwxAmountPrev, passedPeriods,
}) {
const gwxBurned = Math.min(
Math.floor(
(passedPeriods * this.blocksInPeriod * gwxAmountStart) / this.maxLockDuration,
),
gwxAmountPrev,
);

return gwxBurned;
}

async init({
factoryAddress,
referralsAddress,
votingEmissionAddress,
Expand All @@ -42,7 +80,7 @@ export const boosting = {
nextUserNum = 0,
blocksInPeriod,
lockStepBlocks,
}) => {
}) {
const dataTx = data({
data: [
{ key: '%s%s__config__factoryAddress', type: 'string', value: factoryAddress },
Expand All @@ -59,17 +97,17 @@ export const boosting = {
],
additionalFee: 4e5,
chainId,
}, caller);
}, this.seed);

return broadcastAndWait(dataTx);
},
}

lock: async ({
dApp, caller, duration, payments = [],
}) => {
async lock({
caller, duration, payments = [],
}) {
const invokeTx = invokeScript(
{
dApp,
dApp: this.address(),
call: {
function: 'lock',
args: [
Expand All @@ -83,14 +121,14 @@ export const boosting = {
caller,
);
return broadcastAndWait(invokeTx);
},
}

unlock: async ({
dApp, caller, txId,
}) => {
async unlock({
caller, txId,
}) {
const invokeTx = invokeScript(
{
dApp,
dApp: this.address(),
call: {
function: 'unlock',
args: [
Expand All @@ -104,5 +142,7 @@ export const boosting = {
caller,
);
return broadcastAndWait(invokeTx);
},
};
}
}

export const boosting = new Boosting();
6 changes: 4 additions & 2 deletions test/components/boosting/lock.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ describe('boosting: lock.mjs', /** @this {MochaSuiteModified} */() => {
}, this.accounts.emission.seed));

const { stateChanges, id: lockTxId } = await boosting.lock({
dApp: this.accounts.boosting.addr,
caller: this.accounts.user0.seed,
duration: lockDuration,
payments: [
Expand All @@ -42,7 +41,10 @@ describe('boosting: lock.mjs', /** @this {MochaSuiteModified} */() => {
boostingDataChanges[lockKey],
);

const expectedGwxAmount = Math.floor((lockWxAmount * lockDuration) / this.maxLockDuration);
const expectedGwxAmount = boosting.calcGwxAmountStart({
wxAmount: lockWxAmount,
duration: lockDuration,
});
expect(lockParams.wxAmount).to.equal(lockWxAmount);
expect(lockParams.gwxAmount).to.equal(expectedGwxAmount);
expect(
Expand Down
26 changes: 13 additions & 13 deletions test/components/boosting/unlock.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe('boosting: unlock.mjs', /** @this {MochaSuiteModified} */() => {

let stateChanges;
({ id: lockTxId, height: lockHeight, stateChanges } = await boosting.lock({
dApp: this.accounts.boosting.addr,
caller: this.accounts.user0.seed,
duration: lockDuration,
payments: [
Expand All @@ -57,7 +56,6 @@ describe('boosting: unlock.mjs', /** @this {MochaSuiteModified} */() => {
heightDiff += 1;
}
const { stateChanges, height: unlockHeight } = await boosting.unlock({
dApp: this.accounts.boosting.addr,
caller: this.accounts.user0.seed,
txId: lockTxId,
});
Expand All @@ -70,18 +68,20 @@ describe('boosting: unlock.mjs', /** @this {MochaSuiteModified} */() => {
boostingDataChanges[lockKey],
);

const t = Math.floor((unlockHeight - lockHeight) / this.blocksInPeriod);
const exponent = (t * 8 * this.blocksInPeriod) / lockDuration;
// if height > lockEnd then userAmount
const wxWithdrawable = Math.floor(lockWxAmount * (1 - 0.5 ** exponent));
const gwxAmountStart = Math.floor((lockWxAmount * lockDuration) / this.maxLockDuration);
const passedPeriods = Math.floor((unlockHeight - lockHeight) / this.blocksInPeriod);
const wxWithdrawable = boosting.calcWxWithdrawable({
lockWxAmount,
lockDuration,
passedPeriods,
});
const gwxAmountStart = boosting.calcGwxAmountStart({
wxAmount: lockWxAmount,
duration: lockDuration,
});
const gwxAmountPrev = lockParamsPrev.gwxAmount;
const gwxBurned = Math.min(
Math.floor(
(t * this.blocksInPeriod * gwxAmountStart) / this.maxLockDuration,
),
gwxAmountPrev,
);
const gwxBurned = boosting.calcGwxAmountBurned({
gwxAmountStart, gwxAmountPrev, passedPeriods,
});

expect(lockParams.wxClaimed).to.equal(wxWithdrawable, 'wxClaimed');
expect(lockParams.gwxAmount).to.equal(gwxAmountPrev - gwxBurned, 'gwxAmount');
Expand Down

0 comments on commit e43f0c9

Please sign in to comment.