@augustdigital/vaults
Version:
JS SDK for web3 interactions with the August Digital Lending Pools
411 lines • 23.4 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.AugustVaults = void 0;
const utils_1 = require("@augustdigital/utils");
const ethers_1 = require("ethers");
const getters_1 = require("./getters");
const services_1 = require("@augustdigital/services");
class AugustVaults extends utils_1.AugustBase {
constructor(baseConfig) {
super(baseConfig);
}
getVaults(options) {
return __awaiter(this, void 0, void 0, function* () {
const allVaults = yield (0, utils_1.fetchTokenizedVaults)();
const vaultsPerAvailableProviders = allVaults.filter((v) => { var _a; return !!((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v === null || v === void 0 ? void 0 : v.chain]) ? true : false; });
const vaultsPerChainId = (options === null || options === void 0 ? void 0 : options.chainIds)
? vaultsPerAvailableProviders.filter((v) => options.chainIds.includes(v.chain))
: vaultsPerAvailableProviders;
const vaultResponses = yield Promise.all(vaultsPerChainId.map((v) => {
var _a;
return (0, getters_1.getVault)({
vault: v.address,
loans: typeof (options === null || options === void 0 ? void 0 : options.loans) !== 'undefined' ? options === null || options === void 0 ? void 0 : options.loans : true,
allocations: typeof (options === null || options === void 0 ? void 0 : options.allocations) !== 'undefined'
? options === null || options === void 0 ? void 0 : options.allocations
: true,
options: {
rpcUrl: (_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain],
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
}));
const filteredResponses = vaultResponses.filter((r) => r !== null && r.status === 'active');
if (options.wallet && (0, ethers_1.isAddress)(options.wallet)) {
const vaultResponses = yield Promise.all(vaultsPerAvailableProviders.map((v) => {
var _a;
return (0, getters_1.getVaultPositions)({
vault: v.address,
wallet: options.wallet,
options: {
rpcUrl: (_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain],
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
}));
const positions = vaultResponses
.flat()
.filter((v) => v.status !== 'PENDING');
return filteredResponses.map((r) => (Object.assign(Object.assign({}, r), { position: (positions === null || positions === void 0 ? void 0 : positions.find((pos) => { var _a, _b; return ((_a = pos.vault) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === ((_b = r.address) === null || _b === void 0 ? void 0 : _b.toLowerCase()); })) || null })));
}
if (!!options.wallet && !(0, ethers_1.isAddress)(options.wallet))
console.warn('#getVaults: wallet passed is not of type address');
return filteredResponses;
});
}
getVault(_a) {
return __awaiter(this, arguments, void 0, function* ({ vault, chainId, options, }) {
var _b, _c, _d;
if (!vault)
throw new Error('Vault input parameter is undefined.');
if (!(0, ethers_1.isAddress)(vault))
throw new Error(`Vault input parameter is not an address: ${String(vault)}`);
if (typeof chainId !== 'undefined') {
if (!((_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId]))
console.error('#getVault:', 'missing rpcUrl in initialization for chainId', chainId);
}
const vaultResponse = yield (0, getters_1.getVault)({
vault: vault,
loans: typeof (options === null || options === void 0 ? void 0 : options.loans) !== 'undefined' ? options === null || options === void 0 ? void 0 : options.loans : true,
allocations: typeof (options === null || options === void 0 ? void 0 : options.allocations) !== 'undefined'
? options === null || options === void 0 ? void 0 : options.allocations
: true,
options: {
rpcUrl: chainId ? (_c = this.providers) === null || _c === void 0 ? void 0 : _c[chainId] : this.activeNetwork.rpcUrl,
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
if (options.wallet && (0, ethers_1.isAddress)(options.wallet)) {
const vaultResponses = yield (0, getters_1.getVaultPositions)({
vault: vault,
wallet: options.wallet,
options: {
rpcUrl: chainId
? (_d = this.providers) === null || _d === void 0 ? void 0 : _d[chainId]
: this.activeNetwork.rpcUrl,
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
const positions = vaultResponses.filter((v) => v.status !== 'PENDING');
return Object.assign(Object.assign({}, vaultResponse), { position: (positions === null || positions === void 0 ? void 0 : positions.find((pos) => { var _a; return ((_a = pos.vault) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === (vault === null || vault === void 0 ? void 0 : vault.toLowerCase()); })) || null });
}
if (!!options.wallet && !(0, ethers_1.isAddress)(options.wallet))
console.warn('#getVaults: wallet passed is not of type address');
return vaultResponse;
});
}
getVaultLoans(_a) {
return __awaiter(this, arguments, void 0, function* ({ vault, chainId, }) {
var _b, _c;
if (!vault)
throw new Error('Vault input parameter is undefined.');
if (!(0, ethers_1.isAddress)(vault))
throw new Error(`Vault input parameter is not an address: ${String(vault)}`);
if (typeof chainId !== 'undefined') {
if (!((_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId]))
console.error('#getVaultLoans:', 'missing rpcUrl in initialization for chainId', chainId);
}
const vaultResponse = yield (0, getters_1.getVaultLoans)(vault, {
rpcUrl: chainId ? (_c = this.providers) === null || _c === void 0 ? void 0 : _c[chainId] : this.activeNetwork.rpcUrl,
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
chainId: chainId,
});
return vaultResponse;
});
}
getVaultAllocations(_a) {
return __awaiter(this, arguments, void 0, function* ({ vault, chainId, }) {
var _b, _c;
if (!vault)
throw new Error('Vault input parameter is undefined.');
if (!(0, ethers_1.isAddress)(vault))
throw new Error(`Vault input parameter is not an address: ${String(vault)}`);
if (typeof chainId !== 'undefined') {
if (!((_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId]))
console.error('#getVaultAllocations:', 'missing rpcUrl in initialization for chainId', chainId);
}
const vaultResponse = yield (0, getters_1.getVaultAllocations)(vault, {
rpcUrl: chainId ? (_c = this.providers) === null || _c === void 0 ? void 0 : _c[chainId] : this.activeNetwork.rpcUrl,
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
});
return vaultResponse;
});
}
getVaultApy(vault) {
return __awaiter(this, void 0, void 0, function* () {
if (!vault)
throw new Error('Vault input parameter is undefined.');
if (!(0, ethers_1.isAddress)(vault))
throw new Error(`Vault input parameter is not an address: ${String(vault)}`);
return yield (0, getters_1.getVaultApy)({ vault });
});
}
getAvailableRedemptions(_a) {
return __awaiter(this, arguments, void 0, function* ({ vault, chainId, wallet, }) {
var _b;
if (!this.authorized)
throw new Error('Not authorized.');
if (!vault)
throw new Error('Vault input parameter is undefined.');
if (!(0, ethers_1.isAddress)(vault))
throw new Error(`Vault input parameter is not an address: ${String(vault)}`);
return yield (0, getters_1.getVaultAvailableRedemptions)({
vault,
wallet,
options: {
rpcUrl: chainId ? (_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId] : this.activeNetwork.rpcUrl,
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
});
}
getPositions(_a) {
return __awaiter(this, arguments, void 0, function* ({ vault, wallet, chainId, showAllVaults, }) {
var _b;
if (!(0, ethers_1.isAddress)(wallet))
throw new Error(`Wallet parameter is not an address: ${String(wallet)}`);
if (vault) {
return yield (0, getters_1.getVaultPositions)({
vault,
wallet,
options: {
rpcUrl: chainId
? (_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId]
: this.activeNetwork.rpcUrl,
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
}
const allVaults = yield (0, utils_1.fetchTokenizedVaults)();
if (chainId) {
const vaultsPerChain = allVaults.filter((v) => v.chain === chainId);
const vaultResponses = yield Promise.all(vaultsPerChain.map((v) => {
var _a;
return (0, getters_1.getVaultPositions)({
vault: v.address,
wallet,
options: {
rpcUrl: (_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain],
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
}));
const flattened = vaultResponses.flat();
let final;
if (showAllVaults)
final = flattened;
else
final = flattened.filter((v) => v.status !== 'PENDING');
return final;
}
const vaultsPerAvailableProviders = allVaults.filter((v) => { var _a; return !!((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v === null || v === void 0 ? void 0 : v.chain]) ? true : false; });
const vaultResponses = yield Promise.all(vaultsPerAvailableProviders.map((v) => {
var _a;
return (0, getters_1.getVaultPositions)({
vault: v.address,
wallet,
options: {
rpcUrl: (_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain],
env: this.env,
augustKey: this.augustKey,
subgraphKey: this.subgraphKey,
},
});
}));
const flattened = vaultResponses.flat();
let final;
if (showAllVaults)
final = flattened;
else
final = flattened.filter((v) => v.status !== 'PENDING');
return final;
});
}
getUserHistory(_a) {
return __awaiter(this, arguments, void 0, function* ({ wallet, chainId, vault }) {
var _b;
if (!(0, ethers_1.isAddress)(wallet))
throw new Error(`Wallet parameter is not an address: ${String(wallet)}`);
const _chainId = chainId || this.activeNetwork.chainId;
let finalArray = [];
function formatHistory(history) {
return history.map((h) => ({
timestamp: Number(h.timestamp_),
address: h.address,
amount: (0, utils_1.toNormalizedBn)(BigInt(h.amount), h.decimals),
pool: h.contractId_,
chainId: _chainId,
type: h.type,
transactionHash: h.transactionHash_,
}));
}
if (vault) {
const provider = (0, utils_1.createProvider)((_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId]);
const userHistory = yield (0, services_1.getSubgraphUserHistory)(wallet, provider, vault);
finalArray = formatHistory(userHistory);
}
else {
const allVaults = yield (0, utils_1.fetchTokenizedVaults)();
if (chainId) {
const vaultsPerChain = allVaults.filter((v) => v.chain === chainId);
const vaultResponses = yield Promise.all(vaultsPerChain.map((v) => __awaiter(this, void 0, void 0, function* () {
var _a;
const provider = (0, utils_1.createProvider)((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain]);
const userHistory = yield (0, services_1.getSubgraphUserHistory)(wallet, provider, v.address);
return formatHistory(userHistory);
})));
const flattened = vaultResponses.flat();
finalArray = flattened;
}
else {
const vaultsPerAvailableProviders = allVaults.filter((v) => { var _a; return !!((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v === null || v === void 0 ? void 0 : v.chain]) ? true : false; });
const vaultResponses = yield Promise.all(vaultsPerAvailableProviders.map((v) => __awaiter(this, void 0, void 0, function* () {
var _a;
const provider = (0, utils_1.createProvider)((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain]);
const userHistory = yield (0, services_1.getSubgraphUserHistory)(wallet, provider, v.address);
return formatHistory(userHistory);
})));
const flattened = vaultResponses.flat();
finalArray = flattened;
}
}
if (this.env === 'DEV')
console.log(`#getUserHistory:`, finalArray.slice(0, 3));
return finalArray;
});
}
getUserTransfers(_a) {
return __awaiter(this, arguments, void 0, function* ({ wallet, chainId, vault }) {
var _b;
if (!(0, ethers_1.isAddress)(wallet))
throw new Error(`Wallet parameter is not an address: ${String(wallet)}`);
let finalArray = [];
function formatTransfers(transfers) {
return transfers.map((t) => ({
id: t.id,
value: t.value,
to: t.to,
from: t.from,
pool: t.contractId_,
block: Number(t.block_number),
timestamp: Number(t.timestamp_),
transactionHash: t.transactionHash_
}));
}
if (vault) {
const provider = (0, utils_1.createProvider)((_b = this.providers) === null || _b === void 0 ? void 0 : _b[chainId]);
const userHistory = yield (0, services_1.getSubgraphUserTransfers)(wallet, provider, vault);
finalArray = formatTransfers(userHistory);
}
else {
const allVaults = yield (0, utils_1.fetchTokenizedVaults)();
if (chainId) {
const vaultsPerChain = allVaults.filter((v) => v.chain === chainId);
const vaultResponses = yield Promise.all(vaultsPerChain.map((v) => __awaiter(this, void 0, void 0, function* () {
var _a;
const provider = (0, utils_1.createProvider)((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain]);
const userHistory = yield (0, services_1.getSubgraphUserTransfers)(wallet, provider, v.address);
return userHistory;
})));
const flattened = vaultResponses.flat();
finalArray = formatTransfers(flattened);
}
else {
const vaultsPerAvailableProviders = allVaults.filter((v) => { var _a; return !!((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v === null || v === void 0 ? void 0 : v.chain]) ? true : false; });
const vaultResponses = yield Promise.all(vaultsPerAvailableProviders.map((v) => __awaiter(this, void 0, void 0, function* () {
var _a;
const provider = (0, utils_1.createProvider)((_a = this.providers) === null || _a === void 0 ? void 0 : _a[v.chain]);
const userHistory = yield (0, services_1.getSubgraphUserTransfers)(wallet, provider, v.address);
return userHistory;
})));
const flattened = vaultResponses.flat();
finalArray = formatTransfers(flattened);
}
}
console.log(`#getUserTransfers:`, finalArray.slice(0, 3));
return finalArray;
});
}
getStakingPositions(wallet, chainId) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (chainId) {
const positions = yield (0, getters_1.getRewardsStakingPositions)({
rpcUrl: ((_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]) || this.activeNetwork.rpcUrl,
wallet,
});
return positions;
}
const positions = yield Promise.all(Object.values(this.providers).map((rpcUrl) => {
var _a;
return (0, getters_1.getRewardsStakingPositions)({
rpcUrl: chainId
? (_a = this.providers) === null || _a === void 0 ? void 0 : _a[chainId]
: rpcUrl || this.activeNetwork.rpcUrl,
wallet,
});
}));
return positions.flat();
});
}
getVaultExposure(debankResponse) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
const borrowing_exposure = [];
const supplying_exposure = [];
const wallet_exposure = [];
const positions = (_b = (_a = debankResponse === null || debankResponse === void 0 ? void 0 : debankResponse.subaccount) === null || _a === void 0 ? void 0 : _a.positions) !== null && _b !== void 0 ? _b : [];
const tokens = (_d = (_c = debankResponse === null || debankResponse === void 0 ? void 0 : debankResponse.subaccount) === null || _c === void 0 ? void 0 : _c.tokens) !== null && _d !== void 0 ? _d : [];
for (const position of positions) {
const portfolioItems = (_e = position === null || position === void 0 ? void 0 : position.portfolio_item_list) !== null && _e !== void 0 ? _e : [];
for (const item of portfolioItems) {
const borrowTokens = (_g = (_f = item === null || item === void 0 ? void 0 : item.detail) === null || _f === void 0 ? void 0 : _f.borrow_token_list) !== null && _g !== void 0 ? _g : [];
const supplyTokens = (_j = (_h = item === null || item === void 0 ? void 0 : item.detail) === null || _h === void 0 ? void 0 : _h.supply_token_list) !== null && _j !== void 0 ? _j : [];
for (const token of tokens) {
if ((token === null || token === void 0 ? void 0 : token.symbol) && (token === null || token === void 0 ? void 0 : token.is_core)) {
wallet_exposure.push({ symbol: token.symbol, chain: token.chain, amount: token.amount * token.price });
}
}
for (const token of borrowTokens) {
if ((token === null || token === void 0 ? void 0 : token.symbol) && !token.is_suspicious && !token.is_scam && (token === null || token === void 0 ? void 0 : token.is_core)) {
borrowing_exposure.push({ symbol: token.symbol, chain: token.chain, amount: token.amount * token.price });
}
}
for (const token of supplyTokens) {
if ((token === null || token === void 0 ? void 0 : token.symbol) && !token.is_suspicious && !token.is_scam && (token === null || token === void 0 ? void 0 : token.is_core)) {
supplying_exposure.push({ symbol: token.symbol, chain: token.chain, amount: token.amount * token.price });
}
}
}
}
return { borrowing_exposure, supplying_exposure, wallet_exposure };
}
}
exports.AugustVaults = AugustVaults;
//# sourceMappingURL=main.js.map