permissioned-markets.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 {
  7. Keypair,
  8. Transaction,
  9. TransactionInstruction,
  10. PublicKey,
  11. SystemProgram,
  12. SYSVAR_RENT_PUBKEY,
  13. } = anchor.web3;
  14. const {
  15. DexInstructions,
  16. OpenOrders,
  17. OpenOrdersPda,
  18. Logger,
  19. ReferralFees,
  20. MarketProxyBuilder,
  21. } = serum;
  22. const { initMarket, sleep } = require("./utils");
  23. const DEX_PID = new PublicKey("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");
  24. const REFERRAL_AUTHORITY = new PublicKey(
  25. "3oSfkjQZKCneYvsCTZc9HViGAPqR8pYr4h9YeGB5ZxHf"
  26. );
  27. describe("permissioned-markets", () => {
  28. // Anchor client setup.
  29. const provider = anchor.Provider.env();
  30. anchor.setProvider(provider);
  31. const programs = [
  32. anchor.workspace.PermissionedMarkets,
  33. anchor.workspace.PermissionedMarketsMiddleware,
  34. ];
  35. programs.forEach((program, index) => {
  36. // Token client.
  37. let usdcClient;
  38. // Global DEX accounts and clients shared across all tests.
  39. let marketProxy, tokenAccount, usdcAccount;
  40. let openOrders, openOrdersBump, openOrdersInitAuthority, openOrdersBumpinit;
  41. let usdcPosted;
  42. let referralTokenAddress;
  43. it("BOILERPLATE: Initializes an orderbook", async () => {
  44. const getAuthority = async (market) => {
  45. return (
  46. await PublicKey.findProgramAddress(
  47. [
  48. anchor.utils.bytes.utf8.encode("open-orders-init"),
  49. DEX_PID.toBuffer(),
  50. market.toBuffer(),
  51. ],
  52. program.programId
  53. )
  54. )[0];
  55. };
  56. const marketLoader = async (market) => {
  57. return new MarketProxyBuilder()
  58. .middleware(
  59. new OpenOrdersPda({
  60. proxyProgramId: program.programId,
  61. dexProgramId: DEX_PID,
  62. })
  63. )
  64. .middleware(new ReferralFees())
  65. .middleware(new Identity())
  66. .middleware(new Logger())
  67. .load({
  68. connection: provider.connection,
  69. market,
  70. dexProgramId: DEX_PID,
  71. proxyProgramId: program.programId,
  72. options: { commitment: "recent" },
  73. });
  74. };
  75. const { marketA, godA, godUsdc, usdc } = await initMarket({
  76. provider,
  77. getAuthority,
  78. proxyProgramId: program.programId,
  79. marketLoader,
  80. });
  81. marketProxy = marketA;
  82. usdcAccount = godUsdc;
  83. tokenAccount = godA;
  84. usdcClient = new Token(
  85. provider.connection,
  86. usdc,
  87. TOKEN_PROGRAM_ID,
  88. provider.wallet.payer
  89. );
  90. referral = await usdcClient.createAccount(REFERRAL_AUTHORITY);
  91. });
  92. it("BOILERPLATE: Calculates open orders addresses", async () => {
  93. const [_openOrders, bump] = await PublicKey.findProgramAddress(
  94. [
  95. anchor.utils.bytes.utf8.encode("open-orders"),
  96. DEX_PID.toBuffer(),
  97. marketProxy.market.address.toBuffer(),
  98. program.provider.wallet.publicKey.toBuffer(),
  99. ],
  100. program.programId
  101. );
  102. const [
  103. _openOrdersInitAuthority,
  104. bumpInit,
  105. ] = await PublicKey.findProgramAddress(
  106. [
  107. anchor.utils.bytes.utf8.encode("open-orders-init"),
  108. DEX_PID.toBuffer(),
  109. marketProxy.market.address.toBuffer(),
  110. ],
  111. program.programId
  112. );
  113. // Save global variables re-used across tests.
  114. openOrders = _openOrders;
  115. openOrdersBump = bump;
  116. openOrdersInitAuthority = _openOrdersInitAuthority;
  117. openOrdersBumpInit = bumpInit;
  118. });
  119. it("Creates an open orders account", async () => {
  120. const tx = new Transaction();
  121. tx.add(
  122. await marketProxy.instruction.initOpenOrders(
  123. program.provider.wallet.publicKey,
  124. marketProxy.market.address,
  125. marketProxy.market.address, // Dummy. Replaced by middleware.
  126. marketProxy.market.address // Dummy. Replaced by middleware.
  127. )
  128. );
  129. await provider.send(tx);
  130. const account = await provider.connection.getAccountInfo(openOrders);
  131. assert.ok(account.owner.toString() === DEX_PID.toString());
  132. });
  133. it("Posts a bid on the orderbook", async () => {
  134. const size = 1;
  135. const price = 1;
  136. usdcPosted = new BN(
  137. marketProxy.market._decoded.quoteLotSize.toNumber()
  138. ).mul(
  139. marketProxy.market
  140. .baseSizeNumberToLots(size)
  141. .mul(marketProxy.market.priceNumberToLots(price))
  142. );
  143. const tx = new Transaction();
  144. tx.add(
  145. marketProxy.instruction.newOrderV3({
  146. owner: program.provider.wallet.publicKey,
  147. payer: usdcAccount,
  148. side: "buy",
  149. price,
  150. size,
  151. orderType: "postOnly",
  152. clientId: new BN(999),
  153. openOrdersAddressKey: openOrders,
  154. selfTradeBehavior: "abortTransaction",
  155. })
  156. );
  157. await provider.send(tx);
  158. });
  159. it("Cancels a bid on the orderbook", async () => {
  160. // Given.
  161. const beforeOoAccount = await OpenOrders.load(
  162. provider.connection,
  163. openOrders,
  164. DEX_PID
  165. );
  166. // When.
  167. const tx = new Transaction();
  168. tx.add(
  169. await marketProxy.instruction.cancelOrderByClientId(
  170. program.provider.wallet.publicKey,
  171. openOrders,
  172. new BN(999)
  173. )
  174. );
  175. await provider.send(tx);
  176. // Then.
  177. const afterOoAccount = await OpenOrders.load(
  178. provider.connection,
  179. openOrders,
  180. DEX_PID
  181. );
  182. assert.ok(beforeOoAccount.quoteTokenFree.eq(new BN(0)));
  183. assert.ok(beforeOoAccount.quoteTokenTotal.eq(usdcPosted));
  184. assert.ok(afterOoAccount.quoteTokenFree.eq(usdcPosted));
  185. assert.ok(afterOoAccount.quoteTokenTotal.eq(usdcPosted));
  186. });
  187. // Need to crank the cancel so that we can close later.
  188. it("Cranks the cancel transaction", async () => {
  189. // TODO: can do this in a single transaction if we covert the pubkey bytes
  190. // into a [u64; 4] array and sort. I'm lazy though.
  191. let eq = await marketProxy.market.loadEventQueue(provider.connection);
  192. while (eq.length > 0) {
  193. const tx = new Transaction();
  194. tx.add(
  195. marketProxy.market.makeConsumeEventsInstruction([eq[0].openOrders], 1)
  196. );
  197. await provider.send(tx);
  198. eq = await marketProxy.market.loadEventQueue(provider.connection);
  199. }
  200. });
  201. it("Settles funds on the orderbook", async () => {
  202. // Given.
  203. const beforeTokenAccount = await usdcClient.getAccountInfo(usdcAccount);
  204. // When.
  205. const tx = new Transaction();
  206. tx.add(
  207. await marketProxy.instruction.settleFunds(
  208. openOrders,
  209. provider.wallet.publicKey,
  210. tokenAccount,
  211. usdcAccount,
  212. referral
  213. )
  214. );
  215. await provider.send(tx);
  216. // Then.
  217. const afterTokenAccount = await usdcClient.getAccountInfo(usdcAccount);
  218. assert.ok(
  219. afterTokenAccount.amount.sub(beforeTokenAccount.amount).toNumber() ===
  220. usdcPosted.toNumber()
  221. );
  222. });
  223. it("Closes an open orders account", async () => {
  224. // Given.
  225. const beforeAccount = await program.provider.connection.getAccountInfo(
  226. program.provider.wallet.publicKey
  227. );
  228. // When.
  229. const tx = new Transaction();
  230. tx.add(
  231. marketProxy.instruction.closeOpenOrders(
  232. openOrders,
  233. provider.wallet.publicKey,
  234. provider.wallet.publicKey
  235. )
  236. );
  237. await provider.send(tx);
  238. // Then.
  239. const afterAccount = await program.provider.connection.getAccountInfo(
  240. program.provider.wallet.publicKey
  241. );
  242. const closedAccount = await program.provider.connection.getAccountInfo(
  243. openOrders
  244. );
  245. assert.ok(23352768 === afterAccount.lamports - beforeAccount.lamports);
  246. assert.ok(closedAccount === null);
  247. });
  248. });
  249. });
  250. // Dummy identity middleware used for testing.
  251. class Identity {
  252. initOpenOrders(ix) {
  253. this.proxy(ix);
  254. }
  255. newOrderV3(ix) {
  256. this.proxy(ix);
  257. }
  258. cancelOrderV2(ix) {
  259. this.proxy(ix);
  260. }
  261. cancelOrderByClientIdV2(ix) {
  262. this.proxy(ix);
  263. }
  264. settleFunds(ix) {
  265. this.proxy(ix);
  266. }
  267. closeOpenOrders(ix) {
  268. this.proxy(ix);
  269. }
  270. proxy(ix) {
  271. ix.keys = [
  272. { pubkey: SYSVAR_RENT_PUBKEY, isWritable: false, isSigner: false },
  273. ...ix.keys,
  274. ];
  275. }
  276. }