fix: fix promises
This commit is contained in:
parent
cdf5b58935
commit
eecfb6367f
@ -21,7 +21,6 @@ export default tseslint.config(
|
|||||||
'**/dapp-ui/src/pages/Bot/**',
|
'**/dapp-ui/src/pages/Bot/**',
|
||||||
'**/dapp-ui/src/pages/BotHedging/**',
|
'**/dapp-ui/src/pages/BotHedging/**',
|
||||||
'**/dapp-ui/src/pages/SwapTool/**',
|
'**/dapp-ui/src/pages/SwapTool/**',
|
||||||
'**/market-maker.cm/perf_test/onchain.cjs',
|
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
// rest of the config by module
|
// rest of the config by module
|
||||||
|
|||||||
@ -18,12 +18,16 @@ function hashCode(str: string): string {
|
|||||||
|
|
||||||
export const addWord = async (...[word]: AddWordArgs) => {
|
export const addWord = async (...[word]: AddWordArgs) => {
|
||||||
const id = hashCode(word);
|
const id = hashCode(word);
|
||||||
|
console.log('Before storeOp');
|
||||||
await storeOp(constructClaim(createWordKey(id), { word }, '0x0'));
|
await storeOp(constructClaim(createWordKey(id), { word }, '0x0'));
|
||||||
|
console.log('After storeOp');
|
||||||
|
|
||||||
const wordClaim = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
|
const wordClaim = await readOp<TypedClaim<WordClaimBody>>(createWordKey(id));
|
||||||
|
console.log('After readOp');
|
||||||
const newWord = (wordClaim?.body.word ?? '') + '!!!';
|
const newWord = (wordClaim?.body.word ?? '') + '!!!';
|
||||||
const newId = hashCode(newWord);
|
const newId = hashCode(newWord);
|
||||||
|
|
||||||
|
console.log('Before 2 storeOp');
|
||||||
storeOp(constructClaim(createWordKey(newId), { word: newWord }, '0x0'));
|
storeOp(constructClaim(createWordKey(newId), { word: newWord }, '0x0'));
|
||||||
|
console.log('After 2 storeOp');
|
||||||
};
|
};
|
||||||
|
|||||||
@ -23,8 +23,10 @@ const createModule = (): ContractHandlers => {
|
|||||||
return module;
|
return module;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const cwebMain = async () => {
|
export const cwebMain = () => {
|
||||||
const module = createModule();
|
return (async () => {
|
||||||
|
const module = createModule();
|
||||||
|
|
||||||
await executeHandler(module);
|
await executeHandler(module);
|
||||||
|
})();
|
||||||
};
|
};
|
||||||
|
|||||||
@ -41,7 +41,7 @@ export const executor =
|
|||||||
const execution = new Promise<void>((resolve, reject) => {
|
const execution = new Promise<void>((resolve, reject) => {
|
||||||
abortExecution = resolve;
|
abortExecution = resolve;
|
||||||
|
|
||||||
method(...args).then(resolve, reject);
|
method(...args).then(() => resolve(), reject);
|
||||||
});
|
});
|
||||||
|
|
||||||
await execution;
|
await execution;
|
||||||
|
|||||||
@ -8,20 +8,23 @@ import { pushAwaitedOp } from './awaited';
|
|||||||
import { shiftResolvedOp } from './resolved';
|
import { shiftResolvedOp } from './resolved';
|
||||||
|
|
||||||
export const readOp = <TClaim extends Claim = TypedClaim>(key: ClaimKey) => {
|
export const readOp = <TClaim extends Claim = TypedClaim>(key: ClaimKey) => {
|
||||||
const { op, isOp } = shiftResolvedOp();
|
return new Promise<TClaim | null>((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const { op, isOp } = shiftResolvedOp();
|
||||||
|
|
||||||
if (!isOp) {
|
if (!isOp) {
|
||||||
pushAwaitedOp(constructRead(context.issuer, key));
|
pushAwaitedOp(constructRead(context.issuer, key));
|
||||||
} else if (op && !isResolvedRead(op)) {
|
abort();
|
||||||
throw new Error('Read operation not found');
|
} else {
|
||||||
}
|
if (op && !isResolvedRead(op)) {
|
||||||
|
throw new Error('Read operation not found');
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise<TClaim | null>((resolve) => {
|
const claim = op && ((extractRead(op)?.[0].content ?? null) as TClaim | null);
|
||||||
if (isOp) {
|
resolve(claim);
|
||||||
const claim = op && ((extractRead(op)?.[0] ?? null) as TClaim | null);
|
}
|
||||||
resolve(claim);
|
} catch (error) {
|
||||||
} else {
|
reject(error);
|
||||||
abort();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -6,26 +6,24 @@ import { abort } from '../executor';
|
|||||||
import { pushAwaitedOp } from './awaited';
|
import { pushAwaitedOp } from './awaited';
|
||||||
import { shiftResolvedOp } from './resolved';
|
import { shiftResolvedOp } from './resolved';
|
||||||
|
|
||||||
export const storeOp = (claim: Claim) => {
|
export const storeOp = (claim: Claim) =>
|
||||||
const { op, isOp } = shiftResolvedOp();
|
new Promise<Claim | null>((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const { op, isOp } = shiftResolvedOp();
|
||||||
|
|
||||||
if (!isOp) {
|
if (!isOp) {
|
||||||
pushAwaitedOp(constructStore(claim));
|
pushAwaitedOp(constructStore(claim));
|
||||||
} else if (op && !isResolvedStore(op)) {
|
abort();
|
||||||
throw new Error('Store operation not found');
|
} else {
|
||||||
}
|
if (op && !isResolvedStore(op)) {
|
||||||
|
throw new Error('Store operation not found');
|
||||||
|
}
|
||||||
|
|
||||||
const result = op && extractStore(op);
|
const result = op && extractStore(op);
|
||||||
|
|
||||||
if (!result) {
|
resolve(result);
|
||||||
throw new Error('Wrong store operation');
|
}
|
||||||
}
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
return new Promise<Claim>((resolve) => {
|
|
||||||
if (isOp) {
|
|
||||||
resolve(result);
|
|
||||||
} else {
|
|
||||||
abort();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|||||||
@ -6,21 +6,23 @@ import { abort } from '../executor';
|
|||||||
import { pushAwaitedOp } from './awaited';
|
import { pushAwaitedOp } from './awaited';
|
||||||
import { shiftResolvedOp } from './resolved';
|
import { shiftResolvedOp } from './resolved';
|
||||||
|
|
||||||
export const takeOp = <TClaim extends Claim = TypedClaim>(key: ClaimKey) => {
|
export const takeOp = <TClaim extends Claim = TypedClaim>(key: ClaimKey) =>
|
||||||
const { op, isOp } = shiftResolvedOp();
|
new Promise<TClaim | null>((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
const { op, isOp } = shiftResolvedOp();
|
||||||
|
|
||||||
if (!isOp) {
|
if (!isOp) {
|
||||||
pushAwaitedOp(constructTake(key));
|
pushAwaitedOp(constructTake(key));
|
||||||
} else if (op && !isResolvedTake(op)) {
|
abort();
|
||||||
throw new Error('Take operation not found');
|
} else {
|
||||||
}
|
if (op && !isResolvedTake(op)) {
|
||||||
|
throw new Error('Take operation not found');
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise<TClaim | null>((resolve) => {
|
const claim = op && extractTake(op);
|
||||||
if (isOp) {
|
resolve(claim as TClaim | null);
|
||||||
const claim = op && extractTake(op);
|
}
|
||||||
resolve(claim as TClaim | null);
|
} catch (error) {
|
||||||
} else {
|
reject(error);
|
||||||
abort();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
VITE_API_URL='https://api-cloud.coinweb.io/wallet'
|
VITE_API_URL='https://api-cloud.coinweb.io/wallet'
|
||||||
VITE_EXPLORER_URL='https://explorer.coinweb.io'
|
VITE_EXPLORER_URL='https://explorer.coinweb.io'
|
||||||
|
|
||||||
VITE_CONTRACT_ADDRESS="0x51c4d9ea120f185aab78a2fa74eba43f4e6449a261d6d369fd077012a202aa03"
|
VITE_CONTRACT_ADDRESS="0xb5bd653b3e95ea8588b23d9fad48cd4cc6debd26ad9982d1249e116fad9100d5"
|
||||||
@ -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: <T>(
|
|
||||||
firstPart: NonNullable<unknown>,
|
|
||||||
secondPart: T,
|
|
||||||
range?: {
|
|
||||||
start: T extends null ? unknown : T | null;
|
|
||||||
end: T extends null ? unknown : T | null;
|
|
||||||
},
|
|
||||||
pagination?: Pagination
|
|
||||||
) => Promise<GqlIssuedClaim[]>;
|
|
||||||
updateClient: () => void;
|
|
||||||
contractAddress: string;
|
|
||||||
networkName: NetworkName;
|
|
||||||
};
|
|
||||||
|
|
||||||
const createClient = (contractAddress: string, networkName: NetworkName) => {
|
|
||||||
const createClaimFilter = <T extends NonNullable<unknown>>(
|
|
||||||
firstPart: NonNullable<unknown>,
|
|
||||||
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 <T extends NonNullable<unknown>>(
|
|
||||||
firstPart: NonNullable<unknown>,
|
|
||||||
secondPart: T | null = null,
|
|
||||||
range?: {
|
|
||||||
start: T;
|
|
||||||
end: T;
|
|
||||||
},
|
|
||||||
pagination?: Pagination
|
|
||||||
): Promise<GqlIssuedClaim[]> => {
|
|
||||||
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<Currency, Currency.CWEB>]: 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<Currency, Currency.CWEB>]: 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<string, ContractClient> = 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];
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
@ -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);
|
|
||||||
};
|
|
||||||
@ -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<OrderData[]> => {
|
|
||||||
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
|
|
||||||
);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
*/
|
|
||||||
};
|
|
||||||
@ -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,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -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,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
@ -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<OrderData[]> => {
|
|
||||||
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)));
|
|
||||||
};
|
|
||||||
@ -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<number> => {
|
|
||||||
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;
|
|
||||||
};
|
|
||||||
@ -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);
|
|
||||||
};
|
|
||||||
@ -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 <TCurrency extends Currency>(
|
|
||||||
currency: TCurrency,
|
|
||||||
pagination?: Pagination
|
|
||||||
): Promise<C1Position<TCurrency>[]> => {
|
|
||||||
if (currency === Currency.CWEB) {
|
|
||||||
throw new Error(`Cannot get best active positions for: ${currency}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const client: Client = {
|
|
||||||
fetchClaims: (...params) => baseClients[currency as Exclude<TCurrency, 'CWEB'>].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<typeof position> => 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<TCurrency>[];
|
|
||||||
};
|
|
||||||
@ -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<HistoryAccessClaimBody> =>
|
|
||||||
result.status === 'fulfilled' && !!result.value
|
|
||||||
)?.value;
|
|
||||||
|
|
||||||
return access ?? 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<HistoryData[]> => res.status === 'fulfilled')
|
|
||||||
.flatMap(({ value }) => value);
|
|
||||||
}
|
|
||||||
@ -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<HistoryAccessClaimBody> =>
|
|
||||||
result.status === 'fulfilled' && !!result.value
|
|
||||||
)?.value;
|
|
||||||
|
|
||||||
return access;
|
|
||||||
};
|
|
||||||
|
|
||||||
export async function getHistoryByTrackerOrAccess(trackerOrAccess: Tracker | HistoryAccess): Promise<HistoryData[]> {
|
|
||||||
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<HistoryData[]> => res.status === 'fulfilled')
|
|
||||||
.flatMap(({ value }) => value);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
@ -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)));
|
|
||||||
};
|
|
||||||
@ -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);
|
|
||||||
};
|
|
||||||
@ -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';
|
|
||||||
@ -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;
|
|
||||||
},
|
|
||||||
});
|
|
||||||
@ -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 ?? [];
|
|
||||||
},
|
|
||||||
});
|
|
||||||
@ -1,2 +0,0 @@
|
|||||||
export * from './fetchHistoryByHistoryAccess';
|
|
||||||
export * from './fetchHistoryAccess';
|
|
||||||
@ -24,12 +24,7 @@ export default defineConfig(({ mode }) => {
|
|||||||
output: {
|
output: {
|
||||||
manualChunks: {
|
manualChunks: {
|
||||||
react: ['react', 'react-dom', 'react-router-dom'],
|
react: ['react', 'react-dom', 'react-router-dom'],
|
||||||
reown: ['@reown/appkit'],
|
cms: ['contract.cm'],
|
||||||
'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'],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
onwarn(warning, warn) {
|
onwarn(warning, warn) {
|
||||||
|
|||||||
22
yarn.lock
22
yarn.lock
@ -2975,8 +2975,6 @@ __metadata:
|
|||||||
bech32: "npm:2.0.0"
|
bech32: "npm:2.0.0"
|
||||||
bs58: "npm:6.0.0"
|
bs58: "npm:6.0.0"
|
||||||
esbuild: "npm:^0.20.2"
|
esbuild: "npm:^0.20.2"
|
||||||
js-yaml: "npm:^4.1.0"
|
|
||||||
write-yaml-file: "npm:^5.0.0"
|
|
||||||
languageName: unknown
|
languageName: unknown
|
||||||
linkType: soft
|
linkType: soft
|
||||||
|
|
||||||
@ -9571,26 +9569,6 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
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":
|
"ws@npm:^8.16.0, ws@npm:^8.18.0":
|
||||||
version: 8.18.1
|
version: 8.18.1
|
||||||
resolution: "ws@npm:8.18.1"
|
resolution: "ws@npm:8.18.1"
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user