From eecfb6367f3ae2cf3da4fe80a755843cf2e86d0d Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 4 Apr 2025 14:59:42 +0300 Subject: [PATCH] fix: fix promises --- eslint.config.mjs | 1 - packages/contract.cm/src/onchain/addWord.ts | 6 +- packages/contract.cm/src/onchain/contract.ts | 8 +- packages/cwait/src/onchain/executor.ts | 2 +- packages/cwait/src/onchain/ops/read.ts | 27 ++- packages/cwait/src/onchain/ops/store.ts | 34 ++- packages/cwait/src/onchain/ops/take.ts | 30 +-- packages/ui/.env | 2 +- packages/ui/src/api/client.ts | 220 ------------------ packages/ui/src/api/getActiveMarketOrders.ts | 17 -- packages/ui/src/api/getAllActivePositions.ts | 36 --- packages/ui/src/api/getAllMarketClaim.ts | 21 -- .../src/api/getAllMarketCollateralBalance.ts | 21 -- packages/ui/src/api/getAllUserPositions.ts | 36 --- .../ui/src/api/getAllUserPositionsCount.ts | 32 --- .../ui/src/api/getBestActiveMarketOrders.ts | 17 -- packages/ui/src/api/getBestActivePositions.ts | 62 ----- packages/ui/src/api/getHistoryAccess.ts | 29 --- .../ui/src/api/getHistoryByHistoryAccess.ts | 19 -- .../ui/src/api/getHistoryByTrackerOrAccess.ts | 69 ------ packages/ui/src/api/getMarketMakerOrders.ts | 33 --- packages/ui/src/api/getUtxosInUse.ts | 17 -- packages/ui/src/api/index.ts | 15 -- .../api/withQueryClient/fetchHistoryAccess.ts | 14 -- .../fetchHistoryByHistoryAccess.ts | 14 -- packages/ui/src/api/withQueryClient/index.ts | 2 - packages/ui/vite.config.ts | 7 +- yarn.lock | 22 -- 28 files changed, 60 insertions(+), 753 deletions(-) delete mode 100644 packages/ui/src/api/client.ts delete mode 100644 packages/ui/src/api/getActiveMarketOrders.ts delete mode 100644 packages/ui/src/api/getAllActivePositions.ts delete mode 100644 packages/ui/src/api/getAllMarketClaim.ts delete mode 100644 packages/ui/src/api/getAllMarketCollateralBalance.ts delete mode 100644 packages/ui/src/api/getAllUserPositions.ts delete mode 100644 packages/ui/src/api/getAllUserPositionsCount.ts delete mode 100644 packages/ui/src/api/getBestActiveMarketOrders.ts delete mode 100644 packages/ui/src/api/getBestActivePositions.ts delete mode 100644 packages/ui/src/api/getHistoryAccess.ts delete mode 100644 packages/ui/src/api/getHistoryByHistoryAccess.ts delete mode 100644 packages/ui/src/api/getHistoryByTrackerOrAccess.ts delete mode 100644 packages/ui/src/api/getMarketMakerOrders.ts delete mode 100644 packages/ui/src/api/getUtxosInUse.ts delete mode 100644 packages/ui/src/api/index.ts delete mode 100644 packages/ui/src/api/withQueryClient/fetchHistoryAccess.ts delete mode 100644 packages/ui/src/api/withQueryClient/fetchHistoryByHistoryAccess.ts delete mode 100644 packages/ui/src/api/withQueryClient/index.ts diff --git a/eslint.config.mjs b/eslint.config.mjs index 2930d75..99d2cb2 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -21,7 +21,6 @@ export default tseslint.config( '**/dapp-ui/src/pages/Bot/**', '**/dapp-ui/src/pages/BotHedging/**', '**/dapp-ui/src/pages/SwapTool/**', - '**/market-maker.cm/perf_test/onchain.cjs', ], }, // rest of the config by module diff --git a/packages/contract.cm/src/onchain/addWord.ts b/packages/contract.cm/src/onchain/addWord.ts index b596714..52be61d 100644 --- a/packages/contract.cm/src/onchain/addWord.ts +++ b/packages/contract.cm/src/onchain/addWord.ts @@ -18,12 +18,16 @@ function hashCode(str: string): string { export const addWord = async (...[word]: AddWordArgs) => { const id = hashCode(word); + console.log('Before storeOp'); await storeOp(constructClaim(createWordKey(id), { word }, '0x0')); + console.log('After storeOp'); const wordClaim = await readOp>(createWordKey(id)); - + console.log('After readOp'); const newWord = (wordClaim?.body.word ?? '') + '!!!'; const newId = hashCode(newWord); + console.log('Before 2 storeOp'); storeOp(constructClaim(createWordKey(newId), { word: newWord }, '0x0')); + console.log('After 2 storeOp'); }; diff --git a/packages/contract.cm/src/onchain/contract.ts b/packages/contract.cm/src/onchain/contract.ts index d44f0ba..fc869fe 100644 --- a/packages/contract.cm/src/onchain/contract.ts +++ b/packages/contract.cm/src/onchain/contract.ts @@ -23,8 +23,10 @@ const createModule = (): ContractHandlers => { return module; }; -export const cwebMain = async () => { - const module = createModule(); +export const cwebMain = () => { + return (async () => { + const module = createModule(); - await executeHandler(module); + await executeHandler(module); + })(); }; diff --git a/packages/cwait/src/onchain/executor.ts b/packages/cwait/src/onchain/executor.ts index dbc8e9d..b410049 100644 --- a/packages/cwait/src/onchain/executor.ts +++ b/packages/cwait/src/onchain/executor.ts @@ -41,7 +41,7 @@ export const executor = const execution = new Promise((resolve, reject) => { abortExecution = resolve; - method(...args).then(resolve, reject); + method(...args).then(() => resolve(), reject); }); await execution; diff --git a/packages/cwait/src/onchain/ops/read.ts b/packages/cwait/src/onchain/ops/read.ts index fbf2492..ac9e789 100644 --- a/packages/cwait/src/onchain/ops/read.ts +++ b/packages/cwait/src/onchain/ops/read.ts @@ -8,20 +8,23 @@ import { pushAwaitedOp } from './awaited'; import { shiftResolvedOp } from './resolved'; export const readOp = (key: ClaimKey) => { - const { op, isOp } = shiftResolvedOp(); + return new Promise((resolve, reject) => { + try { + const { op, isOp } = shiftResolvedOp(); - if (!isOp) { - pushAwaitedOp(constructRead(context.issuer, key)); - } else if (op && !isResolvedRead(op)) { - throw new Error('Read operation not found'); - } + if (!isOp) { + pushAwaitedOp(constructRead(context.issuer, key)); + abort(); + } else { + if (op && !isResolvedRead(op)) { + throw new Error('Read operation not found'); + } - return new Promise((resolve) => { - if (isOp) { - const claim = op && ((extractRead(op)?.[0] ?? null) as TClaim | null); - resolve(claim); - } else { - abort(); + const claim = op && ((extractRead(op)?.[0].content ?? null) as TClaim | null); + resolve(claim); + } + } catch (error) { + reject(error); } }); }; diff --git a/packages/cwait/src/onchain/ops/store.ts b/packages/cwait/src/onchain/ops/store.ts index 0357ec5..15ff9da 100644 --- a/packages/cwait/src/onchain/ops/store.ts +++ b/packages/cwait/src/onchain/ops/store.ts @@ -6,26 +6,24 @@ import { abort } from '../executor'; import { pushAwaitedOp } from './awaited'; import { shiftResolvedOp } from './resolved'; -export const storeOp = (claim: Claim) => { - const { op, isOp } = shiftResolvedOp(); +export const storeOp = (claim: Claim) => + new Promise((resolve, reject) => { + try { + const { op, isOp } = shiftResolvedOp(); - if (!isOp) { - pushAwaitedOp(constructStore(claim)); - } else if (op && !isResolvedStore(op)) { - throw new Error('Store operation not found'); - } + if (!isOp) { + pushAwaitedOp(constructStore(claim)); + abort(); + } else { + if (op && !isResolvedStore(op)) { + throw new Error('Store operation not found'); + } - const result = op && extractStore(op); + const result = op && extractStore(op); - if (!result) { - throw new Error('Wrong store operation'); - } - - return new Promise((resolve) => { - if (isOp) { - resolve(result); - } else { - abort(); + resolve(result); + } + } catch (error) { + reject(error); } }); -}; diff --git a/packages/cwait/src/onchain/ops/take.ts b/packages/cwait/src/onchain/ops/take.ts index 9a2f9d8..6f3896c 100644 --- a/packages/cwait/src/onchain/ops/take.ts +++ b/packages/cwait/src/onchain/ops/take.ts @@ -6,21 +6,23 @@ import { abort } from '../executor'; import { pushAwaitedOp } from './awaited'; import { shiftResolvedOp } from './resolved'; -export const takeOp = (key: ClaimKey) => { - const { op, isOp } = shiftResolvedOp(); +export const takeOp = (key: ClaimKey) => + new Promise((resolve, reject) => { + try { + const { op, isOp } = shiftResolvedOp(); - if (!isOp) { - pushAwaitedOp(constructTake(key)); - } else if (op && !isResolvedTake(op)) { - throw new Error('Take operation not found'); - } + if (!isOp) { + pushAwaitedOp(constructTake(key)); + abort(); + } else { + if (op && !isResolvedTake(op)) { + throw new Error('Take operation not found'); + } - return new Promise((resolve) => { - if (isOp) { - const claim = op && extractTake(op); - resolve(claim as TClaim | null); - } else { - abort(); + const claim = op && extractTake(op); + resolve(claim as TClaim | null); + } + } catch (error) { + reject(error); } }); -}; diff --git a/packages/ui/.env b/packages/ui/.env index c000542..4852fd5 100644 --- a/packages/ui/.env +++ b/packages/ui/.env @@ -1,4 +1,4 @@ VITE_API_URL='https://api-cloud.coinweb.io/wallet' VITE_EXPLORER_URL='https://explorer.coinweb.io' -VITE_CONTRACT_ADDRESS="0x51c4d9ea120f185aab78a2fa74eba43f4e6449a261d6d369fd077012a202aa03" \ No newline at end of file +VITE_CONTRACT_ADDRESS="0xb5bd653b3e95ea8588b23d9fad48cd4cc6debd26ad9982d1249e116fad9100d5" \ No newline at end of file diff --git a/packages/ui/src/api/client.ts b/packages/ui/src/api/client.ts deleted file mode 100644 index 7d9453c..0000000 --- a/packages/ui/src/api/client.ts +++ /dev/null @@ -1,220 +0,0 @@ -import { - type GqlClaimFilter, - type GqlIssuedClaim, - type NetworkName, - NodeConnection, - connect_to_node, - fetch_claims, -} from '@coinweb/wallet-lib'; -import { type Pagination } from 'dex-app.cm'; - -import { Currency, CONTRACT_PARAMS } from '../constants'; - -type IndexClaimKey = { - first_part: unknown[]; - second_part: [number | string, ...unknown[]]; -}; - -let cwebWalletNode: NodeConnection | null = null; -const getCwebWalletNode = () => { - if (!cwebWalletNode) { - cwebWalletNode = connect_to_node(import.meta.env.VITE_API_URL as string); - } - - return cwebWalletNode; -}; - -type ContractClient = { - fetchClaims: ( - firstPart: NonNullable, - secondPart: T, - range?: { - start: T extends null ? unknown : T | null; - end: T extends null ? unknown : T | null; - }, - pagination?: Pagination - ) => Promise; - updateClient: () => void; - contractAddress: string; - networkName: NetworkName; -}; - -const createClient = (contractAddress: string, networkName: NetworkName) => { - const createClaimFilter = >( - firstPart: NonNullable, - secondPart: T | null = null, - range?: { - start: T; - end: T; - } - ): GqlClaimFilter => { - return { - issuer: { FromSmartContract: contractAddress }, - keyFirstPart: firstPart, - keySecondPart: secondPart, - startsAtKeySecondPart: range?.start ?? null, - endsAtKeySecondPart: range?.end ?? null, - }; - }; - - const fetchClaims = async >( - firstPart: NonNullable, - secondPart: T | null = null, - range?: { - start: T; - end: T; - }, - pagination?: Pagination - ): Promise => { - if (!contractAddress || !networkName) { - throw new Error('Network parameters are not defined'); - } - - const filter = createClaimFilter(firstPart, secondPart, range); - - const loadClaims = async () => { - const rawData = await fetch_claims(getCwebWalletNode(), [filter], networkName, true); - - const sortedData = rawData.sort((a, b) => { - let aValue: string | number | bigint = (a.content.key as IndexClaimKey).second_part[0]; - let bValue: string | number | bigint = (b.content.key as IndexClaimKey).second_part[0]; - - if (typeof aValue === 'string' && typeof bValue === 'string') { - if (aValue.startsWith('0x')) { - aValue = BigInt(aValue); - } else { - aValue = BigInt(`0x${aValue}`); - } - - if (bValue.startsWith('0x')) { - bValue = BigInt(bValue); - } else { - bValue = BigInt(`0x${bValue}`); - } - } - - if (aValue < bValue) { - return 1; - } else if ((a.content.key as IndexClaimKey).second_part[0] > (b.content.key as IndexClaimKey).second_part[0]) { - return -1; - } else { - return 0; - } - }); - - if (pagination) { - if (pagination.offset < 0) { - return sortedData.slice(0, pagination.limit + pagination.offset); - } - - return sortedData.slice(pagination.offset, pagination.offset + pagination.limit); - } - - return sortedData; - }; - - const claimsRequest = loadClaims(); - - const result = await claimsRequest; - - return result; - }; - - const updateClient = () => {}; - - return { - fetchClaims, - updateClient, - contractAddress, - networkName, - } as ContractClient; -}; - -export const baseClients: { - [key in Exclude]: ContractClient; -} = { - [Currency.ETH]: createClient( - CONTRACT_PARAMS[Currency.ETH].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.ETH].NETWORK_NAME - ), - [Currency.BNB]: createClient( - CONTRACT_PARAMS[Currency.BNB].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.BNB].NETWORK_NAME - ), - [Currency.USDT_ETH]: createClient( - CONTRACT_PARAMS[Currency.USDT_ETH].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.USDT_ETH].NETWORK_NAME - ), - [Currency.USDT_BNB]: createClient( - CONTRACT_PARAMS[Currency.USDT_BNB].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.USDT_BNB].NETWORK_NAME - ), - [Currency.BTC]: createClient( - CONTRACT_PARAMS[Currency.BTC].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.BTC].NETWORK_NAME - ), - [Currency.LTC]: createClient( - CONTRACT_PARAMS[Currency.BTC].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.BTC].NETWORK_NAME - ), - [Currency.EGLD]: createClient( - CONTRACT_PARAMS[Currency.BTC].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.BTC].NETWORK_NAME - ), - [Currency.TRX]: createClient( - CONTRACT_PARAMS[Currency.TRX].L2_CONTRACT_ADDRESS_BASE, - CONTRACT_PARAMS[Currency.TRX].NETWORK_NAME - ), -}; - -export const makerClients: { - [key in Exclude]: ContractClient; -} = { - [Currency.ETH]: createClient( - CONTRACT_PARAMS[Currency.ETH].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.ETH].NETWORK_NAME - ), - [Currency.BNB]: createClient( - CONTRACT_PARAMS[Currency.BNB].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.BNB].NETWORK_NAME - ), - [Currency.USDT_ETH]: createClient( - CONTRACT_PARAMS[Currency.USDT_ETH].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.USDT_ETH].NETWORK_NAME - ), - [Currency.USDT_BNB]: createClient( - CONTRACT_PARAMS[Currency.USDT_BNB].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.USDT_BNB].NETWORK_NAME - ), - [Currency.BTC]: createClient( - CONTRACT_PARAMS[Currency.BTC].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.BTC].NETWORK_NAME - ), - [Currency.LTC]: createClient( - CONTRACT_PARAMS[Currency.BTC].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.BTC].NETWORK_NAME - ), - [Currency.EGLD]: createClient( - CONTRACT_PARAMS[Currency.BTC].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.BTC].NETWORK_NAME - ), - [Currency.TRX]: createClient( - CONTRACT_PARAMS[Currency.TRX].L2_CONTRACT_ADDRESS_MAKER, - CONTRACT_PARAMS[Currency.TRX].NETWORK_NAME - ), -}; - -export const clients: Record = new Proxy( - Object.fromEntries( - [...Object.values(baseClients), ...Object.values(makerClients)].map((client) => [client.contractAddress, client]) - ), - { - get(clients, contractId: string) { - if (!(contractId in clients)) { - throw new Error(`Client for contractId=${String(contractId)} not found`); - } - - return clients[contractId]; - }, - } -); diff --git a/packages/ui/src/api/getActiveMarketOrders.ts b/packages/ui/src/api/getActiveMarketOrders.ts deleted file mode 100644 index 407a9b2..0000000 --- a/packages/ui/src/api/getActiveMarketOrders.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { type Client, getAllActiveOrders, type Pagination } from 'market-maker.cm'; - -import { Currency } from '../constants'; - -import { makerClients } from './client'; - -export const getActiveMarketOrders = async (currency: Currency, pagination?: Pagination) => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get active orders for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => makerClients[currency].fetchClaims(...params, pagination), - }; - - return getAllActiveOrders(client); -}; diff --git a/packages/ui/src/api/getAllActivePositions.ts b/packages/ui/src/api/getAllActivePositions.ts deleted file mode 100644 index b0e5281..0000000 --- a/packages/ui/src/api/getAllActivePositions.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { type Client, type Pagination, getOrderById, getActiveOrderIds, type OrderData } from 'dex-app.cm'; - -// import { decimalsForCurrency } from '@/utils'; - -import { Currency } from '../constants'; - -import { baseClients } from './client'; - -export const getAllActivePositions = async (currency: Currency, pagination?: Pagination): Promise => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get active positions for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => baseClients[currency].fetchClaims(...params, pagination), - }; - - const ids = await getActiveOrderIds(client); - return await Promise.all(ids.map((id: string) => getOrderById(client, id))); - /* - return ( - (await Promise.all(ids.map((id: string) => getOrderById(client, id)))) - // TODO: Remove this filter when we have a proper solution for reversed orders - .filter((position) => { - if (['btcswap'].includes(import.meta.env.MODE)) { - return position.l1Amount === position.minL1Amount; // order.isOwnerless - } else { - return ( - position.funds >= 1n * 10n ** BigInt(decimalsForCurrency(Currency.CWEB)) && - position.l1Amount !== position.minL1Amount - ); - } - }) - ); - */ -}; diff --git a/packages/ui/src/api/getAllMarketClaim.ts b/packages/ui/src/api/getAllMarketClaim.ts deleted file mode 100644 index 72630ad..0000000 --- a/packages/ui/src/api/getAllMarketClaim.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { type PubKey } from '@coinweb/wallet-lib'; -import { type Client, getAllOwnClaims, type Pagination } from 'market-maker.cm'; - -import { Currency } from '../constants'; - -import { makerClients } from './client'; - -export const getAllMarketClaim = async (currency: Currency, pubKey: PubKey, pagination?: Pagination) => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get all market claim for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => makerClients[currency].fetchClaims(...params, pagination), - }; - - return getAllOwnClaims(client, { - auth: 'EcdsaContract', - payload: pubKey, - }); -}; diff --git a/packages/ui/src/api/getAllMarketCollateralBalance.ts b/packages/ui/src/api/getAllMarketCollateralBalance.ts deleted file mode 100644 index 18464bc..0000000 --- a/packages/ui/src/api/getAllMarketCollateralBalance.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { type PubKey } from '@coinweb/wallet-lib'; -import { type Client, getBalance, type Pagination } from 'market-maker.cm'; - -import { Currency } from '../constants'; - -import { makerClients } from './client'; - -export const getAllMarketCollateralBalance = async (currency: Currency, pubKey: PubKey, pagination?: Pagination) => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get market balance for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => makerClients[currency].fetchClaims(...params, pagination), - }; - - return getBalance(client, { - auth: 'EcdsaContract', - payload: pubKey, - }); -}; diff --git a/packages/ui/src/api/getAllUserPositions.ts b/packages/ui/src/api/getAllUserPositions.ts deleted file mode 100644 index d3de9c9..0000000 --- a/packages/ui/src/api/getAllUserPositions.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { type Client, type Pagination, type OrderData, getUserOrderIds, getOrderById, type PubKey } from 'dex-app.cm'; - -import { Currency } from '../constants'; - -import { baseClients } from './client'; - -export const getAllUserPositions = async ( - currency: Currency, - pubKey: PubKey, - pagination?: Pagination -): Promise => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get user positions for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => { - if (params.length < 3) { - params.push(pagination); - } - - return baseClients[currency].fetchClaims(...params, pagination); - }, - }; - - const noPaginationClient: Client = { - fetchClaims: (...params) => baseClients[currency].fetchClaims(...params), - }; - - const ids = await getUserOrderIds(client, { - auth: 'EcdsaContract', - payload: pubKey, - }); - - return Promise.all(ids.map((id: string) => getOrderById(noPaginationClient, id))); -}; diff --git a/packages/ui/src/api/getAllUserPositionsCount.ts b/packages/ui/src/api/getAllUserPositionsCount.ts deleted file mode 100644 index 442035d..0000000 --- a/packages/ui/src/api/getAllUserPositionsCount.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { type Client, type Pagination, getUserOrderIds, type PubKey } from 'dex-app.cm'; - -import { Currency } from '../constants'; - -import { baseClients } from './client'; - -export const getAllUserPositionsCount = async ( - currency: Currency, - pubKey: PubKey, - pagination?: Pagination -): Promise => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get user positions for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => { - if (params.length < 3) { - params.push(pagination); - } - - return baseClients[currency].fetchClaims(...params, pagination); - }, - }; - - const ids = await getUserOrderIds(client, { - auth: 'EcdsaContract', - payload: pubKey, - }); - - return ids.length; -}; diff --git a/packages/ui/src/api/getBestActiveMarketOrders.ts b/packages/ui/src/api/getBestActiveMarketOrders.ts deleted file mode 100644 index 743d0a5..0000000 --- a/packages/ui/src/api/getBestActiveMarketOrders.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { type Client, getBestActiveOrders, type Pagination } from 'market-maker.cm'; - -import { Currency } from '../constants'; - -import { makerClients } from './client'; - -export const getBestActiveMarketOrders = async (currency: Currency, pagination?: Pagination) => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get best active market orders for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => makerClients[currency].fetchClaims(...params, pagination), - }; - - return getBestActiveOrders(client); -}; diff --git a/packages/ui/src/api/getBestActivePositions.ts b/packages/ui/src/api/getBestActivePositions.ts deleted file mode 100644 index 81ebe9b..0000000 --- a/packages/ui/src/api/getBestActivePositions.ts +++ /dev/null @@ -1,62 +0,0 @@ -import { type Client, type Pagination, getOrderById, getBestActiveOrderIds } from 'dex-app.cm'; - -import { getTxOutputDataByVout } from '@/networks/btc'; -import { C1Position } from '@/types'; -import { isBtcCurrency, decimalsForCurrency } from '@/utils'; - -import { Currency } from '../constants'; - -import { baseClients } from './client'; - -export const getBestActivePositions = async ( - currency: TCurrency, - pagination?: Pagination -): Promise[]> => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get best active positions for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => baseClients[currency as Exclude].fetchClaims(...params, pagination), - }; - - const ids = await getBestActiveOrderIds(client); - - const positions = await Promise.all( - ids.map((id: string) => { - return getOrderById(client, id).then(async (position) => { - // Check if the BTC dust lock UTXO is spent, and filter out the position if it is - if (isBtcCurrency(currency)) { - const { l1TxId, vout } = (position.chainData || {}) as { l1TxId: string; vout: number }; - if (l1TxId && Number.isInteger(vout)) { - const utxo = await getTxOutputDataByVout(l1TxId, Number(vout)); - if (utxo.spent) { - return null; - } - } - } - - return position; - }); - }) - ) - .then((positions) => positions.filter((position): position is NonNullable => Boolean(position))) - .then((positions) => { - return ( - positions - // TODO: Remove this filter when we have a proper solution for reversed orders - .filter((position) => { - if (['btcswap'].includes(import.meta.env.MODE)) { - return position.l1Amount === position.minL1Amount; - } else { - return ( - position.funds >= 1n * 10n ** BigInt(decimalsForCurrency(Currency.CWEB)) && - position.l1Amount !== position.minL1Amount - ); - } - }) - ); - }); - - return positions as unknown as C1Position[]; -}; diff --git a/packages/ui/src/api/getHistoryAccess.ts b/packages/ui/src/api/getHistoryAccess.ts deleted file mode 100644 index 204917e..0000000 --- a/packages/ui/src/api/getHistoryAccess.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Client, getHistoryAccess as getHistoryAccessFromContract, HistoryAccessClaimBody } from 'dex-app.cm'; - -import { getContractIdsFromRoute } from '@/components/trackers/HistoryDiagram/utils'; -import { HistoryRoute } from '@/types'; - -import { clients } from './client'; - -export const getHistoryAccess = async (unique: string, route?: HistoryRoute) => { - const contractIds = getContractIdsFromRoute({ - route, - }); - - const results = await Promise.allSettled( - contractIds.map((contractId) => { - const client: Client = { - fetchClaims: (...params) => clients[contractId].fetchClaims(...params), - }; - - return getHistoryAccessFromContract(client, unique); - }) - ); - - const access = results.find( - (result): result is PromiseFulfilledResult => - result.status === 'fulfilled' && !!result.value - )?.value; - - return access ?? null; -}; diff --git a/packages/ui/src/api/getHistoryByHistoryAccess.ts b/packages/ui/src/api/getHistoryByHistoryAccess.ts deleted file mode 100644 index 9a54a6f..0000000 --- a/packages/ui/src/api/getHistoryByHistoryAccess.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { Client, getHistoryByHistoryAccess as getHistory, HistoryData, HistoryAccess } from 'dex-app.cm'; - -import { clients } from './client'; - -export async function getHistoryByHistoryAccess(access: HistoryAccess, contractIds: string[]) { - const results = await Promise.allSettled( - contractIds.map((contractId) => { - const client: Client = { - fetchClaims: (...params) => clients[contractId].fetchClaims(...params), - }; - - return getHistory(client, access); - }) - ); - - return results - .filter((res): res is PromiseFulfilledResult => res.status === 'fulfilled') - .flatMap(({ value }) => value); -} diff --git a/packages/ui/src/api/getHistoryByTrackerOrAccess.ts b/packages/ui/src/api/getHistoryByTrackerOrAccess.ts deleted file mode 100644 index 461bab0..0000000 --- a/packages/ui/src/api/getHistoryByTrackerOrAccess.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Client, getHistoryAccess, HistoryAccessClaimBody, getHistoryByHistoryAccess, HistoryData } from 'dex-app.cm'; - -import { getContractIdsFromRoute } from '@/components/trackers/HistoryDiagram/utils'; -import { HistoryAccess, Tracker } from '@/types'; - -import { clients } from './client'; - -const getAccess = async (trackerOrAccess: Tracker | HistoryAccess) => { - if ('historyId' in trackerOrAccess) { - const access = trackerOrAccess; - - return access; - } - - const tracker = trackerOrAccess; - - const { route, unique } = tracker; - - const contractIds = getContractIdsFromRoute({ - route, - }); - - const results = await Promise.allSettled( - contractIds.map((contractId) => { - const client: Client = { - fetchClaims: (...params) => clients[contractId].fetchClaims(...params), - }; - - return getHistoryAccess(client, unique); - }) - ); - - const access = results.find( - (result): result is PromiseFulfilledResult => - result.status === 'fulfilled' && !!result.value - )?.value; - - return access; -}; - -export async function getHistoryByTrackerOrAccess(trackerOrAccess: Tracker | HistoryAccess): Promise { - const access = await getAccess(trackerOrAccess); - - if (access) { - const { route } = trackerOrAccess; - - const contractIds = getContractIdsFromRoute({ - route, - }); - - const results = await Promise.allSettled( - contractIds.map((contractId) => { - const client: Client = { - fetchClaims: (...params) => clients[contractId].fetchClaims(...params), - }; - - return getHistoryByHistoryAccess(client, access); - }) - ); - - console.log(results, 'results'); - - return results - .filter((res): res is PromiseFulfilledResult => res.status === 'fulfilled') - .flatMap(({ value }) => value); - } - - return []; -} diff --git a/packages/ui/src/api/getMarketMakerOrders.ts b/packages/ui/src/api/getMarketMakerOrders.ts deleted file mode 100644 index 0edb2b0..0000000 --- a/packages/ui/src/api/getMarketMakerOrders.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { type PubKey } from '@coinweb/wallet-lib'; -import { type Client, getAllOwnOrdersIds, getOrderById, type Pagination } from 'market-maker.cm'; - -import { Currency } from '../constants'; - -import { makerClients } from './client'; - -export const getMarketMakerOrders = async (currency: Currency, pubKey: PubKey, pagination?: Pagination) => { - if (currency === Currency.CWEB) { - throw new Error(`Cannot get market maker orders for: ${currency}`); - } - - const client: Client = { - fetchClaims: (...params) => { - if (params.length < 3) { - params.push(pagination); - } - - return makerClients[currency].fetchClaims(...params, pagination); - }, - }; - - const noPaginationClient: Client = { - fetchClaims: (...params) => makerClients[currency].fetchClaims(...params), - }; - - const ids = await getAllOwnOrdersIds(client, { - auth: 'EcdsaContract', - payload: pubKey, - }); - - return Promise.all(ids.map((id: string) => getOrderById(noPaginationClient, id))); -}; diff --git a/packages/ui/src/api/getUtxosInUse.ts b/packages/ui/src/api/getUtxosInUse.ts deleted file mode 100644 index f0c388f..0000000 --- a/packages/ui/src/api/getUtxosInUse.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { type Client, getUtxosInUse as getUtxosInUseFromApi } from 'dex-app.cm'; - -import { Currency } from '../constants'; - -import { baseClients } from './client'; - -export const getBtcUtxosInUse = async (currency: Currency) => { - if (currency !== Currency.BTC) { - return []; - } - - const client: Client = { - fetchClaims: (...params) => baseClients[currency].fetchClaims(...params), - }; - - return getUtxosInUseFromApi(client); -}; diff --git a/packages/ui/src/api/index.ts b/packages/ui/src/api/index.ts deleted file mode 100644 index 33f96ba..0000000 --- a/packages/ui/src/api/index.ts +++ /dev/null @@ -1,15 +0,0 @@ -export * from './client'; -export * from './getActiveMarketOrders'; -export * from './getAllActivePositions'; -export * from './getAllMarketClaim'; -export * from './getAllMarketCollateralBalance'; -export * from './getAllUserPositions'; -export * from './getAllUserPositionsCount'; -export * from './getBestActiveMarketOrders'; -export * from './getBestActivePositions'; -export * from './getHistoryByHistoryAccess'; -export * from './getHistoryByTrackerOrAccess'; -export * from './getMarketMakerOrders'; -export * from './getUtxosInUse'; -export * from './withQueryClient'; -export * from './getHistoryAccess'; diff --git a/packages/ui/src/api/withQueryClient/fetchHistoryAccess.ts b/packages/ui/src/api/withQueryClient/fetchHistoryAccess.ts deleted file mode 100644 index bceeb68..0000000 --- a/packages/ui/src/api/withQueryClient/fetchHistoryAccess.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { queryClient } from '@/app/queryClient'; -import { HistoryRoute } from '@/types'; - -import { getHistoryAccess } from '../getHistoryAccess'; - -export const fetchHistoryAccess = (unique: string, route?: HistoryRoute) => - queryClient.fetchQuery({ - queryKey: ['HistoryAccess', unique], - queryFn: async () => { - const result = await getHistoryAccess(unique, route); - - return result; - }, - }); diff --git a/packages/ui/src/api/withQueryClient/fetchHistoryByHistoryAccess.ts b/packages/ui/src/api/withQueryClient/fetchHistoryByHistoryAccess.ts deleted file mode 100644 index 5494c12..0000000 --- a/packages/ui/src/api/withQueryClient/fetchHistoryByHistoryAccess.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { HistoryAccess } from 'dex-app.cm'; - -import { getHistoryByHistoryAccess } from '@/api'; -import { queryClient } from '@/app/queryClient'; - -export const fetchHistoryByHistoryAccess = (access: HistoryAccess, contractIds: string[]) => - queryClient.fetchQuery({ - queryKey: ['HistoryNotifications', access.historyId], - queryFn: async () => { - const result = await getHistoryByHistoryAccess(access, contractIds); - - return result ?? []; - }, - }); diff --git a/packages/ui/src/api/withQueryClient/index.ts b/packages/ui/src/api/withQueryClient/index.ts deleted file mode 100644 index 51f00ce..0000000 --- a/packages/ui/src/api/withQueryClient/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './fetchHistoryByHistoryAccess'; -export * from './fetchHistoryAccess'; diff --git a/packages/ui/vite.config.ts b/packages/ui/vite.config.ts index 1e54617..abf2266 100644 --- a/packages/ui/vite.config.ts +++ b/packages/ui/vite.config.ts @@ -24,12 +24,7 @@ export default defineConfig(({ mode }) => { output: { manualChunks: { react: ['react', 'react-dom', 'react-router-dom'], - reown: ['@reown/appkit'], - 'reown-adapter-btc': ['@reown/appkit-adapter-bitcoin'], - 'reown-adapter-wagmi': ['@reown/appkit-adapter-wagmi'], - wagmi: ['wagmi', 'viem'], - cms: ['market-maker.cm', 'dex-app.cm'], - particle: ['@particle-network/btc-connectkit'], + cms: ['contract.cm'], }, }, onwarn(warning, warn) { diff --git a/yarn.lock b/yarn.lock index 251ffb2..0e862fd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2975,8 +2975,6 @@ __metadata: bech32: "npm:2.0.0" bs58: "npm:6.0.0" esbuild: "npm:^0.20.2" - js-yaml: "npm:^4.1.0" - write-yaml-file: "npm:^5.0.0" languageName: unknown linkType: soft @@ -9571,26 +9569,6 @@ __metadata: languageName: node linkType: hard -"write-file-atomic@npm:^5.0.1": - version: 5.0.1 - resolution: "write-file-atomic@npm:5.0.1" - dependencies: - imurmurhash: "npm:^0.1.4" - signal-exit: "npm:^4.0.1" - checksum: 10c0/e8c850a8e3e74eeadadb8ad23c9d9d63e4e792bd10f4836ed74189ef6e996763959f1249c5650e232f3c77c11169d239cbfc8342fc70f3fe401407d23810505d - languageName: node - linkType: hard - -"write-yaml-file@npm:^5.0.0": - version: 5.0.0 - resolution: "write-yaml-file@npm:5.0.0" - dependencies: - js-yaml: "npm:^4.1.0" - write-file-atomic: "npm:^5.0.1" - checksum: 10c0/65fc968225dc216ff1e120f0c0329188d8057e7f1d6abe7bda25d5f074b691495f2f8d48272e29aaceb7356d163a4812b400e6c549e26e50e97ac601308ea3c8 - languageName: node - linkType: hard - "ws@npm:^8.16.0, ws@npm:^8.18.0": version: 8.18.1 resolution: "ws@npm:8.18.1"