4

I've been messing around for a while with the @solana/web3.js browser bundle (https://docs.solana.com/developing/clients/javascript-api#bundle) and I'm having some difficulty sending a transaction. My current code utilizes the sendAndConfirmTransaction method, but I'm seeing the following error:

ReferenceError: Buffer is not defined
    at toBufferLE (browser.js:48:24)
    at bigIntLayout.encode (bigint.ts:30:19)
    at Structure.encode (Layout.js:1092:36)
    at encodeData (instruction.ts:32:15)
    at SystemProgram.transfer (system.ts:796:14)
    at sendTransaction ((index):88:36)
    at async Proxy.submitData ((index):65:9)

Below is the function I'm currently working with.

async function sendTransaction() {
  console.log('Sending transaction...')
  try {
    const keypair = await solanaWeb3.Keypair;
    let transaction = new solanaWeb3.Transaction();
    let connection = new solanaWeb3.Connection(solanaWeb3.clusterApiUrl("devnet"));

    transaction.add(
      solanaWeb3.SystemProgram.transfer({
        fromPubkey: keypair.publicKey,
        toPubkey: keypair.publicKey,
        lamports: solanaWeb3.LAMPORTS_PER_SOL,
      }),
    );
    solanaWeb3.sendAndConfirmTransaction(connection, transaction, [keypair]);
  } catch (error) {
    console.log(error)
  }
}

I've read that Buffer is not available in the latest builds of the web3.js library, but the only answers I've seen are related to Webpack updates. I'm assuming I'm missing a dependency? Any insight is greatly appreciated. Thanks!

1 Answer 1

2

Buffer is available in Node.js runtimes, but not in browsers.

Without knowing the exact shape of your build system I can only give general advice, which is that you'll need to install a Buffer polyfill like https://github.com/feross/buffer.

We're actively looking to eliminate Buffer from @solana/web3.js completely.

5
  • I'll give that a shot! What's the timeline on fully eliminating Buffer from the library? Commented Jan 3, 2023 at 16:31
  • 1
    I can't promise anything more than: Stay Tuned™. Commented Jan 9, 2023 at 2:07
  • Circling back to this, I'm not able to find a way to load Buffer using just inline HTML. From what I'm seeing it looks like I would have to use npm or yarn to install the Buffer polyfill. Am I officially stuck until Buffer is removed from @solana/web3.js? Commented Jan 14, 2023 at 18:09
  • You could try some abomination like this, @jeffersonbenson, but really the solution is to build your own polyfill. Relying on others' CDNs is extremely dangerous and can rely on loss of funds. jsbin.com/quzewiticu/1/edit?html,js,console Commented Jan 15, 2023 at 20:47
  • 1
    Good point there @steveluscher. Ultimately I think the answer is there currently is no solution utilizing just the browser bundle to send a transaction to a program. This does kinda shift my project's architecture, but definitely will be helpful in the long run :) Commented Jan 20, 2023 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.