@argonprotocol/mainchain 1.3.4 → 1.3.6
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/browser/index.d.ts +305 -113
- package/browser/index.js +254 -132
- package/browser/index.js.map +1 -1
- package/lib/{chunk-P3OMJABP.js → chunk-FGSUNAT4.js} +255 -133
- package/lib/chunk-FGSUNAT4.js.map +1 -0
- package/lib/{chunk-QVXW4O7X.js → chunk-KMBXJV55.js} +2 -2
- package/lib/{chunk-LB6BAURC.cjs → chunk-KYRBSY6G.cjs} +51 -51
- package/lib/{chunk-LB6BAURC.cjs.map → chunk-KYRBSY6G.cjs.map} +1 -1
- package/lib/{chunk-W5SOPU7I.cjs → chunk-U6J3AWYW.cjs} +264 -142
- package/lib/chunk-U6J3AWYW.cjs.map +1 -0
- package/lib/cli.cjs +6 -6
- package/lib/cli.js +2 -2
- package/lib/clis/index.cjs +3 -3
- package/lib/clis/index.js +2 -2
- package/lib/index.cjs +2 -2
- package/lib/index.d.cts +305 -113
- package/lib/index.d.ts +305 -113
- package/lib/index.js +1 -1
- package/package.json +2 -2
- package/lib/chunk-P3OMJABP.js.map +0 -1
- package/lib/chunk-W5SOPU7I.cjs.map +0 -1
- /package/lib/{chunk-QVXW4O7X.js.map → chunk-KMBXJV55.js.map} +0 -0
|
@@ -261,10 +261,10 @@ var TxResult = class {
|
|
|
261
261
|
import BigNumber, * as BN from "bignumber.js";
|
|
262
262
|
var { ROUND_FLOOR } = BN;
|
|
263
263
|
var MICROGONS_PER_ARGON = 1e6;
|
|
264
|
-
function formatArgons(
|
|
265
|
-
if (
|
|
266
|
-
const isNegative =
|
|
267
|
-
let format = BigNumber(
|
|
264
|
+
function formatArgons(microgons) {
|
|
265
|
+
if (microgons === void 0 || microgons === null) return "na";
|
|
266
|
+
const isNegative = microgons < 0;
|
|
267
|
+
let format = BigNumber(microgons.toString()).abs().div(MICROGONS_PER_ARGON).toFormat(2, ROUND_FLOOR);
|
|
268
268
|
if (format.endsWith(".00")) {
|
|
269
269
|
format = format.slice(0, -3);
|
|
270
270
|
}
|
|
@@ -637,9 +637,13 @@ BLOCK #${header.number}, ${header.hash.toHuman()}`);
|
|
|
637
637
|
};
|
|
638
638
|
|
|
639
639
|
// src/FrameCalculator.ts
|
|
640
|
-
var FrameCalculator = class {
|
|
640
|
+
var FrameCalculator = class _FrameCalculator {
|
|
641
641
|
miningConfig;
|
|
642
642
|
genesisTick;
|
|
643
|
+
tickMillis;
|
|
644
|
+
async load(client) {
|
|
645
|
+
return await this.getConfig(client);
|
|
646
|
+
}
|
|
643
647
|
async getForTick(client, tick) {
|
|
644
648
|
const { ticksBetweenFrames, biddingStartTick } = await this.getConfig(client);
|
|
645
649
|
const ticksSinceMiningStart = tick - biddingStartTick;
|
|
@@ -647,9 +651,10 @@ var FrameCalculator = class {
|
|
|
647
651
|
}
|
|
648
652
|
async getTickRangeForFrame(client, frameId) {
|
|
649
653
|
const { ticksBetweenFrames, biddingStartTick } = await this.getConfig(client);
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
654
|
+
return _FrameCalculator.calculateTickRangeForFrame(frameId, {
|
|
655
|
+
ticksBetweenFrames,
|
|
656
|
+
biddingStartTick
|
|
657
|
+
});
|
|
653
658
|
}
|
|
654
659
|
async getForHeader(client, header) {
|
|
655
660
|
if (header.number.toNumber() === 0) return 0;
|
|
@@ -657,18 +662,30 @@ var FrameCalculator = class {
|
|
|
657
662
|
if (tick === void 0) return void 0;
|
|
658
663
|
return this.getForTick(client, tick);
|
|
659
664
|
}
|
|
665
|
+
static frameToDateRange(frameId, config2) {
|
|
666
|
+
const [start, end] = _FrameCalculator.calculateTickRangeForFrame(frameId, config2);
|
|
667
|
+
return [new Date(start * config2.tickMillis), new Date(end * config2.tickMillis)];
|
|
668
|
+
}
|
|
669
|
+
static calculateTickRangeForFrame(frameId, config2) {
|
|
670
|
+
const { ticksBetweenFrames, biddingStartTick } = config2;
|
|
671
|
+
const startingTick = biddingStartTick + Math.floor(frameId * ticksBetweenFrames);
|
|
672
|
+
const endingTick = startingTick + ticksBetweenFrames - 1;
|
|
673
|
+
return [startingTick, endingTick];
|
|
674
|
+
}
|
|
660
675
|
async getConfig(client) {
|
|
661
676
|
this.miningConfig ??= await client.query.miningSlot.miningConfig().then((x) => ({
|
|
662
677
|
ticksBetweenSlots: x.ticksBetweenSlots.toNumber(),
|
|
663
678
|
slotBiddingStartAfterTicks: x.slotBiddingStartAfterTicks.toNumber()
|
|
664
679
|
}));
|
|
665
680
|
this.genesisTick ??= await client.query.ticks.genesisTick().then((x) => x.toNumber());
|
|
681
|
+
this.tickMillis ??= await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber());
|
|
666
682
|
const config2 = this.miningConfig;
|
|
667
683
|
const genesisTick = this.genesisTick;
|
|
668
684
|
return {
|
|
669
685
|
ticksBetweenFrames: config2.ticksBetweenSlots,
|
|
670
686
|
slotBiddingStartAfterTicks: config2.slotBiddingStartAfterTicks,
|
|
671
687
|
genesisTick,
|
|
688
|
+
tickMillis: this.tickMillis,
|
|
672
689
|
biddingStartTick: genesisTick + config2.slotBiddingStartAfterTicks
|
|
673
690
|
};
|
|
674
691
|
}
|
|
@@ -1656,31 +1673,37 @@ var VaultMonitor = class {
|
|
|
1656
1673
|
|
|
1657
1674
|
// src/CohortBidder.ts
|
|
1658
1675
|
var CohortBidder = class {
|
|
1659
|
-
constructor(accountset, cohortStartingFrameId, subaccounts, options) {
|
|
1676
|
+
constructor(accountset, cohortStartingFrameId, subaccounts, options, callbacks) {
|
|
1660
1677
|
this.accountset = accountset;
|
|
1661
1678
|
this.cohortStartingFrameId = cohortStartingFrameId;
|
|
1662
1679
|
this.subaccounts = subaccounts;
|
|
1663
1680
|
this.options = options;
|
|
1681
|
+
this.callbacks = callbacks;
|
|
1664
1682
|
this.subaccounts.forEach((x) => {
|
|
1665
1683
|
this.myAddresses.add(x.address);
|
|
1666
1684
|
});
|
|
1667
1685
|
}
|
|
1668
|
-
get
|
|
1686
|
+
get clientPromise() {
|
|
1669
1687
|
return this.accountset.client;
|
|
1670
1688
|
}
|
|
1671
1689
|
txFees = 0n;
|
|
1690
|
+
bidsAttempted = 0;
|
|
1672
1691
|
winningBids = [];
|
|
1692
|
+
myAddresses = /* @__PURE__ */ new Set();
|
|
1693
|
+
currentBids = {
|
|
1694
|
+
bids: [],
|
|
1695
|
+
mostRecentBidTick: 0,
|
|
1696
|
+
atTick: 0,
|
|
1697
|
+
atBlockNumber: 0
|
|
1698
|
+
};
|
|
1673
1699
|
unsubscribe;
|
|
1674
1700
|
pendingRequest;
|
|
1675
|
-
retryTimeout;
|
|
1676
1701
|
isStopped = false;
|
|
1677
|
-
needsRebid = false;
|
|
1678
|
-
lastBidTime = 0;
|
|
1679
1702
|
millisPerTick;
|
|
1680
1703
|
minIncrement = 10000n;
|
|
1681
1704
|
nextCohortSize;
|
|
1682
|
-
|
|
1683
|
-
|
|
1705
|
+
lastBidTick = 0;
|
|
1706
|
+
evaluateInterval;
|
|
1684
1707
|
async start() {
|
|
1685
1708
|
console.log(`Starting cohort ${this.cohortStartingFrameId} bidder`, {
|
|
1686
1709
|
maxBid: formatArgons(this.options.maxBid),
|
|
@@ -1690,7 +1713,7 @@ var CohortBidder = class {
|
|
|
1690
1713
|
bidDelay: this.options.bidDelay,
|
|
1691
1714
|
subaccounts: this.subaccounts
|
|
1692
1715
|
});
|
|
1693
|
-
const client = await this.
|
|
1716
|
+
const client = await this.clientPromise;
|
|
1694
1717
|
this.minIncrement = client.consts.miningSlot.bidIncrements.toBigInt();
|
|
1695
1718
|
this.nextCohortSize = await client.query.miningSlot.nextCohortSize().then((x) => x.toNumber());
|
|
1696
1719
|
if (this.subaccounts.length > this.nextCohortSize) {
|
|
@@ -1700,14 +1723,22 @@ var CohortBidder = class {
|
|
|
1700
1723
|
this.subaccounts.length = this.nextCohortSize;
|
|
1701
1724
|
}
|
|
1702
1725
|
this.millisPerTick = await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber());
|
|
1726
|
+
let didStart = false;
|
|
1703
1727
|
this.unsubscribe = await client.queryMulti(
|
|
1704
1728
|
[
|
|
1705
1729
|
client.query.miningSlot.bidsForNextSlotCohort,
|
|
1706
|
-
client.query.miningSlot.nextFrameId
|
|
1730
|
+
client.query.miningSlot.nextFrameId,
|
|
1731
|
+
client.query.ticks.currentTick,
|
|
1732
|
+
client.query.system.number
|
|
1707
1733
|
],
|
|
1708
|
-
async ([
|
|
1734
|
+
async ([rawBids, nextFrameId, currentTick, blockNumber]) => {
|
|
1709
1735
|
if (nextFrameId.toNumber() === this.cohortStartingFrameId) {
|
|
1710
|
-
|
|
1736
|
+
this.updateBidList(rawBids, blockNumber.toNumber(), currentTick.toNumber());
|
|
1737
|
+
if (!didStart) {
|
|
1738
|
+
didStart = true;
|
|
1739
|
+
this.scheduleEvaluation();
|
|
1740
|
+
void this.checkWinningBids();
|
|
1741
|
+
}
|
|
1711
1742
|
}
|
|
1712
1743
|
}
|
|
1713
1744
|
);
|
|
@@ -1715,12 +1746,12 @@ var CohortBidder = class {
|
|
|
1715
1746
|
async stop() {
|
|
1716
1747
|
if (this.isStopped) return this.winningBids;
|
|
1717
1748
|
this.isStopped = true;
|
|
1749
|
+
clearInterval(this.evaluateInterval);
|
|
1718
1750
|
console.log("Stopping bidder for cohort", this.cohortStartingFrameId);
|
|
1719
|
-
clearTimeout(this.retryTimeout);
|
|
1720
1751
|
if (this.unsubscribe) {
|
|
1721
1752
|
this.unsubscribe();
|
|
1722
1753
|
}
|
|
1723
|
-
const client = await this.
|
|
1754
|
+
const client = await this.clientPromise;
|
|
1724
1755
|
const [nextFrameId, isBiddingOpen] = await client.queryMulti([
|
|
1725
1756
|
client.query.miningSlot.nextFrameId,
|
|
1726
1757
|
client.query.miningSlot.isNextSlotBiddingOpen
|
|
@@ -1737,58 +1768,63 @@ var CohortBidder = class {
|
|
|
1737
1768
|
});
|
|
1738
1769
|
}
|
|
1739
1770
|
void await this.pendingRequest;
|
|
1740
|
-
|
|
1741
|
-
let
|
|
1742
|
-
|
|
1743
|
-
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
}
|
|
1747
|
-
header = await client.rpc.chain.getHeader(header.parentHash);
|
|
1748
|
-
api = await client.at(header.hash);
|
|
1771
|
+
const currentFrameId = await client.query.miningSlot.nextFrameId();
|
|
1772
|
+
let blockNumber;
|
|
1773
|
+
if (currentFrameId.toNumber() > this.cohortStartingFrameId) {
|
|
1774
|
+
blockNumber = await client.query.miningSlot.frameStartBlockNumbers().then((x) => x[0]?.toNumber()) - 1;
|
|
1775
|
+
} else {
|
|
1776
|
+
blockNumber = await client.query.system.number().then((x) => x.toNumber());
|
|
1749
1777
|
}
|
|
1750
|
-
const
|
|
1751
|
-
|
|
1778
|
+
const blockHash = await client.query.system.blockHash(blockNumber);
|
|
1779
|
+
const api = await client.at(blockHash);
|
|
1780
|
+
const rawBids = await api.query.miningSlot.bidsForNextSlotCohort();
|
|
1781
|
+
const currentTick = await api.query.ticks.currentTick().then((x) => x.toNumber());
|
|
1782
|
+
this.updateBidList(rawBids, blockNumber, currentTick);
|
|
1752
1783
|
console.log("Bidder stopped", {
|
|
1753
1784
|
cohortStartingFrameId: this.cohortStartingFrameId,
|
|
1754
|
-
blockNumber
|
|
1755
|
-
|
|
1785
|
+
blockNumber,
|
|
1786
|
+
winningBids: this.winningBids
|
|
1756
1787
|
});
|
|
1757
1788
|
return this.winningBids;
|
|
1758
1789
|
}
|
|
1759
|
-
async checkWinningBids(
|
|
1790
|
+
async checkWinningBids() {
|
|
1760
1791
|
if (this.isStopped) return;
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1792
|
+
if (this.pendingRequest) {
|
|
1793
|
+
console.log("Current bid is still in progress, skipping this check");
|
|
1794
|
+
return;
|
|
1795
|
+
}
|
|
1796
|
+
if (this.currentBids.mostRecentBidTick < this.lastBidTick) {
|
|
1797
|
+
console.log(`Waiting for bids more recent than our last attempt.`, {
|
|
1798
|
+
ownAttemptedBidTick: this.lastBidTick,
|
|
1799
|
+
liveBidsTick: this.currentBids.mostRecentBidTick
|
|
1800
|
+
});
|
|
1801
|
+
return;
|
|
1802
|
+
}
|
|
1803
|
+
const bids = [...this.currentBids.bids];
|
|
1804
|
+
const bidsAtTick = this.currentBids.atTick;
|
|
1805
|
+
const blockNumber = this.currentBids.atBlockNumber;
|
|
1806
|
+
const winningBids = bids.filter((x) => this.myAddresses.has(x.address));
|
|
1807
|
+
if (winningBids.length >= this.subaccounts.length) {
|
|
1808
|
+
console.log(`No updates needed. Winning all remaining seats (${winningBids.length}).`);
|
|
1774
1809
|
return;
|
|
1775
1810
|
}
|
|
1776
|
-
if (!this.needsRebid) return;
|
|
1777
1811
|
console.log(
|
|
1778
|
-
|
|
1779
|
-
this.cohortStartingFrameId,
|
|
1780
|
-
this.subaccounts.map((x) => x.index)
|
|
1812
|
+
`Checking bids for cohort ${this.cohortStartingFrameId}, Still trying for seats: ${this.subaccounts.length}`
|
|
1781
1813
|
);
|
|
1782
|
-
const winningAddresses = new Set(
|
|
1783
|
-
let lowestBid
|
|
1784
|
-
|
|
1785
|
-
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
1814
|
+
const winningAddresses = new Set(winningBids.map((x) => x.address));
|
|
1815
|
+
let lowestBid;
|
|
1816
|
+
let myAllocatedBids = 0n;
|
|
1817
|
+
for (const bid of bids) {
|
|
1818
|
+
lowestBid ??= bid.bidMicrogons;
|
|
1819
|
+
if (this.myAddresses.has(bid.address)) {
|
|
1820
|
+
myAllocatedBids += bid.bidMicrogons;
|
|
1821
|
+
} else {
|
|
1822
|
+
if (bid.bidMicrogons < lowestBid) {
|
|
1823
|
+
lowestBid = bid.bidMicrogons;
|
|
1789
1824
|
}
|
|
1790
1825
|
}
|
|
1791
1826
|
}
|
|
1827
|
+
lowestBid ??= -this.options.bidIncrement;
|
|
1792
1828
|
let nextBid = lowestBid + this.options.bidIncrement;
|
|
1793
1829
|
if (nextBid < this.options.minBid) {
|
|
1794
1830
|
nextBid = this.options.minBid;
|
|
@@ -1800,39 +1836,59 @@ var CohortBidder = class {
|
|
|
1800
1836
|
subaccounts: this.subaccounts,
|
|
1801
1837
|
bidAmount: nextBid
|
|
1802
1838
|
});
|
|
1803
|
-
let availableBalanceForBids = await
|
|
1804
|
-
|
|
1805
|
-
if (this.myAddresses.has(bid.accountId.toHuman())) {
|
|
1806
|
-
availableBalanceForBids += bid.bid.toBigInt();
|
|
1807
|
-
}
|
|
1808
|
-
}
|
|
1839
|
+
let availableBalanceForBids = await this.accountset.submitterBalance();
|
|
1840
|
+
availableBalanceForBids += myAllocatedBids;
|
|
1809
1841
|
const tip = this.options.tipPerTransaction ?? 0n;
|
|
1810
1842
|
const feeEstimate = await fakeTx.feeEstimate(tip);
|
|
1811
|
-
const
|
|
1812
|
-
let budgetForSeats = this.options.maxBudget -
|
|
1843
|
+
const estimatedFeePlusTip = feeEstimate + tip;
|
|
1844
|
+
let budgetForSeats = this.options.maxBudget - estimatedFeePlusTip;
|
|
1813
1845
|
if (budgetForSeats > availableBalanceForBids) {
|
|
1814
|
-
budgetForSeats = availableBalanceForBids -
|
|
1846
|
+
budgetForSeats = availableBalanceForBids - estimatedFeePlusTip;
|
|
1815
1847
|
}
|
|
1816
1848
|
if (nextBid < lowestBid) {
|
|
1817
1849
|
console.log(
|
|
1818
|
-
`
|
|
1850
|
+
`Next bid within parameters is ${formatArgons(nextBid)}, but it's not enough. Current lowest bid is ${formatArgons(lowestBid)}.`
|
|
1819
1851
|
);
|
|
1852
|
+
this.safeRecordParamsAdjusted({
|
|
1853
|
+
tick: bidsAtTick,
|
|
1854
|
+
blockNumber,
|
|
1855
|
+
maxSeats: 0,
|
|
1856
|
+
winningBidCount: winningBids.length,
|
|
1857
|
+
reason: "max-bid-too-low",
|
|
1858
|
+
availableBalanceForBids
|
|
1859
|
+
});
|
|
1820
1860
|
return;
|
|
1821
1861
|
}
|
|
1822
1862
|
if (nextBid - lowestBid < Number(this.minIncrement)) {
|
|
1823
1863
|
console.log(
|
|
1824
|
-
`Can't make any more bids for ${this.cohortStartingFrameId} with given constraints.`,
|
|
1864
|
+
`Can't make any more bids for ${this.cohortStartingFrameId} with given constraints (next bid below min increment).`,
|
|
1825
1865
|
{
|
|
1826
1866
|
lowestCurrentBid: formatArgons(lowestBid),
|
|
1827
1867
|
nextAttemptedBid: formatArgons(nextBid),
|
|
1828
1868
|
maxBid: formatArgons(this.options.maxBid)
|
|
1829
1869
|
}
|
|
1830
1870
|
);
|
|
1871
|
+
this.safeRecordParamsAdjusted({
|
|
1872
|
+
tick: bidsAtTick,
|
|
1873
|
+
blockNumber,
|
|
1874
|
+
maxSeats: 0,
|
|
1875
|
+
winningBidCount: winningBids.length,
|
|
1876
|
+
reason: "max-bid-too-low",
|
|
1877
|
+
availableBalanceForBids
|
|
1878
|
+
});
|
|
1831
1879
|
return;
|
|
1832
1880
|
}
|
|
1833
1881
|
const seatsInBudget = nextBid === 0n ? this.subaccounts.length : Number(budgetForSeats / nextBid);
|
|
1834
1882
|
let accountsToUse = [...this.subaccounts];
|
|
1835
1883
|
if (accountsToUse.length > seatsInBudget) {
|
|
1884
|
+
this.safeRecordParamsAdjusted({
|
|
1885
|
+
tick: bidsAtTick,
|
|
1886
|
+
blockNumber,
|
|
1887
|
+
maxSeats: this.subaccounts.length,
|
|
1888
|
+
winningBidCount: winningBids.length,
|
|
1889
|
+
reason: availableBalanceForBids - estimatedFeePlusTip < nextBid * BigInt(seatsInBudget) ? "insufficient-balance" : "max-budget-too-low",
|
|
1890
|
+
availableBalanceForBids
|
|
1891
|
+
});
|
|
1836
1892
|
accountsToUse.sort((a, b) => {
|
|
1837
1893
|
const isWinningA = winningAddresses.has(a.address);
|
|
1838
1894
|
const isWinningB = winningAddresses.has(b.address);
|
|
@@ -1844,18 +1900,16 @@ var CohortBidder = class {
|
|
|
1844
1900
|
});
|
|
1845
1901
|
accountsToUse.length = seatsInBudget;
|
|
1846
1902
|
}
|
|
1847
|
-
if (accountsToUse.length > winningBids) {
|
|
1848
|
-
this.pendingRequest = this.
|
|
1903
|
+
if (accountsToUse.length > winningBids.length) {
|
|
1904
|
+
this.pendingRequest = this.submitBids(nextBid, accountsToUse);
|
|
1849
1905
|
}
|
|
1850
|
-
this.needsRebid = false;
|
|
1851
1906
|
}
|
|
1852
|
-
async
|
|
1853
|
-
const prevLastBidTime = this.lastBidTime;
|
|
1907
|
+
async submitBids(microgonsPerSeat, subaccounts) {
|
|
1854
1908
|
try {
|
|
1855
|
-
this.
|
|
1909
|
+
this.bidsAttempted += subaccounts.length;
|
|
1856
1910
|
const submitter = await this.accountset.createMiningBidTx({
|
|
1857
1911
|
subaccounts,
|
|
1858
|
-
bidAmount:
|
|
1912
|
+
bidAmount: microgonsPerSeat
|
|
1859
1913
|
});
|
|
1860
1914
|
const tip = this.options.tipPerTransaction ?? 0n;
|
|
1861
1915
|
const txResult = await submitter.submit({
|
|
@@ -1863,50 +1917,108 @@ var CohortBidder = class {
|
|
|
1863
1917
|
useLatestNonce: true
|
|
1864
1918
|
});
|
|
1865
1919
|
const bidError = await txResult.inBlockPromise.then(() => void 0).catch((x) => x);
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1920
|
+
const client = await this.clientPromise;
|
|
1921
|
+
let api = txResult.includedInBlock ? await client.at(txResult.includedInBlock) : client;
|
|
1922
|
+
this.lastBidTick = await api.query.ticks.currentTick().then((x) => x.toNumber());
|
|
1923
|
+
const blockNumber = await api.query.system.number().then((x) => x.toNumber());
|
|
1924
|
+
const bidAtTick = this.lastBidTick;
|
|
1925
|
+
try {
|
|
1926
|
+
this.callbacks?.onBidsSubmitted?.({
|
|
1927
|
+
tick: bidAtTick,
|
|
1928
|
+
blockNumber,
|
|
1929
|
+
microgonsPerSeat,
|
|
1930
|
+
txFeePlusTip: txResult.finalFee ?? 0n,
|
|
1931
|
+
submittedCount: subaccounts.length
|
|
1932
|
+
});
|
|
1933
|
+
} catch (error) {
|
|
1934
|
+
console.error("Error in onBidsSubmitted callback:", error);
|
|
1871
1935
|
}
|
|
1872
1936
|
const successfulBids = txResult.batchInterruptedIndex ?? subaccounts.length;
|
|
1873
1937
|
this.txFees += txResult.finalFee ?? 0n;
|
|
1874
|
-
|
|
1875
|
-
this.lastBidBlockNumber = Math.max(blockNumber, this.lastBidBlockNumber);
|
|
1876
|
-
}
|
|
1877
|
-
console.log("Done creating bids for cohort", {
|
|
1938
|
+
console.log("Result of bids for cohort", {
|
|
1878
1939
|
successfulBids,
|
|
1879
|
-
|
|
1880
|
-
|
|
1940
|
+
bidsPlaced: subaccounts.length,
|
|
1941
|
+
bidPerSeat: formatArgons(microgonsPerSeat),
|
|
1942
|
+
bidAtTick
|
|
1881
1943
|
});
|
|
1882
|
-
if (bidError)
|
|
1944
|
+
if (bidError) {
|
|
1945
|
+
try {
|
|
1946
|
+
this.callbacks?.onBidsRejected?.({
|
|
1947
|
+
tick: bidAtTick,
|
|
1948
|
+
blockNumber,
|
|
1949
|
+
microgonsPerSeat,
|
|
1950
|
+
submittedCount: subaccounts.length,
|
|
1951
|
+
rejectedCount: subaccounts.length - successfulBids,
|
|
1952
|
+
bidError
|
|
1953
|
+
});
|
|
1954
|
+
} catch (error) {
|
|
1955
|
+
console.error("Error in onBidsRejected callback:", error);
|
|
1956
|
+
}
|
|
1957
|
+
throw bidError;
|
|
1958
|
+
}
|
|
1883
1959
|
} catch (err) {
|
|
1884
|
-
this.lastBidTime = prevLastBidTime;
|
|
1885
1960
|
console.error(`Error bidding for cohort ${this.cohortStartingFrameId}:`, err);
|
|
1886
|
-
clearTimeout(this.retryTimeout);
|
|
1887
|
-
this.retryTimeout = setTimeout(() => void this.checkCurrentSeats(), 1e3);
|
|
1888
1961
|
} finally {
|
|
1889
1962
|
this.pendingRequest = void 0;
|
|
1890
|
-
|
|
1891
|
-
if (this.needsRebid) {
|
|
1892
|
-
this.needsRebid = false;
|
|
1893
|
-
await this.checkCurrentSeats();
|
|
1963
|
+
this.scheduleEvaluation();
|
|
1894
1964
|
}
|
|
1895
1965
|
}
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1966
|
+
scheduleEvaluation() {
|
|
1967
|
+
const millisPerTick = this.millisPerTick;
|
|
1968
|
+
const delayTicks = Math.max(this.options.bidDelay, 1);
|
|
1969
|
+
const delay = delayTicks * millisPerTick;
|
|
1970
|
+
if (this.evaluateInterval) clearInterval(this.evaluateInterval);
|
|
1971
|
+
console.log(`Scheduling next evaluation in ${delay}ms`);
|
|
1972
|
+
this.evaluateInterval = setInterval(() => this.checkWinningBids().catch(console.error), delay);
|
|
1973
|
+
}
|
|
1974
|
+
updateBidList(rawBids, blockNumber, tick) {
|
|
1975
|
+
try {
|
|
1976
|
+
let mostRecentBidTick = 0;
|
|
1977
|
+
let hasDiffs = this.currentBids.bids.length !== rawBids.length;
|
|
1978
|
+
const bids = [];
|
|
1979
|
+
for (let i = 0; i < rawBids.length; i += 1) {
|
|
1980
|
+
const rawBid = rawBids[i];
|
|
1981
|
+
const bidAtTick = rawBid.bidAtTick.toNumber();
|
|
1982
|
+
if (bidAtTick > mostRecentBidTick) {
|
|
1983
|
+
mostRecentBidTick = bidAtTick;
|
|
1984
|
+
}
|
|
1985
|
+
const address = rawBid.accountId.toHuman();
|
|
1986
|
+
const bidMicrogons = rawBid.bid.toBigInt();
|
|
1987
|
+
if (!hasDiffs) {
|
|
1988
|
+
const existing = this.currentBids.bids[i];
|
|
1989
|
+
hasDiffs = existing?.address !== address || existing?.bidMicrogons !== bidMicrogons;
|
|
1990
|
+
}
|
|
1991
|
+
bids.push({
|
|
1992
|
+
address,
|
|
1993
|
+
bidMicrogons,
|
|
1994
|
+
bidAtTick
|
|
1995
|
+
});
|
|
1996
|
+
}
|
|
1997
|
+
if (blockNumber > this.currentBids.atBlockNumber && hasDiffs) {
|
|
1998
|
+
this.currentBids.bids = bids;
|
|
1999
|
+
this.currentBids.mostRecentBidTick = mostRecentBidTick;
|
|
2000
|
+
this.currentBids.atTick = tick;
|
|
2001
|
+
this.currentBids.atBlockNumber = blockNumber;
|
|
2002
|
+
this.winningBids = bids.filter((x) => this.myAddresses.has(x.address));
|
|
2003
|
+
console.log("Now winning bids:", this.winningBids.length);
|
|
2004
|
+
if (this.callbacks?.onBidsUpdated) {
|
|
2005
|
+
this.callbacks.onBidsUpdated({
|
|
2006
|
+
bids: this.winningBids,
|
|
2007
|
+
atBlockNumber: blockNumber,
|
|
2008
|
+
tick: mostRecentBidTick
|
|
2009
|
+
});
|
|
2010
|
+
}
|
|
1903
2011
|
}
|
|
2012
|
+
} catch (err) {
|
|
2013
|
+
console.error("Error processing updated bids list:", err);
|
|
1904
2014
|
}
|
|
1905
2015
|
}
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
2016
|
+
safeRecordParamsAdjusted(args) {
|
|
2017
|
+
try {
|
|
2018
|
+
this.callbacks?.onBidParamsAdjusted?.(args);
|
|
2019
|
+
} catch (err) {
|
|
2020
|
+
console.error("Error in onBidParamsAdjusted callback:", err);
|
|
2021
|
+
}
|
|
1910
2022
|
}
|
|
1911
2023
|
};
|
|
1912
2024
|
|
|
@@ -2283,7 +2395,7 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2283
2395
|
const client = await this.client;
|
|
2284
2396
|
const bitcoinNetwork = await client.query.bitcoinUtxos.bitcoinNetwork();
|
|
2285
2397
|
return {
|
|
2286
|
-
|
|
2398
|
+
lockReleaseCosignDeadlineFrames: client.consts.bitcoinLocks.lockReleaseCosignDeadlineFrames.toNumber(),
|
|
2287
2399
|
pendingConfirmationExpirationBlocks: client.consts.bitcoinUtxos.maxPendingConfirmationBlocks.toNumber(),
|
|
2288
2400
|
tickDurationMillis: await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber()),
|
|
2289
2401
|
bitcoinNetwork
|
|
@@ -2324,19 +2436,18 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2324
2436
|
const blockHash = await client.rpc.chain.getBlockHash(atHeight);
|
|
2325
2437
|
client = await client.at(blockHash);
|
|
2326
2438
|
}
|
|
2327
|
-
const
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
return {
|
|
2331
|
-
toScriptPubkey: request.toScriptPubkey.toHex(),
|
|
2332
|
-
bitcoinNetworkFee: request.bitcoinNetworkFee.toBigInt(),
|
|
2333
|
-
dueBlockHeight: request.cosignDueBlock.toNumber(),
|
|
2334
|
-
vaultId: request.vaultId.toNumber(),
|
|
2335
|
-
redemptionPrice: request.redemptionPrice.toBigInt()
|
|
2336
|
-
};
|
|
2337
|
-
}
|
|
2439
|
+
const requestMaybe = await client.query.bitcoinLocks.lockReleaseRequestsByUtxoId(utxoId);
|
|
2440
|
+
if (!requestMaybe.isSome) {
|
|
2441
|
+
return void 0;
|
|
2338
2442
|
}
|
|
2339
|
-
|
|
2443
|
+
const request = requestMaybe.unwrap();
|
|
2444
|
+
return {
|
|
2445
|
+
toScriptPubkey: request.toScriptPubkey.toHex(),
|
|
2446
|
+
bitcoinNetworkFee: request.bitcoinNetworkFee.toBigInt(),
|
|
2447
|
+
dueFrame: request.cosignDueFrame.toNumber(),
|
|
2448
|
+
vaultId: request.vaultId.toNumber(),
|
|
2449
|
+
redemptionPrice: request.redemptionPrice.toBigInt()
|
|
2450
|
+
};
|
|
2340
2451
|
}
|
|
2341
2452
|
async submitVaultSignature(args) {
|
|
2342
2453
|
const { utxoId, vaultSignature, argonKeyring, txProgressCallback } = args;
|
|
@@ -2506,7 +2617,8 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2506
2617
|
argonKeyring
|
|
2507
2618
|
);
|
|
2508
2619
|
const marketPrice = await this.getMarketRate(BigInt(satoshis));
|
|
2509
|
-
const
|
|
2620
|
+
const isVaultOwner = argonKeyring.address === vault.operatorAccountId;
|
|
2621
|
+
const securityFee = isVaultOwner ? 0n : vault.calculateBitcoinFee(marketPrice);
|
|
2510
2622
|
const { canAfford, availableBalance, txFee } = await submitter.canAfford({
|
|
2511
2623
|
tip,
|
|
2512
2624
|
unavailableBalance: securityFee + (args.reducedBalanceBy ?? 0n),
|
|
@@ -2519,17 +2631,8 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2519
2631
|
}
|
|
2520
2632
|
return { tx, securityFee, txFee };
|
|
2521
2633
|
}
|
|
2522
|
-
async
|
|
2523
|
-
const { argonKeyring, tip = 0n, txProgressCallback } = args;
|
|
2634
|
+
async getBitcoinLockFromTxResult(txResult) {
|
|
2524
2635
|
const client = await this.client;
|
|
2525
|
-
const { tx, securityFee } = await this.createInitializeLockTx(args);
|
|
2526
|
-
const submitter = new TxSubmitter(client, tx, argonKeyring);
|
|
2527
|
-
const txResult = await submitter.submit({
|
|
2528
|
-
waitForBlock: true,
|
|
2529
|
-
logResults: true,
|
|
2530
|
-
tip,
|
|
2531
|
-
txProgressCallback
|
|
2532
|
-
});
|
|
2533
2636
|
const blockHash = await txResult.inBlockPromise;
|
|
2534
2637
|
const blockHeight = await client.at(blockHash).then((x) => x.query.system.number()).then((x) => x.toNumber());
|
|
2535
2638
|
const utxoId = await this.getUtxoIdFromEvents(txResult.events) ?? 0;
|
|
@@ -2540,7 +2643,26 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2540
2643
|
if (!lock) {
|
|
2541
2644
|
throw new Error(`Lock with ID ${utxoId} not found after initialization`);
|
|
2542
2645
|
}
|
|
2543
|
-
return { lock, createdAtHeight: blockHeight, txResult
|
|
2646
|
+
return { lock, createdAtHeight: blockHeight, txResult };
|
|
2647
|
+
}
|
|
2648
|
+
async initializeLock(args) {
|
|
2649
|
+
const { argonKeyring, tip = 0n, txProgressCallback } = args;
|
|
2650
|
+
const client = await this.client;
|
|
2651
|
+
const { tx, securityFee } = await this.createInitializeLockTx(args);
|
|
2652
|
+
const submitter = new TxSubmitter(client, tx, argonKeyring);
|
|
2653
|
+
const txResult = await submitter.submit({
|
|
2654
|
+
waitForBlock: true,
|
|
2655
|
+
logResults: true,
|
|
2656
|
+
tip,
|
|
2657
|
+
txProgressCallback
|
|
2658
|
+
});
|
|
2659
|
+
const { lock, createdAtHeight } = await this.getBitcoinLockFromTxResult(txResult);
|
|
2660
|
+
return {
|
|
2661
|
+
lock,
|
|
2662
|
+
createdAtHeight,
|
|
2663
|
+
txResult,
|
|
2664
|
+
securityFee
|
|
2665
|
+
};
|
|
2544
2666
|
}
|
|
2545
2667
|
async requiredSatoshisForArgonLiquidity(argonAmount) {
|
|
2546
2668
|
const marketRatePerBitcoin = await this.getMarketRate(SATS_PER_BTC);
|
|
@@ -2845,4 +2967,4 @@ export {
|
|
|
2845
2967
|
u64,
|
|
2846
2968
|
u8
|
|
2847
2969
|
};
|
|
2848
|
-
//# sourceMappingURL=chunk-
|
|
2970
|
+
//# sourceMappingURL=chunk-FGSUNAT4.js.map
|