impermax-sdk 1.1.56 → 1.1.57

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.
@@ -648,6 +648,21 @@
648
648
  "stateMutability": "view",
649
649
  "type": "function"
650
650
  },
651
+ {
652
+ "constant": true,
653
+ "inputs": [],
654
+ "name": "safetyMargin",
655
+ "outputs": [
656
+ {
657
+ "internalType": "uint256",
658
+ "name": "",
659
+ "type": "uint256"
660
+ }
661
+ ],
662
+ "payable": false,
663
+ "stateMutability": "view",
664
+ "type": "function"
665
+ },
651
666
  {
652
667
  "constant": true,
653
668
  "inputs": [],
@@ -678,6 +693,26 @@
678
693
  "stateMutability": "nonpayable",
679
694
  "type": "function"
680
695
  },
696
+ {
697
+ "constant": false,
698
+ "inputs": [],
699
+ "name": "getTwapReserves",
700
+ "outputs": [
701
+ {
702
+ "internalType": "uint112",
703
+ "name": "twapReserve0",
704
+ "type": "uint112"
705
+ },
706
+ {
707
+ "internalType": "uint112",
708
+ "name": "twapReserve1",
709
+ "type": "uint112"
710
+ }
711
+ ],
712
+ "payable": false,
713
+ "stateMutability": "nonpayable",
714
+ "type": "function"
715
+ },
681
716
  {
682
717
  "constant": false,
683
718
  "inputs": [],
@@ -903,6 +938,36 @@
903
938
  "stateMutability": "pure",
904
939
  "type": "function"
905
940
  },
941
+ {
942
+ "constant": true,
943
+ "inputs": [],
944
+ "name": "SAFETY_MARGIN_MIN",
945
+ "outputs": [
946
+ {
947
+ "internalType": "uint256",
948
+ "name": "",
949
+ "type": "uint256"
950
+ }
951
+ ],
952
+ "payable": false,
953
+ "stateMutability": "pure",
954
+ "type": "function"
955
+ },
956
+ {
957
+ "constant": true,
958
+ "inputs": [],
959
+ "name": "SAFETY_MARGIN_MAX",
960
+ "outputs": [
961
+ {
962
+ "internalType": "uint256",
963
+ "name": "",
964
+ "type": "uint256"
965
+ }
966
+ ],
967
+ "payable": false,
968
+ "stateMutability": "pure",
969
+ "type": "function"
970
+ },
906
971
  {
907
972
  "constant": true,
908
973
  "inputs": [],
@@ -983,6 +1048,21 @@
983
1048
  "stateMutability": "nonpayable",
984
1049
  "type": "function"
985
1050
  },
1051
+ {
1052
+ "constant": false,
1053
+ "inputs": [
1054
+ {
1055
+ "internalType": "uint256",
1056
+ "name": "newSafetyMargin",
1057
+ "type": "uint256"
1058
+ }
1059
+ ],
1060
+ "name": "_setSafetyMargin",
1061
+ "outputs": [],
1062
+ "payable": false,
1063
+ "stateMutability": "nonpayable",
1064
+ "type": "function"
1065
+ },
986
1066
  {
987
1067
  "constant": false,
988
1068
  "inputs": [
@@ -179,16 +179,24 @@ class OnchainLendingPool {
179
179
  getMarketPrice() {
180
180
  return __awaiter(this, void 0, void 0, function* () {
181
181
  const [reserve0, reserve1] = yield this.getReserves();
182
- return this.impermaxFactory.getOnchain().priceInverted ? 1 * reserve0 / reserve1 : 1 * reserve1 / reserve0;
182
+ const priceInverted = this.impermaxFactory.getOnchain().priceInverted;
183
+ if (factories_1.STABLE_FACTORIES.includes(this.impermaxFactory.getFactory()))
184
+ return getPriceFromReservesSolidlyStable(reserve0, reserve1, priceInverted);
185
+ else
186
+ return getPriceFromReservesUniswapV2(reserve0, reserve1, priceInverted);
183
187
  });
184
188
  }
185
189
  // TWAP Price
186
190
  initializeTWAPPrice() {
187
191
  return __awaiter(this, void 0, void 0, function* () {
188
192
  try {
193
+ const collateral = yield this.getCollateral().getPoolToken();
194
+ if (factories_1.STABLE_FACTORIES.includes(this.impermaxFactory.getFactory())) {
195
+ const { twapReserve0, twapReserve1 } = yield collateral.methods.getTwapReserves().call();
196
+ return getPriceFromReservesSolidlyStable(twapReserve0, twapReserve1);
197
+ }
189
198
  let price;
190
199
  if (factories_1.SOLIDEX_FACTORIES.includes(this.impermaxFactory.getFactory())) {
191
- const collateral = yield this.getCollateral().getPoolToken();
192
200
  price = yield collateral.methods.getTwapPrice112x112().call();
193
201
  }
194
202
  else {
@@ -0,0 +1,2 @@
1
+ declare function getPriceFromReservesUniswapV2(x: number, y: number, priceInverted?: boolean): number;
2
+ declare function getPriceFromReservesSolidlyStable(x: number, y: number, priceInverted?: boolean): number;
@@ -0,0 +1,14 @@
1
+ // return X/Y price
2
+ /*
3
+ * X/Y price is given by dividing Y reserves by X reserves
4
+ */
5
+ function getPriceFromReservesUniswapV2(x, y, priceInverted = true) {
6
+ const price = y / x;
7
+ return !priceInverted ? price : 1 / price;
8
+ }
9
+ function getPriceFromReservesSolidlyStable(x, y, priceInverted = false) {
10
+ const N = 3 * x ^ 2 * y + y ^ 3;
11
+ const D = 3 * y ^ 2 * x + x ^ 3;
12
+ const price = N / D;
13
+ return !priceInverted ? price : 1 / price;
14
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "impermax-sdk",
3
- "version": "1.1.56",
3
+ "version": "1.1.57",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "module": "./lib/index.js",