我正在尝试发送1个solana
我有这个代码
但它抛出了一个错误:Uncaught(在promise中(TypeError:y.pubkey.toBase58不是函数交易时间:ts:264:33
帮助解决此错误
async function connectWallet() {
let provider = null
if ("solana" in window) {
provider = window.solana;
if (provider.isPhantom) {
console.log('provider', provider)
}
} else {
window.open("https://phantom.app/", "_blank");
}
publicKey = null
if (provider != null) {
try {
const resp = await window.solana.connect();
publicKey = resp.publicKey.toString();
} catch (err) {
// { code: 4001, message: 'User rejected the request.' }
console.log(err)
}
}
if (publicKey != null) {
const message = `Sign below to authenticate`;
const encodedMessage = new TextEncoder().encode(message);
const signedMessage = await window.solana.request({
method: "signMessage",
params: {
message: encodedMessage,
display: "utf-8",
},
});
var recieverWallet = new solanaWeb3.PublicKey("4iSD5Q6AnyhRHu6Uz4u1KAzXh3TwNwwQshEGhZbEXUTw");
const connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl("mainnet-beta"));
var transaction = new solanaWeb3.Transaction().add(
solanaWeb3.SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: recieverWallet,
lamports: solanaWeb3.LAMPORTS_PER_SOL
}),
);
transaction.feePayer = publicKey;
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;
if(transaction) {
console.log("Txn created successfully");
}
let signed = await provider.signTransaction(transaction);
let signature = await connection.sendRawTransaction(signed.serialize());
await connection.confirmTransaction(signature);
}
}
我认为问题出在这一行,let signed=wait provider.signTransaction(transaction(;
问题是publicKey
变量是字符串,而不是PublicKey
,因此在尝试编译事务时,TypeError: y.pubkey.toBase58 is not a function at transaction.ts:264:33
编译失败。
相反,你可以做:
publicKey = resp.publicKey;