utils.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import * as anchor from '@coral-xyz/anchor';
  2. import { createMint, getAssociatedTokenAddressSync, getOrCreateAssociatedTokenAccount, mintTo } from '@solana/spl-token';
  3. import { type Connection, Keypair, PublicKey, type Signer } from '@solana/web3.js';
  4. import { BN } from 'bn.js';
  5. export async function sleep(seconds: number) {
  6. new Promise((resolve) => setTimeout(resolve, seconds * 1000));
  7. }
  8. export const generateSeededKeypair = (seed: string) => {
  9. return Keypair.fromSeed(anchor.utils.bytes.utf8.encode(anchor.utils.sha256.hash(seed)).slice(0, 32));
  10. };
  11. export const expectRevert = async (promise: Promise<any>) => {
  12. try {
  13. await promise;
  14. throw new Error('Expected a revert');
  15. } catch {
  16. return;
  17. }
  18. };
  19. export const mintingTokens = async ({
  20. connection,
  21. creator,
  22. holder = creator,
  23. mintAKeypair,
  24. mintBKeypair,
  25. mintedAmount = 100,
  26. decimals = 6,
  27. }: {
  28. connection: Connection;
  29. creator: Signer;
  30. holder?: Signer;
  31. mintAKeypair: Keypair;
  32. mintBKeypair: Keypair;
  33. mintedAmount?: number;
  34. decimals?: number;
  35. }) => {
  36. // Mint tokens
  37. await connection.confirmTransaction(await connection.requestAirdrop(creator.publicKey, 10 ** 10));
  38. await createMint(connection, creator, creator.publicKey, creator.publicKey, decimals, mintAKeypair);
  39. await createMint(connection, creator, creator.publicKey, creator.publicKey, decimals, mintBKeypair);
  40. await getOrCreateAssociatedTokenAccount(connection, holder, mintAKeypair.publicKey, holder.publicKey, true);
  41. await getOrCreateAssociatedTokenAccount(connection, holder, mintBKeypair.publicKey, holder.publicKey, true);
  42. await mintTo(
  43. connection,
  44. creator,
  45. mintAKeypair.publicKey,
  46. getAssociatedTokenAddressSync(mintAKeypair.publicKey, holder.publicKey, true),
  47. creator.publicKey,
  48. mintedAmount * 10 ** decimals,
  49. );
  50. await mintTo(
  51. connection,
  52. creator,
  53. mintBKeypair.publicKey,
  54. getAssociatedTokenAddressSync(mintBKeypair.publicKey, holder.publicKey, true),
  55. creator.publicKey,
  56. mintedAmount * 10 ** decimals,
  57. );
  58. };
  59. export interface TestValues {
  60. id: PublicKey;
  61. fee: number;
  62. admin: Keypair;
  63. mintAKeypair: Keypair;
  64. mintBKeypair: Keypair;
  65. defaultSupply: anchor.BN;
  66. ammKey: PublicKey;
  67. minimumLiquidity: anchor.BN;
  68. poolKey: PublicKey;
  69. poolAuthority: PublicKey;
  70. mintLiquidity: PublicKey;
  71. depositAmountA: anchor.BN;
  72. depositAmountB: anchor.BN;
  73. liquidityAccount: PublicKey;
  74. poolAccountA: PublicKey;
  75. poolAccountB: PublicKey;
  76. holderAccountA: PublicKey;
  77. holderAccountB: PublicKey;
  78. }
  79. type TestValuesDefaults = {
  80. [K in keyof TestValues]+?: TestValues[K];
  81. };
  82. export function createValues(defaults?: TestValuesDefaults): TestValues {
  83. const id = defaults?.id || Keypair.generate().publicKey;
  84. const admin = Keypair.generate();
  85. const ammKey = PublicKey.findProgramAddressSync([id.toBuffer()], anchor.workspace.SwapExample.programId)[0];
  86. // Making sure tokens are in the right order
  87. const mintAKeypair = Keypair.generate();
  88. let mintBKeypair = Keypair.generate();
  89. while (new BN(mintBKeypair.publicKey.toBytes()).lt(new BN(mintAKeypair.publicKey.toBytes()))) {
  90. mintBKeypair = Keypair.generate();
  91. }
  92. const poolAuthority = PublicKey.findProgramAddressSync(
  93. [ammKey.toBuffer(), mintAKeypair.publicKey.toBuffer(), mintBKeypair.publicKey.toBuffer(), Buffer.from('authority')],
  94. anchor.workspace.SwapExample.programId,
  95. )[0];
  96. const mintLiquidity = PublicKey.findProgramAddressSync(
  97. [ammKey.toBuffer(), mintAKeypair.publicKey.toBuffer(), mintBKeypair.publicKey.toBuffer(), Buffer.from('liquidity')],
  98. anchor.workspace.SwapExample.programId,
  99. )[0];
  100. const poolKey = PublicKey.findProgramAddressSync(
  101. [ammKey.toBuffer(), mintAKeypair.publicKey.toBuffer(), mintBKeypair.publicKey.toBuffer()],
  102. anchor.workspace.SwapExample.programId,
  103. )[0];
  104. return {
  105. id,
  106. fee: 500,
  107. admin,
  108. ammKey,
  109. mintAKeypair,
  110. mintBKeypair,
  111. mintLiquidity,
  112. poolKey,
  113. poolAuthority,
  114. poolAccountA: getAssociatedTokenAddressSync(mintAKeypair.publicKey, poolAuthority, true),
  115. poolAccountB: getAssociatedTokenAddressSync(mintBKeypair.publicKey, poolAuthority, true),
  116. liquidityAccount: getAssociatedTokenAddressSync(mintLiquidity, admin.publicKey, true),
  117. holderAccountA: getAssociatedTokenAddressSync(mintAKeypair.publicKey, admin.publicKey, true),
  118. holderAccountB: getAssociatedTokenAddressSync(mintBKeypair.publicKey, admin.publicKey, true),
  119. depositAmountA: new BN(4 * 10 ** 6),
  120. depositAmountB: new BN(1 * 10 ** 6),
  121. minimumLiquidity: new BN(100),
  122. defaultSupply: new BN(100 * 10 ** 6),
  123. };
  124. }