Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deno Support for Web3.js API #1120

Closed
zicklag opened this issue Nov 28, 2021 · 12 comments
Closed

Deno Support for Web3.js API #1120

zicklag opened this issue Nov 28, 2021 · 12 comments

Comments

@zicklag
Copy link

zicklag commented Nov 28, 2021

Problem

I would like to use Deno to interface with the Solana chain, but the web3.js API cannot import properly into deno.

Proposed Solution

I'm not 100% sure the best strategy for supporting Deno yet, but I might be investigating and I'll post here if I find any promising strategies.

I have tried solutions such as esm.sh and other Deno ESM module proxies to NPM, but none of them have been successful.

It may actually be easier to port the browser build to Deno, because Deno implements many of the browser APIs such a fetch(), etc.

@zicklag
Copy link
Author

zicklag commented Nov 28, 2021

I just discovered that importing web3.js will work like this through esm.sh. It's a decent workaround for now, but it would be nice to have official support somehow, too.

import * as solana from "https://esm.sh/@solana/web3.js@1.31.0/lib/index.browser.esm.js";

@zicklag
Copy link
Author

zicklag commented Nov 29, 2021

I'm discovering that the above workaround doesn't work well because as soon as I need another library such as the @orca-so/sdk in Deno as well, esm.sh won't resolve the dependency correctly.

I can't find a way to get the Orca SDK and Solana web3.js to work in Deno.

I'll have to see how other projects manage to support Deno and NodeJS at the same time.

@azerpas
Copy link

azerpas commented Feb 15, 2022

@zicklag have you managed to find a solution?

@zicklag
Copy link
Author

zicklag commented Feb 15, 2022

@azerpas no, I haven't figured it out yet. I think there's got to be a way to organize the hosting of the Solana web3.js builds so that esm.js or another option such as https://www.skypack.dev/ would be able to load it, but I haven't had time to look into exactly what's necessary.

@azerpas
Copy link

azerpas commented Feb 15, 2022

I've finally managed to make it work with jspm, like you it was conflicting with another library (here anchor) whenever I've imported it with esm (cc @zicklag)

import * as solana from "https://dev.jspm.io/@solana/web3.js";
import * as anchor from "https://esm.sh/@project-serum/anchor@0.17.0?dev&no-check"

@zicklag
Copy link
Author

zicklag commented Feb 15, 2022

Oh sweet! Good find. I'll have to try that out. I haven't tried jspm yet.

@mikecann
Copy link

mikecann commented Apr 3, 2022

@azerpas were you able to get types working?

@azerpas
Copy link

azerpas commented Apr 4, 2022

@azerpas were you able to get types working?

Sadly no, switched to JS

@f8122dac91
Copy link

I would really love to see Deno getting supported! +1

@steveluscher steveluscher transferred this issue from solana-labs/solana Feb 1, 2023
@whymidnight
Copy link

gravedigging this to report that Deno support is somewhat functional with esm.sh. unfortunately, skypack.dev did not work for me. there are of course some things broken/incomplete like opening a websocket and lsp type definitions. at the very least, submitting transactions work.

for reference, here is a working submission of a transaction:

// adapted from `https://stackoverflow.com/a/68167035`
import {
  Connection,
  Keypair,
  Transaction,
  clusterApiUrl,
  SystemProgram,
  LAMPORTS_PER_SOL,
} from "https://esm.sh/v111/@solana/web3.js@1.73.3/deno/web3.js";

const DEMO_FROM_SECRET_KEY = new Uint8Array([
  37, 21, 197, 185, 105, 201, 212, 148, 164, 108, 251, 159, 174, 252, 43, 246,
  225, 156, 38, 203, 99, 42, 244, 73, 252, 143, 34, 239, 15, 222, 217, 91, 132,
  167, 105, 60, 17, 211, 120, 243, 197, 99, 113, 34, 76, 127, 190, 18, 91, 246,
  121, 93, 189, 55, 165, 129, 196, 104, 25, 157, 209, 168, 165, 149,
]);
export async function TestSolTransfer() {
  // Connect to cluster
  const connection = new Connection(clusterApiUrl("devnet"));
  // Construct a `Keypair` from secret key
  const from = Keypair.fromSecretKey(DEMO_FROM_SECRET_KEY);
  // Generate a new random public key
  const to = Keypair.generate();
  // Add transfer instruction to transaction
  const transaction = new Transaction().add(
    SystemProgram.transfer({
      fromPubkey: from.publicKey,
      toPubkey: to.publicKey,
      lamports: LAMPORTS_PER_SOL / 100,
    })
  );
  // Sign transaction, broadcast, and confirm
  const signature = await connection.sendTransaction(transaction, [from]);

  console.log("SIGNATURE", signature);
  console.log("SUCCESS");
}

image

@steveluscher
Copy link
Collaborator

Seems to work to me! You have to use the npm: specifier when importing the library.

// index.ts
import { Connection } from 'npm:@solana/web3.js';

const connection = new Connection('https://api.mainnet-beta.solana.com');
const blockHeight = await connection.getBlockHeight();

console.log('blockHeight', blockHeight);

…then

deno index.ts

🎉

@github-actions
Copy link
Contributor

github-actions bot commented Jun 2, 2023

Because there has been no activity on this issue for 7 days since it was closed, it has been automatically locked. Please open a new issue if it requires a follow up.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants