all.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { Wallet } from "@coral-xyz/anchor";
  2. import type { Connection } from "@solana/web3.js";
  3. import {
  4. Keypair,
  5. PublicKey,
  6. sendAndConfirmTransaction,
  7. SystemProgram,
  8. Transaction,
  9. } from "@solana/web3.js";
  10. import type { CustomAbortController } from "./start-validator";
  11. import { startValidatorRaw } from "./start-validator";
  12. import { getCurrentEpoch } from "../src";
  13. import { getConfigAddress } from "../src/pdas";
  14. import { PythStakingClient } from "../src/pyth-staking-client";
  15. import type { GlobalConfig } from "../src/types";
  16. describe("Test", () => {
  17. let connection: Connection;
  18. let controller: CustomAbortController;
  19. let wallet: Wallet;
  20. let pythStakingClient: PythStakingClient;
  21. let rewardProgramAuthority: Keypair;
  22. let poolData: Keypair;
  23. let config: GlobalConfig;
  24. beforeAll(async () => {
  25. ({ connection, controller, wallet } = await startValidatorRaw());
  26. pythStakingClient = new PythStakingClient({ connection, wallet });
  27. rewardProgramAuthority = Keypair.generate();
  28. poolData = Keypair.generate();
  29. });
  30. afterAll(() => {
  31. return controller.abort();
  32. });
  33. test("config", async () => {
  34. const tmpConfig: GlobalConfig = {
  35. bump: getConfigAddress()[1],
  36. governanceAuthority: PublicKey.unique(),
  37. pythTokenMint: PublicKey.unique(),
  38. pythGovernanceRealm: PublicKey.unique(),
  39. removedUnlockingDuration: 0,
  40. epochDuration: 100n,
  41. freeze: false,
  42. pdaAuthority: PublicKey.unique(),
  43. governanceProgram: PublicKey.unique(),
  44. pythTokenListTime: null,
  45. agreementHash: Array.from<number>({ length: 32 }).fill(0),
  46. mockClockTime: 0n,
  47. poolAuthority: PublicKey.unique(),
  48. };
  49. await pythStakingClient.initGlobalConfig(tmpConfig);
  50. config = await pythStakingClient.getGlobalConfig();
  51. expect(config).toEqual(tmpConfig);
  52. });
  53. test("initialize pool", async () => {
  54. const poolDataSpace = 2 * 1024 * 1024;
  55. const balance =
  56. await connection.getMinimumBalanceForRentExemption(poolDataSpace);
  57. const transaction = new Transaction().add(
  58. SystemProgram.createAccount({
  59. fromPubkey: wallet.publicKey,
  60. newAccountPubkey: poolData.publicKey,
  61. lamports: balance,
  62. space: poolDataSpace,
  63. programId: pythStakingClient.integrityPoolProgram.programId,
  64. }),
  65. );
  66. await sendAndConfirmTransaction(connection, transaction, [
  67. wallet.payer,
  68. poolData,
  69. ]);
  70. await pythStakingClient.initializePool({
  71. rewardProgramAuthority: rewardProgramAuthority.publicKey,
  72. y: 100n,
  73. poolData: poolData.publicKey,
  74. });
  75. const poolConfig = await pythStakingClient.getPoolConfigAccount();
  76. expect(poolConfig).toEqual({
  77. poolData: poolData.publicKey,
  78. rewardProgramAuthority: rewardProgramAuthority.publicKey,
  79. pythTokenMint: config.pythTokenMint,
  80. y: 100n,
  81. });
  82. const poolDataAccount = await pythStakingClient.getPoolDataAccount();
  83. expect(poolDataAccount).toEqual({
  84. lastUpdatedEpoch: (await getCurrentEpoch(connection)) - 1n,
  85. claimableRewards: 0n,
  86. publishers: Array.from({ length: 1024 }).fill(PublicKey.default),
  87. delState: Array.from({ length: 1024 }).fill({
  88. totalDelegation: 0n,
  89. deltaDelegation: 0n,
  90. }),
  91. selfDelState: Array.from({ length: 1024 }).fill({
  92. totalDelegation: 0n,
  93. deltaDelegation: 0n,
  94. }),
  95. publisherStakeAccounts: Array.from({ length: 1024 }).fill(
  96. PublicKey.default,
  97. ),
  98. events: Array.from({ length: 52 }).fill({
  99. epoch: 0n,
  100. y: 0n,
  101. extraSpace: Array.from({ length: 7 }).fill(0n),
  102. eventData: Array.from({ length: 1024 }).fill({
  103. selfRewardRatio: 0n,
  104. otherRewardRatio: 0n,
  105. delegationFee: 0n,
  106. }),
  107. }),
  108. numEvents: 0n,
  109. numSlashEvents: Array.from({ length: 1024 }).fill(0n),
  110. delegationFees: Array.from({ length: 1024 }).fill(0n),
  111. });
  112. });
  113. });