solana web3.js事务参考:可以找到变量:缓冲区
我使用solana package @solana/web3.js在打字稿中遇到了一个错误。我已经开发了一个钱包应用程序,以生成新的键盘,建立连接,接收空调,获取我的公共关键的交易列表。但是,我坚持将交易发送给其他钱包。
我在下面运行代码,并且捕获方法记录了错误: [ReferenceError:找不到变量:buffer] 我的代码:
import {
clusterApiUrl,
Connection,
LAMPORTS_PER_SOL,
Keypair,
ParsedTransactionWithMeta,
Transaction,
SystemProgram,
sendAndConfirmTransaction,
PublicKey,
Signer,
} from '@solana/web3.js';
const [keypair, setKeypair] = useState<Keypair>();
const [keypair2, setKeypair2] = useState<Keypair>();
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const generateNewKeys = () => {
console.log('trying to get new keypair');
setKeypair(() => Keypair.generate());
setKeypair2(() => Keypair.generate());
console.log('Generated new keypair');
};
const sendTransaction = async () => {
console.log('initializing sending');
if (keypair && keypair2) {
try {
console.log('Attempting to send');
let transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: keypair.publicKey,
toPubkey: keypair2.publicKey,
lamports: amount * LAMPORTS_PER_SOL,
}),
);
console.log('Created transaction');
const signer: Signer = keypair;
await sendAndConfirmTransaction(connection, transaction, [signer]);
updateBalance();
updateTransactions();
} catch (e) {
console.log(e);
}
}
};
运行sendtransaction函数时,它会记录“尝试发送”,而不是“创建的事务”,它指向创建变量“交易”的问题。
老实说,我被卡住了,我将代码与其他人进行了比较,尽管没有差异,但它行不通。
I have experienced a bug in Typescript with Solana package @solana/web3.js. I have developed a wallet application to the point where I generate new keypair, establish connection, receive an airdrop, get list of transacations for my publicKey. However, i am stuck at sending a transaction to other wallets.
I run the code below and the catch method logs the error:
[ReferenceError: Can't find variable: Buffer]
My code:
import {
clusterApiUrl,
Connection,
LAMPORTS_PER_SOL,
Keypair,
ParsedTransactionWithMeta,
Transaction,
SystemProgram,
sendAndConfirmTransaction,
PublicKey,
Signer,
} from '@solana/web3.js';
const [keypair, setKeypair] = useState<Keypair>();
const [keypair2, setKeypair2] = useState<Keypair>();
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const generateNewKeys = () => {
console.log('trying to get new keypair');
setKeypair(() => Keypair.generate());
setKeypair2(() => Keypair.generate());
console.log('Generated new keypair');
};
const sendTransaction = async () => {
console.log('initializing sending');
if (keypair && keypair2) {
try {
console.log('Attempting to send');
let transaction = new Transaction().add(
SystemProgram.transfer({
fromPubkey: keypair.publicKey,
toPubkey: keypair2.publicKey,
lamports: amount * LAMPORTS_PER_SOL,
}),
);
console.log('Created transaction');
const signer: Signer = keypair;
await sendAndConfirmTransaction(connection, transaction, [signer]);
updateBalance();
updateTransactions();
} catch (e) {
console.log(e);
}
}
};
When running the sendTransaction function it logs "Attempting to send" but not the "Created transaction" which points to the problem with creating the variable "transaction".
Honestly i'm stuck, I have compared my code to others, and despite no differences it doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这是一个使用Create React App(CRA)和WebPack 5进行的项目。WebPack5不会自动执行Node.js API Polyfills,因此您需要手动执行此操作。
实际上,这意味着:
还需要覆盖您的WebPack配置,
这是用CRA和WebPack 5 https://github.com/bonfida/serverless-merch/blob/master/ui/config-overrides.js
I assume it's a project made using Create React App (CRA) and Webpack 5. Webpack 5 does not automatically perform Node.js API polyfills as it used to, so you need to manually do it.
In practice it means:
Also need to override your Webpack config
Here is an example of Solana dApp made with CRA and Webpack 5 https://github.com/Bonfida/serverless-merch/blob/master/ui/config-overrides.js