@liberfi.io/react-predict 0.3.52 → 0.3.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +26 -2
- package/dist/index.d.ts +26 -2
- package/dist/index.js +477 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +478 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1567,7 +1567,6 @@ function useOrder(params, queryOptions = {}) {
|
|
|
1567
1567
|
}
|
|
1568
1568
|
function useCancelOrder(options, mutationOptions = {}) {
|
|
1569
1569
|
const client = usePredictClient();
|
|
1570
|
-
const queryClient = reactQuery.useQueryClient();
|
|
1571
1570
|
const { onSuccess, onError, ...restMutationOptions } = mutationOptions;
|
|
1572
1571
|
return reactQuery.useMutation({
|
|
1573
1572
|
mutationFn: async (vars) => {
|
|
@@ -1575,7 +1574,6 @@ function useCancelOrder(options, mutationOptions = {}) {
|
|
|
1575
1574
|
return client.cancelOrder(vars.id, vars.source, headers);
|
|
1576
1575
|
},
|
|
1577
1576
|
onSuccess: (...args) => {
|
|
1578
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "orders"] });
|
|
1579
1577
|
onSuccess?.(...args);
|
|
1580
1578
|
},
|
|
1581
1579
|
onError: (...args) => {
|
|
@@ -1584,6 +1582,480 @@ function useCancelOrder(options, mutationOptions = {}) {
|
|
|
1584
1582
|
...restMutationOptions
|
|
1585
1583
|
});
|
|
1586
1584
|
}
|
|
1585
|
+
var DEFAULT_MAX_DURATION_MS = 3e4;
|
|
1586
|
+
var FAST_INTERVAL_MS = 2e3;
|
|
1587
|
+
var SLOW_INTERVAL_MS = 3e3;
|
|
1588
|
+
var SLOW_AFTER_MS = 12e3;
|
|
1589
|
+
var QUERY_PREFIXES = [
|
|
1590
|
+
["predict", "trades"],
|
|
1591
|
+
["predict", "trades-by-wallet"],
|
|
1592
|
+
["predict", "available-shares"]
|
|
1593
|
+
];
|
|
1594
|
+
var OPEN_ORDER_STATUSES = /* @__PURE__ */ new Set(["live", "open", "submitted", "pending"]);
|
|
1595
|
+
function getPositionsWallets(input) {
|
|
1596
|
+
const wallets = {
|
|
1597
|
+
kalshi_user: input.kalshiUser ?? (input.source === "kalshi" ? input.user : void 0),
|
|
1598
|
+
polymarket_user: input.polymarketUser ?? (input.source === "polymarket" ? input.user : void 0)
|
|
1599
|
+
};
|
|
1600
|
+
return wallets.kalshi_user || wallets.polymarket_user ? wallets : void 0;
|
|
1601
|
+
}
|
|
1602
|
+
function getOrdersWalletSets(input) {
|
|
1603
|
+
const wallets = getPositionsWallets(input);
|
|
1604
|
+
const sourceWallet = input.source === "kalshi" ? { kalshi_user: input.user } : { polymarket_user: input.user };
|
|
1605
|
+
const result = [];
|
|
1606
|
+
if (input.user) {
|
|
1607
|
+
result.push(sourceWallet);
|
|
1608
|
+
}
|
|
1609
|
+
if (wallets) {
|
|
1610
|
+
const duplicate = result.some(
|
|
1611
|
+
(item) => item.kalshi_user === wallets.kalshi_user && item.polymarket_user === wallets.polymarket_user
|
|
1612
|
+
);
|
|
1613
|
+
if (!duplicate) {
|
|
1614
|
+
result.push(wallets);
|
|
1615
|
+
}
|
|
1616
|
+
}
|
|
1617
|
+
return result;
|
|
1618
|
+
}
|
|
1619
|
+
function getSourceOnlyWallets(input) {
|
|
1620
|
+
if (!input.user) return void 0;
|
|
1621
|
+
return input.source === "kalshi" ? { kalshi_user: input.user } : { polymarket_user: input.user };
|
|
1622
|
+
}
|
|
1623
|
+
function dataForSourcePositions(data, source) {
|
|
1624
|
+
if (!data || typeof data !== "object") return data;
|
|
1625
|
+
const response = data;
|
|
1626
|
+
if (!Array.isArray(response.positions)) return data;
|
|
1627
|
+
return {
|
|
1628
|
+
...response,
|
|
1629
|
+
positions: response.positions.filter(
|
|
1630
|
+
(position) => !position.source || position.source === source
|
|
1631
|
+
)
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1634
|
+
function normalizeBalanceData(data) {
|
|
1635
|
+
if (!data || typeof data !== "object") return data;
|
|
1636
|
+
const balance = data;
|
|
1637
|
+
return {
|
|
1638
|
+
source: balance.source,
|
|
1639
|
+
user: balance.user,
|
|
1640
|
+
balance: balance.balance,
|
|
1641
|
+
raw_balance: balance.raw_balance
|
|
1642
|
+
};
|
|
1643
|
+
}
|
|
1644
|
+
function normalizePositionsData(data, input) {
|
|
1645
|
+
if (!data || typeof data !== "object") return data;
|
|
1646
|
+
const response = data;
|
|
1647
|
+
return (response.positions ?? []).filter((position) => {
|
|
1648
|
+
const eventSlug = typeof position.event === "object" && position.event ? position.event.slug : void 0;
|
|
1649
|
+
const marketSlug = typeof position.market === "object" && position.market ? position.market.slug : void 0;
|
|
1650
|
+
if (input.marketSlug && marketSlug && marketSlug !== input.marketSlug) {
|
|
1651
|
+
return false;
|
|
1652
|
+
}
|
|
1653
|
+
if (input.eventSlug && eventSlug && eventSlug !== input.eventSlug) {
|
|
1654
|
+
return false;
|
|
1655
|
+
}
|
|
1656
|
+
if (position.source && position.source !== input.source) return false;
|
|
1657
|
+
return true;
|
|
1658
|
+
}).map((position) => ({
|
|
1659
|
+
source: position.source,
|
|
1660
|
+
side: position.side,
|
|
1661
|
+
size: position.size,
|
|
1662
|
+
redeemable: position.redeemable,
|
|
1663
|
+
eventSlug: typeof position.event === "object" && position.event ? position.event.slug : void 0,
|
|
1664
|
+
marketSlug: typeof position.market === "object" && position.market ? position.market.slug : void 0
|
|
1665
|
+
})).sort(
|
|
1666
|
+
(a, b) => `${a.source}:${a.marketSlug}:${a.side}`.localeCompare(
|
|
1667
|
+
`${b.source}:${b.marketSlug}:${b.side}`
|
|
1668
|
+
)
|
|
1669
|
+
);
|
|
1670
|
+
}
|
|
1671
|
+
function normalizeOrdersData(data, input) {
|
|
1672
|
+
if (!data || typeof data !== "object") return data;
|
|
1673
|
+
const response = data;
|
|
1674
|
+
return (response.orders ?? response.items ?? []).filter((order) => {
|
|
1675
|
+
const eventSlug = typeof order.event === "object" && order.event ? order.event.slug : void 0;
|
|
1676
|
+
const marketSlug = typeof order.market === "object" && order.market ? order.market.slug : void 0;
|
|
1677
|
+
const status = typeof order.status === "string" ? order.status : "";
|
|
1678
|
+
if (!OPEN_ORDER_STATUSES.has(status)) return false;
|
|
1679
|
+
if (input.marketSlug && marketSlug && marketSlug !== input.marketSlug) {
|
|
1680
|
+
return false;
|
|
1681
|
+
}
|
|
1682
|
+
if (input.eventSlug && eventSlug && eventSlug !== input.eventSlug) {
|
|
1683
|
+
return false;
|
|
1684
|
+
}
|
|
1685
|
+
if (order.source && order.source !== input.source) return false;
|
|
1686
|
+
return true;
|
|
1687
|
+
}).map((order) => ({
|
|
1688
|
+
id: order.id,
|
|
1689
|
+
source: order.source,
|
|
1690
|
+
status: order.status,
|
|
1691
|
+
side: order.side,
|
|
1692
|
+
outcome: order.outcome,
|
|
1693
|
+
price: order.price,
|
|
1694
|
+
originalSize: order.original_size,
|
|
1695
|
+
marketSlug: typeof order.market === "object" && order.market ? order.market.slug : void 0,
|
|
1696
|
+
eventSlug: typeof order.event === "object" && order.event ? order.event.slug : void 0
|
|
1697
|
+
})).sort((a, b) => String(a.id).localeCompare(String(b.id)));
|
|
1698
|
+
}
|
|
1699
|
+
function getCachedAccountSnapshot(client, input) {
|
|
1700
|
+
const snapshot = {};
|
|
1701
|
+
if (input.user) {
|
|
1702
|
+
const balance = client.getQueryData(
|
|
1703
|
+
balanceQueryKey(input.source, input.user)
|
|
1704
|
+
);
|
|
1705
|
+
if (balance !== void 0) {
|
|
1706
|
+
snapshot.balance = normalizeBalanceData(balance);
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
const wallets = getPositionsWallets(input);
|
|
1710
|
+
const multiPositions = wallets ? client.getQueryData(positionsMultiQueryKey(wallets)) : void 0;
|
|
1711
|
+
const singlePositions = input.user ? client.getQueryData(positionsQueryKey(input.user, input.source)) : void 0;
|
|
1712
|
+
const positions = multiPositions ?? singlePositions;
|
|
1713
|
+
if (positions !== void 0) {
|
|
1714
|
+
snapshot.positions = normalizePositionsData(positions, input);
|
|
1715
|
+
}
|
|
1716
|
+
const orders = input.user ? client.getQueryData(
|
|
1717
|
+
ordersQueryKey({ source: input.source, wallet_address: input.user })
|
|
1718
|
+
) : void 0;
|
|
1719
|
+
if (orders !== void 0) {
|
|
1720
|
+
snapshot.orders = normalizeOrdersData(orders, input);
|
|
1721
|
+
}
|
|
1722
|
+
return snapshot;
|
|
1723
|
+
}
|
|
1724
|
+
function serializeSnapshotField(value) {
|
|
1725
|
+
return JSON.stringify(value);
|
|
1726
|
+
}
|
|
1727
|
+
function didFieldChange(field, baseline, current) {
|
|
1728
|
+
if (baseline[field] === void 0 || current[field] === void 0) {
|
|
1729
|
+
return false;
|
|
1730
|
+
}
|
|
1731
|
+
return serializeSnapshotField(current[field]) !== serializeSnapshotField(baseline[field]);
|
|
1732
|
+
}
|
|
1733
|
+
function parseNumeric(value) {
|
|
1734
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
1735
|
+
if (typeof value === "string") {
|
|
1736
|
+
const parsed = Number(value);
|
|
1737
|
+
return Number.isFinite(parsed) ? parsed : void 0;
|
|
1738
|
+
}
|
|
1739
|
+
return void 0;
|
|
1740
|
+
}
|
|
1741
|
+
function getBalanceAmount(snapshot) {
|
|
1742
|
+
if (!snapshot.balance || typeof snapshot.balance !== "object")
|
|
1743
|
+
return void 0;
|
|
1744
|
+
const balance = snapshot.balance;
|
|
1745
|
+
return parseNumeric(balance.raw_balance) ?? parseNumeric(balance.balance);
|
|
1746
|
+
}
|
|
1747
|
+
function getPositionSizeTotal(snapshot) {
|
|
1748
|
+
if (!Array.isArray(snapshot.positions)) return void 0;
|
|
1749
|
+
return snapshot.positions.reduce((total, position) => {
|
|
1750
|
+
if (!position || typeof position !== "object") return total;
|
|
1751
|
+
const size = parseNumeric(position.size);
|
|
1752
|
+
return total + (size ?? 0);
|
|
1753
|
+
}, 0);
|
|
1754
|
+
}
|
|
1755
|
+
function getOpenOrderCount(snapshot) {
|
|
1756
|
+
if (!Array.isArray(snapshot.orders)) return void 0;
|
|
1757
|
+
return snapshot.orders.length;
|
|
1758
|
+
}
|
|
1759
|
+
function hasBothAccountFields(snapshot) {
|
|
1760
|
+
return snapshot.balance !== void 0 && snapshot.positions !== void 0;
|
|
1761
|
+
}
|
|
1762
|
+
function isTradeResultConfirmed(expectation, baseline, current) {
|
|
1763
|
+
const ordersChanged = didFieldChange("orders", baseline, current);
|
|
1764
|
+
const baselineOpenOrders = getOpenOrderCount(baseline);
|
|
1765
|
+
const currentOpenOrders = getOpenOrderCount(current);
|
|
1766
|
+
const openOrdersIncreased = baselineOpenOrders !== void 0 && currentOpenOrders !== void 0 && currentOpenOrders > baselineOpenOrders;
|
|
1767
|
+
if (expectation === "buy-limit" || expectation === "sell-limit") {
|
|
1768
|
+
if (baselineOpenOrders === void 0) {
|
|
1769
|
+
return currentOpenOrders !== void 0 && currentOpenOrders > 0;
|
|
1770
|
+
}
|
|
1771
|
+
return openOrdersIncreased || ordersChanged;
|
|
1772
|
+
}
|
|
1773
|
+
if (!hasBothAccountFields(baseline) || !hasBothAccountFields(current)) {
|
|
1774
|
+
return false;
|
|
1775
|
+
}
|
|
1776
|
+
const balanceChanged = didFieldChange("balance", baseline, current);
|
|
1777
|
+
const positionsChanged = didFieldChange("positions", baseline, current);
|
|
1778
|
+
const baselineBalance = getBalanceAmount(baseline);
|
|
1779
|
+
const currentBalance = getBalanceAmount(current);
|
|
1780
|
+
const baselinePositionSize = getPositionSizeTotal(baseline);
|
|
1781
|
+
const currentPositionSize = getPositionSizeTotal(current);
|
|
1782
|
+
const balanceDecreased = baselineBalance !== void 0 && currentBalance !== void 0 && currentBalance < baselineBalance;
|
|
1783
|
+
const balanceIncreased = baselineBalance !== void 0 && currentBalance !== void 0 && currentBalance > baselineBalance;
|
|
1784
|
+
const positionsIncreased = baselinePositionSize !== void 0 && currentPositionSize !== void 0 && currentPositionSize > baselinePositionSize;
|
|
1785
|
+
const positionsDecreased = baselinePositionSize !== void 0 && currentPositionSize !== void 0 && currentPositionSize < baselinePositionSize;
|
|
1786
|
+
switch (expectation) {
|
|
1787
|
+
case "buy-market":
|
|
1788
|
+
return balanceDecreased && positionsIncreased;
|
|
1789
|
+
case "sell-market":
|
|
1790
|
+
return balanceIncreased && positionsDecreased;
|
|
1791
|
+
case "redeem":
|
|
1792
|
+
return balanceChanged && positionsChanged;
|
|
1793
|
+
case "cancel-order":
|
|
1794
|
+
return false;
|
|
1795
|
+
default:
|
|
1796
|
+
return false;
|
|
1797
|
+
}
|
|
1798
|
+
}
|
|
1799
|
+
function hasOpenTargetOrder(client, orderId) {
|
|
1800
|
+
const orderQueries = client.getQueriesData({
|
|
1801
|
+
queryKey: ["predict", "orders"]
|
|
1802
|
+
});
|
|
1803
|
+
return orderQueries.some(([, data]) => dataContainsOpenOrder(data, orderId));
|
|
1804
|
+
}
|
|
1805
|
+
function dataContainsOpenOrder(data, orderId) {
|
|
1806
|
+
if (!data || typeof data !== "object") return false;
|
|
1807
|
+
if (Array.isArray(data)) {
|
|
1808
|
+
return data.some((item) => dataContainsOpenOrder(item, orderId));
|
|
1809
|
+
}
|
|
1810
|
+
const record = data;
|
|
1811
|
+
if (record.id === orderId) {
|
|
1812
|
+
const status = typeof record.status === "string" ? record.status : "";
|
|
1813
|
+
return !status || OPEN_ORDER_STATUSES.has(status);
|
|
1814
|
+
}
|
|
1815
|
+
for (const value of Object.values(record)) {
|
|
1816
|
+
if (dataContainsOpenOrder(value, orderId)) return true;
|
|
1817
|
+
}
|
|
1818
|
+
return false;
|
|
1819
|
+
}
|
|
1820
|
+
function refetchActiveTradeResultQueries(queryClient) {
|
|
1821
|
+
for (const queryKey of QUERY_PREFIXES) {
|
|
1822
|
+
void queryClient.refetchQueries({ queryKey, type: "active" });
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
function requestBalanceSnapshot(queryClient, predictClient, input) {
|
|
1826
|
+
if (!input.user) return Promise.resolve(void 0);
|
|
1827
|
+
const queryKey = balanceQueryKey(input.source, input.user);
|
|
1828
|
+
return queryClient.cancelQueries({ queryKey }).then(() => predictClient.getBalance(input.source, input.user)).then((data) => {
|
|
1829
|
+
queryClient.setQueryData(queryKey, data);
|
|
1830
|
+
return normalizeBalanceData(data);
|
|
1831
|
+
});
|
|
1832
|
+
}
|
|
1833
|
+
function requestPositionsSnapshot(queryClient, predictClient, input) {
|
|
1834
|
+
const wallets = getPositionsWallets(input);
|
|
1835
|
+
if (!wallets) return Promise.resolve(void 0);
|
|
1836
|
+
const sourceOnlyWallets = getSourceOnlyWallets(input);
|
|
1837
|
+
const cancelTasks = [
|
|
1838
|
+
queryClient.cancelQueries({ queryKey: positionsMultiQueryKey(wallets) })
|
|
1839
|
+
];
|
|
1840
|
+
if (sourceOnlyWallets) {
|
|
1841
|
+
cancelTasks.push(
|
|
1842
|
+
queryClient.cancelQueries({
|
|
1843
|
+
queryKey: positionsMultiQueryKey(sourceOnlyWallets)
|
|
1844
|
+
})
|
|
1845
|
+
);
|
|
1846
|
+
}
|
|
1847
|
+
if (input.user) {
|
|
1848
|
+
cancelTasks.push(
|
|
1849
|
+
queryClient.cancelQueries({
|
|
1850
|
+
queryKey: positionsQueryKey(input.user, input.source)
|
|
1851
|
+
})
|
|
1852
|
+
);
|
|
1853
|
+
}
|
|
1854
|
+
return Promise.all(cancelTasks).then(() => predictClient.getPositions(wallets)).then((data) => {
|
|
1855
|
+
queryClient.setQueryData(positionsMultiQueryKey(wallets), data);
|
|
1856
|
+
if (sourceOnlyWallets) {
|
|
1857
|
+
queryClient.setQueryData(
|
|
1858
|
+
positionsMultiQueryKey(sourceOnlyWallets),
|
|
1859
|
+
dataForSourcePositions(data, input.source)
|
|
1860
|
+
);
|
|
1861
|
+
}
|
|
1862
|
+
if (input.user) {
|
|
1863
|
+
queryClient.setQueryData(
|
|
1864
|
+
positionsQueryKey(input.user, input.source),
|
|
1865
|
+
dataForSourcePositions(data, input.source)
|
|
1866
|
+
);
|
|
1867
|
+
}
|
|
1868
|
+
return normalizePositionsData(data, input);
|
|
1869
|
+
});
|
|
1870
|
+
}
|
|
1871
|
+
function requestOrdersSnapshot(queryClient, predictClient, input) {
|
|
1872
|
+
if (!input.user) return Promise.resolve(void 0);
|
|
1873
|
+
const params = { source: input.source, wallet_address: input.user };
|
|
1874
|
+
return Promise.resolve(input.getOrdersHeaders?.()).then((headers) => predictClient.listOrders(params, headers)).then((data) => {
|
|
1875
|
+
queryClient.setQueryData(ordersQueryKey(params), data);
|
|
1876
|
+
return normalizeOrdersData(data, input);
|
|
1877
|
+
});
|
|
1878
|
+
}
|
|
1879
|
+
function refreshEnrichedOrdersSnapshot(queryClient, predictClient, input) {
|
|
1880
|
+
const walletSets = getOrdersWalletSets(input);
|
|
1881
|
+
if (walletSets.length === 0) return Promise.resolve();
|
|
1882
|
+
return Promise.resolve(input.getOrdersHeaders?.()).then(
|
|
1883
|
+
(headers) => Promise.all(
|
|
1884
|
+
walletSets.map(
|
|
1885
|
+
(wallets) => predictClient.listOrdersMulti(wallets, headers).then((data) => {
|
|
1886
|
+
queryClient.setQueryData(ordersMultiQueryKey(wallets), data);
|
|
1887
|
+
})
|
|
1888
|
+
)
|
|
1889
|
+
)
|
|
1890
|
+
).then(() => void 0).catch(() => void 0);
|
|
1891
|
+
}
|
|
1892
|
+
function isLimitExpectation(expectation) {
|
|
1893
|
+
return expectation === "buy-limit" || expectation === "sell-limit";
|
|
1894
|
+
}
|
|
1895
|
+
function nextPollInterval(startedAt) {
|
|
1896
|
+
const elapsed = Date.now() - startedAt;
|
|
1897
|
+
return elapsed < SLOW_AFTER_MS ? FAST_INTERVAL_MS : SLOW_INTERVAL_MS;
|
|
1898
|
+
}
|
|
1899
|
+
function createDeferredConfirmation() {
|
|
1900
|
+
let resolveResult = () => void 0;
|
|
1901
|
+
const promise = new Promise((resolve) => {
|
|
1902
|
+
resolveResult = resolve;
|
|
1903
|
+
});
|
|
1904
|
+
return { promise, resolve: resolveResult };
|
|
1905
|
+
}
|
|
1906
|
+
function sleep(ms) {
|
|
1907
|
+
return new Promise((resolve) => {
|
|
1908
|
+
setTimeout(resolve, ms);
|
|
1909
|
+
});
|
|
1910
|
+
}
|
|
1911
|
+
async function runFixedIntervalConfirmation({
|
|
1912
|
+
queryClient,
|
|
1913
|
+
predictClient,
|
|
1914
|
+
input,
|
|
1915
|
+
startedAt,
|
|
1916
|
+
deadlineAt,
|
|
1917
|
+
initialBaseline,
|
|
1918
|
+
hadTargetOrder,
|
|
1919
|
+
confirm,
|
|
1920
|
+
delay
|
|
1921
|
+
}) {
|
|
1922
|
+
const baseline = { ...initialBaseline };
|
|
1923
|
+
const current = { ...initialBaseline };
|
|
1924
|
+
const inFlight = {
|
|
1925
|
+
balance: false,
|
|
1926
|
+
positions: false,
|
|
1927
|
+
orders: false
|
|
1928
|
+
};
|
|
1929
|
+
let settled = false;
|
|
1930
|
+
const finishConfirmed = () => {
|
|
1931
|
+
settled = true;
|
|
1932
|
+
if (!isLimitExpectation(input.expectation)) {
|
|
1933
|
+
confirm();
|
|
1934
|
+
return;
|
|
1935
|
+
}
|
|
1936
|
+
void refreshEnrichedOrdersSnapshot(
|
|
1937
|
+
queryClient,
|
|
1938
|
+
predictClient,
|
|
1939
|
+
input
|
|
1940
|
+
).finally(confirm);
|
|
1941
|
+
};
|
|
1942
|
+
const handleSnapshot = (field, value) => {
|
|
1943
|
+
if (value === void 0 || settled) return;
|
|
1944
|
+
if (baseline[field] === void 0) {
|
|
1945
|
+
current[field] = value;
|
|
1946
|
+
if (field === "orders" && isLimitExpectation(input.expectation) && isTradeResultConfirmed(input.expectation, baseline, current)) {
|
|
1947
|
+
finishConfirmed();
|
|
1948
|
+
return;
|
|
1949
|
+
}
|
|
1950
|
+
baseline[field] = value;
|
|
1951
|
+
return;
|
|
1952
|
+
}
|
|
1953
|
+
current[field] = value;
|
|
1954
|
+
if (isTradeResultConfirmed(input.expectation, baseline, current)) {
|
|
1955
|
+
finishConfirmed();
|
|
1956
|
+
}
|
|
1957
|
+
};
|
|
1958
|
+
const pollField = (field, request) => {
|
|
1959
|
+
if (inFlight[field] || settled) return;
|
|
1960
|
+
inFlight[field] = true;
|
|
1961
|
+
void request().then((value) => handleSnapshot(field, value)).catch(() => void 0).finally(() => {
|
|
1962
|
+
inFlight[field] = false;
|
|
1963
|
+
});
|
|
1964
|
+
};
|
|
1965
|
+
while (!settled && Date.now() <= deadlineAt) {
|
|
1966
|
+
refetchActiveTradeResultQueries(queryClient);
|
|
1967
|
+
if (input.expectation === "cancel-order" && input.orderId && hadTargetOrder) {
|
|
1968
|
+
if (!hasOpenTargetOrder(queryClient, input.orderId)) {
|
|
1969
|
+
settled = true;
|
|
1970
|
+
confirm();
|
|
1971
|
+
return;
|
|
1972
|
+
}
|
|
1973
|
+
} else if (isLimitExpectation(input.expectation)) {
|
|
1974
|
+
pollField(
|
|
1975
|
+
"orders",
|
|
1976
|
+
() => requestOrdersSnapshot(queryClient, predictClient, input)
|
|
1977
|
+
);
|
|
1978
|
+
} else {
|
|
1979
|
+
pollField(
|
|
1980
|
+
"balance",
|
|
1981
|
+
() => requestBalanceSnapshot(queryClient, predictClient, input)
|
|
1982
|
+
);
|
|
1983
|
+
pollField(
|
|
1984
|
+
"positions",
|
|
1985
|
+
() => requestPositionsSnapshot(queryClient, predictClient, input)
|
|
1986
|
+
);
|
|
1987
|
+
}
|
|
1988
|
+
await sleep(
|
|
1989
|
+
Math.min(
|
|
1990
|
+
nextPollInterval(startedAt),
|
|
1991
|
+
Math.max(0, deadlineAt - Date.now())
|
|
1992
|
+
)
|
|
1993
|
+
);
|
|
1994
|
+
}
|
|
1995
|
+
if (!settled) delay();
|
|
1996
|
+
}
|
|
1997
|
+
function useTradeResultConfirmation() {
|
|
1998
|
+
const queryClient = reactQuery.useQueryClient();
|
|
1999
|
+
const predictClient = usePredictClient();
|
|
2000
|
+
const mountedRef = react.useRef(true);
|
|
2001
|
+
const [state, setState] = react.useState({
|
|
2002
|
+
status: "idle"
|
|
2003
|
+
});
|
|
2004
|
+
react.useEffect(() => {
|
|
2005
|
+
return () => {
|
|
2006
|
+
mountedRef.current = false;
|
|
2007
|
+
};
|
|
2008
|
+
}, []);
|
|
2009
|
+
const start = react.useCallback(
|
|
2010
|
+
async (input) => {
|
|
2011
|
+
const {
|
|
2012
|
+
expectation,
|
|
2013
|
+
orderId,
|
|
2014
|
+
maxDurationMs = DEFAULT_MAX_DURATION_MS
|
|
2015
|
+
} = input;
|
|
2016
|
+
const startedAt = Date.now();
|
|
2017
|
+
const deadlineAt = startedAt + maxDurationMs;
|
|
2018
|
+
const preflightBaseline = getCachedAccountSnapshot(queryClient, input);
|
|
2019
|
+
const hadTargetOrder = expectation === "cancel-order" && orderId ? hasOpenTargetOrder(queryClient, orderId) : false;
|
|
2020
|
+
if (mountedRef.current) {
|
|
2021
|
+
setState({ status: "confirming", startedAt, deadlineAt });
|
|
2022
|
+
}
|
|
2023
|
+
const result = createDeferredConfirmation();
|
|
2024
|
+
let resolved = false;
|
|
2025
|
+
const resolveOnce = (confirmationResult) => {
|
|
2026
|
+
if (resolved) return;
|
|
2027
|
+
resolved = true;
|
|
2028
|
+
result.resolve(confirmationResult);
|
|
2029
|
+
};
|
|
2030
|
+
const confirm = () => {
|
|
2031
|
+
if (mountedRef.current) {
|
|
2032
|
+
setState({ status: "confirmed", startedAt, deadlineAt });
|
|
2033
|
+
}
|
|
2034
|
+
resolveOnce("confirmed");
|
|
2035
|
+
};
|
|
2036
|
+
const delay = () => {
|
|
2037
|
+
if (mountedRef.current) {
|
|
2038
|
+
setState({ status: "delayed", startedAt, deadlineAt });
|
|
2039
|
+
}
|
|
2040
|
+
resolveOnce("delayed");
|
|
2041
|
+
};
|
|
2042
|
+
void runFixedIntervalConfirmation({
|
|
2043
|
+
queryClient,
|
|
2044
|
+
predictClient,
|
|
2045
|
+
input,
|
|
2046
|
+
startedAt,
|
|
2047
|
+
deadlineAt,
|
|
2048
|
+
initialBaseline: preflightBaseline,
|
|
2049
|
+
hadTargetOrder,
|
|
2050
|
+
confirm,
|
|
2051
|
+
delay
|
|
2052
|
+
});
|
|
2053
|
+
return await result.promise;
|
|
2054
|
+
},
|
|
2055
|
+
[predictClient, queryClient]
|
|
2056
|
+
);
|
|
2057
|
+
return { start, state };
|
|
2058
|
+
}
|
|
1587
2059
|
|
|
1588
2060
|
// src/hooks/predict/matches.params.ts
|
|
1589
2061
|
function matchesQueryKey(params) {
|
|
@@ -1703,13 +2175,8 @@ function useDFlowQuote(params, queryOptions = {}) {
|
|
|
1703
2175
|
}
|
|
1704
2176
|
function useDFlowSubmit(mutationOptions = {}) {
|
|
1705
2177
|
const client = usePredictClient();
|
|
1706
|
-
const queryClient = reactQuery.useQueryClient();
|
|
1707
2178
|
return reactQuery.useMutation({
|
|
1708
2179
|
mutationFn: (body) => client.submitDFlowTransaction(body),
|
|
1709
|
-
onSuccess: () => {
|
|
1710
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "orders"] });
|
|
1711
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "positions"] });
|
|
1712
|
-
},
|
|
1713
2180
|
...mutationOptions
|
|
1714
2181
|
});
|
|
1715
2182
|
}
|
|
@@ -1853,7 +2320,7 @@ function usePolymarketDeposit(mutationOptions = {}) {
|
|
|
1853
2320
|
}
|
|
1854
2321
|
async function pollTxUntilConfirmed(client, txHash) {
|
|
1855
2322
|
for (let i = 0; i < TX_POLL_MAX_ATTEMPTS; i++) {
|
|
1856
|
-
await
|
|
2323
|
+
await sleep2(TX_POLL_INTERVAL);
|
|
1857
2324
|
const result = await client.depositStatus(txHash, "polymarket");
|
|
1858
2325
|
if (result.status === "confirmed") return;
|
|
1859
2326
|
if (result.status === "failed") {
|
|
@@ -1864,7 +2331,7 @@ async function pollTxUntilConfirmed(client, txHash) {
|
|
|
1864
2331
|
`Deposit transaction timed out after ${TX_POLL_MAX_ATTEMPTS * TX_POLL_INTERVAL / 1e3}s: ${txHash}`
|
|
1865
2332
|
);
|
|
1866
2333
|
}
|
|
1867
|
-
function
|
|
2334
|
+
function sleep2(ms) {
|
|
1868
2335
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
1869
2336
|
}
|
|
1870
2337
|
var polymarketDepositAddressesQueryKey = (safeAddress) => ["polymarket", "deposit-addresses", safeAddress];
|
|
@@ -1938,7 +2405,6 @@ function usePolymarketWithdrawStatusQuery(params) {
|
|
|
1938
2405
|
}
|
|
1939
2406
|
function useRedeemPosition() {
|
|
1940
2407
|
const client = usePredictClient();
|
|
1941
|
-
const queryClient = reactQuery.useQueryClient();
|
|
1942
2408
|
return reactQuery.useMutation({
|
|
1943
2409
|
mutationFn: async ({
|
|
1944
2410
|
wallet_address,
|
|
@@ -1989,10 +2455,6 @@ function useRedeemPosition() {
|
|
|
1989
2455
|
);
|
|
1990
2456
|
}
|
|
1991
2457
|
return result;
|
|
1992
|
-
},
|
|
1993
|
-
onSuccess: () => {
|
|
1994
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "positions"] });
|
|
1995
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "balance"] });
|
|
1996
2458
|
}
|
|
1997
2459
|
});
|
|
1998
2460
|
}
|
|
@@ -2521,7 +2983,6 @@ var TX_POLL_MAX_ATTEMPTS2 = 60;
|
|
|
2521
2983
|
function useCreatePolymarketOrder(mutationOptions = {}) {
|
|
2522
2984
|
const client = usePredictClient();
|
|
2523
2985
|
const { credentials, authenticate } = usePolymarket();
|
|
2524
|
-
const queryClient = reactQuery.useQueryClient();
|
|
2525
2986
|
return reactQuery.useMutation({
|
|
2526
2987
|
mutationFn: async ({
|
|
2527
2988
|
input,
|
|
@@ -2596,11 +3057,6 @@ function useCreatePolymarketOrder(mutationOptions = {}) {
|
|
|
2596
3057
|
});
|
|
2597
3058
|
return result.raw;
|
|
2598
3059
|
},
|
|
2599
|
-
onSuccess: () => {
|
|
2600
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "orders"] });
|
|
2601
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "positions"] });
|
|
2602
|
-
queryClient.invalidateQueries({ queryKey: ["predict", "balance"] });
|
|
2603
|
-
},
|
|
2604
3060
|
...mutationOptions
|
|
2605
3061
|
});
|
|
2606
3062
|
}
|
|
@@ -2850,6 +3306,7 @@ exports.useRunPolymarketSetup = useRunPolymarketSetup;
|
|
|
2850
3306
|
exports.useSearchEvents = useSearchEvents;
|
|
2851
3307
|
exports.useSimilarEvents = useSimilarEvents;
|
|
2852
3308
|
exports.useTickSize = useTickSize;
|
|
3309
|
+
exports.useTradeResultConfirmation = useTradeResultConfirmation;
|
|
2853
3310
|
exports.useTrades = useTrades;
|
|
2854
3311
|
exports.useTradesSubscription = useTradesSubscription;
|
|
2855
3312
|
exports.useWithdrawBuildMutation = useWithdrawBuildMutation;
|