@suilend/sdk 1.0.6 → 1.0.8
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/README.md +5 -1
- package/core/api/events.d.ts +5 -0
- package/core/api/events.js +22 -0
- package/core/constants.d.ts +3 -2
- package/core/constants.js +6 -2
- package/core/parsers/apiReserveAssetDataEvent.d.ts +58 -0
- package/core/parsers/apiReserveAssetDataEvent.js +66 -0
- package/core/parsers/deps.d.ts +3 -3
- package/core/parsers/obligation.js +3 -3
- package/core/parsers/rateLimiter.js +2 -1
- package/core/parsers/reserve.js +10 -12
- package/core/types.d.ts +190 -0
- package/core/utils/events.d.ts +176 -0
- package/core/utils/events.js +104 -0
- package/core/utils/simulate.d.ts +56 -0
- package/core/utils/simulate.js +317 -0
- package/core/utils/utils.d.ts +61 -0
- package/core/utils/utils.js +157 -0
- package/mainnet/api/events.d.ts +1 -0
- package/mainnet/api/events.js +17 -0
- package/mainnet/parsers/apiReserveAssetDataEvent.d.ts +1 -0
- package/mainnet/parsers/apiReserveAssetDataEvent.js +17 -0
- package/mainnet/utils/events.d.ts +1 -144
- package/mainnet/utils/events.js +14 -112
- package/mainnet/utils/obligation.d.ts +4 -4
- package/mainnet/utils/simulate.d.ts +30 -14
- package/mainnet/utils/simulate.js +53 -259
- package/mainnet/utils/utils.d.ts +1 -79
- package/mainnet/utils/utils.js +15 -155
- package/package.json +1 -1
|
@@ -8,277 +8,71 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.totalSupply = exports.cTokenRatio = exports.decimalToBigNumber = exports.stringToDecimal = exports.numberToDecimal = exports.refreshObligation = exports.refreshReservePrice = exports.updatePoolRewardsManager = exports.compoundReserveInterest = exports.calculateSupplyApr = exports.calculateBorrowApr = exports.calculateUtilizationRate = void 0;
|
|
16
|
-
const
|
|
17
|
-
const utils_1 = require("@mysten/sui.js/utils");
|
|
18
|
-
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
12
|
+
exports.compoundDebt = exports.totalSupply = exports.cTokenRatio = exports.getCTokenMarketValueLowerBound = exports.getCTokenMarketValue = exports.decimalToBigNumber = exports.stringToDecimal = exports.numberToDecimal = exports.refreshObligation = exports.updateUserRewardManager = exports.refreshReservePrice = exports.updatePoolRewardsManager = exports.compoundReserveInterest = exports.calculateDepositAprPercent = exports.calculateSupplyApr = exports.calculateBorrowAprPercent = exports.calculateBorrowApr = exports.calculateUtilizationPercent = exports.calculateUtilizationRate = void 0;
|
|
13
|
+
const simulate_1 = require("../../core/utils/simulate");
|
|
19
14
|
const structs_1 = require("../_generated/suilend/decimal/structs");
|
|
20
15
|
const structs_2 = require("../_generated/suilend/liquidity-mining/structs");
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
16
|
+
const structs_3 = require("../_generated/suilend/obligation/structs");
|
|
17
|
+
const structs_4 = require("../_generated/suilend/reserve/structs");
|
|
18
|
+
const structs_5 = require("../_generated/suilend/reserve-config/structs");
|
|
19
|
+
const deps = {
|
|
20
|
+
Decimal: structs_1.Decimal,
|
|
21
|
+
Obligation: (structs_3.Obligation),
|
|
22
|
+
Deposit: structs_3.Deposit,
|
|
23
|
+
Borrow: structs_3.Borrow,
|
|
24
|
+
Reserve: (structs_4.Reserve),
|
|
25
|
+
ReserveConfig: structs_5.ReserveConfig,
|
|
26
|
+
PoolRewardManager: structs_2.PoolRewardManager,
|
|
27
|
+
PoolReward: structs_2.PoolReward,
|
|
28
|
+
UserRewardManager: structs_2.UserRewardManager,
|
|
29
|
+
UserReward: structs_2.UserReward,
|
|
30
|
+
};
|
|
31
|
+
const simulate = new simulate_1.Simulate(deps);
|
|
32
|
+
/**
|
|
33
|
+
* @deprecated since version 1.0.8. Use `calculateUtilizationPercent` instead.
|
|
34
|
+
*/
|
|
35
|
+
const calculateUtilizationRate = (reserve) => simulate.calculateUtilizationRate(reserve);
|
|
29
36
|
exports.calculateUtilizationRate = calculateUtilizationRate;
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const leftUtil = config.interestRateUtils[i - 1] / 100;
|
|
37
|
-
const rightUtil = config.interestRateUtils[i] / 100;
|
|
38
|
-
if (currentUtil >= leftUtil && currentUtil <= rightUtil) {
|
|
39
|
-
const leftAPR = config.interestRateAprs[i - 1];
|
|
40
|
-
const rightAPR = config.interestRateAprs[i];
|
|
41
|
-
const weight = (currentUtil - leftUtil) / (rightUtil - leftUtil);
|
|
42
|
-
const aprDiff = rightAPR - leftAPR;
|
|
43
|
-
return new bignumber_js_1.default(new bignumber_js_1.default(leftAPR.toString())
|
|
44
|
-
.plus(new bignumber_js_1.default(weight).multipliedBy(new bignumber_js_1.default(aprDiff.toString())))
|
|
45
|
-
.toString()).dividedBy(new bignumber_js_1.default(100));
|
|
46
|
-
}
|
|
47
|
-
i = i + 1;
|
|
48
|
-
}
|
|
49
|
-
// Should never reach here
|
|
50
|
-
return new bignumber_js_1.default(0);
|
|
51
|
-
}
|
|
37
|
+
const calculateUtilizationPercent = (reserve) => simulate.calculateUtilizationPercent(reserve);
|
|
38
|
+
exports.calculateUtilizationPercent = calculateUtilizationPercent;
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated since version 1.0.8. Use `calculateBorrowAprPercent` instead.
|
|
41
|
+
*/
|
|
42
|
+
const calculateBorrowApr = (reserve) => simulate.calculateBorrowApr(reserve);
|
|
52
43
|
exports.calculateBorrowApr = calculateBorrowApr;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
44
|
+
const calculateBorrowAprPercent = (reserve) => simulate.calculateBorrowAprPercent(reserve);
|
|
45
|
+
exports.calculateBorrowAprPercent = calculateBorrowAprPercent;
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated since version 1.0.8. Use `calculateDepositAprPercent` instead.
|
|
48
|
+
*/
|
|
49
|
+
const calculateSupplyApr = (reserve) => simulate.calculateSupplyApr(reserve);
|
|
60
50
|
exports.calculateSupplyApr = calculateSupplyApr;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (timeElapsedSeconds === 0) {
|
|
65
|
-
return reserve;
|
|
66
|
-
}
|
|
67
|
-
bignumber_js_1.default.config({ POW_PRECISION: 100 });
|
|
68
|
-
const compoundedBorrowRate = new bignumber_js_1.default(1)
|
|
69
|
-
.plus(calculateBorrowApr(reserve).dividedBy(new bignumber_js_1.default(100 * 365 * 24 * 60 * 60)))
|
|
70
|
-
.pow(new bignumber_js_1.default(timeElapsedSeconds).toNumber());
|
|
71
|
-
const updatedReserve = Object.assign({}, reserve);
|
|
72
|
-
const oldBorrowedAmount = decimalToBigNumber(reserve.borrowedAmount);
|
|
73
|
-
const oldCumulativeBorrowRate = decimalToBigNumber(reserve.cumulativeBorrowRate);
|
|
74
|
-
const oldUnclaimedSpreadFees = decimalToBigNumber(reserve.unclaimedSpreadFees);
|
|
75
|
-
updatedReserve.cumulativeBorrowRate = stringToDecimal(compoundedBorrowRate.multipliedBy(oldCumulativeBorrowRate).toString());
|
|
76
|
-
const netNewDebt = oldBorrowedAmount.multipliedBy(compoundedBorrowRate.minus(1));
|
|
77
|
-
const spreadFee = new bignumber_js_1.default(((_b = (_a = reserve.config.element) === null || _a === void 0 ? void 0 : _a.spreadFeeBps) === null || _b === void 0 ? void 0 : _b.toString()) || 0);
|
|
78
|
-
updatedReserve.unclaimedSpreadFees = stringToDecimal(oldUnclaimedSpreadFees
|
|
79
|
-
.plus(netNewDebt.multipliedBy(spreadFee.dividedBy(10000)))
|
|
80
|
-
.toString());
|
|
81
|
-
updatedReserve.borrowedAmount = stringToDecimal(oldBorrowedAmount.plus(netNewDebt).toString());
|
|
82
|
-
updatedReserve.interestLastUpdateTimestampS = BigInt(now);
|
|
83
|
-
updatedReserve.depositsPoolRewardManager = updatePoolRewardsManager(updatedReserve.depositsPoolRewardManager, now * 1000);
|
|
84
|
-
updatedReserve.borrowsPoolRewardManager = updatePoolRewardsManager(updatedReserve.borrowsPoolRewardManager, now * 1000);
|
|
85
|
-
return updatedReserve;
|
|
86
|
-
}
|
|
51
|
+
const calculateDepositAprPercent = (reserve) => simulate.calculateDepositAprPercent(reserve);
|
|
52
|
+
exports.calculateDepositAprPercent = calculateDepositAprPercent;
|
|
53
|
+
const compoundReserveInterest = (reserve, now) => simulate.compoundReserveInterest(reserve, now);
|
|
87
54
|
exports.compoundReserveInterest = compoundReserveInterest;
|
|
88
|
-
|
|
89
|
-
const updatedManager = Object.assign({}, manager);
|
|
90
|
-
const timeElapsedMs = nowMs - Number(manager.lastUpdateTimeMs);
|
|
91
|
-
if (manager.totalShares === BigInt(0) || timeElapsedMs === 0) {
|
|
92
|
-
return updatedManager;
|
|
93
|
-
}
|
|
94
|
-
updatedManager.poolRewards = updatedManager.poolRewards.map((poolReward) => {
|
|
95
|
-
if (poolReward === null) {
|
|
96
|
-
return poolReward;
|
|
97
|
-
}
|
|
98
|
-
if (nowMs < poolReward.startTimeMs ||
|
|
99
|
-
manager.lastUpdateTimeMs >= poolReward.endTimeMs) {
|
|
100
|
-
return poolReward;
|
|
101
|
-
}
|
|
102
|
-
const updatedPoolReward = Object.assign({}, poolReward);
|
|
103
|
-
const endTimeMs = Number(poolReward.endTimeMs);
|
|
104
|
-
const startTimeMs = Number(poolReward.startTimeMs);
|
|
105
|
-
const lastUpdateTimeMs = Number(manager.lastUpdateTimeMs);
|
|
106
|
-
const timePassedMs = Math.min(nowMs, endTimeMs) - Math.max(startTimeMs, lastUpdateTimeMs);
|
|
107
|
-
const unlockedRewards = new bignumber_js_1.default(poolReward.totalRewards.toString())
|
|
108
|
-
.multipliedBy(timePassedMs)
|
|
109
|
-
.dividedBy(Number(poolReward.endTimeMs - poolReward.startTimeMs));
|
|
110
|
-
updatedPoolReward.allocatedRewards = stringToDecimal(decimalToBigNumber(poolReward.allocatedRewards)
|
|
111
|
-
.plus(unlockedRewards)
|
|
112
|
-
.toString());
|
|
113
|
-
updatedPoolReward.cumulativeRewardsPerShare = stringToDecimal(decimalToBigNumber(poolReward.cumulativeRewardsPerShare)
|
|
114
|
-
.plus(unlockedRewards.dividedBy(Number(manager.totalShares)))
|
|
115
|
-
.toString());
|
|
116
|
-
return updatedPoolReward;
|
|
117
|
-
});
|
|
118
|
-
updatedManager.lastUpdateTimeMs = BigInt(nowMs);
|
|
119
|
-
return updatedManager;
|
|
120
|
-
}
|
|
55
|
+
const updatePoolRewardsManager = (manager, nowMs) => simulate.updatePoolRewardsManager(manager, nowMs);
|
|
121
56
|
exports.updatePoolRewardsManager = updatePoolRewardsManager;
|
|
122
|
-
function refreshReservePrice(reserves, pythConnection)
|
|
123
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
-
const priceIdentifiers = reserves.map((r) => (0, utils_1.toHEX)(new Uint8Array(r.priceIdentifier.bytes)));
|
|
125
|
-
const priceData = yield pythConnection.getLatestPriceFeeds(priceIdentifiers);
|
|
126
|
-
if (!priceData)
|
|
127
|
-
return reserves;
|
|
128
|
-
const updatedReserves = [];
|
|
129
|
-
for (let i = 0; i < reserves.length; i++) {
|
|
130
|
-
const newReserve = Object.assign({}, reserves[i]);
|
|
131
|
-
newReserve.price = stringToDecimal(priceData[i].getPriceUnchecked().getPriceAsNumberUnchecked().toString());
|
|
132
|
-
newReserve.smoothedPrice = stringToDecimal(priceData[i]
|
|
133
|
-
.getEmaPriceUnchecked()
|
|
134
|
-
.getPriceAsNumberUnchecked()
|
|
135
|
-
.toString());
|
|
136
|
-
newReserve.priceLastUpdateTimestampS = BigInt(priceData[i].getPriceUnchecked().publishTime);
|
|
137
|
-
updatedReserves.push(newReserve);
|
|
138
|
-
}
|
|
139
|
-
return updatedReserves;
|
|
140
|
-
});
|
|
141
|
-
}
|
|
57
|
+
const refreshReservePrice = (reserves, pythConnection) => __awaiter(void 0, void 0, void 0, function* () { return simulate.refreshReservePrice(reserves, pythConnection); });
|
|
142
58
|
exports.refreshReservePrice = refreshReservePrice;
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const poolReward = poolManager.poolRewards[i];
|
|
147
|
-
if (poolReward == null) {
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
if (i >= userRewardManager.rewards.length) {
|
|
151
|
-
userRewardManager.rewards.push(null);
|
|
152
|
-
}
|
|
153
|
-
const oldReward = updatedUserRewardManager.rewards[i];
|
|
154
|
-
const reward = Object.assign({}, oldReward);
|
|
155
|
-
if (oldReward != null) {
|
|
156
|
-
const newRewards = decimalToBigNumber(poolReward.cumulativeRewardsPerShare)
|
|
157
|
-
.minus(decimalToBigNumber(oldReward.cumulativeRewardsPerShare))
|
|
158
|
-
.multipliedBy(new bignumber_js_1.default(Number(userRewardManager.share)));
|
|
159
|
-
reward.earnedRewards = stringToDecimal(decimalToBigNumber(oldReward.earnedRewards).plus(newRewards).toString());
|
|
160
|
-
reward.cumulativeRewardsPerShare = poolReward.cumulativeRewardsPerShare;
|
|
161
|
-
updatedUserRewardManager.rewards[i] = reward;
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
if (userRewardManager.lastUpdateTimeMs <= poolReward.endTimeMs) {
|
|
165
|
-
updatedUserRewardManager.rewards[i] = structs_2.UserReward.fromFields({
|
|
166
|
-
pool_reward_id: { bytes: poolReward.id.slice(2) },
|
|
167
|
-
earned_rewards: stringToDecimal((userRewardManager.lastUpdateTimeMs <= poolReward.startTimeMs
|
|
168
|
-
? decimalToBigNumber(poolReward.cumulativeRewardsPerShare).multipliedBy(new bignumber_js_1.default(Number(userRewardManager.share)))
|
|
169
|
-
: new bignumber_js_1.default(0)).toString()),
|
|
170
|
-
cumulative_rewards_per_share: poolReward.cumulativeRewardsPerShare,
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
updatedUserRewardManager.lastUpdateTimeMs = BigInt(now);
|
|
176
|
-
return updatedUserRewardManager;
|
|
177
|
-
}
|
|
178
|
-
function refreshObligation(unrefreshedObligation, refreshedReserves) {
|
|
179
|
-
const obligation = Object.assign({}, unrefreshedObligation);
|
|
180
|
-
// Refresh Deposits
|
|
181
|
-
let depositValueUsd = new bignumber_js_1.default(0);
|
|
182
|
-
let allowedBorrowValueUsd = new bignumber_js_1.default(0);
|
|
183
|
-
let unhealthyBorrowValueUsd = new bignumber_js_1.default(0);
|
|
184
|
-
for (let i = 0; i < obligation.deposits.length; i++) {
|
|
185
|
-
const deposit = Object.assign({}, obligation.deposits[i]);
|
|
186
|
-
const reserve = refreshedReserves.find((r) => r.coinType.name === deposit.coinType.name);
|
|
187
|
-
if (!reserve) {
|
|
188
|
-
throw new Error(`Unable to find reserve for coin type ${deposit.coinType.name}`);
|
|
189
|
-
}
|
|
190
|
-
const config = reserve.config.element;
|
|
191
|
-
obligation.userRewardManagers[deposit.userRewardManagerIndex] = updateUserRewardManager(reserve.depositsPoolRewardManager, obligation.userRewardManagers[deposit.userRewardManagerIndex], Date.now());
|
|
192
|
-
const marketValue = getCTokenMarketValue(reserve, new bignumber_js_1.default(deposit.depositedCtokenAmount.toString()));
|
|
193
|
-
const marketValueLowerBound = getCTokenMarketValueLowerBound(reserve, new bignumber_js_1.default(deposit.depositedCtokenAmount.toString()));
|
|
194
|
-
deposit.marketValue = stringToDecimal(marketValue.toString());
|
|
195
|
-
depositValueUsd = depositValueUsd.plus(new bignumber_js_1.default(marketValue.toString()));
|
|
196
|
-
allowedBorrowValueUsd = allowedBorrowValueUsd.plus(marketValueLowerBound.multipliedBy(new bignumber_js_1.default(config.openLtvPct / 100)));
|
|
197
|
-
unhealthyBorrowValueUsd = unhealthyBorrowValueUsd.plus(marketValue.multipliedBy(new bignumber_js_1.default(config.closeLtvPct / 100)));
|
|
198
|
-
obligation.deposits[i] = deposit;
|
|
199
|
-
}
|
|
200
|
-
obligation.unhealthyBorrowValueUsd = stringToDecimal(unhealthyBorrowValueUsd.toString());
|
|
201
|
-
obligation.allowedBorrowValueUsd = stringToDecimal(allowedBorrowValueUsd.toString());
|
|
202
|
-
obligation.depositedValueUsd = stringToDecimal(depositValueUsd.toString());
|
|
203
|
-
// Refresh borrows
|
|
204
|
-
let unweightedBorrowedValueUsd = new bignumber_js_1.default(0);
|
|
205
|
-
let weightedBorrowedValueUsd = new bignumber_js_1.default(0);
|
|
206
|
-
let weightedBorrowedValueUpperBoundUsd = new bignumber_js_1.default(0);
|
|
207
|
-
for (let i = 0; i < obligation.borrows.length; i++) {
|
|
208
|
-
const unrefreshedBorrow = obligation.borrows[i];
|
|
209
|
-
const reserve = refreshedReserves.find((r) => r.coinType.name == unrefreshedBorrow.coinType.name);
|
|
210
|
-
if (!reserve) {
|
|
211
|
-
throw new Error(`Unable to find reserve for coin type ${unrefreshedBorrow.coinType.name}`);
|
|
212
|
-
}
|
|
213
|
-
const config = reserve.config.element;
|
|
214
|
-
const borrow = Object.assign({}, compoundDebt(unrefreshedBorrow, reserve));
|
|
215
|
-
obligation.userRewardManagers[borrow.userRewardManagerIndex] = updateUserRewardManager(reserve.borrowsPoolRewardManager, obligation.userRewardManagers[borrow.userRewardManagerIndex], Date.now());
|
|
216
|
-
const marketValue = decimalToBigNumber(borrow.borrowedAmount)
|
|
217
|
-
.multipliedBy(decimalToBigNumber(reserve.price))
|
|
218
|
-
.dividedBy(new bignumber_js_1.default(10 ** reserve.mintDecimals));
|
|
219
|
-
const upperBoundPrice = bignumber_js_1.default.max(decimalToBigNumber(reserve.price), decimalToBigNumber(reserve.smoothedPrice));
|
|
220
|
-
const marketValueUpperBound = decimalToBigNumber(reserve.borrowedAmount)
|
|
221
|
-
.multipliedBy(upperBoundPrice)
|
|
222
|
-
.dividedBy(new bignumber_js_1.default(10 ** reserve.mintDecimals));
|
|
223
|
-
borrow.marketValue = stringToDecimal(marketValue.toString());
|
|
224
|
-
unweightedBorrowedValueUsd = unweightedBorrowedValueUsd.plus(marketValue);
|
|
225
|
-
const borrowWeight = new bignumber_js_1.default((config.borrowWeightBps / BigInt(10000)).toString());
|
|
226
|
-
weightedBorrowedValueUsd = weightedBorrowedValueUsd.plus(marketValue.multipliedBy(borrowWeight));
|
|
227
|
-
weightedBorrowedValueUpperBoundUsd =
|
|
228
|
-
weightedBorrowedValueUpperBoundUsd.plus(marketValueUpperBound.multipliedBy(borrowWeight));
|
|
229
|
-
}
|
|
230
|
-
obligation.unweightedBorrowedValueUsd = stringToDecimal(unweightedBorrowedValueUsd.toString());
|
|
231
|
-
obligation.weightedBorrowedValueUpperBoundUsd = stringToDecimal(weightedBorrowedValueUpperBoundUsd.toString());
|
|
232
|
-
obligation.weightedBorrowedValueUsd = stringToDecimal(weightedBorrowedValueUsd.toString());
|
|
233
|
-
return obligation;
|
|
234
|
-
}
|
|
59
|
+
const updateUserRewardManager = (poolManager, userRewardManager, now) => simulate.updateUserRewardManager(poolManager, userRewardManager, now);
|
|
60
|
+
exports.updateUserRewardManager = updateUserRewardManager;
|
|
61
|
+
const refreshObligation = (unrefreshedObligation, refreshedReserves) => simulate.refreshObligation(unrefreshedObligation, refreshedReserves);
|
|
235
62
|
exports.refreshObligation = refreshObligation;
|
|
236
|
-
|
|
237
|
-
const adjustedValue = Math.round(value * 10 ** 18);
|
|
238
|
-
return structs_1.Decimal.fromBcs(bcs_1.bcs.u256().serialize(adjustedValue).toBytes());
|
|
239
|
-
}
|
|
63
|
+
const numberToDecimal = (value) => simulate.numberToDecimal(value);
|
|
240
64
|
exports.numberToDecimal = numberToDecimal;
|
|
241
|
-
|
|
242
|
-
return numberToDecimal(new bignumber_js_1.default(value).toNumber());
|
|
243
|
-
}
|
|
65
|
+
const stringToDecimal = (value) => simulate.stringToDecimal(value);
|
|
244
66
|
exports.stringToDecimal = stringToDecimal;
|
|
245
|
-
|
|
246
|
-
return new bignumber_js_1.default(value.value.toString()).dividedBy(new bignumber_js_1.default(10 ** 18));
|
|
247
|
-
}
|
|
67
|
+
const decimalToBigNumber = (value) => simulate.decimalToBigNumber(value);
|
|
248
68
|
exports.decimalToBigNumber = decimalToBigNumber;
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
255
|
-
function getCTokenMarketValueLowerBound(reserve, depositedCTokenAmount) {
|
|
256
|
-
const liquidityAmount = depositedCTokenAmount.multipliedBy(cTokenRatio(reserve));
|
|
257
|
-
const price = bignumber_js_1.default.min(decimalToBigNumber(reserve.price), decimalToBigNumber(reserve.smoothedPrice));
|
|
258
|
-
return price
|
|
259
|
-
.multipliedBy(liquidityAmount)
|
|
260
|
-
.dividedBy(new bignumber_js_1.default(10 ** reserve.mintDecimals));
|
|
261
|
-
}
|
|
262
|
-
function cTokenRatio(reserve) {
|
|
263
|
-
if (reserve.ctokenSupply === BigInt(0)) {
|
|
264
|
-
return new bignumber_js_1.default(1);
|
|
265
|
-
}
|
|
266
|
-
return totalSupply(reserve).dividedBy(new bignumber_js_1.default(reserve.ctokenSupply.toString()));
|
|
267
|
-
}
|
|
69
|
+
const getCTokenMarketValue = (reserve, depositedCTokenAmount) => simulate.getCTokenMarketValue(reserve, depositedCTokenAmount);
|
|
70
|
+
exports.getCTokenMarketValue = getCTokenMarketValue;
|
|
71
|
+
const getCTokenMarketValueLowerBound = (reserve, depositedCTokenAmount) => simulate.getCTokenMarketValueLowerBound(reserve, depositedCTokenAmount);
|
|
72
|
+
exports.getCTokenMarketValueLowerBound = getCTokenMarketValueLowerBound;
|
|
73
|
+
const cTokenRatio = (reserve) => simulate.cTokenRatio(reserve);
|
|
268
74
|
exports.cTokenRatio = cTokenRatio;
|
|
269
|
-
|
|
270
|
-
return new bignumber_js_1.default(reserve.availableAmount.toString())
|
|
271
|
-
.plus(decimalToBigNumber(reserve.borrowedAmount))
|
|
272
|
-
.minus(decimalToBigNumber(reserve.unclaimedSpreadFees));
|
|
273
|
-
}
|
|
75
|
+
const totalSupply = (reserve) => simulate.totalSupply(reserve);
|
|
274
76
|
exports.totalSupply = totalSupply;
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
const newCumulativeBorrowRate = decimalToBigNumber(reserve.cumulativeBorrowRate);
|
|
278
|
-
const compoundedInterestRate = newCumulativeBorrowRate.dividedBy(decimalToBigNumber(borrow.cumulativeBorrowRate));
|
|
279
|
-
borrowCopy.borrowedAmount = stringToDecimal(decimalToBigNumber(borrow.borrowedAmount)
|
|
280
|
-
.multipliedBy(compoundedInterestRate)
|
|
281
|
-
.toString());
|
|
282
|
-
borrowCopy.cumulativeBorrowRate = stringToDecimal(newCumulativeBorrowRate.toString());
|
|
283
|
-
return borrowCopy;
|
|
284
|
-
}
|
|
77
|
+
const compoundDebt = (borrow, reserve) => simulate.compoundDebt(borrow, reserve);
|
|
78
|
+
exports.compoundDebt = compoundDebt;
|
package/mainnet/utils/utils.d.ts
CHANGED
|
@@ -1,79 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { SharedObjectRef } from "@mysten/sui.js/bcs";
|
|
3
|
-
import { SuiObjectRef } from "@mysten/sui.js/client";
|
|
4
|
-
import { TransactionArgument, TransactionBlock } from "@mysten/sui.js/transactions";
|
|
5
|
-
export type TransactionObjectArgument = Exclude<TransactionArgument, {
|
|
6
|
-
kind: "Input";
|
|
7
|
-
type: "pure";
|
|
8
|
-
}>;
|
|
9
|
-
/**
|
|
10
|
-
* These are the basics types that can be used in SUI
|
|
11
|
-
*/
|
|
12
|
-
export type SuiBasicTypes = "address" | "bool" | "u8" | "u16" | "u32" | "u64" | "u128" | "u256" | "signer";
|
|
13
|
-
export type SuiAddressArg = TransactionArgument | SerializedBcs<any> | string;
|
|
14
|
-
export type SuiTxArg = SuiAddressArg | number | bigint | boolean;
|
|
15
|
-
export type SuiInputTypes = "object" | SuiBasicTypes;
|
|
16
|
-
export type SuiObjectArg = TransactionObjectArgument | string | SharedObjectRef | SuiObjectRef;
|
|
17
|
-
export type SuiVecTxArg = {
|
|
18
|
-
value: SuiTxArg[];
|
|
19
|
-
vecType: SuiInputTypes;
|
|
20
|
-
} | SuiTxArg[];
|
|
21
|
-
export declare function takeAmountFromCoins(txBlock: TransactionBlock, coins: SuiObjectArg[], amount: SuiTxArg): TransactionObjectArgument[];
|
|
22
|
-
export declare function splitSUIFromGas(txBlock: TransactionBlock, amounts: SuiTxArg[]): import("@mysten/sui.js/transactions").TransactionResult;
|
|
23
|
-
/**
|
|
24
|
-
* Convert any valid input into array of TransactionArgument.
|
|
25
|
-
*
|
|
26
|
-
* @param txb The Transaction Block
|
|
27
|
-
* @param args The array of argument to convert.
|
|
28
|
-
* @returns The converted array of TransactionArgument.
|
|
29
|
-
*/
|
|
30
|
-
export declare function convertArgs(txBlock: TransactionBlock, args: (SuiTxArg | SuiVecTxArg)[]): ({
|
|
31
|
-
index: number;
|
|
32
|
-
kind: "Input";
|
|
33
|
-
value?: any;
|
|
34
|
-
type?: "object" | undefined;
|
|
35
|
-
} | {
|
|
36
|
-
index: number;
|
|
37
|
-
kind: "Input";
|
|
38
|
-
type: "pure";
|
|
39
|
-
value?: any;
|
|
40
|
-
} | {
|
|
41
|
-
kind: "GasCoin";
|
|
42
|
-
} | {
|
|
43
|
-
index: number;
|
|
44
|
-
kind: "Result";
|
|
45
|
-
} | {
|
|
46
|
-
index: number;
|
|
47
|
-
resultIndex: number;
|
|
48
|
-
kind: "NestedResult";
|
|
49
|
-
} | SerializedBcs<unknown, unknown> | (SerializedBcs<any, any> & import("@mysten/sui.js/bcs").PureArg))[];
|
|
50
|
-
/**
|
|
51
|
-
* Convert any valid object input into a TransactionArgument.
|
|
52
|
-
*
|
|
53
|
-
* @param txb The Transaction Block
|
|
54
|
-
* @param arg The object argument to convert.
|
|
55
|
-
* @returns The converted TransactionArgument.
|
|
56
|
-
*/
|
|
57
|
-
export declare function convertObjArg(txb: TransactionBlock, arg: SuiObjectArg): TransactionObjectArgument;
|
|
58
|
-
/**
|
|
59
|
-
* Check whether it is an valid move vec input.
|
|
60
|
-
*
|
|
61
|
-
* @param arg The argument to check.
|
|
62
|
-
* @returns boolean.
|
|
63
|
-
*/
|
|
64
|
-
export declare function isMoveVecArg(arg: SuiTxArg | SuiVecTxArg): arg is SuiVecTxArg;
|
|
65
|
-
export declare const getDefaultSuiInputType: (value: SuiTxArg) => SuiInputTypes | undefined;
|
|
66
|
-
/**
|
|
67
|
-
* Since we know the elements in the array are the same type
|
|
68
|
-
* If type is not provided, we will try to infer the type from the first element
|
|
69
|
-
* By default,
|
|
70
|
-
*
|
|
71
|
-
* string is hex and its length equal to 32 =====> object id
|
|
72
|
-
* number, bigint ====> u64
|
|
73
|
-
* boolean =====> bool
|
|
74
|
-
*
|
|
75
|
-
* If type is provided, we will use the type to convert the array
|
|
76
|
-
* @param args
|
|
77
|
-
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'signer' | 'object' | string
|
|
78
|
-
*/
|
|
79
|
-
export declare function makeVecParam(txBlock: TransactionBlock, args: SuiTxArg[], type?: SuiInputTypes): TransactionArgument;
|
|
1
|
+
export * from "../../core/utils/utils";
|
package/mainnet/utils/utils.js
CHANGED
|
@@ -1,157 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return [sendCoin, mergedCoin];
|
|
16
|
-
}
|
|
17
|
-
exports.takeAmountFromCoins = takeAmountFromCoins;
|
|
18
|
-
function splitSUIFromGas(txBlock, amounts) {
|
|
19
|
-
return txBlock.splitCoins(txBlock.gas, convertArgs(txBlock, amounts));
|
|
20
|
-
}
|
|
21
|
-
exports.splitSUIFromGas = splitSUIFromGas;
|
|
22
|
-
/**
|
|
23
|
-
* Convert any valid input into array of TransactionArgument.
|
|
24
|
-
*
|
|
25
|
-
* @param txb The Transaction Block
|
|
26
|
-
* @param args The array of argument to convert.
|
|
27
|
-
* @returns The converted array of TransactionArgument.
|
|
28
|
-
*/
|
|
29
|
-
function convertArgs(txBlock, args) {
|
|
30
|
-
return args.map((arg) => {
|
|
31
|
-
if (typeof arg === "string" && (0, utils_1.isValidSuiObjectId)(arg)) {
|
|
32
|
-
return txBlock.object((0, utils_1.normalizeSuiObjectId)(arg));
|
|
33
|
-
}
|
|
34
|
-
else if (typeof arg == "object" &&
|
|
35
|
-
!(0, bcs_1.isSerializedBcs)(arg) &&
|
|
36
|
-
!(0, bcs_2.isPureArg)(arg) &&
|
|
37
|
-
!isMoveVecArg(arg)) {
|
|
38
|
-
return convertObjArg(txBlock, arg);
|
|
39
|
-
}
|
|
40
|
-
else if (isMoveVecArg(arg)) {
|
|
41
|
-
const vecType = "vecType" in arg;
|
|
42
|
-
return vecType
|
|
43
|
-
? makeVecParam(txBlock, arg.value, arg.vecType)
|
|
44
|
-
: makeVecParam(txBlock, arg);
|
|
45
|
-
}
|
|
46
|
-
else if ((0, bcs_1.isSerializedBcs)(arg)) {
|
|
47
|
-
return arg;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
return txBlock.pure(arg);
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
exports.convertArgs = convertArgs;
|
|
55
|
-
/**
|
|
56
|
-
* Convert any valid object input into a TransactionArgument.
|
|
57
|
-
*
|
|
58
|
-
* @param txb The Transaction Block
|
|
59
|
-
* @param arg The object argument to convert.
|
|
60
|
-
* @returns The converted TransactionArgument.
|
|
61
|
-
*/
|
|
62
|
-
function convertObjArg(txb, arg) {
|
|
63
|
-
if (typeof arg === "string") {
|
|
64
|
-
return txb.object(arg);
|
|
65
|
-
}
|
|
66
|
-
if ("digest" in arg && "version" in arg && "objectId" in arg) {
|
|
67
|
-
return txb.objectRef(arg);
|
|
68
|
-
}
|
|
69
|
-
if ("objectId" in arg && "initialSharedVersion" in arg && "mutable" in arg) {
|
|
70
|
-
return txb.sharedObjectRef(arg);
|
|
71
|
-
}
|
|
72
|
-
if ("Object" in arg) {
|
|
73
|
-
if ("ImmOrOwned" in arg.Object) {
|
|
74
|
-
return txb.object(transactions_1.Inputs.ObjectRef(arg.Object.ImmOrOwned));
|
|
75
|
-
}
|
|
76
|
-
else if ("Shared" in arg.Object) {
|
|
77
|
-
return txb.object(transactions_1.Inputs.SharedObjectRef(arg.Object.Shared));
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
throw new Error("Invalid argument type");
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if ("kind" in arg) {
|
|
84
|
-
return arg;
|
|
85
|
-
}
|
|
86
|
-
throw new Error("Invalid argument type");
|
|
87
|
-
}
|
|
88
|
-
exports.convertObjArg = convertObjArg;
|
|
89
|
-
/**
|
|
90
|
-
* Check whether it is an valid move vec input.
|
|
91
|
-
*
|
|
92
|
-
* @param arg The argument to check.
|
|
93
|
-
* @returns boolean.
|
|
94
|
-
*/
|
|
95
|
-
function isMoveVecArg(arg) {
|
|
96
|
-
if (typeof arg === "object" && "vecType" in arg && "value" in arg) {
|
|
97
|
-
return true;
|
|
98
|
-
}
|
|
99
|
-
else if (Array.isArray(arg)) {
|
|
100
|
-
return true;
|
|
101
|
-
}
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
exports.isMoveVecArg = isMoveVecArg;
|
|
105
|
-
const getDefaultSuiInputType = (value) => {
|
|
106
|
-
if (typeof value === "string" && (0, utils_1.isValidSuiObjectId)(value)) {
|
|
107
|
-
return "object";
|
|
108
|
-
}
|
|
109
|
-
else if (typeof value === "number" || typeof value === "bigint") {
|
|
110
|
-
return "u64";
|
|
111
|
-
}
|
|
112
|
-
else if (typeof value === "boolean") {
|
|
113
|
-
return "bool";
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
return undefined;
|
|
117
|
-
}
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
118
15
|
};
|
|
119
|
-
exports
|
|
120
|
-
|
|
121
|
-
* Since we know the elements in the array are the same type
|
|
122
|
-
* If type is not provided, we will try to infer the type from the first element
|
|
123
|
-
* By default,
|
|
124
|
-
*
|
|
125
|
-
* string is hex and its length equal to 32 =====> object id
|
|
126
|
-
* number, bigint ====> u64
|
|
127
|
-
* boolean =====> bool
|
|
128
|
-
*
|
|
129
|
-
* If type is provided, we will use the type to convert the array
|
|
130
|
-
* @param args
|
|
131
|
-
* @param type 'address' | 'bool' | 'u8' | 'u16' | 'u32' | 'u64' | 'u128' | 'u256' | 'signer' | 'object' | string
|
|
132
|
-
*/
|
|
133
|
-
function makeVecParam(txBlock, args, type) {
|
|
134
|
-
if (args.length === 0)
|
|
135
|
-
throw new Error("Transaction builder error: Empty array is not allowed");
|
|
136
|
-
// Using first element value as default type
|
|
137
|
-
const defaultSuiType = (0, exports.getDefaultSuiInputType)(args[0]);
|
|
138
|
-
const VECTOR_REGEX = /^vector<(.+)>$/;
|
|
139
|
-
const STRUCT_REGEX = /^([^:]+)::([^:]+)::([^<]+)(<(.+)>)?/;
|
|
140
|
-
type = type || defaultSuiType;
|
|
141
|
-
if (type === "object") {
|
|
142
|
-
const objects = args.map((arg) => typeof arg === "string" && (0, utils_1.isValidSuiObjectId)(arg)
|
|
143
|
-
? txBlock.object((0, utils_1.normalizeSuiObjectId)(arg))
|
|
144
|
-
: convertObjArg(txBlock, arg));
|
|
145
|
-
return txBlock.makeMoveVec({ objects });
|
|
146
|
-
}
|
|
147
|
-
else if (typeof type === "string" &&
|
|
148
|
-
!VECTOR_REGEX.test(type) &&
|
|
149
|
-
!STRUCT_REGEX.test(type)) {
|
|
150
|
-
return txBlock.pure(args, `vector<${type}>`);
|
|
151
|
-
}
|
|
152
|
-
else {
|
|
153
|
-
const objects = args.map((arg) => convertObjArg(txBlock, arg));
|
|
154
|
-
return txBlock.makeMoveVec({ objects, type });
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
exports.makeVecParam = makeVecParam;
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("../../core/utils/utils"), exports);
|