2

I'm trying to send a SPL Token but when I execute the code below it creates an unknown token and sends it, It does not recognize the token address / programID. Where should I pass the token address?

    const mint = await createMint(
      connection,
      fromAddress.addressKeypair,
      fromAddress.addressKeypair.publicKey,
      null,
      9,
      undefined,
      {},
      "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID
    );
    console.log(mint);
    // Get the token account of the fromWallet address, and if it does not exist, create it
    const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
      connection,
      fromAddress.addressKeypair,
      mint,
      fromAddress.addressKeypair.publicKey,
      "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID

    );

    // Get the token account of the toWallet address, and if it does not exist, create it
    const toTokenAccount = await getOrCreateAssociatedTokenAccount(
      connection,
      fromAddress.addressKeypair,
      mint,
      toaddress,
      "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID

    );

    // Mint 1 new token to the "fromTokenAccount" account we just created
    let signature = await mintTo(
      connection,
      fromAddress.addressKeypair,
      mint,
      fromTokenAccount.address,
      fromAddress.addressKeypair.publicKey,
      1000000000,
      undefined,
      {},
      "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID
    );
    console.log("mint tx:", signature);

    // Transfer the new token to the "toTokenAccount" we just created
    signature = await transfer(
      connection,
      fromAddress.addressKeypair,
      fromTokenAccount.address,
      toTokenAccount.address,
      fromAddress.addressKeypair.publicKey,
      amount,
      undefined,
      {},
      "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID
    ); 
    console.log("sig: ", signature);

    response["result"] = 1;
    response["data"] = { txid: signature };
    response["error"] = null;
    return console.log(response);
  } catch (error) {
    response["result"] = 0;
    response["data"] = { txid: null };
    response["error"] = error;
    return console.log(response);
  }

1 Answer 1

1

I assume you have copied this code from an example.

You don't need createMint and mintTo parts because you already have the token and you possess enough amount of token to transfer (I assume).

replace mint variable with the address of the token you would like to transfer.

const mint = new PublicKey("<address of your token>");

// Get the token account of the fromWallet address, and if it does not exist, create it
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  fromAddress.addressKeypair,
  mint,
  fromAddress.addressKeypair.publicKey,
  "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID

);

// Get the token account of the toWallet address, and if it does not exist, create it
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
  connection,
  fromAddress.addressKeypair,
  mint,
  toaddress,
  "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID

);

// Transfer the token to the "toTokenAccount" we just created
signature = await transfer(
  connection,
  fromAddress.addressKeypair,
  fromTokenAccount.address,
  toTokenAccount.address,
  fromAddress.addressKeypair.publicKey,
  amount,
  undefined,
  {},
  "7i5KKsX2weiTkry7jA4ZwSuXGhs5eJBEjY8vVxR4pfRx".TOKEN_PROGRAM_ID
); 
console.log("sig: ", signature);

response["result"] = 1;
response["data"] = { txid: signature };
response["error"] = null;
return console.log(response);
} catch (error) {
response["result"] = 0;
response["data"] = { txid: null };
response["error"] = error;
return console.log(response);
}

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.