permissioned-markets.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. const assert = require("assert");
  2. const { Token, TOKEN_PROGRAM_ID } = require("@solana/spl-token");
  3. const anchor = require("@project-serum/anchor");
  4. const serum = require("@project-serum/serum");
  5. const { BN } = anchor;
  6. const { Transaction, TransactionInstruction } = anchor.web3;
  7. const { DexInstructions, OpenOrders, Market } = serum;
  8. const { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } = anchor.web3;
  9. const { initMarket, sleep } = require("./utils");
  10. const DEX_PID = new PublicKey("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");
  11. const REFERRAL = new PublicKey("2k1bb16Hu7ocviT2KC3wcCgETtnC8tEUuvFBH4C5xStG");
  12. describe("permissioned-markets", () => {
  13. // Anchor client setup.
  14. const provider = anchor.Provider.env();
  15. anchor.setProvider(provider);
  16. const program = anchor.workspace.PermissionedMarkets;
  17. // Token clients.
  18. let usdcClient;
  19. // Global DEX accounts and clients shared accross all tests.
  20. let marketClient, tokenAccount, usdcAccount;
  21. let openOrders, openOrdersBump, openOrdersInitAuthority, openOrdersBumpinit;
  22. let usdcPosted;
  23. let marketMakerOpenOrders;
  24. it("BOILERPLATE: Initializes an orderbook", async () => {
  25. const {
  26. marketMakerOpenOrders: mmOo,
  27. marketA,
  28. godA,
  29. godUsdc,
  30. usdc,
  31. } = await initMarket({ provider });
  32. marketClient = marketA;
  33. marketClient._programId = program.programId;
  34. usdcAccount = godUsdc;
  35. tokenAccount = godA;
  36. marketMakerOpenOrders = mmOo;
  37. usdcClient = new Token(
  38. provider.connection,
  39. usdc,
  40. TOKEN_PROGRAM_ID,
  41. provider.wallet.payer
  42. );
  43. });
  44. it("BOILERPLATE: Calculates open orders addresses", async () => {
  45. const [_openOrders, bump] = await PublicKey.findProgramAddress(
  46. [
  47. anchor.utils.bytes.utf8.encode("open-orders"),
  48. marketClient.address.toBuffer(),
  49. program.provider.wallet.publicKey.toBuffer(),
  50. ],
  51. program.programId
  52. );
  53. const [
  54. _openOrdersInitAuthority,
  55. bumpInit,
  56. ] = await PublicKey.findProgramAddress(
  57. [
  58. anchor.utils.bytes.utf8.encode("open-orders-init"),
  59. marketClient.address.toBuffer(),
  60. ],
  61. program.programId
  62. );
  63. // Save global variables re-used across tests.
  64. openOrders = _openOrders;
  65. openOrdersBump = bump;
  66. openOrdersInitAuthority = _openOrdersInitAuthority;
  67. openOrdersBumpInit = bumpInit;
  68. });
  69. it("Creates an open orders account", async () => {
  70. await program.rpc.initAccount(openOrdersBump, openOrdersBumpInit, {
  71. accounts: {
  72. openOrdersInitAuthority,
  73. openOrders,
  74. authority: program.provider.wallet.publicKey,
  75. market: marketClient.address,
  76. rent: SYSVAR_RENT_PUBKEY,
  77. systemProgram: SystemProgram.programId,
  78. dexProgram: DEX_PID,
  79. },
  80. });
  81. const account = await provider.connection.getAccountInfo(openOrders);
  82. assert.ok(account.owner.toString() === DEX_PID.toString());
  83. });
  84. it("Posts a bid on the orderbook", async () => {
  85. const size = 1;
  86. const price = 1;
  87. // The amount of USDC transferred into the dex for the trade.
  88. usdcPosted = new BN(marketClient._decoded.quoteLotSize.toNumber()).mul(
  89. marketClient
  90. .baseSizeNumberToLots(size)
  91. .mul(marketClient.priceNumberToLots(price))
  92. );
  93. // Note: Prepend delegate approve to the tx since the owner of the token
  94. // account must match the owner of the open orders account. We
  95. // can probably hide this in the serum client.
  96. const tx = new Transaction();
  97. tx.add(
  98. Token.createApproveInstruction(
  99. TOKEN_PROGRAM_ID,
  100. usdcAccount,
  101. openOrders,
  102. program.provider.wallet.publicKey,
  103. [],
  104. usdcPosted.toNumber()
  105. )
  106. );
  107. tx.add(
  108. serumProxy(
  109. marketClient.makePlaceOrderInstruction(program.provider.connection, {
  110. owner: program.provider.wallet.publicKey,
  111. payer: usdcAccount,
  112. side: "buy",
  113. price,
  114. size,
  115. orderType: "postOnly",
  116. clientId: new BN(999),
  117. openOrdersAddressKey: openOrders,
  118. selfTradeBehavior: "abortTransaction",
  119. })
  120. )
  121. );
  122. await provider.send(tx);
  123. });
  124. it("Cancels a bid on the orderbook", async () => {
  125. // Given.
  126. const beforeOoAccount = await OpenOrders.load(
  127. provider.connection,
  128. openOrders,
  129. DEX_PID
  130. );
  131. // When.
  132. const tx = new Transaction();
  133. tx.add(
  134. serumProxy(
  135. (
  136. await marketClient.makeCancelOrderByClientIdTransaction(
  137. program.provider.connection,
  138. program.provider.wallet.publicKey,
  139. openOrders,
  140. new BN(999)
  141. )
  142. ).instructions[0]
  143. )
  144. );
  145. await provider.send(tx);
  146. // Then.
  147. const afterOoAccount = await OpenOrders.load(
  148. provider.connection,
  149. openOrders,
  150. DEX_PID
  151. );
  152. assert.ok(beforeOoAccount.quoteTokenFree.eq(new BN(0)));
  153. assert.ok(beforeOoAccount.quoteTokenTotal.eq(usdcPosted));
  154. assert.ok(afterOoAccount.quoteTokenFree.eq(usdcPosted));
  155. assert.ok(afterOoAccount.quoteTokenTotal.eq(usdcPosted));
  156. });
  157. // Need to crank the cancel so that we can close later.
  158. it("Cranks the cancel transaction", async () => {
  159. // TODO: can do this in a single transaction if we covert the pubkey bytes
  160. // into a [u64; 4] array and sort. I'm lazy though.
  161. let eq = await marketClient.loadEventQueue(provider.connection);
  162. while (eq.length > 0) {
  163. const tx = new Transaction();
  164. tx.add(
  165. DexInstructions.consumeEvents({
  166. market: marketClient._decoded.ownAddress,
  167. eventQueue: marketClient._decoded.eventQueue,
  168. coinFee: marketClient._decoded.eventQueue,
  169. pcFee: marketClient._decoded.eventQueue,
  170. openOrdersAccounts: [eq[0].openOrders],
  171. limit: 1,
  172. programId: DEX_PID,
  173. })
  174. );
  175. await provider.send(tx);
  176. eq = await marketClient.loadEventQueue(provider.connection);
  177. }
  178. });
  179. it("Settles funds on the orderbook", async () => {
  180. // Given.
  181. const beforeTokenAccount = await usdcClient.getAccountInfo(usdcAccount);
  182. // When.
  183. const tx = new Transaction();
  184. tx.add(
  185. serumProxy(
  186. DexInstructions.settleFunds({
  187. market: marketClient._decoded.ownAddress,
  188. openOrders,
  189. owner: provider.wallet.publicKey,
  190. baseVault: marketClient._decoded.baseVault,
  191. quoteVault: marketClient._decoded.quoteVault,
  192. baseWallet: tokenAccount,
  193. quoteWallet: usdcAccount,
  194. vaultSigner: await PublicKey.createProgramAddress(
  195. [
  196. marketClient.address.toBuffer(),
  197. marketClient._decoded.vaultSignerNonce.toArrayLike(
  198. Buffer,
  199. "le",
  200. 8
  201. ),
  202. ],
  203. DEX_PID
  204. ),
  205. programId: program.programId,
  206. referrerQuoteWallet: usdcAccount,
  207. })
  208. )
  209. );
  210. await provider.send(tx);
  211. // Then.
  212. const afterTokenAccount = await usdcClient.getAccountInfo(usdcAccount);
  213. assert.ok(
  214. afterTokenAccount.amount.sub(beforeTokenAccount.amount).toNumber() ===
  215. usdcPosted.toNumber()
  216. );
  217. });
  218. it("Closes an open orders account", async () => {
  219. // Given.
  220. const beforeAccount = await program.provider.connection.getAccountInfo(
  221. program.provider.wallet.publicKey
  222. );
  223. // When.
  224. const tx = new Transaction();
  225. tx.add(
  226. serumProxy(
  227. DexInstructions.closeOpenOrders({
  228. market: marketClient._decoded.ownAddress,
  229. openOrders,
  230. owner: program.provider.wallet.publicKey,
  231. solWallet: program.provider.wallet.publicKey,
  232. programId: program.programId,
  233. })
  234. )
  235. );
  236. await provider.send(tx);
  237. // Then.
  238. const afterAccount = await program.provider.connection.getAccountInfo(
  239. program.provider.wallet.publicKey
  240. );
  241. const closedAccount = await program.provider.connection.getAccountInfo(
  242. openOrders
  243. );
  244. assert.ok(23352768 === afterAccount.lamports - beforeAccount.lamports);
  245. assert.ok(closedAccount === null);
  246. });
  247. });
  248. // Adds the serum dex account to the instruction so that proxies can
  249. // relay (CPI requires the executable account).
  250. //
  251. // TODO: we should add flag in the dex client that says if a proxy is being
  252. // used, and if so, do this automatically.
  253. function serumProxy(ix) {
  254. ix.keys = [
  255. { pubkey: DEX_PID, isWritable: false, isSigner: false },
  256. ...ix.keys,
  257. ];
  258. return ix;
  259. }