@augustdigital/pools
Version:
JS SDK for web3 interactions with the August Digital Lending Pools
268 lines • 12.5 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AugustPools = void 0;
const utils_1 = require("@augustdigital/utils");
const getters_1 = require("./getters");
const ethers_1 = require("ethers");
const user_actions_1 = require("./user-actions");
const staking_1 = require("./staking");
class AugustPools extends utils_1.AugustBase {
constructor(baseConfig) {
super(baseConfig);
}
getPools(options, initialData) {
return __awaiter(this, void 0, void 0, function* () {
const pools = yield Promise.all(Object.values(this.providers).map((rpcUrl) => {
return (0, getters_1.getLendingPools)({
rpcUrl,
optionalFields: {
loans: (options === null || options === void 0 ? void 0 : options.loans) || false,
loanAllocations: (options === null || options === void 0 ? void 0 : options.allocations) || false,
},
env: this.env,
apiKey: this.augustKey,
pools: initialData === null || initialData === void 0 ? void 0 : initialData.map((p) => {
var _a;
return (Object.assign(Object.assign({}, p), { rpcUrl: (_a = this.providers) === null || _a === void 0 ? void 0 : _a[p.chainId] }));
}),
});
}));
return pools.flat();
});
}
getPool(pool, chainId, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (!this.authorized)
throw new Error('Not authorized.');
if (!pool)
throw new Error('Pool input parameter is undefined.');
if (!(0, ethers_1.isAddress)(pool))
throw new Error(`Pool input parameter is not an address: ${String(pool)}`);
if (typeof chainId !== 'undefined') {
if (!((_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]))
console.error('#getPool:', 'missing rpcUrl in initialization for chainId', chainId);
}
return yield (0, getters_1.getLendingPool)({
pool,
rpcUrl: chainId ? (_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId] : this.activeNetwork.rpcUrl,
optionalFields: {
loans: (options === null || options === void 0 ? void 0 : options.loans) || true,
loanAllocations: (options === null || options === void 0 ? void 0 : options.allocations) || true,
},
env: this.env,
apiKey: this.augustKey,
});
});
}
getPoolApr(pool, chainId, optionalParams) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!this.authorized)
throw new Error('Not authorized.');
if (!pool)
throw new Error('Pool input parameter is undefined.');
if (!(0, ethers_1.isAddress)(pool))
throw new Error(`Pool input parameter is not an address: ${String(pool)}`);
const rpcUrl = chainId
? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]
: this.activeNetwork.rpcUrl;
return yield (0, getters_1.getLendingPoolApr)({
pool,
rpcUrl: rpcUrl,
inputParams: optionalParams,
apiKey: this.augustKey,
});
});
}
getPoolTvl(pool, chainId, optionalParams) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!this.authorized)
throw new Error('Not authorized.');
if (!pool)
throw new Error('Pool input parameter is undefined.');
if (!(0, ethers_1.isAddress)(pool))
throw new Error(`Pool input parameter is not an address: ${String(pool)}`);
const rpcUrl = chainId
? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]
: this.activeNetwork.rpcUrl;
return yield (0, getters_1.getLendingPoolTvl)({
pool,
rpcUrl: rpcUrl,
inputParams: optionalParams,
});
});
}
getAvailableRedemptions(pool, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!this.authorized)
throw new Error('Not authorized.');
if (!pool)
throw new Error('Pool input parameter is undefined.');
if (!(0, ethers_1.isAddress)(pool))
throw new Error(`Pool input parameter is not an address: ${String(pool)}`);
return yield (0, getters_1.getLendingPoolAvailableRedemptions)({
pool,
rpcUrl: (options === null || options === void 0 ? void 0 : options.chainId)
? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[options === null || options === void 0 ? void 0 : options.chainId]
: this.activeNetwork.rpcUrl,
address: options === null || options === void 0 ? void 0 : options.address,
apiKey: this.subgraphKey,
});
});
}
getAllPositions(address, chainId, pools) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (!this.authorized)
throw new Error('Not authorized.');
if (!(0, ethers_1.isAddress)(address))
throw new Error(`Address parameter is not an address: ${String(address)}`);
if (typeof pools !== 'undefined' && (pools === null || pools === void 0 ? void 0 : pools.length)) {
return yield (0, getters_1.getAllLendingPoolPositions)({
rpcUrl: chainId ? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId] : this.activeNetwork.rpcUrl,
address,
env: this.env,
pools: pools.map((p) => (Object.assign(Object.assign({}, p), { rpcUrl: this.providers[p.chainId] }))),
apiKey: this.subgraphKey,
});
}
if (chainId) {
return yield (0, getters_1.getAllLendingPoolPositions)({
rpcUrl: chainId ? (_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId] : this.activeNetwork.rpcUrl,
address,
env: this.env,
apiKey: this.subgraphKey,
});
}
const positions = yield Promise.all(Object.values(this.providers).map((rpcUrl) => {
return (0, getters_1.getAllLendingPoolPositions)({
rpcUrl,
address,
env: this.env,
apiKey: this.subgraphKey,
});
}));
return positions.flat();
});
}
getPoolLoans(pool, chainId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
return (0, getters_1.getLendingPoolLoans)({
pool,
rpcUrl: chainId ? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId] : this.activeNetwork.rpcUrl,
env: this.env,
optionalFields: { loanAllocations: true, loans: true },
apiKey: this.augustKey,
});
});
}
getManagementFee(pool, chainId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
return yield (0, utils_1.getManagementFeePercent)((0, utils_1.createProvider)(chainId ? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId] : this.activeNetwork.rpcUrl), pool);
});
}
getStakingPositions(wallet, chainId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (chainId) {
const positions = yield (0, staking_1.getRewardsStakingPositions)({
rpcUrl: ((_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]) || this.activeNetwork.rpcUrl,
env: this.env,
wallet,
});
return positions;
}
const positions = yield Promise.all(Object.values(this.providers).map((rpcUrl) => {
var _a;
return (0, staking_1.getRewardsStakingPositions)({
rpcUrl: chainId
? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]
: this.activeNetwork.rpcUrl,
env: this.env,
wallet,
});
}));
return positions.flat();
});
}
getPoolsBorrowerHealthFactor(chainId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (chainId) {
const health_factors = yield (0, getters_1.getHealthFactorOfBorrowersByPool)({
rpcUrl: ((_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]) || this.activeNetwork.rpcUrl,
env: this.env,
apiKey: this.augustKey,
});
return health_factors;
}
const health_factors = yield Promise.all(Object.values(this.providers).map((rpcUrl) => {
var _a;
return (0, getters_1.getHealthFactorOfBorrowersByPool)({
rpcUrl: chainId
? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]
: this.activeNetwork.rpcUrl,
env: this.env,
apiKey: this.augustKey,
});
}));
return health_factors.flat();
});
}
allowance(pool, signer) {
return __awaiter(this, void 0, void 0, function* () {
const wallet = (yield signer.getAddress());
return yield (0, user_actions_1.allowance)(signer, { pool, wallet, wait: true });
});
}
approve(pool, signer, inputParams) {
return __awaiter(this, void 0, void 0, function* () {
const wallet = (yield signer.getAddress());
return yield (0, user_actions_1.approve)(signer, {
pool,
wallet,
wait: true,
amount: inputParams.amount,
});
});
}
deposit(pool, signer, inputParams) {
return __awaiter(this, void 0, void 0, function* () {
const wallet = (yield signer.getAddress());
return yield (0, user_actions_1.deposit)(signer, {
amount: inputParams.amount,
wallet,
pool: pool,
wait: true,
});
});
}
requestWithdrawal(pool, signer) {
return __awaiter(this, void 0, void 0, function* () {
const wallet = (yield signer.getAddress());
return yield (0, user_actions_1.requestRedeem)(signer, { pool, wallet, wait: true });
});
}
withdraw(pool, signer, inputParams) {
return __awaiter(this, void 0, void 0, function* () {
const wallet = (yield signer.getAddress());
return yield (0, user_actions_1.redeem)(signer, Object.assign(Object.assign({ wallet, pool }, inputParams), { wait: true }));
});
}
}
exports.AugustPools = AugustPools;
//# sourceMappingURL=main.js.map