Skip to content

Instantly share code, notes, and snippets.

@pedrouid
Last active June 26, 2024 13:04
Show Gist options
  • Save pedrouid/e8f6d74e4ca36128b3220c49612bf0e1 to your computer and use it in GitHub Desktop.
Save pedrouid/e8f6d74e4ca36128b3220c49612bf0e1 to your computer and use it in GitHub Desktop.
WalletConnect Instant ⚡

WalletConnect Instant ⚡

What's an Instant Request?

WalletConnect Instant feature introduces the ability to make an ephemeral session that bypasses the process of session approval by the wallet, displaying to the user a call request to be signed from the Dapp right after scanning the QR Code. This is useful for one-time use-cases like payments, topping-up or withdrawals. This is still highly experimental and hasn't been published to the official library. A PoC to aggregate feedback on this feature!

Source code available at WalletConnect/walletconnect-monorepo on walletconnect-instant branch.

Install

yarn add @walletconnect/browser @walletconnect/qrcode-modal

# OR

npm install --save @walletconnect/browser @walletconnect/qrcode-modal

Create Instant Request

import WalletConnect from "@walletconnect/browser";
import WalletConnectQRCodeModal from "@walletconnect/qrcode-modal";

/**
 *  Create a walletConnector
 */
const walletConnector = new WalletConnect({
  bridge: "https://bridge.walletconnect.org" // Required
});

/**
 *  Draft Instant Request
 */
const instantRequest = {
  id: 1,
  jsonrpc: "2.0",
  method: "eth_sendTransaction",
  params: [
    {
      from: "0xbc28ea04101f03ea7a94c1379bc3ab32e65e62d3",
      to: "0x0000000000000000000000000000000000000000",
      nonce: 1,
      gas: 100000,
      value: 0,
      data: "0x0"
    }
  ]
};

/**
 *  Subscribe to Instant Request URI
 */
walletConnector.on("instant_uri", (error, payload) => {
  if (error) {
    throw error;
  }

  const uri = payload.params[0].uri;

  /**
   *  Display QR Code Modal
   */
  WalletConnectQRCodeModal.open(uri, () => {
    console.log("QR Code Modal closed");
  });
});

/**
 *  Create Instant Request
 */
walletConnector
  .createInstantRequest(instantRequest)
  .then(result => {
    /**
     *  Get Instant Request Result
     */
    console.log(result);

    /**
     *  Close QR Code Modal
     */

    WalletConnectQRCodeModal.close();
  })
  .catch(error => {
    /**
     *  Handle Error or Rejection
     */

    console.error(error);
  });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment