escrow.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { assert } from 'node:console';
  2. import { describe, test } from 'node:test';
  3. import { IdlV01, rootNodeFromAnchor } from '@kinobi-so/nodes-from-anchor';
  4. import { renderJavaScriptVisitor } from '@kinobi-so/renderers';
  5. import { createSolanaRpc } from '@solana/web3.js';
  6. // From https://github.com/kinobi-so/kinobi?tab=readme-ov-file#from-program-to-kinobi
  7. import { createFromRoot } from 'kinobi';
  8. import anchorIdl from '../target/idl/escrow.json';
  9. const rootNode = rootNodeFromAnchor(anchorIdl as IdlV01);
  10. const kinobi = createFromRoot(rootNode);
  11. const log = console.log;
  12. log(kinobi);
  13. // that the first argument of the renderJavaScriptVisitor function should be a folder not a file.
  14. const visitor = renderJavaScriptVisitor('generated-client', {
  15. formatCode: true,
  16. });
  17. kinobi.accept(visitor);
  18. // import { TOKEN_2022_PROGRAM_ID, type TOKEN_PROGRAM_ID, getAssociatedTokenAddressSync } from '@solana/spl-token';
  19. // import { LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js';
  20. // import type { Escrow } from '../target/types/escrow';
  21. const CLUSTERS = {
  22. devnet: 'https://api.devnet.solana.com',
  23. testnet: 'https://api.testnet.solana.com',
  24. mainnet: 'https://api.mainnet-beta.solana.com',
  25. localnet: 'http://127.0.0.1:8899',
  26. };
  27. const CLUSTER_NAMES = Object.keys(CLUSTERS);
  28. const connect = (clusterNameOrURL: string) => {
  29. if (CLUSTER_NAMES.includes(clusterNameOrURL)) {
  30. return createSolanaRpc(clusterNameOrURL);
  31. }
  32. return createSolanaRpc(clusterNameOrURL);
  33. };
  34. const rpc = connect('localnet');
  35. // import * as Escrow from 'escrow-generated.ts'
  36. // // import { confirmTransaction, createAccountsMintsAndTokenAccounts, makeKeypairs } from '@solana-developers/helpers';
  37. // // Work on both Token Program and new Token Extensions Program
  38. // // const TOKEN_PROGRAM: typeof TOKEN_2022_PROGRAM_ID | typeof TOKEN_PROGRAM_ID = TOKEN_2022_PROGRAM_ID;
  39. // const SECONDS = 1000;
  40. // // Tests must complete within half this time otherwise
  41. // // they are marked as slow. Since Anchor involves a little
  42. // // network IO, these tests usually take about 15 seconds.
  43. // const ANCHOR_SLOW_TEST_THRESHOLD = 40 * SECONDS;
  44. // function getRandomBigInt(max = 256) {
  45. // return BigInt(Math.floor(Math.random() * max));
  46. // }
  47. describe('escrow', async () => {
  48. test('escrow', async () => {
  49. assert(true);
  50. });
  51. // // See https://github.com/coral-xyz/anchor/issues/3122
  52. // const payer = user;
  53. // const connection = provider.connection;
  54. // const program = anchor.workspace.Escrow as Program<Escrow>;
  55. // // We're going to reuse these accounts across multiple tests
  56. // const accounts: Record<string, PublicKey> = {
  57. // tokenProgram: TOKEN_PROGRAM,
  58. // };
  59. // let alice: anchor.web3.Keypair;
  60. // let bob: anchor.web3.Keypair;
  61. // let tokenMintA: anchor.web3.Keypair;
  62. // let tokenMintB: anchor.web3.Keypair;
  63. // [alice, bob, tokenMintA, tokenMintB] = makeKeypairs(4);
  64. // const tokenAOfferedAmount = new BN(1_000_000);
  65. // const tokenBWantedAmount = new BN(1_000_000);
  66. // before('Creates Alice and Bob accounts, 2 token mints, and associated token accounts for both tokens for both users', async () => {
  67. // const usersMintsAndTokenAccounts = await createAccountsMintsAndTokenAccounts(
  68. // [
  69. // // Alice's token balances
  70. // [
  71. // // 1_000_000_000 of token A
  72. // 1_000_000_000,
  73. // // 0 of token B
  74. // 0,
  75. // ],
  76. // // Bob's token balances
  77. // [
  78. // // 0 of token A
  79. // 0,
  80. // // 1_000_000_000 of token B
  81. // 1_000_000_000,
  82. // ],
  83. // ],
  84. // 1 * LAMPORTS_PER_SOL,
  85. // connection,
  86. // payer,
  87. // );
  88. // // Alice will be the maker (creator) of the offer
  89. // // Bob will be the taker (acceptor) of the offer
  90. // const users = usersMintsAndTokenAccounts.users;
  91. // alice = users[0];
  92. // bob = users[1];
  93. // // tokenMintA represents the token Alice is offering
  94. // // tokenMintB represents the token Alice wants in return
  95. // const mints = usersMintsAndTokenAccounts.mints;
  96. // tokenMintA = mints[0];
  97. // tokenMintB = mints[1];
  98. // const tokenAccounts = usersMintsAndTokenAccounts.tokenAccounts;
  99. // // aliceTokenAccountA is Alice's account for tokenA (the token she's offering)
  100. // // aliceTokenAccountB is Alice's account for tokenB (the token she wants)
  101. // const aliceTokenAccountA = tokenAccounts[0][0];
  102. // const aliceTokenAccountB = tokenAccounts[0][1];
  103. // // bobTokenAccountA is Bob's account for tokenA (the token Alice is offering)
  104. // // bobTokenAccountB is Bob's account for tokenB (the token Alice wants)
  105. // const bobTokenAccountA = tokenAccounts[1][0];
  106. // const bobTokenAccountB = tokenAccounts[1][1];
  107. // // Save the accounts for later use
  108. // accounts.maker = alice.publicKey;
  109. // accounts.taker = bob.publicKey;
  110. // accounts.tokenMintA = tokenMintA.publicKey;
  111. // accounts.makerTokenAccountA = aliceTokenAccountA;
  112. // accounts.takerTokenAccountA = bobTokenAccountA;
  113. // accounts.tokenMintB = tokenMintB.publicKey;
  114. // accounts.makerTokenAccountB = aliceTokenAccountB;
  115. // accounts.takerTokenAccountB = bobTokenAccountB;
  116. // });
  117. // it('Puts the tokens Alice offers into the vault when Alice makes an offer', async () => {
  118. // // Pick a random ID for the offer we'll make
  119. // const offerId = getRandomBigNumber();
  120. // // Then determine the account addresses we'll use for the offer and the vault
  121. // const offer = PublicKey.findProgramAddressSync(
  122. // [Buffer.from('offer'), accounts.maker.toBuffer(), offerId.toArrayLike(Buffer, 'le', 8)],
  123. // program.programId,
  124. // )[0];
  125. // const vault = getAssociatedTokenAddressSync(accounts.tokenMintA, offer, true, TOKEN_PROGRAM);
  126. // accounts.offer = offer;
  127. // accounts.vault = vault;
  128. // const transactionSignature = await program.methods
  129. // .makeOffer(offerId, tokenAOfferedAmount, tokenBWantedAmount)
  130. // .accounts({ ...accounts })
  131. // .signers([alice])
  132. // .rpc();
  133. // await confirmTransaction(connection, transactionSignature);
  134. // // Check our vault contains the tokens offered
  135. // const vaultBalanceResponse = await connection.getTokenAccountBalance(vault);
  136. // const vaultBalance = new BN(vaultBalanceResponse.value.amount);
  137. // assert(vaultBalance.eq(tokenAOfferedAmount));
  138. // // Check our Offer account contains the correct data
  139. // const offerAccount = await program.account.offer.fetch(offer);
  140. // assert(offerAccount.maker.equals(alice.publicKey));
  141. // assert(offerAccount.tokenMintA.equals(accounts.tokenMintA));
  142. // assert(offerAccount.tokenMintB.equals(accounts.tokenMintB));
  143. // assert(offerAccount.tokenBWantedAmount.eq(tokenBWantedAmount));
  144. // }).slow(ANCHOR_SLOW_TEST_THRESHOLD);
  145. // it("Puts the tokens from the vault into Bob's account, and gives Alice Bob's tokens, when Bob takes an offer", async () => {
  146. // const transactionSignature = await program.methods
  147. // .takeOffer()
  148. // .accounts({ ...accounts })
  149. // .signers([bob])
  150. // .rpc();
  151. // await confirmTransaction(connection, transactionSignature);
  152. // // Check the offered tokens are now in Bob's account
  153. // // (note: there is no before balance as Bob didn't have any offered tokens before the transaction)
  154. // const bobTokenAccountBalanceAfterResponse = await connection.getTokenAccountBalance(accounts.takerTokenAccountA);
  155. // const bobTokenAccountBalanceAfter = new BN(bobTokenAccountBalanceAfterResponse.value.amount);
  156. // assert(bobTokenAccountBalanceAfter.eq(tokenAOfferedAmount));
  157. // // Check the wanted tokens are now in Alice's account
  158. // // (note: there is no before balance as Alice didn't have any wanted tokens before the transaction)
  159. // const aliceTokenAccountBalanceAfterResponse = await connection.getTokenAccountBalance(accounts.makerTokenAccountB);
  160. // const aliceTokenAccountBalanceAfter = new BN(aliceTokenAccountBalanceAfterResponse.value.amount);
  161. // assert(aliceTokenAccountBalanceAfter.eq(tokenBWantedAmount));
  162. // }).slow(ANCHOR_SLOW_TEST_THRESHOLD);
  163. });