开发者问题收集

幻影钱包中出现“TypeError:x.pubkey.toBase58 不是函数”...使用 JavaScript 使用@solana/web3.js 将 sol 转移到另一个帐户

2022-01-24
3562
let transaction = new solanalib.Transaction().add(
  solanalib.SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: 'GJ7bZskjGFqph51T88W2E1A1TeT1YVuuFM8atQAtVhSz',
    lamports: solanalib.LAMPORTS_PER_SOL,
  }),
);
transaction.feePayer = publicKey;
let blockhashObj = await connection.getRecentBlockhash();
transaction.recentBlockhash = await blockhashObj.blockhash;
if (transaction) {
  console.log('Txn created successfully');
} else {
  console.log('Sorry');
}

const signedTransaction = await window.solana.signTransaction(transaction);
const signature = await connection.sendRawTransaction(signedTransaction.serialize());
console.log('Signature: ', signature);
1个回答

我刚刚设法解决了这个问题。问题是你传递了一个字符串作为 toPubKey - 应该是 PublicKey 实例。试试这个:

let transaction = new solanalib.Transaction().add(
  solanalib.SystemProgram.transfer({
    fromPubkey: publicKey,
    toPubkey: new solanalib.PublicKey('GJ7bZskjGFqph51T88W2E1A1TeT1YVuuFM8atQAtVhSz'),
    lamports: solanalib.LAMPORTS_PER_SOL,
  })
);
gal liberman
2022-01-30