瀏覽代碼

examples: Use @solana/spl-token in ido-pool test (#636)

skrrb 4 年之前
父節點
當前提交
142728d271
共有 2 個文件被更改,包括 37 次插入121 次删除
  1. 21 19
      examples/ido-pool/tests/ido-pool.js
  2. 16 102
      examples/ido-pool/tests/utils/index.js

+ 21 - 19
examples/ido-pool/tests/ido-pool.js

@@ -6,7 +6,6 @@ const {
   getTokenAccount,
   createMint,
   createTokenAccount,
-  mintToAccount,
 } = require("./utils");
 
 describe("ido-pool", () => {
@@ -22,14 +21,18 @@ describe("ido-pool", () => {
 
   // These are all of the variables we assume exist in the world already and
   // are available to the client.
+  let usdcMintToken = null;
   let usdcMint = null;
+  let watermelonMintToken = null;
   let watermelonMint = null;
   let creatorUsdc = null;
   let creatorWatermelon = null;
 
   it("Initializes the state-of-the-world", async () => {
-    usdcMint = await createMint(provider);
-    watermelonMint = await createMint(provider);
+    usdcMintToken = await createMint(provider);
+    watermelonMintToken = await createMint(provider);
+    usdcMint = usdcMintToken.publicKey;
+    watermelonMint = watermelonMintToken.publicKey;
     creatorUsdc = await createTokenAccount(
       provider,
       usdcMint,
@@ -41,12 +44,11 @@ describe("ido-pool", () => {
       provider.wallet.publicKey
     );
     // Mint Watermelon tokens the will be distributed from the IDO pool.
-    await mintToAccount(
-      provider,
-      watermelonMint,
+    await watermelonMintToken.mintTo(
       creatorWatermelon,
-      watermelonIdoAmount,
-      provider.wallet.publicKey
+      provider.wallet.publicKey,
+      [],
+      watermelonIdoAmount.toString(),
     );
     creator_watermelon_account = await getTokenAccount(
       provider,
@@ -58,6 +60,7 @@ describe("ido-pool", () => {
   // These are all variables the client will have to create to initialize the
   // IDO pool
   let poolSigner = null;
+  let redeemableMintToken = null;
   let redeemableMint = null;
   let poolWatermelon = null;
   let poolUsdc = null;
@@ -77,7 +80,8 @@ describe("ido-pool", () => {
 
     // Pool doesn't need a Redeemable SPL token account because it only
     // burns and mints redeemable tokens, it never stores them.
-    redeemableMint = await createMint(provider, poolSigner);
+    redeemableMintToken = await createMint(provider, poolSigner);
+    redeemableMint = redeemableMintToken.publicKey;
     poolWatermelon = await createTokenAccount(
       provider,
       watermelonMint,
@@ -145,12 +149,11 @@ describe("ido-pool", () => {
       usdcMint,
       provider.wallet.publicKey
     );
-    await mintToAccount(
-      provider,
-      usdcMint,
+    await usdcMintToken.mintTo(
       userUsdc,
-      firstDeposit,
-      provider.wallet.publicKey
+      provider.wallet.publicKey,
+      [],
+      firstDeposit.toString(),
     );
     userRedeemable = await createTokenAccount(
       provider,
@@ -191,12 +194,11 @@ describe("ido-pool", () => {
       usdcMint,
       provider.wallet.publicKey
     );
-    await mintToAccount(
-      provider,
-      usdcMint,
+    await usdcMintToken.mintTo(
       secondUserUsdc,
-      secondDeposit,
-      provider.wallet.publicKey
+      provider.wallet.publicKey,
+      [],
+      secondDeposit.toString(),
     );
     secondUserRedeemable = await createTokenAccount(
       provider,

+ 16 - 102
examples/ido-pool/tests/utils/index.js

@@ -1,5 +1,4 @@
-// TODO: use the `@solana/spl-token` package instead of utils here.
-
+const spl = require("@solana/spl-token");
 const anchor = require("@project-serum/anchor");
 const serumCmn = require("@project-serum/common");
 const TokenInstructions = require("@project-serum/serum").TokenInstructions;
@@ -23,117 +22,32 @@ async function createMint(provider, authority) {
   if (authority === undefined) {
     authority = provider.wallet.publicKey;
   }
-  const mint = anchor.web3.Keypair.generate();
-  const instructions = await createMintInstructions(
-    provider,
+  const mint = await spl.Token.createMint(
+    provider.connection,
+    provider.wallet.payer,
     authority,
-    mint.publicKey
+    null,
+    6,
+    TOKEN_PROGRAM_ID
   );
-
-  const tx = new anchor.web3.Transaction();
-  tx.add(...instructions);
-
-  await provider.send(tx, [mint]);
-
-  return mint.publicKey;
-}
-
-async function createMintInstructions(provider, authority, mint) {
-  let instructions = [
-    anchor.web3.SystemProgram.createAccount({
-      fromPubkey: provider.wallet.publicKey,
-      newAccountPubkey: mint,
-      space: 82,
-      lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
-      programId: TOKEN_PROGRAM_ID,
-    }),
-    TokenInstructions.initializeMint({
-      mint,
-      decimals: 6,
-      mintAuthority: authority,
-    }),
-  ];
-  return instructions;
+  return mint;
 }
 
 async function createTokenAccount(provider, mint, owner) {
-  const vault = anchor.web3.Keypair.generate();
-  const tx = new anchor.web3.Transaction();
-  tx.add(
-    ...(await createTokenAccountInstrs(provider, vault.publicKey, mint, owner))
-  );
-  await provider.send(tx, [vault]);
-  return vault.publicKey;
-}
-
-async function createTokenAccountInstrs(
-  provider,
-  newAccountPubkey,
-  mint,
-  owner,
-  lamports
-) {
-  if (lamports === undefined) {
-    lamports = await provider.connection.getMinimumBalanceForRentExemption(165);
-  }
-  return [
-    anchor.web3.SystemProgram.createAccount({
-      fromPubkey: provider.wallet.publicKey,
-      newAccountPubkey,
-      space: 165,
-      lamports,
-      programId: TOKEN_PROGRAM_ID,
-    }),
-    TokenInstructions.initializeAccount({
-      account: newAccountPubkey,
-      mint,
-      owner,
-    }),
-  ];
-}
-
-async function mintToAccount(
-  provider,
-  mint,
-  destination,
-  amount,
-  mintAuthority
-) {
-  // mint authority is the provider
-  const tx = new anchor.web3.Transaction();
-  tx.add(
-    ...(await createMintToAccountInstrs(
-      mint,
-      destination,
-      amount,
-      mintAuthority
-    ))
+  const token = new spl.Token(
+    provider.connection,
+    mint,
+    TOKEN_PROGRAM_ID,
+    provider.wallet.payer
   );
-  await provider.send(tx, []);
-  return;
-}
-
-async function createMintToAccountInstrs(
-  mint,
-  destination,
-  amount,
-  mintAuthority
-) {
-  return [
-    TokenInstructions.mintTo({
-      mint,
-      destination: destination,
-      amount: amount,
-      mintAuthority: mintAuthority,
-    }),
-  ];
+  let vault = await token.createAccount(owner);
+  return vault;
 }
 
 module.exports = {
   TOKEN_PROGRAM_ID,
   sleep,
   getTokenAccount,
-  createMint,
   createTokenAccount,
-  mintToAccount,
+  createMint,
 };