From 0399c9d1471a017742677e9dcfa6c7e5d13f5d65 Mon Sep 17 00:00:00 2001 From: Phillip Lang Martinez Date: Wed, 17 Jul 2024 17:00:43 -0400 Subject: [PATCH] get ltc balance --- public/content-script.js | 29 +++++++++++++++++++++++ src/App.tsx | 26 +++++++++++++++++++- src/background.ts | 51 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/public/content-script.js b/public/content-script.js index d3d2bd5..2fc77f7 100644 --- a/public/content-script.js +++ b/public/content-script.js @@ -121,6 +121,35 @@ document.addEventListener('qortalExtensionRequests', async (event) => { })); } }); + } else if(type === 'REQUEST_LTC_BALANCE'){ + + + const hostname = window.location.hostname + const res = await connection(hostname) + if(!res){ + document.dispatchEvent(new CustomEvent('qortalExtensionResponses', { + detail: { type: "USER_INFO", data: { + error: "Not authorized" + }, requestId } + })); + return + } + chrome.runtime.sendMessage({ action: "ltcBalance", payload: { + hostname + }, timeout }, (response) => { + if (response.error) { + document.dispatchEvent(new CustomEvent('qortalExtensionResponses', { + detail: { type: "LTC_BALANCE", data: { + error: response.error + }, requestId } + })); + } else { + // Include the requestId in the detail when dispatching the response + document.dispatchEvent(new CustomEvent('qortalExtensionResponses', { + detail: { type: "LTC_BALANCE", data: response, requestId } + })); + } + }); } else if (type === 'REQUEST_AUTHENTICATION') { const hostname = window.location.hostname const res = await connection(hostname) diff --git a/src/App.tsx b/src/App.tsx index ccff2f9..ad40f94 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -56,6 +56,7 @@ type extStates = | "transfer-success-request" | "wallet-dropped" | "web-app-request-buy-order" + | "buy-order-submitted" ; function App() { @@ -366,7 +367,7 @@ function App() { }, (response) => { if (response === true) { - setExtstate("transfer-success-request"); + setExtstate("buy-order-submitted"); setCountdown(null); } else { @@ -1433,6 +1434,29 @@ function App() { )} + {extState === "buy-order-submitted" && ( + <> + + + + + Your buy order was submitted + + + { + window.close(); + }} + > + Close + + + )} {countdown && ( { // const validApi = await findUsableApi(); @@ -261,10 +282,13 @@ async function decryptWallet({ password, wallet, walletVersion }) { const wallet2 = new PhraseWallet(response, walletVersion); const keyPair = wallet2._addresses[0].keyPair; const ltcPrivateKey = wallet2._addresses[0].ltcWallet.derivedMasterPrivateKey + const ltcPublicKey = wallet2._addresses[0].ltcWallet.derivedMasterPublicKey + const toSave = { privateKey: Base58.encode(keyPair.privateKey), publicKey: Base58.encode(keyPair.publicKey), - ltcPrivateKey: ltcPrivateKey + ltcPrivateKey: ltcPrivateKey, + ltcPublicKey : ltcPublicKey } const dataString = JSON.stringify(toSave) await new Promise((resolve, reject) => { @@ -442,7 +466,9 @@ async function createBuyOrderTx({ crosschainAtInfo }) { signature: res?.signature, }) - return { atAddress: crosschainAtInfo.qortalAtAddress, chatSignature: res?.signature, node: buyTradeNodeBaseUrl } + return { atAddress: crosschainAtInfo.qortalAtAddress, chatSignature: res?.signature, node: buyTradeNodeBaseUrl, qortAddress: address } + } else { + throw new Error("Unable to send buy order message") } } catch (error) { @@ -522,12 +548,17 @@ function fetchMessages(apiCall) { }); } -function fetchMessagesForBuyOrders(apiCall, signature, senderPublicKey) { +async function fetchMessagesForBuyOrders(apiCall, signature, senderPublicKey) { let retryDelay = 2000; // Start with a 2-second delay const maxDuration = 360000 * 2; // Maximum duration set to 12 minutes const startTime = Date.now(); // Record the start time let triedChatMessage = [] // Promise to handle polling logic + await new Promise((res)=> { + setTimeout(() => { + res() + }, 40000); + }) return new Promise((resolve, reject) => { const attemptFetch = async () => { if (Date.now() - startTime > maxDuration) { @@ -734,6 +765,18 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { console.error(error.message); }); break; + case "ltcBalance": { + getLTCBalance() + .then((balance) => { + sendResponse(balance); + }) + .catch((error) => { + console.error(error.message); + }); + + + } + break; case "sendCoin": { const { receiver, password, amount } = request.payload; @@ -1184,7 +1227,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { .catch((error) => { console.error(error.message); sendResponse({ error: error.message }); - originalSendResponse({ error: error.message }); + // originalSendResponse({ error: error.message }); }); }