claytantor / zoetic-xumm

A working ReactJS/Tailwinds CSS xApp using the "Universal Xumm API"
MIT License
2 stars 3 forks source link

1 change websocket implementation to include internal version #4

Closed claytantor closed 1 year ago

claytantor commented 1 year ago

Added the native implementation of the Xumm payload creation with websocket subscription as an event with create and subscribe

/**
   * uses the create and subscribe method from the xumm
   * SDK to listen for events
   * 
   * @param {*} xummSDK 
   * @param {*} xummPayload 
   */
  const handleTxPayloadNativeWS = async (xummSDK, xummPayload) => {
    try {

      const pong = await xummSDK.ping()
      console.log(pong.application)

      const payloadResponse = xummSDK.payload.createAndSubscribe(xummPayload, e => {
        console.log("event subscription",e.data);
        setWebsocketMessage(e.data);

        if (typeof e.data.signed !== 'undefined') {
          setTxStatus(1);
          setTxStatusMessage(`Payment signed.`);
          setPaymentPayload(null);
          setFormState({'destination': ''});

          // wait 5 seconds and then clear the message
          setTimeout(function () {
            setWebsocketMessage(null);
            setTxStatusMessage(null);
            setTxStatus(0);
          }, 5000);
            return e.data
          };

      });

      const r = await payloadResponse
      setPaymentPayload(await r.created);
      setTxStatusMessage("Listening for the TX Sign request to the Wallet.");

    } catch (e) {
      console.log({error: e.message, stack: e.stack})
    }
  }