如何修改我的代码以发送自定义 SPL 令牌而不是常规 SOL?



我正在建立一个网站,人们可以在其中登录他们的幻影钱包,然后通过单击一个按钮,他们将一定数量的自定义令牌发送到一个钱包。

下面显示的代码适用于 SOL,我想让它与我们的自定义 SPL 令牌一起使用,我有令牌的铸币厂地址,但我找不到任何方法使其工作。谁能帮我?

async function transferSOL(toSend) {
// Detecing and storing the phantom wallet of the user (creator in this case)
var provider = await getProvider();
console.log("Public key of the emitter: ",provider.publicKey.toString());

// Establishing connection
var connection = new web3.Connection(
"https://api.mainnet-beta.solana.com/"
);

// I have hardcoded my secondary wallet address here. You can take this address either from user input or your DB or wherever
var recieverWallet = new web3.PublicKey("address of the wallet recieving the custom SPL Token");

var transaction = new web3.Transaction().add(
web3.SystemProgram.transfer({
fromPubkey: provider.publicKey,
toPubkey: recieverWallet,
lamports: (web3.LAMPORTS_PER_SOL)*toSend //Investing 1 SOL. Remember 1 Lamport = 10^-9 SOL.
}),
);

// Setting the variables for the transaction
transaction.feePayer = await provider.publicKey;
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;

// Request creator to sign the transaction (allow the transaction)
let signed = await provider.signTransaction(transaction);
// The signature is generated
let signature = await connection.sendRawTransaction(signed.serialize());
// Confirm whether the transaction went through or not
console.log(await connection.confirmTransaction(signature));

//Signature chhap diya idhar
console.log("Signature: ", signature);
}

我想指定人们将使用 phantom,我无法访问他们的私钥(因为我在互联网上找到的所有答案都需要它)

你非常接近!您只需要将web3.SystemProgram.transfer指令替换为传输 SPL 令牌的指令,并引用正确的帐户。 Solana Cookbook中有一个例子涵盖了这种情况:https://solanacookbook.com/references/token.html#transfer-token

您可以在anchorspl-token的帮助下执行此操作,它用于处理 Solana 上的自定义令牌。 这是一个自定义传递函数。您将需要令牌的铸币地址,将从中获取令牌的钱包(当用户连接钱包时,您会在前端获得该地址。可以利用solana-web3),来寻址和金额。

import * as splToken from "@solana/spl-token";
import { web3, Wallet } from "@project-serum/anchor";

async function transfer(tokenMintAddress: string, wallet: Wallet, to: string, connection: web3.Connection, amount: number) {

const mintPublicKey = new web3.PublicKey(tokenMintAddress);  
const {TOKEN_PROGRAM_ID} = splToken

const fromTokenAccount = await splToken.getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
mintPublicKey,
wallet.publicKey
);

const destPublicKey = new web3.PublicKey(to);

// Get the derived address of the destination wallet which will hold the custom token
const associatedDestinationTokenAddr = await splToken.getOrCreateAssociatedTokenAccount(
connection,
wallet.payer,
mintPublicKey,
destPublicKey
);


const receiverAccount = await connection.getAccountInfo(associatedDestinationTokenAddr.address);

const instructions: web3.TransactionInstruction[] = [];  


instructions.push(
splToken.createTransferInstruction(
fromTokenAccount.address,
associatedDestinationTokenAddr.address,
wallet.publicKey,
amount,
[],
TOKEN_PROGRAM_ID
)
);

const transaction = new web3.Transaction().add(...instructions);
transaction.feePayer = wallet.publicKey;
transaction.recentBlockhash = (await connection.getRecentBlockhash()).blockhash;

const transactionSignature = await connection.sendRawTransaction(
transaction.serialize(),
{ skipPreflight: true }
);

await connection.confirmTransaction(transactionSignature);
}

相关内容

  • 没有找到相关文章

最新更新