utils.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import * as anchor from "@coral-xyz/anchor";
  2. import {
  3. createMint,
  4. getAssociatedTokenAddressSync,
  5. getOrCreateAssociatedTokenAccount,
  6. mintTo,
  7. } from "@solana/spl-token";
  8. import { Keypair, PublicKey, Connection, Signer } from "@solana/web3.js";
  9. import { BN } from "bn.js";
  10. export async function sleep(seconds: number) {
  11. new Promise((resolve) => setTimeout(resolve, seconds * 1000));
  12. }
  13. export const generateSeededKeypair = (seed: string) => {
  14. return Keypair.fromSeed(
  15. anchor.utils.bytes.utf8.encode(anchor.utils.sha256.hash(seed)).slice(0, 32)
  16. );
  17. };
  18. export const expectRevert = async (promise: Promise<any>) => {
  19. try {
  20. await promise;
  21. throw new Error("Expected a revert");
  22. } catch {
  23. return;
  24. }
  25. };
  26. export const mintingTokens = async ({
  27. connection,
  28. creator,
  29. holder = creator,
  30. mintAKeypair,
  31. mintBKeypair,
  32. mintedAmount = 100,
  33. decimals = 6,
  34. }: {
  35. connection: Connection;
  36. creator: Signer;
  37. holder?: Signer;
  38. mintAKeypair: Keypair;
  39. mintBKeypair: Keypair;
  40. mintedAmount?: number;
  41. decimals?: number;
  42. }) => {
  43. // Mint tokens
  44. await connection.confirmTransaction(
  45. await connection.requestAirdrop(creator.publicKey, 10 ** 10)
  46. );
  47. await createMint(
  48. connection,
  49. creator,
  50. creator.publicKey,
  51. creator.publicKey,
  52. decimals,
  53. mintAKeypair
  54. );
  55. await createMint(
  56. connection,
  57. creator,
  58. creator.publicKey,
  59. creator.publicKey,
  60. decimals,
  61. mintBKeypair
  62. );
  63. await getOrCreateAssociatedTokenAccount(
  64. connection,
  65. holder,
  66. mintAKeypair.publicKey,
  67. holder.publicKey,
  68. true
  69. );
  70. await getOrCreateAssociatedTokenAccount(
  71. connection,
  72. holder,
  73. mintBKeypair.publicKey,
  74. holder.publicKey,
  75. true
  76. );
  77. await mintTo(
  78. connection,
  79. creator,
  80. mintAKeypair.publicKey,
  81. getAssociatedTokenAddressSync(
  82. mintAKeypair.publicKey,
  83. holder.publicKey,
  84. true
  85. ),
  86. creator.publicKey,
  87. mintedAmount * 10 ** decimals
  88. );
  89. await mintTo(
  90. connection,
  91. creator,
  92. mintBKeypair.publicKey,
  93. getAssociatedTokenAddressSync(
  94. mintBKeypair.publicKey,
  95. holder.publicKey,
  96. true
  97. ),
  98. creator.publicKey,
  99. mintedAmount * 10 ** decimals
  100. );
  101. };
  102. export interface TestValues {
  103. id: PublicKey;
  104. fee: number;
  105. admin: Keypair;
  106. mintAKeypair: Keypair;
  107. mintBKeypair: Keypair;
  108. defaultSupply: anchor.BN;
  109. ammKey: PublicKey;
  110. minimumLiquidity: anchor.BN;
  111. poolKey: PublicKey;
  112. poolAuthority: PublicKey;
  113. mintLiquidity: PublicKey;
  114. depositAmountA: anchor.BN;
  115. depositAmountB: anchor.BN;
  116. liquidityAccount: PublicKey;
  117. poolAccountA: PublicKey;
  118. poolAccountB: PublicKey;
  119. holderAccountA: PublicKey;
  120. holderAccountB: PublicKey;
  121. }
  122. type TestValuesDefaults = {
  123. [K in keyof TestValues]+?: TestValues[K];
  124. };
  125. export function createValues(defaults?: TestValuesDefaults): TestValues {
  126. const id = defaults?.id || Keypair.generate().publicKey;
  127. const admin = Keypair.generate();
  128. const ammKey = PublicKey.findProgramAddressSync(
  129. [id.toBuffer()],
  130. anchor.workspace.AmmTutorial.programId
  131. )[0];
  132. // Making sure tokens are in the right order
  133. const mintAKeypair = Keypair.generate();
  134. let mintBKeypair = Keypair.generate();
  135. while (
  136. new BN(mintBKeypair.publicKey.toBytes()).lt(
  137. new BN(mintAKeypair.publicKey.toBytes())
  138. )
  139. ) {
  140. mintBKeypair = Keypair.generate();
  141. }
  142. const poolAuthority = PublicKey.findProgramAddressSync(
  143. [
  144. ammKey.toBuffer(),
  145. mintAKeypair.publicKey.toBuffer(),
  146. mintBKeypair.publicKey.toBuffer(),
  147. Buffer.from("authority"),
  148. ],
  149. anchor.workspace.AmmTutorial.programId
  150. )[0];
  151. const mintLiquidity = PublicKey.findProgramAddressSync(
  152. [
  153. ammKey.toBuffer(),
  154. mintAKeypair.publicKey.toBuffer(),
  155. mintBKeypair.publicKey.toBuffer(),
  156. Buffer.from("liquidity"),
  157. ],
  158. anchor.workspace.AmmTutorial.programId
  159. )[0];
  160. const poolKey = PublicKey.findProgramAddressSync(
  161. [
  162. ammKey.toBuffer(),
  163. mintAKeypair.publicKey.toBuffer(),
  164. mintBKeypair.publicKey.toBuffer(),
  165. ],
  166. anchor.workspace.AmmTutorial.programId
  167. )[0];
  168. return {
  169. id,
  170. fee: 500,
  171. admin,
  172. ammKey,
  173. mintAKeypair,
  174. mintBKeypair,
  175. mintLiquidity,
  176. poolKey,
  177. poolAuthority,
  178. poolAccountA: getAssociatedTokenAddressSync(
  179. mintAKeypair.publicKey,
  180. poolAuthority,
  181. true
  182. ),
  183. poolAccountB: getAssociatedTokenAddressSync(
  184. mintBKeypair.publicKey,
  185. poolAuthority,
  186. true
  187. ),
  188. liquidityAccount: getAssociatedTokenAddressSync(
  189. mintLiquidity,
  190. admin.publicKey,
  191. true
  192. ),
  193. holderAccountA: getAssociatedTokenAddressSync(
  194. mintAKeypair.publicKey,
  195. admin.publicKey,
  196. true
  197. ),
  198. holderAccountB: getAssociatedTokenAddressSync(
  199. mintBKeypair.publicKey,
  200. admin.publicKey,
  201. true
  202. ),
  203. depositAmountA: new BN(4 * 10 ** 6),
  204. depositAmountB: new BN(1 * 10 ** 6),
  205. minimumLiquidity: new BN(100),
  206. defaultSupply: new BN(100 * 10 ** 6),
  207. };
  208. }