@argonprotocol/mainchain 1.3.8 → 1.3.9

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.js CHANGED
@@ -390,8 +390,8 @@ var Vault = class _Vault {
390
390
  FIXED_U128_DECIMALS
391
391
  ),
392
392
  bitcoinBaseFee: vault.terms.bitcoinBaseFee.toBigInt(),
393
- liquidityPoolProfitSharing: fromFixedNumber(
394
- vault.terms.liquidityPoolProfitSharing.toBigInt(),
393
+ treasuryProfitSharing: fromFixedNumber(
394
+ vault.terms.treasuryProfitSharing.toBigInt(),
395
395
  PERMILL_DECIMALS
396
396
  )
397
397
  };
@@ -406,8 +406,8 @@ var Vault = class _Vault {
406
406
  FIXED_U128_DECIMALS
407
407
  ),
408
408
  bitcoinBaseFee: terms.bitcoinBaseFee.toBigInt(),
409
- liquidityPoolProfitSharing: fromFixedNumber(
410
- vault.terms.liquidityPoolProfitSharing.toBigInt(),
409
+ treasuryProfitSharing: fromFixedNumber(
410
+ vault.terms.treasuryProfitSharing.toBigInt(),
411
411
  PERMILL_DECIMALS
412
412
  )
413
413
  };
@@ -439,7 +439,7 @@ var Vault = class _Vault {
439
439
  return BigInt(maxRatio.multipliedBy(activated.toString()).toFixed(0, ROUND_FLOOR2));
440
440
  }
441
441
  /**
442
- * Returns the amount of Argons available to match per liquidity pool
442
+ * Returns the amount of Argons available to match per treasury pool
443
443
  */
444
444
  activatedSecuritizationPerSlot() {
445
445
  const activated = this.activatedSecuritization();
@@ -483,10 +483,7 @@ var Vault = class _Vault {
483
483
  // convert to fixed u128
484
484
  bitcoinAnnualPercentRate: toFixedNumber(annualPercentRate, FIXED_U128_DECIMALS),
485
485
  bitcoinBaseFee: BigInt(baseFee),
486
- liquidityPoolProfitSharing: toFixedNumber(
487
- args.liquidityPoolProfitSharing,
488
- PERMILL_DECIMALS
489
- )
486
+ treasuryProfitSharing: toFixedNumber(args.treasuryProfitSharing, PERMILL_DECIMALS)
490
487
  },
491
488
  securitizationRatio: toFixedNumber(securitizationRatio, FIXED_U128_DECIMALS),
492
489
  securitization: BigInt(securitization),
@@ -560,7 +557,31 @@ var BitcoinLocks = class {
560
557
  }
561
558
  return void 0;
562
559
  }
563
- async getMarketRate(satoshis) {
560
+ async getMarketRate(priceIndex, satoshis) {
561
+ return priceIndex.getBtcMicrogonPrice(satoshis);
562
+ }
563
+ async getRedemptionRate(priceIndex, details) {
564
+ const { satoshis, peggedPrice } = details;
565
+ const satsPerArgon = Number(SATS_PER_BTC) / MICROGONS_PER_ARGON;
566
+ let price = Number(priceIndex.btcUsdPrice);
567
+ price = price / satsPerArgon * Number(satoshis);
568
+ if (peggedPrice !== void 0 && peggedPrice < price) {
569
+ price = Number(peggedPrice);
570
+ }
571
+ const r = Number(priceIndex.rValue);
572
+ let multiplier;
573
+ if (r >= 1) {
574
+ multiplier = 1;
575
+ } else if (r >= 0.9) {
576
+ multiplier = 20 * (r * r) - 38 * r + 19;
577
+ } else if (r >= 0.01) {
578
+ multiplier = (0.5618 * r + 0.3944) / r;
579
+ } else {
580
+ multiplier = 1 / r * (0.576 * r + 0.4);
581
+ }
582
+ return BigInt(Math.floor(price * multiplier));
583
+ }
584
+ async getMarketRateApi(satoshis) {
564
585
  const client = this.client;
565
586
  const sats = client.createType("U64", satoshis.toString());
566
587
  const marketRate = await client.rpc.state.call("BitcoinApis_market_rate", sats.toHex(true));
@@ -570,7 +591,7 @@ var BitcoinLocks = class {
570
591
  }
571
592
  return rate.value.toBigInt();
572
593
  }
573
- async getRedemptionRate(satoshis) {
594
+ async getRedemptionRateApi(satoshis) {
574
595
  const client = this.client;
575
596
  const sats = client.createType("U64", satoshis.toString());
576
597
  const marketRate = await client.rpc.state.call("BitcoinApis_redemption_rate", sats.toHex(true));
@@ -665,6 +686,7 @@ var BitcoinLocks = class {
665
686
  cosignHdIndex: cosign_hd_index.toNumber(),
666
687
  claimHdIndex: claim_hd_index.toNumber()
667
688
  };
689
+ const securityFees = utxo.securityFees.toBigInt();
668
690
  const vaultClaimHeight = utxo.vaultClaimHeight.toNumber();
669
691
  const openClaimHeight = utxo.openClaimHeight.toNumber();
670
692
  const createdAtHeight = utxo.createdAtHeight.toNumber();
@@ -688,6 +710,7 @@ var BitcoinLocks = class {
688
710
  vaultClaimHeight,
689
711
  openClaimHeight,
690
712
  createdAtHeight,
713
+ securityFees,
691
714
  isVerified,
692
715
  isRejectedNeedsRelease,
693
716
  fundHoldExtensionsByBitcoinExpirationHeight
@@ -783,7 +806,7 @@ var BitcoinLocks = class {
783
806
  return mintsPending;
784
807
  }
785
808
  async createInitializeLockTx(args) {
786
- const { vault, argonKeyring, satoshis, tip = 0n, ownerBitcoinPubkey } = args;
809
+ const { vault, priceIndex, argonKeyring, satoshis, tip = 0n, ownerBitcoinPubkey } = args;
787
810
  const client = this.client;
788
811
  if (ownerBitcoinPubkey.length !== 33) {
789
812
  throw new Error(
@@ -796,7 +819,7 @@ var BitcoinLocks = class {
796
819
  client.tx.bitcoinLocks.initialize(vault.vaultId, satoshis, ownerBitcoinPubkey),
797
820
  argonKeyring
798
821
  );
799
- const marketPrice = await this.getMarketRate(BigInt(satoshis));
822
+ const marketPrice = await this.getMarketRate(priceIndex, satoshis);
800
823
  const isVaultOwner = argonKeyring.address === vault.operatorAccountId;
801
824
  const securityFee = isVaultOwner ? 0n : vault.calculateBitcoinFee(marketPrice);
802
825
  const { canAfford, availableBalance, txFee } = await submitter.canAfford({
@@ -844,14 +867,15 @@ var BitcoinLocks = class {
844
867
  securityFee
845
868
  };
846
869
  }
847
- async requiredSatoshisForArgonLiquidity(argonAmount) {
848
- const marketRatePerBitcoin = await this.getMarketRate(SATS_PER_BTC);
870
+ async requiredSatoshisForArgonLiquidity(priceIndex, argonAmount) {
871
+ const marketRatePerBitcoin = priceIndex.getBtcMicrogonPrice(SATS_PER_BTC);
849
872
  return argonAmount * SATS_PER_BTC / marketRatePerBitcoin;
850
873
  }
851
874
  async requestRelease(args) {
852
875
  const client = this.client;
853
876
  const {
854
877
  lock,
878
+ priceIndex,
855
879
  releaseRequest: { bitcoinNetworkFee, toScriptPubkey },
856
880
  argonKeyring,
857
881
  tip,
@@ -865,10 +889,7 @@ var BitcoinLocks = class {
865
889
  client.tx.bitcoinLocks.requestRelease(lock.utxoId, toScriptPubkey, bitcoinNetworkFee),
866
890
  argonKeyring
867
891
  );
868
- let redemptionPrice = await this.getRedemptionRate(lock.satoshis);
869
- if (redemptionPrice > lock.peggedPrice) {
870
- redemptionPrice = lock.peggedPrice;
871
- }
892
+ const redemptionPrice = await this.getRedemptionRate(priceIndex, lock);
872
893
  const canAfford = await submitter.canAfford({
873
894
  tip,
874
895
  unavailableBalance: BigInt(redemptionPrice)
@@ -891,17 +912,13 @@ var BitcoinLocks = class {
891
912
  blockHeight
892
913
  };
893
914
  }
894
- async releasePrice(satoshis, peggedPrice) {
895
- const redemptionRate = await this.getRedemptionRate(satoshis);
896
- if (redemptionRate > peggedPrice) {
897
- return redemptionRate;
898
- }
899
- return peggedPrice;
915
+ async releasePrice(priceIndex, lock) {
916
+ return await this.getRedemptionRate(priceIndex, lock);
900
917
  }
901
- async getRatchetPrice(lock, vault) {
918
+ async getRatchetPrice(lock, priceIndex, vault) {
902
919
  const { createdAtHeight, vaultClaimHeight, peggedPrice, satoshis } = lock;
903
920
  const client = this.client;
904
- const marketRate = await this.getMarketRate(BigInt(satoshis));
921
+ const marketRate = await this.getMarketRate(priceIndex, BigInt(satoshis));
905
922
  let ratchetingFee = vault.terms.bitcoinBaseFee;
906
923
  let burnAmount = 0n;
907
924
  if (marketRate > peggedPrice) {
@@ -912,7 +929,7 @@ var BitcoinLocks = class {
912
929
  const remainingDuration = 1 - elapsed;
913
930
  ratchetingFee = BigInt(remainingDuration * Number(lockFee));
914
931
  } else {
915
- burnAmount = await this.releasePrice(lock.satoshis, peggedPrice);
932
+ burnAmount = await this.releasePrice(priceIndex, lock);
916
933
  }
917
934
  return {
918
935
  ratchetingFee,
@@ -921,9 +938,9 @@ var BitcoinLocks = class {
921
938
  };
922
939
  }
923
940
  async ratchet(args) {
924
- const { lock, argonKeyring, tip = 0n, vault, txProgressCallback } = args;
941
+ const { lock, priceIndex, argonKeyring, tip = 0n, vault, txProgressCallback } = args;
925
942
  const client = this.client;
926
- const ratchetPrice = await this.getRatchetPrice(lock, vault);
943
+ const ratchetPrice = await this.getRatchetPrice(lock, priceIndex, vault);
927
944
  const txSubmitter = new TxSubmitter(
928
945
  client,
929
946
  client.tx.bitcoinLocks.ratchet(lock.utxoId),
@@ -955,15 +972,22 @@ var BitcoinLocks = class {
955
972
  const api = await client.at(blockHash);
956
973
  const blockHeight = await api.query.system.number().then((x) => x.toNumber());
957
974
  const bitcoinBlockHeight = await api.query.bitcoinUtxos.confirmedBitcoinBlockTip().then((x) => x.unwrap().blockHeight.toNumber());
958
- const { amountBurned, newPeggedPrice, originalPeggedPrice } = ratchetEvent.data;
959
- let mintAmount = newPeggedPrice.toBigInt();
960
- if (newPeggedPrice > originalPeggedPrice) {
975
+ const {
976
+ amountBurned,
977
+ liquidityPromised: liquidityPromisedRaw,
978
+ newPeggedPrice,
979
+ originalPeggedPrice
980
+ } = ratchetEvent.data;
981
+ const liquidityPromised = liquidityPromisedRaw.toBigInt();
982
+ let mintAmount = liquidityPromised;
983
+ if (liquidityPromised > originalPeggedPrice.toBigInt()) {
961
984
  mintAmount -= originalPeggedPrice.toBigInt();
962
985
  }
963
986
  return {
964
987
  txFee: submission.finalFee ?? 0n,
965
988
  securityFee: ratchetPrice.ratchetingFee,
966
989
  pendingMint: mintAmount,
990
+ liquidityPromised,
967
991
  newPeggedPrice: newPeggedPrice.toBigInt(),
968
992
  burned: amountBurned.toBigInt(),
969
993
  blockHeight,
@@ -971,6 +995,63 @@ var BitcoinLocks = class {
971
995
  };
972
996
  }
973
997
  };
998
+ var PriceIndex = class {
999
+ constructor() {
1000
+ __publicField(this, "btcUsdPrice");
1001
+ __publicField(this, "argonotUsdPrice");
1002
+ __publicField(this, "argonUsdPrice");
1003
+ __publicField(this, "argonUsdTargetPrice");
1004
+ __publicField(this, "argonTimeWeightedAverageLiquidity");
1005
+ __publicField(this, "lastUpdatedTick");
1006
+ }
1007
+ async load(client) {
1008
+ const current = await client.query.priceIndex.current();
1009
+ if (!current.isSome) {
1010
+ this.argonUsdPrice = void 0;
1011
+ this.argonotUsdPrice = void 0;
1012
+ this.btcUsdPrice = void 0;
1013
+ this.argonUsdTargetPrice = void 0;
1014
+ this.argonTimeWeightedAverageLiquidity = void 0;
1015
+ this.lastUpdatedTick = void 0;
1016
+ return this;
1017
+ }
1018
+ const value = current.unwrap();
1019
+ this.btcUsdPrice = fromFixedNumber(value.btcUsdPrice.toBigInt(), FIXED_U128_DECIMALS);
1020
+ this.argonotUsdPrice = fromFixedNumber(value.argonotUsdPrice.toBigInt(), FIXED_U128_DECIMALS);
1021
+ this.argonUsdPrice = fromFixedNumber(value.argonUsdPrice.toBigInt(), FIXED_U128_DECIMALS);
1022
+ this.argonUsdTargetPrice = fromFixedNumber(
1023
+ value.argonUsdTargetPrice.toBigInt(),
1024
+ FIXED_U128_DECIMALS
1025
+ );
1026
+ this.argonTimeWeightedAverageLiquidity = fromFixedNumber(
1027
+ value.argonTimeWeightedAverageLiquidity.toBigInt(),
1028
+ FIXED_U128_DECIMALS
1029
+ );
1030
+ this.lastUpdatedTick = value.tick.toNumber();
1031
+ return this;
1032
+ }
1033
+ getBtcMicrogonPrice(satoshis) {
1034
+ if (this.btcUsdPrice === void 0 || this.argonUsdPrice === void 0) {
1035
+ throw new Error("PriceIndex not loaded");
1036
+ }
1037
+ const satoshiCents = this.btcUsdPrice.multipliedBy(satoshis).dividedBy(SATS_PER_BTC);
1038
+ const microgons = satoshiCents.multipliedBy(MICROGONS_PER_ARGON).dividedBy(this.argonUsdPrice);
1039
+ return BigInt(microgons.integerValue(BigNumber2__default.ROUND_DOWN).toString());
1040
+ }
1041
+ get rValue() {
1042
+ if (this.argonUsdTargetPrice === void 0 || this.argonUsdPrice === void 0) {
1043
+ throw new Error("PriceIndex not loaded");
1044
+ }
1045
+ return this.argonUsdPrice.div(this.argonUsdTargetPrice);
1046
+ }
1047
+ get argonCpi() {
1048
+ if (this.argonUsdTargetPrice === void 0 || this.argonUsdPrice === void 0) {
1049
+ throw new Error("PriceIndex not loaded");
1050
+ }
1051
+ const ratio = this.argonUsdTargetPrice.div(this.argonUsdPrice);
1052
+ return ratio.minus(1);
1053
+ }
1054
+ };
974
1055
  async function waitForLoad() {
975
1056
  await cryptoWaitReady();
976
1057
  }
@@ -984,6 +1065,6 @@ async function getClient(host, options) {
984
1065
  return await ApiPromise.create({ provider, noInitWarn: true, ...options ?? {} });
985
1066
  }
986
1067
 
987
- export { BitcoinLocks, ExtrinsicError2 as ExtrinsicError, FIXED_U128_DECIMALS, MICROGONS_PER_ARGON, PERMILL_DECIMALS, SATS_PER_BTC, TxResult, TxSubmitter, Vault, WageProtector, checkForExtrinsicSuccess, createKeyringPair, dispatchErrorToExtrinsicError, dispatchErrorToString, formatArgons, fromFixedNumber, getAuthorFromHeader, getClient, getTickFromHeader, gettersToObject, keyringFromSuri, toFixedNumber, waitForLoad };
1068
+ export { BitcoinLocks, ExtrinsicError2 as ExtrinsicError, FIXED_U128_DECIMALS, MICROGONS_PER_ARGON, PERMILL_DECIMALS, PriceIndex, SATS_PER_BTC, TxResult, TxSubmitter, Vault, WageProtector, checkForExtrinsicSuccess, createKeyringPair, dispatchErrorToExtrinsicError, dispatchErrorToString, formatArgons, fromFixedNumber, getAuthorFromHeader, getClient, getTickFromHeader, gettersToObject, keyringFromSuri, toFixedNumber, waitForLoad };
988
1069
  //# sourceMappingURL=index.js.map
989
1070
  //# sourceMappingURL=index.js.map