import * as anchor from "@project-serum/anchor"; import { Program, BN } from "@project-serum/anchor"; import { PsyStake } from "../target/types/psy_stake"; import { deriveStakePool, deriveStakingTokenAccount } from "./pdas"; import { assert } from "chai"; import { Keypair, PublicKey } from "@solana/web3.js"; const { setupTestAccounts, MIN_EPOCH_DURATION } = require("./setup"); const { sleep } = require("./utils"); import { updateStakePoolEpochRpc } from "../packages/psystake/src/instructions"; describe("Test update StakePool instruction.", () => { const program = anchor.workspace.PsyStake as Program; const provider = anchor.Provider.env(); anchor.setProvider(provider); let network, stakePoolAuthority: Keypair; const uniqueSeed = 1; let stakePool: PublicKey, stakePoolBump: number, stakingTokenAccount: PublicKey, stakingTokenAccountBump: number, stakingTokenMint: PublicKey; before(async () => { ({ network, stakePoolAuthority } = await setupTestAccounts( program, provider )); stakingTokenMint = network.usdcToken.publicKey; [stakePool, stakePoolBump] = await deriveStakePool( program, network.usdcToken.publicKey, new BN(uniqueSeed) ); [stakingTokenAccount, stakingTokenAccountBump] = await deriveStakingTokenAccount(program, stakePool); const epochDuration = new BN(MIN_EPOCH_DURATION); const nextEpochStartTime = new BN(new Date().getTime() / 1000 + 2); await program.methods .createStakePool(uniqueSeed, nextEpochStartTime, epochDuration) .accounts({ stakePoolAuthority: stakePoolAuthority.publicKey, stakePool, stakingTokenMint, stakingTokenAccount, }) .signers([stakePoolAuthority]) .rpc(); }); it("UpdateStakePoolEpoch should fail when current epoch has not ended.", async () => { try { await updateStakePoolEpochRpc(program, stakePool, []); assert(false); } catch (err) { assert(err.message, "The current epoch has not ended."); } }); it("UpdateStakePoolEpoch succeeds when epoch has ended.", async () => { const stakePoolData = await program.account.stakePool.fetch(stakePool); const sleepDurationSeconds = stakePoolData.nextEpochStartTime.sub( new BN(new Date().getTime() / 1000) ); const sleepDurationMs = sleepDurationSeconds.toNumber() * 1000 + 2000; console.log(`Sleep for ${sleepDurationMs}ms`); const success = await sleep(sleepDurationMs).then(async () => { await updateStakePoolEpochRpc(program, stakePool, []); const stakePoolData2 = await program.account.stakePool.fetch(stakePool); assert.equal(stakePoolData2.currentEpoch, stakePoolData.currentEpoch + 1); assert( stakePoolData2.nextEpochStartTime.eq( stakePoolData.nextEpochStartTime.add(new BN(MIN_EPOCH_DURATION)) ) ); return true; }); assert(success); }); });