@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/lib/index.js CHANGED
@@ -401,8 +401,8 @@ var Vault = class _Vault {
401
401
  FIXED_U128_DECIMALS
402
402
  ),
403
403
  bitcoinBaseFee: vault.terms.bitcoinBaseFee.toBigInt(),
404
- liquidityPoolProfitSharing: fromFixedNumber(
405
- vault.terms.liquidityPoolProfitSharing.toBigInt(),
404
+ treasuryProfitSharing: fromFixedNumber(
405
+ vault.terms.treasuryProfitSharing.toBigInt(),
406
406
  PERMILL_DECIMALS
407
407
  )
408
408
  };
@@ -417,8 +417,8 @@ var Vault = class _Vault {
417
417
  FIXED_U128_DECIMALS
418
418
  ),
419
419
  bitcoinBaseFee: terms.bitcoinBaseFee.toBigInt(),
420
- liquidityPoolProfitSharing: fromFixedNumber(
421
- vault.terms.liquidityPoolProfitSharing.toBigInt(),
420
+ treasuryProfitSharing: fromFixedNumber(
421
+ vault.terms.treasuryProfitSharing.toBigInt(),
422
422
  PERMILL_DECIMALS
423
423
  )
424
424
  };
@@ -450,7 +450,7 @@ var Vault = class _Vault {
450
450
  return BigInt(maxRatio.multipliedBy(activated.toString()).toFixed(0, ROUND_FLOOR2));
451
451
  }
452
452
  /**
453
- * Returns the amount of Argons available to match per liquidity pool
453
+ * Returns the amount of Argons available to match per treasury pool
454
454
  */
455
455
  activatedSecuritizationPerSlot() {
456
456
  const activated = this.activatedSecuritization();
@@ -494,10 +494,7 @@ var Vault = class _Vault {
494
494
  // convert to fixed u128
495
495
  bitcoinAnnualPercentRate: toFixedNumber(annualPercentRate, FIXED_U128_DECIMALS),
496
496
  bitcoinBaseFee: BigInt(baseFee),
497
- liquidityPoolProfitSharing: toFixedNumber(
498
- args.liquidityPoolProfitSharing,
499
- PERMILL_DECIMALS
500
- )
497
+ treasuryProfitSharing: toFixedNumber(args.treasuryProfitSharing, PERMILL_DECIMALS)
501
498
  },
502
499
  securitizationRatio: toFixedNumber(securitizationRatio, FIXED_U128_DECIMALS),
503
500
  securitization: BigInt(securitization),
@@ -577,7 +574,31 @@ var BitcoinLocks = class {
577
574
  }
578
575
  return void 0;
579
576
  }
580
- async getMarketRate(satoshis) {
577
+ async getMarketRate(priceIndex, satoshis) {
578
+ return priceIndex.getBtcMicrogonPrice(satoshis);
579
+ }
580
+ async getRedemptionRate(priceIndex, details) {
581
+ const { satoshis, peggedPrice } = details;
582
+ const satsPerArgon = Number(SATS_PER_BTC) / MICROGONS_PER_ARGON;
583
+ let price = Number(priceIndex.btcUsdPrice);
584
+ price = price / satsPerArgon * Number(satoshis);
585
+ if (peggedPrice !== void 0 && peggedPrice < price) {
586
+ price = Number(peggedPrice);
587
+ }
588
+ const r = Number(priceIndex.rValue);
589
+ let multiplier;
590
+ if (r >= 1) {
591
+ multiplier = 1;
592
+ } else if (r >= 0.9) {
593
+ multiplier = 20 * (r * r) - 38 * r + 19;
594
+ } else if (r >= 0.01) {
595
+ multiplier = (0.5618 * r + 0.3944) / r;
596
+ } else {
597
+ multiplier = 1 / r * (0.576 * r + 0.4);
598
+ }
599
+ return BigInt(Math.floor(price * multiplier));
600
+ }
601
+ async getMarketRateApi(satoshis) {
581
602
  const client = this.client;
582
603
  const sats = client.createType("U64", satoshis.toString());
583
604
  const marketRate = await client.rpc.state.call("BitcoinApis_market_rate", sats.toHex(true));
@@ -587,7 +608,7 @@ var BitcoinLocks = class {
587
608
  }
588
609
  return rate.value.toBigInt();
589
610
  }
590
- async getRedemptionRate(satoshis) {
611
+ async getRedemptionRateApi(satoshis) {
591
612
  const client = this.client;
592
613
  const sats = client.createType("U64", satoshis.toString());
593
614
  const marketRate = await client.rpc.state.call("BitcoinApis_redemption_rate", sats.toHex(true));
@@ -682,6 +703,7 @@ var BitcoinLocks = class {
682
703
  cosignHdIndex: cosign_hd_index.toNumber(),
683
704
  claimHdIndex: claim_hd_index.toNumber()
684
705
  };
706
+ const securityFees = utxo.securityFees.toBigInt();
685
707
  const vaultClaimHeight = utxo.vaultClaimHeight.toNumber();
686
708
  const openClaimHeight = utxo.openClaimHeight.toNumber();
687
709
  const createdAtHeight = utxo.createdAtHeight.toNumber();
@@ -705,6 +727,7 @@ var BitcoinLocks = class {
705
727
  vaultClaimHeight,
706
728
  openClaimHeight,
707
729
  createdAtHeight,
730
+ securityFees,
708
731
  isVerified,
709
732
  isRejectedNeedsRelease,
710
733
  fundHoldExtensionsByBitcoinExpirationHeight
@@ -800,7 +823,7 @@ var BitcoinLocks = class {
800
823
  return mintsPending;
801
824
  }
802
825
  async createInitializeLockTx(args) {
803
- const { vault, argonKeyring, satoshis, tip = 0n, ownerBitcoinPubkey } = args;
826
+ const { vault, priceIndex, argonKeyring, satoshis, tip = 0n, ownerBitcoinPubkey } = args;
804
827
  const client = this.client;
805
828
  if (ownerBitcoinPubkey.length !== 33) {
806
829
  throw new Error(
@@ -813,7 +836,7 @@ var BitcoinLocks = class {
813
836
  client.tx.bitcoinLocks.initialize(vault.vaultId, satoshis, ownerBitcoinPubkey),
814
837
  argonKeyring
815
838
  );
816
- const marketPrice = await this.getMarketRate(BigInt(satoshis));
839
+ const marketPrice = await this.getMarketRate(priceIndex, satoshis);
817
840
  const isVaultOwner = argonKeyring.address === vault.operatorAccountId;
818
841
  const securityFee = isVaultOwner ? 0n : vault.calculateBitcoinFee(marketPrice);
819
842
  const { canAfford, availableBalance, txFee } = await submitter.canAfford({
@@ -861,14 +884,15 @@ var BitcoinLocks = class {
861
884
  securityFee
862
885
  };
863
886
  }
864
- async requiredSatoshisForArgonLiquidity(argonAmount) {
865
- const marketRatePerBitcoin = await this.getMarketRate(SATS_PER_BTC);
887
+ async requiredSatoshisForArgonLiquidity(priceIndex, argonAmount) {
888
+ const marketRatePerBitcoin = priceIndex.getBtcMicrogonPrice(SATS_PER_BTC);
866
889
  return argonAmount * SATS_PER_BTC / marketRatePerBitcoin;
867
890
  }
868
891
  async requestRelease(args) {
869
892
  const client = this.client;
870
893
  const {
871
894
  lock,
895
+ priceIndex,
872
896
  releaseRequest: { bitcoinNetworkFee, toScriptPubkey },
873
897
  argonKeyring,
874
898
  tip,
@@ -882,10 +906,7 @@ var BitcoinLocks = class {
882
906
  client.tx.bitcoinLocks.requestRelease(lock.utxoId, toScriptPubkey, bitcoinNetworkFee),
883
907
  argonKeyring
884
908
  );
885
- let redemptionPrice = await this.getRedemptionRate(lock.satoshis);
886
- if (redemptionPrice > lock.peggedPrice) {
887
- redemptionPrice = lock.peggedPrice;
888
- }
909
+ const redemptionPrice = await this.getRedemptionRate(priceIndex, lock);
889
910
  const canAfford = await submitter.canAfford({
890
911
  tip,
891
912
  unavailableBalance: BigInt(redemptionPrice)
@@ -908,17 +929,13 @@ var BitcoinLocks = class {
908
929
  blockHeight
909
930
  };
910
931
  }
911
- async releasePrice(satoshis, peggedPrice) {
912
- const redemptionRate = await this.getRedemptionRate(satoshis);
913
- if (redemptionRate > peggedPrice) {
914
- return redemptionRate;
915
- }
916
- return peggedPrice;
932
+ async releasePrice(priceIndex, lock) {
933
+ return await this.getRedemptionRate(priceIndex, lock);
917
934
  }
918
- async getRatchetPrice(lock, vault) {
935
+ async getRatchetPrice(lock, priceIndex, vault) {
919
936
  const { createdAtHeight, vaultClaimHeight, peggedPrice, satoshis } = lock;
920
937
  const client = this.client;
921
- const marketRate = await this.getMarketRate(BigInt(satoshis));
938
+ const marketRate = await this.getMarketRate(priceIndex, BigInt(satoshis));
922
939
  let ratchetingFee = vault.terms.bitcoinBaseFee;
923
940
  let burnAmount = 0n;
924
941
  if (marketRate > peggedPrice) {
@@ -929,7 +946,7 @@ var BitcoinLocks = class {
929
946
  const remainingDuration = 1 - elapsed;
930
947
  ratchetingFee = BigInt(remainingDuration * Number(lockFee));
931
948
  } else {
932
- burnAmount = await this.releasePrice(lock.satoshis, peggedPrice);
949
+ burnAmount = await this.releasePrice(priceIndex, lock);
933
950
  }
934
951
  return {
935
952
  ratchetingFee,
@@ -938,9 +955,9 @@ var BitcoinLocks = class {
938
955
  };
939
956
  }
940
957
  async ratchet(args) {
941
- const { lock, argonKeyring, tip = 0n, vault, txProgressCallback } = args;
958
+ const { lock, priceIndex, argonKeyring, tip = 0n, vault, txProgressCallback } = args;
942
959
  const client = this.client;
943
- const ratchetPrice = await this.getRatchetPrice(lock, vault);
960
+ const ratchetPrice = await this.getRatchetPrice(lock, priceIndex, vault);
944
961
  const txSubmitter = new TxSubmitter(
945
962
  client,
946
963
  client.tx.bitcoinLocks.ratchet(lock.utxoId),
@@ -972,15 +989,22 @@ var BitcoinLocks = class {
972
989
  const api = await client.at(blockHash);
973
990
  const blockHeight = await api.query.system.number().then((x) => x.toNumber());
974
991
  const bitcoinBlockHeight = await api.query.bitcoinUtxos.confirmedBitcoinBlockTip().then((x) => x.unwrap().blockHeight.toNumber());
975
- const { amountBurned, newPeggedPrice, originalPeggedPrice } = ratchetEvent.data;
976
- let mintAmount = newPeggedPrice.toBigInt();
977
- if (newPeggedPrice > originalPeggedPrice) {
992
+ const {
993
+ amountBurned,
994
+ liquidityPromised: liquidityPromisedRaw,
995
+ newPeggedPrice,
996
+ originalPeggedPrice
997
+ } = ratchetEvent.data;
998
+ const liquidityPromised = liquidityPromisedRaw.toBigInt();
999
+ let mintAmount = liquidityPromised;
1000
+ if (liquidityPromised > originalPeggedPrice.toBigInt()) {
978
1001
  mintAmount -= originalPeggedPrice.toBigInt();
979
1002
  }
980
1003
  return {
981
1004
  txFee: submission.finalFee ?? 0n,
982
1005
  securityFee: ratchetPrice.ratchetingFee,
983
1006
  pendingMint: mintAmount,
1007
+ liquidityPromised,
984
1008
  newPeggedPrice: newPeggedPrice.toBigInt(),
985
1009
  burned: amountBurned.toBigInt(),
986
1010
  blockHeight,
@@ -989,6 +1013,64 @@ var BitcoinLocks = class {
989
1013
  }
990
1014
  };
991
1015
 
1016
+ // src/PriceIndex.ts
1017
+ import BigNumber4 from "bignumber.js";
1018
+ var PriceIndex = class {
1019
+ btcUsdPrice;
1020
+ argonotUsdPrice;
1021
+ argonUsdPrice;
1022
+ argonUsdTargetPrice;
1023
+ argonTimeWeightedAverageLiquidity;
1024
+ lastUpdatedTick;
1025
+ async load(client) {
1026
+ const current = await client.query.priceIndex.current();
1027
+ if (!current.isSome) {
1028
+ this.argonUsdPrice = void 0;
1029
+ this.argonotUsdPrice = void 0;
1030
+ this.btcUsdPrice = void 0;
1031
+ this.argonUsdTargetPrice = void 0;
1032
+ this.argonTimeWeightedAverageLiquidity = void 0;
1033
+ this.lastUpdatedTick = void 0;
1034
+ return this;
1035
+ }
1036
+ const value = current.unwrap();
1037
+ this.btcUsdPrice = fromFixedNumber(value.btcUsdPrice.toBigInt(), FIXED_U128_DECIMALS);
1038
+ this.argonotUsdPrice = fromFixedNumber(value.argonotUsdPrice.toBigInt(), FIXED_U128_DECIMALS);
1039
+ this.argonUsdPrice = fromFixedNumber(value.argonUsdPrice.toBigInt(), FIXED_U128_DECIMALS);
1040
+ this.argonUsdTargetPrice = fromFixedNumber(
1041
+ value.argonUsdTargetPrice.toBigInt(),
1042
+ FIXED_U128_DECIMALS
1043
+ );
1044
+ this.argonTimeWeightedAverageLiquidity = fromFixedNumber(
1045
+ value.argonTimeWeightedAverageLiquidity.toBigInt(),
1046
+ FIXED_U128_DECIMALS
1047
+ );
1048
+ this.lastUpdatedTick = value.tick.toNumber();
1049
+ return this;
1050
+ }
1051
+ getBtcMicrogonPrice(satoshis) {
1052
+ if (this.btcUsdPrice === void 0 || this.argonUsdPrice === void 0) {
1053
+ throw new Error("PriceIndex not loaded");
1054
+ }
1055
+ const satoshiCents = this.btcUsdPrice.multipliedBy(satoshis).dividedBy(SATS_PER_BTC);
1056
+ const microgons = satoshiCents.multipliedBy(MICROGONS_PER_ARGON).dividedBy(this.argonUsdPrice);
1057
+ return BigInt(microgons.integerValue(BigNumber4.ROUND_DOWN).toString());
1058
+ }
1059
+ get rValue() {
1060
+ if (this.argonUsdTargetPrice === void 0 || this.argonUsdPrice === void 0) {
1061
+ throw new Error("PriceIndex not loaded");
1062
+ }
1063
+ return this.argonUsdPrice.div(this.argonUsdTargetPrice);
1064
+ }
1065
+ get argonCpi() {
1066
+ if (this.argonUsdTargetPrice === void 0 || this.argonUsdPrice === void 0) {
1067
+ throw new Error("PriceIndex not loaded");
1068
+ }
1069
+ const ratio = this.argonUsdTargetPrice.div(this.argonUsdPrice);
1070
+ return ratio.minus(1);
1071
+ }
1072
+ };
1073
+
992
1074
  // src/index.ts
993
1075
  import { u8aToHex as u8aToHex2, hexToU8a as hexToU8a2, u8aEq } from "@polkadot/util";
994
1076
  import { GenericEvent as GenericEvent2, GenericBlock, GenericAddress } from "@polkadot/types/generic";
@@ -1045,6 +1127,7 @@ export {
1045
1127
  Null,
1046
1128
  Option,
1047
1129
  PERMILL_DECIMALS,
1130
+ PriceIndex,
1048
1131
  Range,
1049
1132
  Result,
1050
1133
  SATS_PER_BTC,