@argonprotocol/mainchain 1.4.3-dev.5e4d8d6a → 1.4.3-dev.6de0a801

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/lib/index.cjs CHANGED
@@ -596,6 +596,21 @@ var Vault = class _Vault {
596
596
  availableSecuritization() {
597
597
  return this.securitization - this.securitizationLocked;
598
598
  }
599
+ availableBondSpace(priceIndex, bondLots, bondFullCapacityPerFrame) {
600
+ if (this.securitizedSatoshis <= 0) return 0n;
601
+ const microgonsPerBond = BigInt(MICROGONS_PER_ARGON);
602
+ const totalBondCapacityMicrogons = priceIndex.getSatoshiPriceInTargetMicrogons(
603
+ BigInt(this.securitizedSatoshis)
604
+ );
605
+ const capacityMicrogons = bondFullCapacityPerFrame ? totalBondCapacityMicrogons : totalBondCapacityMicrogons / 10n;
606
+ const bondCapacity = Number(capacityMicrogons / microgonsPerBond);
607
+ const activeBonds = [..._nullishCoalesce(bondLots, () => ( []))].reduce(
608
+ (sum, bondLot) => sum + bondLot.activeBonds,
609
+ 0
610
+ );
611
+ const availableBonds = activeBonds < bondCapacity ? bondCapacity - activeBonds : 0;
612
+ return BigInt(availableBonds) * microgonsPerBond;
613
+ }
599
614
  getRelockCapacity() {
600
615
  return [...this.securitizationReleaseSchedule.values()].reduce((acc, val) => acc + val, 0n);
601
616
  }
@@ -1051,20 +1066,22 @@ var BitcoinLock = class _BitcoinLock {
1051
1066
  if (maxMicrogonsAtTarget !== void 0 && maxMicrogonsAtTarget < btcMicrogonsAtTarget) {
1052
1067
  price = maxMicrogonsAtTarget;
1053
1068
  }
1069
+ const multiplierBn = this.redemptionMultiplierBn(priceIndex);
1070
+ return BigInt(
1071
+ new (0, BN.default)(price.toString()).times(multiplierBn).integerValue(BN.default.ROUND_DOWN).toFixed(0)
1072
+ );
1073
+ }
1074
+ static redemptionMultiplierBn(priceIndex) {
1054
1075
  const r = priceIndex.rValue;
1055
- let multiplier;
1056
1076
  if (r.gte(1)) {
1057
- multiplier = new (0, BN.default)(1);
1077
+ return new (0, BN.default)(1);
1058
1078
  } else if (r.gte(0.9)) {
1059
- multiplier = new (0, BN.default)(20).times(r.pow(2)).minus(new (0, BN.default)(38).times(r)).plus(19);
1079
+ return new (0, BN.default)(20).times(r.pow(2)).minus(new (0, BN.default)(38).times(r)).plus(19);
1060
1080
  } else if (r.gte(0.01)) {
1061
- multiplier = new (0, BN.default)(0.5618).times(r).plus(0.3944).div(r);
1081
+ return new (0, BN.default)(0.5618).times(r).plus(0.3944).div(r);
1062
1082
  } else {
1063
- multiplier = new (0, BN.default)(1).div(r).times(new (0, BN.default)(0.576).times(r).plus(0.4));
1083
+ return new (0, BN.default)(1).div(r).times(new (0, BN.default)(0.576).times(r).plus(0.4));
1064
1084
  }
1065
- return BigInt(
1066
- new (0, BN.default)(price.toString()).times(multiplier).integerValue(BN.default.ROUND_DOWN).toFixed(0)
1067
- );
1068
1085
  }
1069
1086
  static async getConfig(client) {
1070
1087
  const bitcoinNetwork = await client.query.bitcoinUtxos.bitcoinNetwork();
@@ -1302,9 +1319,26 @@ var BitcoinLock = class _BitcoinLock {
1302
1319
  }
1303
1320
  return { lock, createdAtHeight: blockHeight, txResult };
1304
1321
  }
1305
- static async requiredSatoshisForArgonLiquidity(priceIndex, argonAmount) {
1306
- const marketRatePerBitcoin = priceIndex.getSatoshiPriceInMarketMicrogons(SATS_PER_BTC);
1307
- return argonAmount * SATS_PER_BTC / marketRatePerBitcoin;
1322
+ static satoshisRequiredForRedemptionAmount(priceIndex, redemptionAmount) {
1323
+ if (redemptionAmount <= 0n) return 0n;
1324
+ const multiplierBn = this.redemptionMultiplierBn(priceIndex);
1325
+ const targetMicrogonsPerBtc = priceIndex.getSatoshiPriceInTargetMicrogons(SATS_PER_BTC);
1326
+ let requiredTargetMicrogons = BigInt(
1327
+ new (0, BN.default)(redemptionAmount.toString()).div(multiplierBn).integerValue(BN.default.ROUND_CEIL).toFixed(0)
1328
+ );
1329
+ while (this.calculateRedemptionAmount(priceIndex, requiredTargetMicrogons) < redemptionAmount) {
1330
+ requiredTargetMicrogons += 1n;
1331
+ }
1332
+ let satoshis = BigInt(
1333
+ new (0, BN.default)(requiredTargetMicrogons.toString()).times(SATS_PER_BTC.toString()).div(targetMicrogonsPerBtc.toString()).integerValue(BN.default.ROUND_CEIL).toFixed(0)
1334
+ );
1335
+ while (this.calculateRedemptionAmountFromSatoshis(priceIndex, satoshis) < redemptionAmount) {
1336
+ satoshis += 1n;
1337
+ }
1338
+ while (satoshis > 0n && this.calculateRedemptionAmountFromSatoshis(priceIndex, satoshis - 1n) >= redemptionAmount) {
1339
+ satoshis -= 1n;
1340
+ }
1341
+ return satoshis;
1308
1342
  }
1309
1343
  };
1310
1344