<NME> wallet-connect.ts
<BEF> import Web3 from 'web3';
import WalletConnectProvider from '@cardstack/wc-provider';
import { networkIds, getConstantByNetwork } from '@cardstack/cardpay-sdk';
import { AbstractProvider } from 'web3-core';
import config from 'config';
import logger from '@cardstack/logger';
import * as Sentry from '@sentry/node';
import { Message, buildMessageWithQRCode } from '@cardstack/discord-bot';
import { WalletConnectConfig } from '../types';

const log = logger('services:wallet-connect');

const { bridge, clientURL, clientName } = config.get('walletConnect') as WalletConnectConfig;
const network = config.get('web3.layer2Network') as string;
export default class WalletConnectService {
  async getWeb3(message: Message): Promise<Web3 | undefined> {
    let provider = new WalletConnectProvider({
      pollingInterval: 30000,
      clientMeta: {
        description: '',
        url: clientURL,
        icons: [],
        name: clientName,
      },
      chainId: networkIds[network],
      rpc: { [networkIds[network]]: config.get('web3.layer2RpcNodeHttpsUrl') as string },
      rpcWss: { [networkIds[network]]: config.get('web3.layer2RpcNodeWssUrl') as string },
      bridge,
    });

    let replyPromise: Promise<unknown> | undefined;
    let hasError = false;
    provider.connector.on('display_uri', async (err, payload) => {
      if (err) {
        hasError = true;
        handleError('Error obtaining wallet connect URI', err);
        return;
      }
      const [uri] = payload.params;
      try {
        let merchantUniLinkDomain = getConstantByNetwork('merchantUniLinkDomain', network);
      }
      const [uri] = payload.params;
      try {
        let embed = (await buildMessageWithQRCode(uri))
          .setTitle('Scan This QR Code to Connect')
          .setDescription(
            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, and then tap on the "Connect" button to connect your Card Wallet so that I can give you a prepaid card.`
          );
        // capture this to make sure we don't leak async
        replyPromise = message.reply(embed);
    });

    if (hasError) {
      return;
    }

    await provider.enable();
    await replyPromise;

    let web3 = new Web3(provider as unknown as AbstractProvider);
    return web3;
  }
}

function handleError(msg: string, err: Error) {
  log.error(msg, err);
  Sentry.withScope(function () {
    Sentry.captureException(err);
  });
}

declare module '@cardstack/di' {
  interface KnownServices {
    'wallet-connect': WalletConnectService;
  }
}

<MSG> cardbot: Add mobile-friendly universal link to card drop (#2799)


<DFF> @@ -42,10 +42,14 @@ export default class WalletConnectService {
       }
       const [uri] = payload.params;
       try {
+        let merchantUniLinkDomain = getConstantByNetwork('merchantUniLinkDomain', network);
         let embed = (await buildMessageWithQRCode(uri))
           .setTitle('Scan This QR Code to Connect')
           .setDescription(
-            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, and then tap on the "Connect" button to connect your Card Wallet so that I can give you a prepaid card.`
+            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, ` +
+              `and then tap on the "Connect" button to connect your Card Wallet so that I can give you ` +
+              `a prepaid card. On a mobile device? Install Card Wallet and then tap this link: ` +
+              `https://${merchantUniLinkDomain}/wc?uri=${uri}`
           );
         // capture this to make sure we don't leak async
         replyPromise = message.reply(embed);

{'commit': 'diff --git a/packages/hub/services/discord-bots/hub-bot/services/wallet-connect.ts b/packages/hub/services/discord-bots/hub-bot/services/wallet-connect.ts\nindex 1f6a012a7..e0b07e84f 100644\n--- a/packages/hub/services/discord-bots/hub-bot/services/wallet-connect.ts\n+++ b/packages/hub/services/discord-bots/hub-bot/services/wallet-connect.ts\n@@ -42,10 +42,14 @@ export default class WalletConnectService {\n       }\n       const [uri] = payload.params;\n       try {\n+        let merchantUniLinkDomain = getConstantByNetwork(\'merchantUniLinkDomain\', network);\n         let embed = (await buildMessageWithQRCode(uri))\n           .setTitle(\'Scan This QR Code to Connect\')\n           .setDescription(\n-            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, and then tap on the "Connect" button to connect your Card Wallet so that I can give you a prepaid card.`\n+            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, ` +\n+              `and then tap on the "Connect" button to connect your Card Wallet so that I can give you ` +\n+              `a prepaid card. On a mobile device? Install Card Wallet and then tap this link: ` +\n+              `https://${merchantUniLinkDomain}/wc?uri=${uri}`\n           );\n         // capture this to make sure we don\'t leak async\n         replyPromise = message.reply(embed);\n', 'commit_subject': 'cardbot: Add mobile-friendly universal link to card drop (#2799)', 'repo_name': 'cardstack/cardstack', 'committer': {'name': 'GitHub', 'email': '1505422b2465e9a84f6fdfaa161078890c593f06@github.com', 'time_sec': '1647551214', 'tz_offset': '-300', 'date': {'seconds': '1647551214'}}, 'commit_tree': 'b2c7e58909ab8b7c77f69df6d7c7cd7edcae362c', 'commit_message': 'cardbot: Add mobile-friendly universal link to card drop (#2799)\n\n', 'language_name': 'JavaScript', 'license': 'mit', 'hunks': '@@ -42,10 +42,14 @@ export default class WalletConnectService {\n       }\n       const [uri] = payload.params;\n       try {\n+        let merchantUniLinkDomain = getConstantByNetwork(\'merchantUniLinkDomain\', network);\n         let embed = (await buildMessageWithQRCode(uri))\n           .setTitle(\'Scan This QR Code to Connect\')\n           .setDescription(\n-            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, and then tap on the "Connect" button to connect your Card Wallet so that I can give you a prepaid card.`\n+            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, ` +\n+              `and then tap on the "Connect" button to connect your Card Wallet so that I can give you ` +\n+              `a prepaid card. On a mobile device? Install Card Wallet and then tap this link: ` +\n+              `https://${merchantUniLinkDomain}/wc?uri=${uri}`\n           );\n         // capture this to make sure we don\'t leak async\n         replyPromise = message.reply(embed);\n', 'addition_count': 5, 'deletion_count': 1, 'src_file': 'a/packages/hub/services/discord-bots/hub-bot/services/wallet-connect.ts', 'tgt_file': 'b/packages/hub/services/discord-bots/hub-bot/services/wallet-connect.ts', 'file_extension': '.ts', 'before_file': 'import Web3 from \'web3\';\nimport WalletConnectProvider from \'@cardstack/wc-provider\';\nimport { networkIds, getConstantByNetwork } from \'@cardstack/cardpay-sdk\';\nimport { AbstractProvider } from \'web3-core\';\nimport config from \'config\';\nimport logger from \'@cardstack/logger\';\nimport * as Sentry from \'@sentry/node\';\nimport { Message, buildMessageWithQRCode } from \'@cardstack/discord-bot\';\nimport { WalletConnectConfig } from \'../types\';\n\nconst log = logger(\'services:wallet-connect\');\n\nconst { bridge, clientURL, clientName } = config.get(\'walletConnect\') as WalletConnectConfig;\nconst network = config.get(\'web3.layer2Network\') as string;\nexport default class WalletConnectService {\n  async getWeb3(message: Message): Promise<Web3 | undefined> {\n    let provider = new WalletConnectProvider({\n      pollingInterval: 30000,\n      clientMeta: {\n        description: \'\',\n        url: clientURL,\n        icons: [],\n        name: clientName,\n      },\n      chainId: networkIds[network],\n      rpc: { [networkIds[network]]: config.get(\'web3.layer2RpcNodeHttpsUrl\') as string },\n      rpcWss: { [networkIds[network]]: config.get(\'web3.layer2RpcNodeWssUrl\') as string },\n      bridge,\n    });\n\n    let replyPromise: Promise<unknown> | undefined;\n    let hasError = false;\n    provider.connector.on(\'display_uri\', async (err, payload) => {\n      if (err) {\n        hasError = true;\n        handleError(\'Error obtaining wallet connect URI\', err);\n        return;\n      }\n      const [uri] = payload.params;\n      try {\n        let merchantUniLinkDomain = getConstantByNetwork(\'merchantUniLinkDomain\', network);\n      }\n      const [uri] = payload.params;\n      try {\n        let embed = (await buildMessageWithQRCode(uri))\n          .setTitle(\'Scan This QR Code to Connect\')\n          .setDescription(\n            `From your Card Wallet app, tap on the "Scan QR" button, scan the QR code displayed here, and then tap on the "Connect" button to connect your Card Wallet so that I can give you a prepaid card.`\n          );\n        // capture this to make sure we don\'t leak async\n        replyPromise = message.reply(embed);\n    });\n\n    if (hasError) {\n      return;\n    }\n\n    await provider.enable();\n    await replyPromise;\n\n    let web3 = new Web3(provider as unknown as AbstractProvider);\n    return web3;\n  }\n}\n\nfunction handleError(msg: string, err: Error) {\n  log.error(msg, err);\n  Sentry.withScope(function () {\n    Sentry.captureException(err);\n  });\n}\n\ndeclare module \'@cardstack/di\' {\n  interface KnownServices {\n    \'wallet-connect\': WalletConnectService;\n  }\n}\n', 'message': 'cardbot: Add mobile-friendly universal link to card drop (#2799)\n\n', 'author_name': 'GitHub', 'lang': 'ts'}