Skip to content

Commit

Permalink
feat: add testcase Should withdraw all before withdraw and harvest so…
Browse files Browse the repository at this point in the history
… that the staked amount is zero
  • Loading branch information
ipromise2324 committed Apr 26, 2024
1 parent 2e9bf06 commit dc4b0e2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
4 changes: 2 additions & 2 deletions JettonMasterChefCosts.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Jetton MasterChef Costs in each operation:
Deposit Cost: 0.0337868 TON
Deposit Cost: 0.0337932 TON
Harvest Cost: 0.0155238059 TON
Withdraw Cost: 0.0159109001 TON
Withdraw & Harvest Cost: 0.0259639059 TON
Withdraw & Harvest Cost: 0.0259807059 TON
4 changes: 2 additions & 2 deletions TONMasterChefCosts.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
TON MasterChef Costs in each operation:
Deposit Cost: 0.0331955 TON
Deposit Cost: 0.0332019 TON
Harvest Cost: 0.0052698059 TON
Withdraw Cost: 0.0157693006 TON
Withdraw & Harvest Cost: 0.0171868059 TON
Withdraw & Harvest Cost: 0.0172036059 TON
1 change: 1 addition & 0 deletions contracts/mini_chef.tact
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ contract MiniChef with Deployable {
receive(msg: WithdrawAndHarvestInternal) {
require(sender() == self.masterChef, "only masterChef can harvest");
let userInfo: UserInfo = self.userInfo.get(msg.lpTokenAddress)!!;
require(userInfo.amount >= msg.withdrawAmount, "insufficient balance");
let accumulatedReward: Int = userInfo.amount * msg.accRewardPerShare / ACC_PRECISION;
let _pendingReward: Int = accumulatedReward - userInfo.rewardDebt;
if (_pendingReward <= 0) {
Expand Down
40 changes: 40 additions & 0 deletions tests/JettonMasterChef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1833,10 +1833,50 @@ describe('Jetton MasterChef Tests', () => {
},
);

// Users have no reward to harvest but they still can get withdraw amount
const userUSDTBalanceAfterWH = (await userJettonWallet.getGetWalletData()).balance;
expect(userUSDTBalanceAfterWH - userUSDTBalanceAfter).toEqual(userWithdrawAmount);
});

it('Should withdraw all before withdraw and harvest so that the staked amount is zero', async () => {
const userDepositAmount = 1n * TOKEN_DECIMALS;
const userWithdrawAmount = userDepositAmount;
const periodTime = 1000;
const userJettonWallet = blockchain.openContract(await JettonWalletUSDT.fromInit(user.address, usdt.address));
// deposit first
await deposit(masterChef, user, masterChefJettonWallet, usdt, userDepositAmount);

// get the balance of usdt before withdraw
const userUSDTBalanceBeforeWH = (await userJettonWallet.getGetWalletData()).balance;

// Update time to periodTime, so that we can withdraw
blockchain.now!! += periodTime;
const userUSDTBalanceBefore = (await userJettonWallet.getGetWalletData()).balance;

const withdrawResult = await withdraw(masterChef, user, masterChefJettonWallet, userWithdrawAmount);

// User JettonWallet should have received the reward
const userUSDTBalanceAfter = (await userJettonWallet.getGetWalletData()).balance;
expect(userUSDTBalanceAfter).toEqual(userUSDTBalanceBefore + userWithdrawAmount);

const result = await masterChef.send(
user.getSender(),
{ value: toNano('2') },
{
$$type: 'WithdrawAndHarvest',
queryId: 0n,
lpTokenAddress: masterChefJettonWallet.address,
withdrawAmount: 0n,
beneficiary: user.address,
},
);

// User can't get withdraw amount but he can get reward
const benefit = (userDepositAmount * BigInt(periodTime) * rewardPerSecond) / TOKEN_DECIMALS;
const userUSDTBalanceAfterWH = (await userJettonWallet.getGetWalletData()).balance;
expect(userUSDTBalanceAfterWH).toEqual(userUSDTBalanceAfter + benefit)
});

it('Should harvest -> withdraw and harvest -> harvest -> withdraw', async () => {
const userDepositAmount = 1n * TOKEN_DECIMALS;
const userWithdrawAmount = 5n * 10n ** 5n;
Expand Down

0 comments on commit dc4b0e2

Please sign in to comment.