@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
package/browser/index.js
CHANGED
|
@@ -256,10 +256,10 @@ var TxResult = class {
|
|
|
256
256
|
};
|
|
257
257
|
var { ROUND_FLOOR } = BigNumber;
|
|
258
258
|
var MICROGONS_PER_ARGON = 1e6;
|
|
259
|
-
function formatArgons(
|
|
260
|
-
if (
|
|
261
|
-
const isNegative =
|
|
262
|
-
let format = BigNumber__default(
|
|
259
|
+
function formatArgons(microgons) {
|
|
260
|
+
if (microgons === void 0 || microgons === null) return "na";
|
|
261
|
+
const isNegative = microgons < 0;
|
|
262
|
+
let format = BigNumber__default(microgons.toString()).abs().div(MICROGONS_PER_ARGON).toFormat(2, ROUND_FLOOR);
|
|
263
263
|
if (format.endsWith(".00")) {
|
|
264
264
|
format = format.slice(0, -3);
|
|
265
265
|
}
|
|
@@ -637,10 +637,14 @@ 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
|
constructor() {
|
|
642
642
|
__publicField(this, "miningConfig");
|
|
643
643
|
__publicField(this, "genesisTick");
|
|
644
|
+
__publicField(this, "tickMillis");
|
|
645
|
+
}
|
|
646
|
+
async load(client) {
|
|
647
|
+
return await this.getConfig(client);
|
|
644
648
|
}
|
|
645
649
|
async getForTick(client, tick) {
|
|
646
650
|
const { ticksBetweenFrames, biddingStartTick } = await this.getConfig(client);
|
|
@@ -649,9 +653,10 @@ var FrameCalculator = class {
|
|
|
649
653
|
}
|
|
650
654
|
async getTickRangeForFrame(client, frameId) {
|
|
651
655
|
const { ticksBetweenFrames, biddingStartTick } = await this.getConfig(client);
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
656
|
+
return _FrameCalculator.calculateTickRangeForFrame(frameId, {
|
|
657
|
+
ticksBetweenFrames,
|
|
658
|
+
biddingStartTick
|
|
659
|
+
});
|
|
655
660
|
}
|
|
656
661
|
async getForHeader(client, header) {
|
|
657
662
|
if (header.number.toNumber() === 0) return 0;
|
|
@@ -659,18 +664,30 @@ var FrameCalculator = class {
|
|
|
659
664
|
if (tick === void 0) return void 0;
|
|
660
665
|
return this.getForTick(client, tick);
|
|
661
666
|
}
|
|
667
|
+
static frameToDateRange(frameId, config2) {
|
|
668
|
+
const [start, end] = _FrameCalculator.calculateTickRangeForFrame(frameId, config2);
|
|
669
|
+
return [new Date(start * config2.tickMillis), new Date(end * config2.tickMillis)];
|
|
670
|
+
}
|
|
671
|
+
static calculateTickRangeForFrame(frameId, config2) {
|
|
672
|
+
const { ticksBetweenFrames, biddingStartTick } = config2;
|
|
673
|
+
const startingTick = biddingStartTick + Math.floor(frameId * ticksBetweenFrames);
|
|
674
|
+
const endingTick = startingTick + ticksBetweenFrames - 1;
|
|
675
|
+
return [startingTick, endingTick];
|
|
676
|
+
}
|
|
662
677
|
async getConfig(client) {
|
|
663
678
|
this.miningConfig ?? (this.miningConfig = await client.query.miningSlot.miningConfig().then((x) => ({
|
|
664
679
|
ticksBetweenSlots: x.ticksBetweenSlots.toNumber(),
|
|
665
680
|
slotBiddingStartAfterTicks: x.slotBiddingStartAfterTicks.toNumber()
|
|
666
681
|
})));
|
|
667
682
|
this.genesisTick ?? (this.genesisTick = await client.query.ticks.genesisTick().then((x) => x.toNumber()));
|
|
683
|
+
this.tickMillis ?? (this.tickMillis = await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber()));
|
|
668
684
|
const config2 = this.miningConfig;
|
|
669
685
|
const genesisTick = this.genesisTick;
|
|
670
686
|
return {
|
|
671
687
|
ticksBetweenFrames: config2.ticksBetweenSlots,
|
|
672
688
|
slotBiddingStartAfterTicks: config2.slotBiddingStartAfterTicks,
|
|
673
689
|
genesisTick,
|
|
690
|
+
tickMillis: this.tickMillis,
|
|
674
691
|
biddingStartTick: genesisTick + config2.slotBiddingStartAfterTicks
|
|
675
692
|
};
|
|
676
693
|
}
|
|
@@ -1646,29 +1663,35 @@ var VaultMonitor = class {
|
|
|
1646
1663
|
|
|
1647
1664
|
// src/CohortBidder.ts
|
|
1648
1665
|
var CohortBidder = class {
|
|
1649
|
-
constructor(accountset, cohortStartingFrameId, subaccounts, options) {
|
|
1666
|
+
constructor(accountset, cohortStartingFrameId, subaccounts, options, callbacks) {
|
|
1650
1667
|
this.accountset = accountset;
|
|
1651
1668
|
this.cohortStartingFrameId = cohortStartingFrameId;
|
|
1652
1669
|
this.subaccounts = subaccounts;
|
|
1653
1670
|
this.options = options;
|
|
1671
|
+
this.callbacks = callbacks;
|
|
1654
1672
|
__publicField(this, "txFees", 0n);
|
|
1673
|
+
__publicField(this, "bidsAttempted", 0);
|
|
1655
1674
|
__publicField(this, "winningBids", []);
|
|
1675
|
+
__publicField(this, "myAddresses", /* @__PURE__ */ new Set());
|
|
1676
|
+
__publicField(this, "currentBids", {
|
|
1677
|
+
bids: [],
|
|
1678
|
+
mostRecentBidTick: 0,
|
|
1679
|
+
atTick: 0,
|
|
1680
|
+
atBlockNumber: 0
|
|
1681
|
+
});
|
|
1656
1682
|
__publicField(this, "unsubscribe");
|
|
1657
1683
|
__publicField(this, "pendingRequest");
|
|
1658
|
-
__publicField(this, "retryTimeout");
|
|
1659
1684
|
__publicField(this, "isStopped", false);
|
|
1660
|
-
__publicField(this, "needsRebid", false);
|
|
1661
|
-
__publicField(this, "lastBidTime", 0);
|
|
1662
1685
|
__publicField(this, "millisPerTick");
|
|
1663
1686
|
__publicField(this, "minIncrement", 10000n);
|
|
1664
1687
|
__publicField(this, "nextCohortSize");
|
|
1665
|
-
__publicField(this, "
|
|
1666
|
-
__publicField(this, "
|
|
1688
|
+
__publicField(this, "lastBidTick", 0);
|
|
1689
|
+
__publicField(this, "evaluateInterval");
|
|
1667
1690
|
this.subaccounts.forEach((x) => {
|
|
1668
1691
|
this.myAddresses.add(x.address);
|
|
1669
1692
|
});
|
|
1670
1693
|
}
|
|
1671
|
-
get
|
|
1694
|
+
get clientPromise() {
|
|
1672
1695
|
return this.accountset.client;
|
|
1673
1696
|
}
|
|
1674
1697
|
async start() {
|
|
@@ -1680,7 +1703,7 @@ var CohortBidder = class {
|
|
|
1680
1703
|
bidDelay: this.options.bidDelay,
|
|
1681
1704
|
subaccounts: this.subaccounts
|
|
1682
1705
|
});
|
|
1683
|
-
const client = await this.
|
|
1706
|
+
const client = await this.clientPromise;
|
|
1684
1707
|
this.minIncrement = client.consts.miningSlot.bidIncrements.toBigInt();
|
|
1685
1708
|
this.nextCohortSize = await client.query.miningSlot.nextCohortSize().then((x) => x.toNumber());
|
|
1686
1709
|
if (this.subaccounts.length > this.nextCohortSize) {
|
|
@@ -1690,14 +1713,22 @@ var CohortBidder = class {
|
|
|
1690
1713
|
this.subaccounts.length = this.nextCohortSize;
|
|
1691
1714
|
}
|
|
1692
1715
|
this.millisPerTick = await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber());
|
|
1716
|
+
let didStart = false;
|
|
1693
1717
|
this.unsubscribe = await client.queryMulti(
|
|
1694
1718
|
[
|
|
1695
1719
|
client.query.miningSlot.bidsForNextSlotCohort,
|
|
1696
|
-
client.query.miningSlot.nextFrameId
|
|
1720
|
+
client.query.miningSlot.nextFrameId,
|
|
1721
|
+
client.query.ticks.currentTick,
|
|
1722
|
+
client.query.system.number
|
|
1697
1723
|
],
|
|
1698
|
-
async ([
|
|
1724
|
+
async ([rawBids, nextFrameId, currentTick, blockNumber]) => {
|
|
1699
1725
|
if (nextFrameId.toNumber() === this.cohortStartingFrameId) {
|
|
1700
|
-
|
|
1726
|
+
this.updateBidList(rawBids, blockNumber.toNumber(), currentTick.toNumber());
|
|
1727
|
+
if (!didStart) {
|
|
1728
|
+
didStart = true;
|
|
1729
|
+
this.scheduleEvaluation();
|
|
1730
|
+
void this.checkWinningBids();
|
|
1731
|
+
}
|
|
1701
1732
|
}
|
|
1702
1733
|
}
|
|
1703
1734
|
);
|
|
@@ -1705,12 +1736,12 @@ var CohortBidder = class {
|
|
|
1705
1736
|
async stop() {
|
|
1706
1737
|
if (this.isStopped) return this.winningBids;
|
|
1707
1738
|
this.isStopped = true;
|
|
1739
|
+
clearInterval(this.evaluateInterval);
|
|
1708
1740
|
console.log("Stopping bidder for cohort", this.cohortStartingFrameId);
|
|
1709
|
-
clearTimeout(this.retryTimeout);
|
|
1710
1741
|
if (this.unsubscribe) {
|
|
1711
1742
|
this.unsubscribe();
|
|
1712
1743
|
}
|
|
1713
|
-
const client = await this.
|
|
1744
|
+
const client = await this.clientPromise;
|
|
1714
1745
|
const [nextFrameId, isBiddingOpen] = await client.queryMulti([
|
|
1715
1746
|
client.query.miningSlot.nextFrameId,
|
|
1716
1747
|
client.query.miningSlot.isNextSlotBiddingOpen
|
|
@@ -1727,58 +1758,63 @@ var CohortBidder = class {
|
|
|
1727
1758
|
});
|
|
1728
1759
|
}
|
|
1729
1760
|
void await this.pendingRequest;
|
|
1730
|
-
|
|
1731
|
-
let
|
|
1732
|
-
|
|
1733
|
-
|
|
1734
|
-
|
|
1735
|
-
|
|
1736
|
-
}
|
|
1737
|
-
header = await client.rpc.chain.getHeader(header.parentHash);
|
|
1738
|
-
api = await client.at(header.hash);
|
|
1761
|
+
const currentFrameId = await client.query.miningSlot.nextFrameId();
|
|
1762
|
+
let blockNumber;
|
|
1763
|
+
if (currentFrameId.toNumber() > this.cohortStartingFrameId) {
|
|
1764
|
+
blockNumber = await client.query.miningSlot.frameStartBlockNumbers().then((x) => x[0]?.toNumber()) - 1;
|
|
1765
|
+
} else {
|
|
1766
|
+
blockNumber = await client.query.system.number().then((x) => x.toNumber());
|
|
1739
1767
|
}
|
|
1740
|
-
const
|
|
1741
|
-
|
|
1768
|
+
const blockHash = await client.query.system.blockHash(blockNumber);
|
|
1769
|
+
const api = await client.at(blockHash);
|
|
1770
|
+
const rawBids = await api.query.miningSlot.bidsForNextSlotCohort();
|
|
1771
|
+
const currentTick = await api.query.ticks.currentTick().then((x) => x.toNumber());
|
|
1772
|
+
this.updateBidList(rawBids, blockNumber, currentTick);
|
|
1742
1773
|
console.log("Bidder stopped", {
|
|
1743
1774
|
cohortStartingFrameId: this.cohortStartingFrameId,
|
|
1744
|
-
blockNumber
|
|
1745
|
-
|
|
1775
|
+
blockNumber,
|
|
1776
|
+
winningBids: this.winningBids
|
|
1746
1777
|
});
|
|
1747
1778
|
return this.winningBids;
|
|
1748
1779
|
}
|
|
1749
|
-
async checkWinningBids(
|
|
1780
|
+
async checkWinningBids() {
|
|
1750
1781
|
if (this.isStopped) return;
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1782
|
+
if (this.pendingRequest) {
|
|
1783
|
+
console.log("Current bid is still in progress, skipping this check");
|
|
1784
|
+
return;
|
|
1785
|
+
}
|
|
1786
|
+
if (this.currentBids.mostRecentBidTick < this.lastBidTick) {
|
|
1787
|
+
console.log(`Waiting for bids more recent than our last attempt.`, {
|
|
1788
|
+
ownAttemptedBidTick: this.lastBidTick,
|
|
1789
|
+
liveBidsTick: this.currentBids.mostRecentBidTick
|
|
1790
|
+
});
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1793
|
+
const bids = [...this.currentBids.bids];
|
|
1794
|
+
const bidsAtTick = this.currentBids.atTick;
|
|
1795
|
+
const blockNumber = this.currentBids.atBlockNumber;
|
|
1796
|
+
const winningBids = bids.filter((x) => this.myAddresses.has(x.address));
|
|
1797
|
+
if (winningBids.length >= this.subaccounts.length) {
|
|
1798
|
+
console.log(`No updates needed. Winning all remaining seats (${winningBids.length}).`);
|
|
1764
1799
|
return;
|
|
1765
1800
|
}
|
|
1766
|
-
if (!this.needsRebid) return;
|
|
1767
1801
|
console.log(
|
|
1768
|
-
|
|
1769
|
-
this.cohortStartingFrameId,
|
|
1770
|
-
this.subaccounts.map((x) => x.index)
|
|
1802
|
+
`Checking bids for cohort ${this.cohortStartingFrameId}, Still trying for seats: ${this.subaccounts.length}`
|
|
1771
1803
|
);
|
|
1772
|
-
const winningAddresses = new Set(
|
|
1773
|
-
let lowestBid
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1804
|
+
const winningAddresses = new Set(winningBids.map((x) => x.address));
|
|
1805
|
+
let lowestBid;
|
|
1806
|
+
let myAllocatedBids = 0n;
|
|
1807
|
+
for (const bid of bids) {
|
|
1808
|
+
lowestBid ?? (lowestBid = bid.bidMicrogons);
|
|
1809
|
+
if (this.myAddresses.has(bid.address)) {
|
|
1810
|
+
myAllocatedBids += bid.bidMicrogons;
|
|
1811
|
+
} else {
|
|
1812
|
+
if (bid.bidMicrogons < lowestBid) {
|
|
1813
|
+
lowestBid = bid.bidMicrogons;
|
|
1779
1814
|
}
|
|
1780
1815
|
}
|
|
1781
1816
|
}
|
|
1817
|
+
lowestBid ?? (lowestBid = -this.options.bidIncrement);
|
|
1782
1818
|
let nextBid = lowestBid + this.options.bidIncrement;
|
|
1783
1819
|
if (nextBid < this.options.minBid) {
|
|
1784
1820
|
nextBid = this.options.minBid;
|
|
@@ -1790,39 +1826,59 @@ var CohortBidder = class {
|
|
|
1790
1826
|
subaccounts: this.subaccounts,
|
|
1791
1827
|
bidAmount: nextBid
|
|
1792
1828
|
});
|
|
1793
|
-
let availableBalanceForBids = await
|
|
1794
|
-
|
|
1795
|
-
if (this.myAddresses.has(bid.accountId.toHuman())) {
|
|
1796
|
-
availableBalanceForBids += bid.bid.toBigInt();
|
|
1797
|
-
}
|
|
1798
|
-
}
|
|
1829
|
+
let availableBalanceForBids = await this.accountset.submitterBalance();
|
|
1830
|
+
availableBalanceForBids += myAllocatedBids;
|
|
1799
1831
|
const tip = this.options.tipPerTransaction ?? 0n;
|
|
1800
1832
|
const feeEstimate = await fakeTx.feeEstimate(tip);
|
|
1801
|
-
const
|
|
1802
|
-
let budgetForSeats = this.options.maxBudget -
|
|
1833
|
+
const estimatedFeePlusTip = feeEstimate + tip;
|
|
1834
|
+
let budgetForSeats = this.options.maxBudget - estimatedFeePlusTip;
|
|
1803
1835
|
if (budgetForSeats > availableBalanceForBids) {
|
|
1804
|
-
budgetForSeats = availableBalanceForBids -
|
|
1836
|
+
budgetForSeats = availableBalanceForBids - estimatedFeePlusTip;
|
|
1805
1837
|
}
|
|
1806
1838
|
if (nextBid < lowestBid) {
|
|
1807
1839
|
console.log(
|
|
1808
|
-
`
|
|
1840
|
+
`Next bid within parameters is ${formatArgons(nextBid)}, but it's not enough. Current lowest bid is ${formatArgons(lowestBid)}.`
|
|
1809
1841
|
);
|
|
1842
|
+
this.safeRecordParamsAdjusted({
|
|
1843
|
+
tick: bidsAtTick,
|
|
1844
|
+
blockNumber,
|
|
1845
|
+
maxSeats: 0,
|
|
1846
|
+
winningBidCount: winningBids.length,
|
|
1847
|
+
reason: "max-bid-too-low",
|
|
1848
|
+
availableBalanceForBids
|
|
1849
|
+
});
|
|
1810
1850
|
return;
|
|
1811
1851
|
}
|
|
1812
1852
|
if (nextBid - lowestBid < Number(this.minIncrement)) {
|
|
1813
1853
|
console.log(
|
|
1814
|
-
`Can't make any more bids for ${this.cohortStartingFrameId} with given constraints.`,
|
|
1854
|
+
`Can't make any more bids for ${this.cohortStartingFrameId} with given constraints (next bid below min increment).`,
|
|
1815
1855
|
{
|
|
1816
1856
|
lowestCurrentBid: formatArgons(lowestBid),
|
|
1817
1857
|
nextAttemptedBid: formatArgons(nextBid),
|
|
1818
1858
|
maxBid: formatArgons(this.options.maxBid)
|
|
1819
1859
|
}
|
|
1820
1860
|
);
|
|
1861
|
+
this.safeRecordParamsAdjusted({
|
|
1862
|
+
tick: bidsAtTick,
|
|
1863
|
+
blockNumber,
|
|
1864
|
+
maxSeats: 0,
|
|
1865
|
+
winningBidCount: winningBids.length,
|
|
1866
|
+
reason: "max-bid-too-low",
|
|
1867
|
+
availableBalanceForBids
|
|
1868
|
+
});
|
|
1821
1869
|
return;
|
|
1822
1870
|
}
|
|
1823
1871
|
const seatsInBudget = nextBid === 0n ? this.subaccounts.length : Number(budgetForSeats / nextBid);
|
|
1824
1872
|
let accountsToUse = [...this.subaccounts];
|
|
1825
1873
|
if (accountsToUse.length > seatsInBudget) {
|
|
1874
|
+
this.safeRecordParamsAdjusted({
|
|
1875
|
+
tick: bidsAtTick,
|
|
1876
|
+
blockNumber,
|
|
1877
|
+
maxSeats: this.subaccounts.length,
|
|
1878
|
+
winningBidCount: winningBids.length,
|
|
1879
|
+
reason: availableBalanceForBids - estimatedFeePlusTip < nextBid * BigInt(seatsInBudget) ? "insufficient-balance" : "max-budget-too-low",
|
|
1880
|
+
availableBalanceForBids
|
|
1881
|
+
});
|
|
1826
1882
|
accountsToUse.sort((a, b) => {
|
|
1827
1883
|
const isWinningA = winningAddresses.has(a.address);
|
|
1828
1884
|
const isWinningB = winningAddresses.has(b.address);
|
|
@@ -1834,18 +1890,16 @@ var CohortBidder = class {
|
|
|
1834
1890
|
});
|
|
1835
1891
|
accountsToUse.length = seatsInBudget;
|
|
1836
1892
|
}
|
|
1837
|
-
if (accountsToUse.length > winningBids) {
|
|
1838
|
-
this.pendingRequest = this.
|
|
1893
|
+
if (accountsToUse.length > winningBids.length) {
|
|
1894
|
+
this.pendingRequest = this.submitBids(nextBid, accountsToUse);
|
|
1839
1895
|
}
|
|
1840
|
-
this.needsRebid = false;
|
|
1841
1896
|
}
|
|
1842
|
-
async
|
|
1843
|
-
const prevLastBidTime = this.lastBidTime;
|
|
1897
|
+
async submitBids(microgonsPerSeat, subaccounts) {
|
|
1844
1898
|
try {
|
|
1845
|
-
this.
|
|
1899
|
+
this.bidsAttempted += subaccounts.length;
|
|
1846
1900
|
const submitter = await this.accountset.createMiningBidTx({
|
|
1847
1901
|
subaccounts,
|
|
1848
|
-
bidAmount:
|
|
1902
|
+
bidAmount: microgonsPerSeat
|
|
1849
1903
|
});
|
|
1850
1904
|
const tip = this.options.tipPerTransaction ?? 0n;
|
|
1851
1905
|
const txResult = await submitter.submit({
|
|
@@ -1853,50 +1907,108 @@ var CohortBidder = class {
|
|
|
1853
1907
|
useLatestNonce: true
|
|
1854
1908
|
});
|
|
1855
1909
|
const bidError = await txResult.inBlockPromise.then(() => void 0).catch((x) => x);
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1910
|
+
const client = await this.clientPromise;
|
|
1911
|
+
let api = txResult.includedInBlock ? await client.at(txResult.includedInBlock) : client;
|
|
1912
|
+
this.lastBidTick = await api.query.ticks.currentTick().then((x) => x.toNumber());
|
|
1913
|
+
const blockNumber = await api.query.system.number().then((x) => x.toNumber());
|
|
1914
|
+
const bidAtTick = this.lastBidTick;
|
|
1915
|
+
try {
|
|
1916
|
+
this.callbacks?.onBidsSubmitted?.({
|
|
1917
|
+
tick: bidAtTick,
|
|
1918
|
+
blockNumber,
|
|
1919
|
+
microgonsPerSeat,
|
|
1920
|
+
txFeePlusTip: txResult.finalFee ?? 0n,
|
|
1921
|
+
submittedCount: subaccounts.length
|
|
1922
|
+
});
|
|
1923
|
+
} catch (error) {
|
|
1924
|
+
console.error("Error in onBidsSubmitted callback:", error);
|
|
1861
1925
|
}
|
|
1862
1926
|
const successfulBids = txResult.batchInterruptedIndex ?? subaccounts.length;
|
|
1863
1927
|
this.txFees += txResult.finalFee ?? 0n;
|
|
1864
|
-
|
|
1865
|
-
this.lastBidBlockNumber = Math.max(blockNumber, this.lastBidBlockNumber);
|
|
1866
|
-
}
|
|
1867
|
-
console.log("Done creating bids for cohort", {
|
|
1928
|
+
console.log("Result of bids for cohort", {
|
|
1868
1929
|
successfulBids,
|
|
1869
|
-
|
|
1870
|
-
|
|
1930
|
+
bidsPlaced: subaccounts.length,
|
|
1931
|
+
bidPerSeat: formatArgons(microgonsPerSeat),
|
|
1932
|
+
bidAtTick
|
|
1871
1933
|
});
|
|
1872
|
-
if (bidError)
|
|
1934
|
+
if (bidError) {
|
|
1935
|
+
try {
|
|
1936
|
+
this.callbacks?.onBidsRejected?.({
|
|
1937
|
+
tick: bidAtTick,
|
|
1938
|
+
blockNumber,
|
|
1939
|
+
microgonsPerSeat,
|
|
1940
|
+
submittedCount: subaccounts.length,
|
|
1941
|
+
rejectedCount: subaccounts.length - successfulBids,
|
|
1942
|
+
bidError
|
|
1943
|
+
});
|
|
1944
|
+
} catch (error) {
|
|
1945
|
+
console.error("Error in onBidsRejected callback:", error);
|
|
1946
|
+
}
|
|
1947
|
+
throw bidError;
|
|
1948
|
+
}
|
|
1873
1949
|
} catch (err) {
|
|
1874
|
-
this.lastBidTime = prevLastBidTime;
|
|
1875
1950
|
console.error(`Error bidding for cohort ${this.cohortStartingFrameId}:`, err);
|
|
1876
|
-
clearTimeout(this.retryTimeout);
|
|
1877
|
-
this.retryTimeout = setTimeout(() => void this.checkCurrentSeats(), 1e3);
|
|
1878
1951
|
} finally {
|
|
1879
1952
|
this.pendingRequest = void 0;
|
|
1880
|
-
|
|
1881
|
-
if (this.needsRebid) {
|
|
1882
|
-
this.needsRebid = false;
|
|
1883
|
-
await this.checkCurrentSeats();
|
|
1953
|
+
this.scheduleEvaluation();
|
|
1884
1954
|
}
|
|
1885
1955
|
}
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1956
|
+
scheduleEvaluation() {
|
|
1957
|
+
const millisPerTick = this.millisPerTick;
|
|
1958
|
+
const delayTicks = Math.max(this.options.bidDelay, 1);
|
|
1959
|
+
const delay = delayTicks * millisPerTick;
|
|
1960
|
+
if (this.evaluateInterval) clearInterval(this.evaluateInterval);
|
|
1961
|
+
console.log(`Scheduling next evaluation in ${delay}ms`);
|
|
1962
|
+
this.evaluateInterval = setInterval(() => this.checkWinningBids().catch(console.error), delay);
|
|
1963
|
+
}
|
|
1964
|
+
updateBidList(rawBids, blockNumber, tick) {
|
|
1965
|
+
try {
|
|
1966
|
+
let mostRecentBidTick = 0;
|
|
1967
|
+
let hasDiffs = this.currentBids.bids.length !== rawBids.length;
|
|
1968
|
+
const bids = [];
|
|
1969
|
+
for (let i = 0; i < rawBids.length; i += 1) {
|
|
1970
|
+
const rawBid = rawBids[i];
|
|
1971
|
+
const bidAtTick = rawBid.bidAtTick.toNumber();
|
|
1972
|
+
if (bidAtTick > mostRecentBidTick) {
|
|
1973
|
+
mostRecentBidTick = bidAtTick;
|
|
1974
|
+
}
|
|
1975
|
+
const address = rawBid.accountId.toHuman();
|
|
1976
|
+
const bidMicrogons = rawBid.bid.toBigInt();
|
|
1977
|
+
if (!hasDiffs) {
|
|
1978
|
+
const existing = this.currentBids.bids[i];
|
|
1979
|
+
hasDiffs = existing?.address !== address || existing?.bidMicrogons !== bidMicrogons;
|
|
1980
|
+
}
|
|
1981
|
+
bids.push({
|
|
1982
|
+
address,
|
|
1983
|
+
bidMicrogons,
|
|
1984
|
+
bidAtTick
|
|
1985
|
+
});
|
|
1986
|
+
}
|
|
1987
|
+
if (blockNumber > this.currentBids.atBlockNumber && hasDiffs) {
|
|
1988
|
+
this.currentBids.bids = bids;
|
|
1989
|
+
this.currentBids.mostRecentBidTick = mostRecentBidTick;
|
|
1990
|
+
this.currentBids.atTick = tick;
|
|
1991
|
+
this.currentBids.atBlockNumber = blockNumber;
|
|
1992
|
+
this.winningBids = bids.filter((x) => this.myAddresses.has(x.address));
|
|
1993
|
+
console.log("Now winning bids:", this.winningBids.length);
|
|
1994
|
+
if (this.callbacks?.onBidsUpdated) {
|
|
1995
|
+
this.callbacks.onBidsUpdated({
|
|
1996
|
+
bids: this.winningBids,
|
|
1997
|
+
atBlockNumber: blockNumber,
|
|
1998
|
+
tick: mostRecentBidTick
|
|
1999
|
+
});
|
|
2000
|
+
}
|
|
1893
2001
|
}
|
|
2002
|
+
} catch (err) {
|
|
2003
|
+
console.error("Error processing updated bids list:", err);
|
|
1894
2004
|
}
|
|
1895
2005
|
}
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
2006
|
+
safeRecordParamsAdjusted(args) {
|
|
2007
|
+
try {
|
|
2008
|
+
this.callbacks?.onBidParamsAdjusted?.(args);
|
|
2009
|
+
} catch (err) {
|
|
2010
|
+
console.error("Error in onBidParamsAdjusted callback:", err);
|
|
2011
|
+
}
|
|
1900
2012
|
}
|
|
1901
2013
|
};
|
|
1902
2014
|
var EMPTY_TABLE = {
|
|
@@ -2268,7 +2380,7 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2268
2380
|
const client = await this.client;
|
|
2269
2381
|
const bitcoinNetwork = await client.query.bitcoinUtxos.bitcoinNetwork();
|
|
2270
2382
|
return {
|
|
2271
|
-
|
|
2383
|
+
lockReleaseCosignDeadlineFrames: client.consts.bitcoinLocks.lockReleaseCosignDeadlineFrames.toNumber(),
|
|
2272
2384
|
pendingConfirmationExpirationBlocks: client.consts.bitcoinUtxos.maxPendingConfirmationBlocks.toNumber(),
|
|
2273
2385
|
tickDurationMillis: await client.query.ticks.genesisTicker().then((x) => x.tickDurationMillis.toNumber()),
|
|
2274
2386
|
bitcoinNetwork
|
|
@@ -2309,19 +2421,18 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2309
2421
|
const blockHash = await client.rpc.chain.getBlockHash(atHeight);
|
|
2310
2422
|
client = await client.at(blockHash);
|
|
2311
2423
|
}
|
|
2312
|
-
const
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
return {
|
|
2316
|
-
toScriptPubkey: request.toScriptPubkey.toHex(),
|
|
2317
|
-
bitcoinNetworkFee: request.bitcoinNetworkFee.toBigInt(),
|
|
2318
|
-
dueBlockHeight: request.cosignDueBlock.toNumber(),
|
|
2319
|
-
vaultId: request.vaultId.toNumber(),
|
|
2320
|
-
redemptionPrice: request.redemptionPrice.toBigInt()
|
|
2321
|
-
};
|
|
2322
|
-
}
|
|
2424
|
+
const requestMaybe = await client.query.bitcoinLocks.lockReleaseRequestsByUtxoId(utxoId);
|
|
2425
|
+
if (!requestMaybe.isSome) {
|
|
2426
|
+
return void 0;
|
|
2323
2427
|
}
|
|
2324
|
-
|
|
2428
|
+
const request = requestMaybe.unwrap();
|
|
2429
|
+
return {
|
|
2430
|
+
toScriptPubkey: request.toScriptPubkey.toHex(),
|
|
2431
|
+
bitcoinNetworkFee: request.bitcoinNetworkFee.toBigInt(),
|
|
2432
|
+
dueFrame: request.cosignDueFrame.toNumber(),
|
|
2433
|
+
vaultId: request.vaultId.toNumber(),
|
|
2434
|
+
redemptionPrice: request.redemptionPrice.toBigInt()
|
|
2435
|
+
};
|
|
2325
2436
|
}
|
|
2326
2437
|
async submitVaultSignature(args) {
|
|
2327
2438
|
const { utxoId, vaultSignature, argonKeyring, txProgressCallback } = args;
|
|
@@ -2491,7 +2602,8 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2491
2602
|
argonKeyring
|
|
2492
2603
|
);
|
|
2493
2604
|
const marketPrice = await this.getMarketRate(BigInt(satoshis));
|
|
2494
|
-
const
|
|
2605
|
+
const isVaultOwner = argonKeyring.address === vault.operatorAccountId;
|
|
2606
|
+
const securityFee = isVaultOwner ? 0n : vault.calculateBitcoinFee(marketPrice);
|
|
2495
2607
|
const { canAfford, availableBalance, txFee } = await submitter.canAfford({
|
|
2496
2608
|
tip,
|
|
2497
2609
|
unavailableBalance: securityFee + (args.reducedBalanceBy ?? 0n),
|
|
@@ -2504,17 +2616,8 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2504
2616
|
}
|
|
2505
2617
|
return { tx, securityFee, txFee };
|
|
2506
2618
|
}
|
|
2507
|
-
async
|
|
2508
|
-
const { argonKeyring, tip = 0n, txProgressCallback } = args;
|
|
2619
|
+
async getBitcoinLockFromTxResult(txResult) {
|
|
2509
2620
|
const client = await this.client;
|
|
2510
|
-
const { tx, securityFee } = await this.createInitializeLockTx(args);
|
|
2511
|
-
const submitter = new TxSubmitter(client, tx, argonKeyring);
|
|
2512
|
-
const txResult = await submitter.submit({
|
|
2513
|
-
waitForBlock: true,
|
|
2514
|
-
logResults: true,
|
|
2515
|
-
tip,
|
|
2516
|
-
txProgressCallback
|
|
2517
|
-
});
|
|
2518
2621
|
const blockHash = await txResult.inBlockPromise;
|
|
2519
2622
|
const blockHeight = await client.at(blockHash).then((x) => x.query.system.number()).then((x) => x.toNumber());
|
|
2520
2623
|
const utxoId = await this.getUtxoIdFromEvents(txResult.events) ?? 0;
|
|
@@ -2525,7 +2628,26 @@ var BitcoinLocks = class _BitcoinLocks {
|
|
|
2525
2628
|
if (!lock) {
|
|
2526
2629
|
throw new Error(`Lock with ID ${utxoId} not found after initialization`);
|
|
2527
2630
|
}
|
|
2528
|
-
return { lock, createdAtHeight: blockHeight, txResult
|
|
2631
|
+
return { lock, createdAtHeight: blockHeight, txResult };
|
|
2632
|
+
}
|
|
2633
|
+
async initializeLock(args) {
|
|
2634
|
+
const { argonKeyring, tip = 0n, txProgressCallback } = args;
|
|
2635
|
+
const client = await this.client;
|
|
2636
|
+
const { tx, securityFee } = await this.createInitializeLockTx(args);
|
|
2637
|
+
const submitter = new TxSubmitter(client, tx, argonKeyring);
|
|
2638
|
+
const txResult = await submitter.submit({
|
|
2639
|
+
waitForBlock: true,
|
|
2640
|
+
logResults: true,
|
|
2641
|
+
tip,
|
|
2642
|
+
txProgressCallback
|
|
2643
|
+
});
|
|
2644
|
+
const { lock, createdAtHeight } = await this.getBitcoinLockFromTxResult(txResult);
|
|
2645
|
+
return {
|
|
2646
|
+
lock,
|
|
2647
|
+
createdAtHeight,
|
|
2648
|
+
txResult,
|
|
2649
|
+
securityFee
|
|
2650
|
+
};
|
|
2529
2651
|
}
|
|
2530
2652
|
async requiredSatoshisForArgonLiquidity(argonAmount) {
|
|
2531
2653
|
const marketRatePerBitcoin = await this.getMarketRate(SATS_PER_BTC);
|