@allbridge/bridge-core-sdk 0.4.1 → 0.4.2

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.
@@ -50663,8 +50663,12 @@ function fromSystemPrecision(amount, decimals) {
50663
50663
  return convertAmountPrecision(amount, SYSTEM_PRECISION, decimals);
50664
50664
  }
50665
50665
  function swapToVUsd(amount, tokenInfo) {
50666
- const fee = Big(amount).times(tokenInfo.feeShare);
50667
- const amountWithoutFee = Big(amount).minus(fee);
50666
+ const amountValue = Big(amount);
50667
+ if (amountValue.lte(0)) {
50668
+ return Big(0);
50669
+ }
50670
+ const fee = amountValue.times(tokenInfo.feeShare);
50671
+ const amountWithoutFee = amountValue.minus(fee);
50668
50672
  const inSystemPrecision = toSystemPrecision(
50669
50673
  amountWithoutFee,
50670
50674
  tokenInfo.decimals
@@ -50675,8 +50679,12 @@ function swapToVUsd(amount, tokenInfo) {
50675
50679
  return Big(poolInfo.vUsdBalance).minus(vUsdNewAmount).round(0, 0);
50676
50680
  }
50677
50681
  function swapFromVUsd(amount, tokenInfo) {
50682
+ const amountValue = Big(amount);
50683
+ if (amountValue.lte(0)) {
50684
+ return Big(0);
50685
+ }
50678
50686
  const poolInfo = tokenInfo.poolInfo;
50679
- const vUsdBalance = Big(amount).plus(poolInfo.vUsdBalance);
50687
+ const vUsdBalance = amountValue.plus(poolInfo.vUsdBalance);
50680
50688
  const newAmount = getY(vUsdBalance, poolInfo.aValue, poolInfo.dValue);
50681
50689
  const result = fromSystemPrecision(
50682
50690
  Big(poolInfo.tokenBalance).minus(newAmount),
@@ -50686,6 +50694,9 @@ function swapFromVUsd(amount, tokenInfo) {
50686
50694
  return Big(result).minus(fee).round(0, 0);
50687
50695
  }
50688
50696
  function swapToVUsdReverse(amount, tokenInfo) {
50697
+ if (Big(amount).lte(0)) {
50698
+ return Big(0);
50699
+ }
50689
50700
  const poolInfo = tokenInfo.poolInfo;
50690
50701
  const vUsdNewAmount = Big(poolInfo.vUsdBalance).minus(amount);
50691
50702
  const tokenBalance = getY(vUsdNewAmount, poolInfo.aValue, poolInfo.dValue);
@@ -50697,14 +50708,17 @@ function swapToVUsdReverse(amount, tokenInfo) {
50697
50708
  const reversedFeeShare = Big(tokenInfo.feeShare).div(
50698
50709
  Big(1).minus(tokenInfo.feeShare)
50699
50710
  );
50700
- const fee = Big(amountWithoutFee).times(reversedFeeShare);
50711
+ const fee = Big(amountWithoutFee).times(reversedFeeShare).round(0, Big.roundUp);
50701
50712
  return Big(amountWithoutFee).plus(fee).round(0, 0);
50702
50713
  }
50703
50714
  function swapFromVUsdReverse(amount, tokenInfo) {
50715
+ if (Big(amount).lte(0)) {
50716
+ return Big(0);
50717
+ }
50704
50718
  const reversedFeeShare = Big(tokenInfo.feeShare).div(
50705
50719
  Big(1).minus(tokenInfo.feeShare)
50706
50720
  );
50707
- const fee = Big(amount).times(reversedFeeShare);
50721
+ const fee = Big(amount).times(reversedFeeShare).round(0, Big.roundUp);
50708
50722
  const amountWithFee = Big(amount).plus(fee);
50709
50723
  const inSystemPrecision = toSystemPrecision(
50710
50724
  amountWithFee,
@@ -50732,9 +50746,9 @@ function getY(x, a, d) {
50732
50746
  const commonPartBig = Big(4).times(a).times(Big(d).minus(x)).minus(d);
50733
50747
  const dCubed = Big(d).pow(3);
50734
50748
  const commonPartSquared = commonPartBig.pow(2);
50735
- const sqrtBig = Big(x).times(Big(x).times(commonPartSquared).plus(Big(4).times(a).times(dCubed))).sqrt();
50749
+ const sqrtBig = Big(x).times(Big(x).times(commonPartSquared).plus(Big(4).times(a).times(dCubed))).sqrt().round(0, 0);
50736
50750
  const dividerBig = Big(8).times(a).times(x);
50737
- return commonPartBig.times(x).plus(sqrtBig).div(dividerBig);
50751
+ return commonPartBig.times(x).plus(sqrtBig).div(dividerBig).round(0, 0).plus(1);
50738
50752
  }
50739
50753
 
50740
50754
  // src/bridge/models/bridge.model.ts
@@ -53734,8 +53748,8 @@ var AllbridgeCoreSdk = class {
53734
53748
  amountToBeReceivedFloat,
53735
53749
  destinationChainToken.decimals
53736
53750
  );
53737
- const usd = swapFromVUsdReverse(amountToBeReceived, destinationChainToken);
53738
- const resultInt = swapToVUsdReverse(usd, sourceChainToken);
53751
+ const vUsd = swapFromVUsdReverse(amountToBeReceived, destinationChainToken);
53752
+ const resultInt = swapToVUsdReverse(vUsd, sourceChainToken);
53739
53753
  if (resultInt.lte(0)) {
53740
53754
  throw new InsufficientPoolLiquidity();
53741
53755
  }