From 463755ca6fbca7b8aa59352ef636eca4abdf315f Mon Sep 17 00:00:00 2001 From: SocioDroid Date: Mon, 9 Jan 2023 19:35:01 +0530 Subject: [PATCH 1/3] Adding get set test to MAX DEPOSIT Entrypoint --- test/Stake/deposit.ts | 77 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/test/Stake/deposit.ts b/test/Stake/deposit.ts index 40a6a2e9f..c99f9b1cd 100644 --- a/test/Stake/deposit.ts +++ b/test/Stake/deposit.ts @@ -9,6 +9,7 @@ import { } from "../../typechain"; import { StakeConfigStruct } from "../../typechain/contracts/stake/Stake"; import { + Debug, getBlockTimestamp, memoryOperand, MemoryType, @@ -821,4 +822,80 @@ describe("Stake deposit", async function () { "alice has not received correct token units after withdrawing" ); }); + + it.only("should set and get a value in MAX_DEPOSIT_ENTRYPOINT source", async function () { + + // This test changes the maxDeposit once 2 successful deposits are completed + const signers = await ethers.getSigners(); + const deployer = signers[0]; + const alice = signers[2]; + + const FIVE = ethers.BigNumber.from("5" + eighteenZeros); + const TEN = ethers.BigNumber.from("10" + eighteenZeros); + const depositCount = 2; + + const constants = [FIVE, TEN, max_uint256, depositCount, 1]; + + const max_deposit_initial = op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 0)); + + const max_deposit_later = op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 1)); + + const aliceAddress = op(Opcode.CONTEXT, 0x0100); + + const max_withdraw = op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 2)); + + // prettier-ignore + const depositSource = concat([ + + + aliceAddress, + op(Opcode.GET), // Deposit count set for alice + op(Opcode.DEBUG, Debug.StatePacked), + op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 3)), // Max Deposit Count + op(Opcode.GREATER_THAN), // Condition + max_deposit_later, // true + max_deposit_initial, // false + op(Opcode.EAGER_IF), + + // Setting value + aliceAddress, // key + aliceAddress, + op(Opcode.GET), // Deposit count set for alice + op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 4)), + op(Opcode.ADD, 2), // Value + op(Opcode.SET), + ]); + + const withdrawSource = max_withdraw; + + const source = [depositSource, withdrawSource]; // max_deposit set to 10 + + const stakeConfigStruct: StakeConfigStruct = { + name: "Stake Token", + symbol: "STKN", + asset: token.address, + expressionDeployer: expressionDeployer.address, + interpreter: interpreter.address, + stateConfig: { + sources: source, + constants: constants, + }, + }; + + const stake = await stakeDeploy(deployer, stakeFactory, stakeConfigStruct); + + const depositsAlice0_ = await getDeposits(stake, alice.address); + assert(depositsAlice0_.length === 0); + + let maxDeposit = await stake.maxDeposit(alice.address); + maxDeposit = await stake.maxDeposit(alice.address); + maxDeposit = await stake.maxDeposit(alice.address); + maxDeposit = await stake.maxDeposit(alice.address); + maxDeposit = await stake.maxDeposit(alice.address); + maxDeposit = await stake.maxDeposit(alice.address); + + assert(maxDeposit.eq(FIVE), "maxDeposit is not equal to FIVE"); + }); + + }); From 0284fd0bd5386ced554c38659f912739458a7234 Mon Sep 17 00:00:00 2001 From: SocioDroid Date: Mon, 9 Jan 2023 19:42:57 +0530 Subject: [PATCH 2/3] Failing test --- test/Stake/deposit.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/Stake/deposit.ts b/test/Stake/deposit.ts index c99f9b1cd..0584d6b83 100644 --- a/test/Stake/deposit.ts +++ b/test/Stake/deposit.ts @@ -889,12 +889,12 @@ describe("Stake deposit", async function () { let maxDeposit = await stake.maxDeposit(alice.address); maxDeposit = await stake.maxDeposit(alice.address); + assert(maxDeposit.eq(FIVE), "maxDeposit is not equal to FIVE"); + + // Exceeds max deposit count i.e 2 maxDeposit = await stake.maxDeposit(alice.address); - maxDeposit = await stake.maxDeposit(alice.address); - maxDeposit = await stake.maxDeposit(alice.address); - maxDeposit = await stake.maxDeposit(alice.address); + assert(maxDeposit.eq(TEN), "maxDeposit is not equal to TEN"); - assert(maxDeposit.eq(FIVE), "maxDeposit is not equal to FIVE"); }); From a9e8aad635531723335b4d509beec3e546f08288 Mon Sep 17 00:00:00 2001 From: SocioDroid Date: Mon, 9 Jan 2023 22:47:37 +0530 Subject: [PATCH 3/3] tests for withdraw entrypoint --- test/Stake/deposit.ts | 92 ++++++++++++++++++++++++++--------- test/Stake/withdraw.ts | 107 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 23 deletions(-) diff --git a/test/Stake/deposit.ts b/test/Stake/deposit.ts index 0584d6b83..503e097c4 100644 --- a/test/Stake/deposit.ts +++ b/test/Stake/deposit.ts @@ -822,36 +822,44 @@ describe("Stake deposit", async function () { "alice has not received correct token units after withdrawing" ); }); - - it.only("should set and get a value in MAX_DEPOSIT_ENTRYPOINT source", async function () { + it("should set and get a value in MAX_DEPOSIT_ENTRYPOINT source", async function () { // This test changes the maxDeposit once 2 successful deposits are completed const signers = await ethers.getSigners(); const deployer = signers[0]; const alice = signers[2]; + const TWENTY_FIVE = ethers.BigNumber.from("25" + eighteenZeros); const FIVE = ethers.BigNumber.from("5" + eighteenZeros); const TEN = ethers.BigNumber.from("10" + eighteenZeros); const depositCount = 2; const constants = [FIVE, TEN, max_uint256, depositCount, 1]; - const max_deposit_initial = op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 0)); - - const max_deposit_later = op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 1)); - + const max_deposit_initial = op( + Opcode.READ_MEMORY, + memoryOperand(MemoryType.Constant, 0) + ); + + const max_deposit_later = op( + Opcode.READ_MEMORY, + memoryOperand(MemoryType.Constant, 1) + ); + const aliceAddress = op(Opcode.CONTEXT, 0x0100); - - const max_withdraw = op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 2)); + const max_withdraw = op( + Opcode.READ_MEMORY, + memoryOperand(MemoryType.Constant, 2) + ); + + // (UserDepositCount > DepositCount) ? FIVE : TEN // prettier-ignore const depositSource = concat([ - aliceAddress, op(Opcode.GET), // Deposit count set for alice - op(Opcode.DEBUG, Debug.StatePacked), - op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 3)), // Max Deposit Count + op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 3)), op(Opcode.GREATER_THAN), // Condition max_deposit_later, // true max_deposit_initial, // false @@ -882,20 +890,58 @@ describe("Stake deposit", async function () { }, }; - const stake = await stakeDeploy(deployer, stakeFactory, stakeConfigStruct); + const stake = await stakeDeploy(alice, stakeFactory, stakeConfigStruct); - const depositsAlice0_ = await getDeposits(stake, alice.address); - assert(depositsAlice0_.length === 0); + // Transfer tokens from the deployer to the alice with the instances + await token.connect(deployer).approve(deployer.address, TWENTY_FIVE); - let maxDeposit = await stake.maxDeposit(alice.address); - maxDeposit = await stake.maxDeposit(alice.address); - assert(maxDeposit.eq(FIVE), "maxDeposit is not equal to FIVE"); - - // Exceeds max deposit count i.e 2 - maxDeposit = await stake.maxDeposit(alice.address); - assert(maxDeposit.eq(TEN), "maxDeposit is not equal to TEN"); - - }); + //await token.connect(alice); + await token.transferFrom(deployer.address, alice.address, TWENTY_FIVE); + + await token + .connect(alice) + .approve(stake.address, ethers.constants.MaxUint256); + // Initial Deposit + let amountToDeposit = FIVE; + let depositedAmount = ethers.BigNumber.from("0"); + // alice depositing + for (let i = 0; i <= depositCount; i++) { + await stake.deposit(amountToDeposit, alice.address); + depositedAmount = depositedAmount.add(amountToDeposit); + + const tokenBalanceStake1 = await token.balanceOf(stake.address); + const stTokenBalanceAlice0 = await stake.balanceOf(alice.address); + + assert( + tokenBalanceStake1.eq(depositedAmount), + "stake contract token balance is not equal to deposited amount" + ); + assert( + stTokenBalanceAlice0.eq(depositedAmount), + "Alice has not received correct share units" + ); + } + + // Post 3 successful deposits, alice should be able to deposit TEN at max + // Initial Deposit + amountToDeposit = TEN; + + // alice depositing + await stake.deposit(amountToDeposit, alice.address); + depositedAmount = depositedAmount.add(amountToDeposit); + + const tokenBalanceStake1 = await token.balanceOf(stake.address); + const stTokenBalanceAlice0 = await stake.balanceOf(alice.address); + + assert( + tokenBalanceStake1.eq(depositedAmount), + "stake contract token balance is not equal to deposited amount" + ); + assert( + stTokenBalanceAlice0.eq(depositedAmount), + "Alice has not received correct share units" + ); + }); }); diff --git a/test/Stake/withdraw.ts b/test/Stake/withdraw.ts index aae2b8d53..7e66a1d2e 100644 --- a/test/Stake/withdraw.ts +++ b/test/Stake/withdraw.ts @@ -903,4 +903,111 @@ describe("Stake withdraw", async function () { assert(depositsAlice_[0].timestamp === time1_); assert(depositsAlice_[0].amount.eq(tokenBalanceAlice0.sub(ONE).sub(ONE))); }); + + + it("should set and get a value in MAX_WITHDRAW_ENTRYPOINT source", async function () { + // This test changes the maxDeposit once 2 successful deposits are completed + const signers = await ethers.getSigners(); + const deployer = signers[0]; + const alice = signers[2]; + + const TWENTY_FIVE = ethers.BigNumber.from("25" + eighteenZeros); + const TWO = ethers.BigNumber.from("2" + eighteenZeros); + const withdrawCount = 2; + + const constants = [TWENTY_FIVE, ONE, max_uint256, withdrawCount, 1, TWO]; + + const max_deposit = op( + Opcode.READ_MEMORY, + memoryOperand(MemoryType.Constant, 0) + ); + + const max_withdraw_initial = op( + Opcode.READ_MEMORY, + memoryOperand(MemoryType.Constant, 1) + ); + + const max_withdraw_later = op( + Opcode.READ_MEMORY, + memoryOperand(MemoryType.Constant, 5) + ); + + const aliceAddress = op(Opcode.CONTEXT, 0x0100); + + const depositSource = concat([ + max_deposit + ]); + + // (UserWithdrawCount > WithdrawCount) ? FIVE : TEN + // prettier-ignore + const withdrawSource = concat([ + + aliceAddress, + op(Opcode.GET), // Deposit count set for alice + op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 3)), + op(Opcode.GREATER_THAN), // Condition + max_withdraw_later, // true + max_withdraw_initial, // false + op(Opcode.EAGER_IF), + + // Setting value + aliceAddress, // key + aliceAddress, + op(Opcode.GET), // Deposit count set for alice + op(Opcode.READ_MEMORY,memoryOperand(MemoryType.Constant, 4)), + op(Opcode.ADD, 2), // Value + op(Opcode.SET), + ]); + + const source = [depositSource, withdrawSource]; // max_deposit set to 10 + + const stakeConfigStruct: StakeConfigStruct = { + name: "Stake Token", + symbol: "STKN", + asset: token.address, + expressionDeployer: expressionDeployer.address, + interpreter: interpreter.address, + stateConfig: { + sources: source, + constants: constants, + }, + }; + + const stake = await stakeDeploy(deployer, stakeFactory, stakeConfigStruct); + + const depositsAlice0_ = await getDeposits(stake, alice.address); + assert(depositsAlice0_.length === 0); + + // Give Alice some reserve tokens and deposit them + await token.transfer( + alice.address, + TWENTY_FIVE + ); + + // Deposits 10 tokens + let tokenBalanceAlice0 = await token.balanceOf(alice.address); + await token.connect(alice).approve(stake.address, tokenBalanceAlice0); + await stake.connect(alice).deposit(tokenBalanceAlice0, alice.address); + + const depositsAlice1_ = await getDeposits(stake, alice.address); + const time1_ = await getBlockTimestamp(); + assert(depositsAlice1_[0].timestamp === time1_); + assert(depositsAlice1_[0].amount.eq(tokenBalanceAlice0)); + + + // Initial Withdraw + for(let i = 0; i<=withdrawCount; i++){ + await stake.connect(alice).withdraw(ONE, alice.address, alice.address); + const withdrawsAlice0_ = await getDeposits(stake, alice.address); + tokenBalanceAlice0 = tokenBalanceAlice0.sub(ONE); + assert(withdrawsAlice0_[0].amount.eq(tokenBalanceAlice0)); + + } + + // Later withdraw [ if user has withdrawn more than depositCount, increase the withdraw limit] + await stake.connect(alice).withdraw(TWO, alice.address, alice.address); + tokenBalanceAlice0 = tokenBalanceAlice0.sub(TWO); + const withdrawsAlice0_ = await getDeposits(stake, alice.address); + assert(withdrawsAlice0_[0].amount.eq(tokenBalanceAlice0)); + }); });