1
    import {
  PublicKey,
  Keypair,
  Transaction,
  Connection,
  SystemProgram,
} from "@solana/web3.js";
import {
  TokenSwap,
  TOKEN_SWAP_PROGRAM_ID,
  TokenSwapLayout,
  CurveType,
} from "@solana/spl-token-swap";
import createAccount from "../createAccountToken";
import createTokenFunction from "../createTokenFunction";
import GetAccount from "../getAssociatedTokenAddress";
import { tokenAccountPool, tokenSwapStateAccount } from "../../utils/constants";
import { getAccount, TOKEN_PROGRAM_ID } from "@solana/spl-token";

const createSwap = async (
  connection: Connection,
  payer: PublicKey,
  tokenA: PublicKey,
  tokenB: PublicKey
) => {
  // number 1 -> create account that cointain information on swap
  const transaction = new Transaction();
  const tokenSwapAccount = Keypair.generate();
  const rent = await TokenSwap.getMinBalanceRentForExemptTokenSwap(connection);

  const tokenSwapStateAccountInstruction = await SystemProgram.createAccount({
    newAccountPubkey: tokenSwapAccount.publicKey,
    fromPubkey: payer,
    lamports: rent,
    space: TokenSwapLayout.span,
    programId: TOKEN_SWAP_PROGRAM_ID,
  });

  transaction.add(tokenSwapStateAccountInstruction);

  /// ==================================

  // number 2 -> find PDA

  const [swapAuthority, bump] = await PublicKey.findProgramAddress(
    [tokenSwapAccount.publicKey.toBuffer()],

    TOKEN_SWAP_PROGRAM_ID
  );

  // number 3 -> create token accounts for Token A and Token B

  const tokenAAccount = await createAccount(swapAuthority, tokenA, true);
  const tokenBAccount = await createAccount(swapAuthority, tokenB, true);

  transaction.add(tokenAAccount, tokenBAccount);

  // number 4 -> create token that rappresenting receipt of deposit (LP token)
  const { transaction: poolTokenMintTX, mintAccountKeypair: poolTokenMint } =
    await createTokenFunction(
      "non serve",
      connection,
      payer,
      swapAuthority,
      null,
      2
    );

  transaction.add(poolTokenMintTX);

  // number 5 -> create Account that contain miter token before send to user
  const accountPoolTokenMint = await createAccount(
    payer,
    poolTokenMint.publicKey
  );

  transaction.add(accountPoolTokenMint);

  // number 6 -> create Pool Token Fee Account (in this case is a specific serum account (still serum  in this moment? ????))

  const feeOwner = new PublicKey(
    "HfoTxFR1Tm6kGmWgYWD6J7YHVy1UwqSULUGVLXkJqaKN"
  );

  const poolTokenFeeAccount = await createAccount(
    feeOwner,
    poolTokenMint.publicKey
  );

  transaction.add(poolTokenFeeAccount);

  //const _tokenSwapAccount = await getAccount(connection,poolTokenMint.publicKey)

  const poolAccountTokenA = await GetAccount(connection, swapAuthority, tokenA);

  const poolAccountTokenB = await GetAccount(connection, swapAuthority, tokenB);

  const accountFeeOwner = await GetAccount(
    connection,
    feeOwner,
    poolTokenMint.publicKey
  );
  if (!poolAccountTokenA || !poolAccountTokenB || !accountFeeOwner) {
    return;
  }
  const createSwapInstruction = TokenSwap.createInitSwapInstruction(
    tokenSwapAccount.publicKey, **** error *******
    swapAuthority,
    poolAccountTokenA,
    poolAccountTokenB,
    poolTokenMint.publicKey,
    accountFeeOwner,
    poolTokenMint.publicKey,
    TOKEN_PROGRAM_ID, // Token Program ID
    TOKEN_SWAP_PROGRAM_ID, // Token Swap Program ID
    0, // Trade fee numerator
    10000, // Trade fee denominator
    5, // Owner trade fee numerator
    10000, // Owner trade fee denominator
    0, // Owner withdraw fee numerator
    0, // Owner withdraw fee denominator
    20, // Host fee numerator
    100, // Host fee denominator
    CurveType.ConstantProduct // Curve type
  );

  transaction.add(createSwapInstruction);

  return transaction;
};

export default createSwap;

ERROR _ L'argomento di tipo 'PublicKey' non è assegnabile al parametro di tipo 'Account'. Nel tipo 'PublicKey' mancano le proprietà seguenti del tipo 'Account': publicKey,secretKey

translate

The argument of type 'PublicKey' is not assignable to the parameter of type 'Account'. Type 'PublicKey' is missing the following properties of type 'Account': publicKey,secretKey

2
  • Can you add the line that's triggering the error? It should be a pretty easy fix after that, since you're just passing in a PublicKey instead of an Account somewhere
    – Jon C
    Commented Dec 20, 2022 at 12:38
  • this line take me the error : << tokenSwapAccount.publicKey, **** error *******>> In tutorial pass one publicKey , but in my code require Account.
    – Leluk
    Commented Dec 20, 2022 at 19:37

1 Answer 1

1

Ah great, thanks for the extra info! You need to change this to:

const createSwapInstruction = TokenSwap.createInitSwapInstruction(
    tokenSwapAccount,
    swapAuthority,
    poolAccountTokenA,
    poolAccountTokenB,
    poolTokenMint.publicKey,
    accountFeeOwner,
    poolTokenMint.publicKey,
    TOKEN_PROGRAM_ID, // Token Program ID
    TOKEN_SWAP_PROGRAM_ID, // Token Swap Program ID
    0, // Trade fee numerator
    10000, // Trade fee denominator
    5, // Owner trade fee numerator
    10000, // Owner trade fee denominator
    0, // Owner withdraw fee numerator
    0, // Owner withdraw fee denominator
    20, // Host fee numerator
    100, // Host fee denominator
    CurveType.ConstantProduct // Curve type
  );

Since createInitSwapInstruction takes an Account for the first parameter, not a PublicKey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.