changed targetOrigin and started qr buy order

This commit is contained in:
2024-11-03 16:10:28 +02:00
parent d50f928520
commit cf845cae1b
8 changed files with 184 additions and 75 deletions

View File

@@ -12,7 +12,8 @@ import {
joinGroup as joinGroupFunc,
sendQortFee,
sendCoin as sendCoinFunc,
isUsingLocal
isUsingLocal,
createBuyOrderTx
} from "../background";
import { getNameInfo } from "../backgroundFunctions/encryption";
import { showSaveFilePicker } from "../components/Apps/useQortalMessageListener";
@@ -32,12 +33,17 @@ import { createTransaction } from "../transactions/transactions";
import { mimeToExtensionMap } from "../utils/memeTypes";
const btcFeePerByte = 0.00000100
const ltcFeePerByte = 0.00000030
const dogeFeePerByte = 0.00001000
const dgbFeePerByte = 0.00000010
const rvnFeePerByte = 0.00001125
function roundUpToDecimals(number, decimals = 8) {
const factor = Math.pow(10, decimals); // Create a factor based on the number of decimals
return Math.ceil(+number * factor) / factor;
}
const _createPoll = async ({pollName, pollDescription, options}, isFromExtension) => {
const fee = await getFee("CREATE_POLL");
@@ -206,11 +212,12 @@ function getFileFromContentScript(fileId) {
const requestId = `getFile_${fileId}_${Date.now()}`;
fileRequestResolvers.set(requestId, { resolve, reject }); // Store resolvers by requestId
const targetOrigin = window.location.origin;
// Send the request message
window.postMessage(
{ action: "getFileFromIndexedDB", fileId, requestId },
"*"
targetOrigin
);
// Timeout to handle no response scenario
@@ -253,11 +260,12 @@ async function getUserPermission(payload, isFromExtension) {
return new Promise((resolve) => {
const requestId = `qortalRequest_${Date.now()}`;
responseResolvers.set(requestId, resolve); // Store resolver by requestId
const targetOrigin = window.location.origin;
// Send the request message
window.postMessage(
{ action: "QORTAL_REQUEST_PERMISSION", payload, requestId, isFromExtension },
"*"
targetOrigin
);
// Optional timeout to handle no response scenario
@@ -2390,3 +2398,63 @@ export const sendCoin = async (data, isFromExtension) => {
}
}
};
export const createBuyOrder = async (data, isFromExtension) => {
const requiredFields = [
"crosschainAtInfo",
"processType"
];
const missingFields: string[] = [];
requiredFields.forEach((field) => {
if (!data[field]) {
missingFields.push(field);
}
});
if (missingFields.length > 0) {
const missingFieldsString = missingFields.join(", ");
const errorMsg = `Missing fields: ${missingFieldsString}`;
throw new Error(errorMsg);
}
const crosschainAtInfo = data.crosschainAtInfo;
const atAddresses = data.crosschainAtInfo?.map((order)=> order.qortalAtAddress);
const processType = data.processType;
if(processType !== 'local' && processType !== 'gateway'){
throw new Error('Process Type must be either local or gateway')
}
try {
const resPermission = await getUserPermission({
text1: "Do you give this application permission to perform a buy order?",
text2: `${atAddresses?.length}${" "}
${`buy order${
atAddresses?.length === 1 ? "" : "s"
}`}`,
text3: `${crosschainAtInfo?.reduce((latest, cur) => {
return latest + +cur?.qortAmount;
}, 0)} QORT FOR ${roundUpToDecimals(
crosschainAtInfo?.reduce((latest, cur) => {
return latest + +cur?.foreignAmount;
}, 0)
)}
${` ${crosschainAtInfo?.[0]?.foreignBlockchain}`}`,
highlightedText: `Using ${processType}`,
fee: ''
}, isFromExtension);
const { accepted } = resPermission;
if (accepted) {
const resBuyOrder = await createBuyOrderTx(
{
crosschainAtInfo,
useLocal: processType === 'local' ? true : false
}
);
return resBuyOrder;
} else {
throw new Error("User declined request");
}
} catch (error) {
throw new Error(error?.message || "Failed to submit trade order.");
}
};