impermax-sdk 1.1.56 → 1.1.58
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": [
|
|
@@ -16,6 +16,7 @@ const types_1 = require("../../../config/types");
|
|
|
16
16
|
const onchainCollateral_1 = __importDefault(require("./onchainCollateral"));
|
|
17
17
|
const onchainBorrowable_1 = __importDefault(require("./onchainBorrowable"));
|
|
18
18
|
const factories_1 = require("../../../config/factories");
|
|
19
|
+
const price_from_reserves_1 = require("../../../utils/price-from-reserves");
|
|
19
20
|
class OnchainLendingPool {
|
|
20
21
|
constructor(impermaxFactory, pairAddress) {
|
|
21
22
|
this.cache = {};
|
|
@@ -179,16 +180,24 @@ class OnchainLendingPool {
|
|
|
179
180
|
getMarketPrice() {
|
|
180
181
|
return __awaiter(this, void 0, void 0, function* () {
|
|
181
182
|
const [reserve0, reserve1] = yield this.getReserves();
|
|
182
|
-
|
|
183
|
+
const priceInverted = this.impermaxFactory.getOnchain().priceInverted;
|
|
184
|
+
if (factories_1.STABLE_FACTORIES.includes(this.impermaxFactory.getFactory()))
|
|
185
|
+
return (0, price_from_reserves_1.getPriceFromReservesSolidlyStable)(reserve0, reserve1, priceInverted);
|
|
186
|
+
else
|
|
187
|
+
return (0, price_from_reserves_1.getPriceFromReservesUniswapV2)(reserve0, reserve1, priceInverted);
|
|
183
188
|
});
|
|
184
189
|
}
|
|
185
190
|
// TWAP Price
|
|
186
191
|
initializeTWAPPrice() {
|
|
187
192
|
return __awaiter(this, void 0, void 0, function* () {
|
|
188
193
|
try {
|
|
194
|
+
const collateral = yield this.getCollateral().getPoolToken();
|
|
195
|
+
if (factories_1.STABLE_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
196
|
+
const { twapReserve0, twapReserve1 } = yield collateral.methods.getTwapReserves().call();
|
|
197
|
+
return (0, price_from_reserves_1.getPriceFromReservesSolidlyStable)(twapReserve0, twapReserve1);
|
|
198
|
+
}
|
|
189
199
|
let price;
|
|
190
200
|
if (factories_1.SOLIDEX_FACTORIES.includes(this.impermaxFactory.getFactory())) {
|
|
191
|
-
const collateral = yield this.getCollateral().getPoolToken();
|
|
192
201
|
price = yield collateral.methods.getTwapPrice112x112().call();
|
|
193
202
|
}
|
|
194
203
|
else {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// return X/Y price
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.getPriceFromReservesSolidlyStable = exports.getPriceFromReservesUniswapV2 = void 0;
|
|
5
|
+
/*
|
|
6
|
+
* X/Y price is given by dividing Y reserves by X reserves
|
|
7
|
+
*/
|
|
8
|
+
function getPriceFromReservesUniswapV2(x, y, priceInverted = true) {
|
|
9
|
+
const price = y / x;
|
|
10
|
+
return !priceInverted ? price : 1 / price;
|
|
11
|
+
}
|
|
12
|
+
exports.getPriceFromReservesUniswapV2 = getPriceFromReservesUniswapV2;
|
|
13
|
+
function getPriceFromReservesSolidlyStable(x, y, priceInverted = false) {
|
|
14
|
+
const N = 3 * x ^ 2 * y + y ^ 3;
|
|
15
|
+
const D = 3 * y ^ 2 * x + x ^ 3;
|
|
16
|
+
const price = N / D;
|
|
17
|
+
return !priceInverted ? price : 1 / price;
|
|
18
|
+
}
|
|
19
|
+
exports.getPriceFromReservesSolidlyStable = getPriceFromReservesSolidlyStable;
|