0

大家晚上好,我正在建立一个网站,人们可以登录到他们的虚拟钱包,然后通过单击一个按钮,他们会将一定数量的我们的自定义令牌发送到一个钱包。

下面显示的代码与 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);
        }

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

4

1 回答 1

0

你很亲近!您只需将web3.SystemProgram.transfer指令替换为转移 SPL 代币的指令,并引用正确的帐户。Solana Cookbook 中有一个示例涵盖了这种情况:https ://solanacookbook.com/references/token.html#transfer-token

于 2022-01-31T13:11:05.987 回答