cfo.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. const assert = require("assert");
  2. const { Token } = require("@solana/spl-token");
  3. const anchor = require("@project-serum/anchor");
  4. const serumCmn = require("@project-serum/common");
  5. const { Market } = require("@project-serum/serum");
  6. const { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } = anchor.web3;
  7. const utils = require("./utils");
  8. const { setupStakePool } = require("./utils/stake");
  9. const DEX_PID = new PublicKey("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");
  10. const SWAP_PID = new PublicKey("22Y43yTVxuUkoRKdm9thyRhQ3SdgQS7c7kB6UNCiaczD");
  11. const TOKEN_PID = new PublicKey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA");
  12. const REGISTRY_PID = new PublicKey(
  13. "GrAkKfEpTKQuVHG2Y97Y2FF4i7y7Q5AHLK94JBy7Y5yv"
  14. );
  15. const LOCKUP_PID = new PublicKey(
  16. "6ebQNeTPZ1j7k3TtkCCtEPRvG7GQsucQrZ7sSEDQi9Ks"
  17. );
  18. const FEES = "6160355581";
  19. describe("cfo", () => {
  20. anchor.setProvider(anchor.Provider.env());
  21. const program = anchor.workspace.Cfo;
  22. let officer;
  23. let TOKEN_CLIENT;
  24. let officerAccount;
  25. const sweepAuthority = program.provider.wallet.publicKey;
  26. // Accounts used to setup the orderbook.
  27. let ORDERBOOK_ENV,
  28. // Accounts used for A -> USDC swap transactions.
  29. SWAP_A_USDC_ACCOUNTS,
  30. // Accounts used for USDC -> A swap transactions.
  31. SWAP_USDC_A_ACCOUNTS,
  32. // Serum DEX vault PDA for market A/USDC.
  33. marketAVaultSigner,
  34. // Serum DEX vault PDA for market B/USDC.
  35. marketBVaultSigner;
  36. let registrar, msrmRegistrar;
  37. it("BOILERPLATE: Sets up a market with funded fees", async () => {
  38. ORDERBOOK_ENV = await utils.initMarket({
  39. provider: program.provider,
  40. });
  41. console.log("Token A: ", ORDERBOOK_ENV.marketA.baseMintAddress.toString());
  42. console.log(
  43. "Token USDC: ",
  44. ORDERBOOK_ENV.marketA.quoteMintAddress.toString()
  45. );
  46. TOKEN_CLIENT = new Token(
  47. program.provider.connection,
  48. ORDERBOOK_ENV.usdc,
  49. TOKEN_PID,
  50. program.provider.wallet.payer
  51. );
  52. await TOKEN_CLIENT.transfer(
  53. ORDERBOOK_ENV.godUsdc,
  54. ORDERBOOK_ENV.marketA._decoded.quoteVault,
  55. program.provider.wallet.payer,
  56. [],
  57. 10000000000000
  58. );
  59. const tokenAccount = await TOKEN_CLIENT.getAccountInfo(
  60. ORDERBOOK_ENV.marketA._decoded.quoteVault
  61. );
  62. assert.ok(tokenAccount.amount.toString() === "10000902263700");
  63. });
  64. it("BOILERPLATE: Executes trades to generate fees", async () => {
  65. await utils.runTradeBot(
  66. ORDERBOOK_ENV.marketA._decoded.ownAddress,
  67. program.provider,
  68. 1
  69. );
  70. let marketClient = await Market.load(
  71. program.provider.connection,
  72. ORDERBOOK_ENV.marketA._decoded.ownAddress,
  73. { commitment: "recent" },
  74. DEX_PID
  75. );
  76. assert.ok(marketClient._decoded.quoteFeesAccrued.toString() === FEES);
  77. });
  78. it("BOILERPLATE: Sets up the staking pools", async () => {
  79. await setupStakePool(ORDERBOOK_ENV.mintA, ORDERBOOK_ENV.godA);
  80. registrar = ORDERBOOK_ENV.usdc;
  81. msrmRegistrar = registrar;
  82. });
  83. it("Creates a CFO!", async () => {
  84. let distribution = {
  85. burn: 80,
  86. stake: 20,
  87. treasury: 0,
  88. };
  89. const [_officer, officerBump] = await PublicKey.findProgramAddress(
  90. [DEX_PID.toBuffer()],
  91. program.programId
  92. );
  93. officer = _officer;
  94. const [srmVault, srmBump] = await PublicKey.findProgramAddress(
  95. [anchor.utils.bytes.utf8.encode("vault"), officer.toBuffer()],
  96. program.programId
  97. );
  98. const [stake, stakeBump] = await PublicKey.findProgramAddress(
  99. [anchor.utils.bytes.utf8.encode("stake"), officer.toBuffer()],
  100. program.programId
  101. );
  102. const [treasury, treasuryBump] = await PublicKey.findProgramAddress(
  103. [
  104. Buffer.from(anchor.utils.bytes.utf8.encode("treasury")),
  105. officer.toBuffer(),
  106. ],
  107. program.programId
  108. );
  109. const bumps = {
  110. bump: officerBump,
  111. srm: srmBump,
  112. stake: stakeBump,
  113. treasury: treasuryBump,
  114. };
  115. await program.rpc.createOfficer(
  116. bumps,
  117. distribution,
  118. registrar,
  119. msrmRegistrar,
  120. {
  121. accounts: {
  122. officer,
  123. srmVault,
  124. stake,
  125. treasury,
  126. mint: ORDERBOOK_ENV.mintA,
  127. authority: program.provider.wallet.publicKey,
  128. dexProgram: DEX_PID,
  129. swapProgram: SWAP_PID,
  130. tokenProgram: TOKEN_PID,
  131. systemProgram: SystemProgram.programId,
  132. rent: SYSVAR_RENT_PUBKEY,
  133. },
  134. }
  135. );
  136. officerAccount = await program.account.officer.fetch(officer);
  137. assert.ok(
  138. officerAccount.authority.equals(program.provider.wallet.publicKey)
  139. );
  140. assert.ok(
  141. JSON.stringify(officerAccount.distribution) ===
  142. JSON.stringify(distribution)
  143. );
  144. });
  145. it("Creates a token account for the officer associated with the market", async () => {
  146. const [token, bump] = await PublicKey.findProgramAddress(
  147. [officer.toBuffer(), ORDERBOOK_ENV.usdc.toBuffer()],
  148. program.programId
  149. );
  150. await program.rpc.createOfficerToken(bump, {
  151. accounts: {
  152. officer,
  153. token,
  154. mint: ORDERBOOK_ENV.usdc,
  155. payer: program.provider.wallet.publicKey,
  156. systemProgram: SystemProgram.programId,
  157. tokenProgram: TOKEN_PID,
  158. rent: SYSVAR_RENT_PUBKEY,
  159. },
  160. });
  161. const tokenAccount = await TOKEN_CLIENT.getAccountInfo(token);
  162. assert.ok(tokenAccount.state === 1);
  163. assert.ok(tokenAccount.isInitialized);
  164. });
  165. it("Sweeps fees", async () => {
  166. const [sweepVault, bump] = await PublicKey.findProgramAddress(
  167. [officer.toBuffer(), ORDERBOOK_ENV.usdc.toBuffer()],
  168. program.programId
  169. );
  170. const beforeTokenAccount = await serumCmn.getTokenAccount(
  171. program.provider,
  172. sweepVault
  173. );
  174. await program.rpc.sweepFees({
  175. accounts: {
  176. officer,
  177. sweepVault,
  178. mint: ORDERBOOK_ENV.usdc,
  179. dex: {
  180. market: ORDERBOOK_ENV.marketA._decoded.ownAddress,
  181. pcVault: ORDERBOOK_ENV.marketA._decoded.quoteVault,
  182. sweepAuthority,
  183. vaultSigner: ORDERBOOK_ENV.vaultSigner,
  184. dexProgram: DEX_PID,
  185. tokenProgram: TOKEN_PID,
  186. },
  187. },
  188. });
  189. const afterTokenAccount = await serumCmn.getTokenAccount(
  190. program.provider,
  191. sweepVault
  192. );
  193. assert.ok(
  194. afterTokenAccount.amount.sub(beforeTokenAccount.amount).toString() ===
  195. FEES
  196. );
  197. });
  198. it("TODO", async () => {
  199. // todo
  200. });
  201. });