solana web3.js事务参考:可以找到变量:缓冲区

发布于 2025-02-12 22:36:30 字数 1666 浏览 0 评论 0原文

我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南城追梦 2025-02-19 22:36:30

我认为这是一个使用Create React App(CRA)和WebPack 5进行的项目。WebPack5不会自动执行Node.js API Polyfills,因此您需要手动执行此操作。

实际上,这意味着:

yarn add -D buffer

还需要覆盖您的WebPack配置,

const webpack = require("webpack");

module.exports = function override(webpackConfig) {
  // ..
  webpackConfig.resolve.fallback = {
    buffer: require.resolve("buffer"),
  };

  webpackConfig.plugins = [
    ...webpackConfig.plugins,
    new webpack.ProvidePlugin({
      Buffer: ["buffer", "Buffer"],
    }),
  ];
  return webpackConfig;
};

这是用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:

yarn add -D buffer

Also need to override your Webpack config

const webpack = require("webpack");

module.exports = function override(webpackConfig) {
  // ..
  webpackConfig.resolve.fallback = {
    buffer: require.resolve("buffer"),
  };

  webpackConfig.plugins = [
    ...webpackConfig.plugins,
    new webpack.ProvidePlugin({
      Buffer: ["buffer", "Buffer"],
    }),
  ];
  return webpackConfig;
};

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文