From d6ca55fd20a39fdd9255c93b99be30272a1b98aa Mon Sep 17 00:00:00 2001 From: PhilReact Date: Thu, 26 Dec 2024 09:19:24 +0200 Subject: [PATCH] added gateway single buy trade for arrr --- package-lock.json | 59 ++++++++++++++++++++++++++++++----------------- package.json | 5 ++-- src/background.ts | 57 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 87 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index eded9dd..fce63ce 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "qortal-go", - "version": "0.3.8", + "version": "0.3.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "qortal-go", - "version": "0.3.8", + "version": "0.3.9", "dependencies": { "@capacitor/android": "^6.1.2", "@capacitor/app": "^6.0.1", @@ -43,6 +43,7 @@ "@transistorsoft/capacitor-background-fetch": "^6.0.1", "@types/chrome": "^0.0.263", "asmcrypto.js": "2.3.2", + "axios": "^1.7.9", "bcryptjs": "2.4.3", "buffer": "6.0.3", "compressorjs": "^1.2.1", @@ -5314,10 +5315,7 @@ "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", - "dev": true, - "license": "MIT", - "optional": true, - "peer": true + "license": "MIT" }, "node_modules/at-least-node": { "version": "1.0.0", @@ -5335,6 +5333,16 @@ "node": ">=4" } }, + "node_modules/axios": { + "version": "1.7.9", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.9.tgz", + "integrity": "sha512-LhLcE7Hbiryz8oMDdDptSrWowmB4Bl6RCt6sIJKpRB4XtVf0iEgewX3au/pJqm+Py1kCASkb/FFKjxQaLtxJvw==", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/babel-plugin-macros": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/babel-plugin-macros/-/babel-plugin-macros-3.1.0.tgz", @@ -5984,10 +5992,7 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -6317,10 +6322,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", - "dev": true, "license": "MIT", - "optional": true, - "peer": true, "engines": { "node": ">=0.4.0" } @@ -7128,14 +7130,30 @@ "integrity": "sha512-X8cqMLLie7KsNUDSdzeN8FYK9rEt4Dt67OsG/DNGnYTSDBG4uFAJFBnUeiV+zCVAvwFy56IjM9sH51jVaEhNxw==", "dev": true }, + "node_modules/follow-redirects": { + "version": "1.15.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", + "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/form-data": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", - "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -8459,10 +8477,7 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", - "dev": true, "license": "MIT", - "optional": true, - "peer": true, "engines": { "node": ">= 0.6" } @@ -8471,10 +8486,7 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", - "dev": true, "license": "MIT", - "optional": true, - "peer": true, "dependencies": { "mime-db": "1.52.0" }, @@ -11735,6 +11747,11 @@ "prosemirror-transform": "^1.1.0" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/psl": { "version": "1.9.0", "resolved": "https://registry.npmjs.org/psl/-/psl-1.9.0.tgz", diff --git a/package.json b/package.json index 52bd3c3..cc1336c 100644 --- a/package.json +++ b/package.json @@ -47,6 +47,7 @@ "@transistorsoft/capacitor-background-fetch": "^6.0.1", "@types/chrome": "^0.0.263", "asmcrypto.js": "2.3.2", + "axios": "^1.7.9", "bcryptjs": "2.4.3", "buffer": "6.0.3", "compressorjs": "^1.2.1", @@ -83,9 +84,9 @@ "slate-react": "^0.109.0", "tippy.js": "^6.3.7", "tiptap-extension-resize-image": "^1.1.8", + "ts-key-enum": "^2.0.12", "vite-plugin-top-level-await": "^1.4.4", - "vite-plugin-wasm": "^3.3.0", - "ts-key-enum": "^2.0.12" + "vite-plugin-wasm": "^3.3.0" }, "devDependencies": { "@testing-library/dom": "^10.3.0", diff --git a/src/background.ts b/src/background.ts index ed7c6e9..8fe5bb3 100644 --- a/src/background.ts +++ b/src/background.ts @@ -30,7 +30,7 @@ import { RequestQueueWithPromise } from "./utils/queue/queue"; import { validateAddress } from "./utils/validateAddress"; import { Sha256 } from "asmcrypto.js"; import NativePOW from './utils/nativepow' - +import axios from 'axios' import { TradeBotRespondMultipleRequest } from "./transactions/TradeBotRespondMultipleRequest"; import { RESOURCE_TYPE_NUMBER_GROUP_CHAT_REACTIONS } from "./constants/resourceTypes"; import { @@ -1382,12 +1382,13 @@ export async function handleActiveGroupDataFromSocket({ groups, directs }) { } catch (error) {} } -async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message }) { +async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message, atAddresses, isSingle }) { let _reference = new Uint8Array(64); self.crypto.getRandomValues(_reference); let sendTimestamp = Date.now(); - + const wallet = await getSaveWallet(); + const address = wallet.address0; let reference = Base58.encode(_reference); const resKeyPair = await getKeyPair(); const parsedData = resKeyPair; @@ -1406,7 +1407,9 @@ async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message }) }; const finalJson = { callRequest: jsonData, - extra: "whatever additional data goes here", + extra: { + type: isSingle ? "single" : "multiple" + }, }; const messageStringified = JSON.stringify(finalJson); @@ -1422,7 +1425,38 @@ async function sendChatForBuyOrder({ qortAddress, recipientPublicKey, message }) isText: 1, }); if (!hasEnoughBalance) { - throw new Error('You must have at least 4 QORT to trade using the gateway.') + const _encryptedMessage = tx._encryptedMessage; + const encryptedMessageToBase58 = Base58.encode(_encryptedMessage); + const signature = "id-" + Date.now() + "-" + Math.floor(Math.random() * 1000) + const checkGatewayStatusRes = await fetch(`${buyTradeNodeBaseUrl}/admin/status`) + const checkGatewayStatusData = await checkGatewayStatusRes.json() + if(+checkGatewayStatusData?.syncPercent !== 100 || checkGatewayStatusData?.isSynchronizing !== false){ + throw new Error("Cannot make trade. Gateway node is synchronizing") + } + const healthCheckRes = await fetch('https://www.qort.trade/api/transaction/healthcheck') + const healthcheckData = await healthCheckRes.json() + if(healthcheckData?.dbConnection !== 'healthy'){ + throw new Error('Could not connect to db. Try again later.') + } + const res = await axios.post( + `https://www.qort.trade/api/transaction/updatetxgateway`, + { + qortalAtAddresses: atAddresses, qortAddress: address, node: buyTradeNodeBaseUrl, status: "message-sent", encryptedMessageToBase58, signature , + reference, senderPublicKey: parsedData.publicKey, + sender: address, + }, + { + headers: { + "Content-Type": "application/json" + }, + } + ); + + return { + encryptedMessageToBase58, + status: "message-sent", + signature + } } const chatBytes = tx.chatBytes; const difficulty = 8; @@ -1713,7 +1747,7 @@ export async function createBuyOrderTx({ crosschainAtInfo, isGateway, foreignBlo callResponse: response, extra: { message: "Transaction processed successfully!", - atAddresses: crosschainAtInfo.map((order)=> order.qortalAtAddress), + atAddresses: foreignBlockchain === 'PIRATECHAIN' ? [crosschainAtInfo[0].qortalAtAddress] : crosschainAtInfo.map((order)=> order.qortalAtAddress), senderAddress: address, node: url }, @@ -1723,7 +1757,7 @@ export async function createBuyOrderTx({ crosschainAtInfo, isGateway, foreignBlo callResponse: "ERROR", extra: { message: response, - atAddresses: crosschainAtInfo.map((order)=> order.qortalAtAddress), + atAddresses: foreignBlockchain === 'PIRATECHAIN' ? [crosschainAtInfo[0].qortalAtAddress] : crosschainAtInfo.map((order)=> order.qortalAtAddress), senderAddress: address, node: url }, @@ -1737,7 +1771,7 @@ export async function createBuyOrderTx({ crosschainAtInfo, isGateway, foreignBlo const message = { - addresses: crosschainAtInfo.map((order)=> order.qortalAtAddress), + addresses: foreignBlockchain === 'PIRATECHAIN' ? [crosschainAtInfo[0].qortalAtAddress] : crosschainAtInfo.map((order)=> order.qortalAtAddress), foreignKey: await getForeignKey(foreignBlockchain), receivingAddress: address, }; @@ -1745,7 +1779,8 @@ export async function createBuyOrderTx({ crosschainAtInfo, isGateway, foreignBlo qortAddress: proxyAccountAddress, recipientPublicKey: proxyAccountPublicKey, message, - atAddresses: crosschainAtInfo.map((order)=> order.qortalAtAddress), + atAddresses: foreignBlockchain === 'PIRATECHAIN' ? [crosschainAtInfo[0].qortalAtAddress] : crosschainAtInfo.map((order)=> order.qortalAtAddress), + isSingle: foreignBlockchain === 'PIRATECHAIN' }); @@ -1758,13 +1793,13 @@ export async function createBuyOrderTx({ crosschainAtInfo, isGateway, foreignBlo signature: res?.signature, }); - const responseMessage = { + const responseMessage = { callResponse: message.callResponse, extra: { message: message?.extra?.message, senderAddress: address, node: buyTradeNodeBaseUrl, - atAddresses: crosschainAtInfo.map((order)=> order.qortalAtAddress), + atAddresses: foreignBlockchain === 'PIRATECHAIN' ? [crosschainAtInfo[0].qortalAtAddress] : crosschainAtInfo.map((order)=> order.qortalAtAddress), } }