@augustdigital/sdk 4.0.9-alpha → 4.2.0-alpha
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 +169 -11
- package/lib/abis/AddressResolver.d.ts +28 -0
- package/lib/abis/AddressResolver.js +23 -0
- package/lib/abis/AddressResolver.js.map +1 -0
- package/lib/abis/ChainlinkV3.d.ts +87 -0
- package/lib/abis/ChainlinkV3.js +117 -0
- package/lib/abis/ChainlinkV3.js.map +1 -0
- package/lib/abis/ERC20.d.ts +168 -0
- package/lib/abis/ERC20.js +226 -0
- package/lib/abis/ERC20.js.map +1 -0
- package/lib/abis/ERC20_Bytes32.d.ts +135 -0
- package/lib/abis/ERC20_Bytes32.js +192 -0
- package/lib/abis/ERC20_Bytes32.js.map +1 -0
- package/lib/abis/ERC4626.d.ts +363 -0
- package/lib/abis/ERC4626.js +506 -0
- package/lib/abis/ERC4626.js.map +1 -0
- package/lib/abis/ERC721.d.ts +230 -0
- package/lib/abis/ERC721.js +320 -0
- package/lib/abis/ERC721.js.map +1 -0
- package/lib/abis/FeeOracle.d.ts +120 -0
- package/lib/abis/FeeOracle.js +161 -0
- package/lib/abis/FeeOracle.js.map +1 -0
- package/lib/abis/LendingPool.d.ts +1393 -0
- package/lib/abis/LendingPool.js +1806 -0
- package/lib/abis/LendingPool.js.map +1 -0
- package/lib/abis/LendingPoolV2.d.ts +55 -0
- package/lib/abis/LendingPoolV2.js +1832 -0
- package/lib/abis/LendingPoolV2.js.map +1 -0
- package/lib/abis/LendingPoolV3.d.ts +35 -0
- package/lib/abis/LendingPoolV3.js +1160 -0
- package/lib/abis/LendingPoolV3.js.map +1 -0
- package/lib/abis/Loan.d.ts +837 -0
- package/lib/abis/Loan.js +1080 -0
- package/lib/abis/Loan.js.map +1 -0
- package/lib/abis/Multicall3.d.ts +30 -0
- package/lib/abis/Multicall3.js +94 -0
- package/lib/abis/Multicall3.js.map +1 -0
- package/lib/abis/PoolAdapter.d.ts +76 -0
- package/lib/abis/PoolAdapter.js +412 -0
- package/lib/abis/PoolAdapter.js.map +1 -0
- package/lib/abis/RewardDistributor.d.ts +46 -0
- package/lib/abis/RewardDistributor.js +352 -0
- package/lib/abis/RewardDistributor.js.map +1 -0
- package/lib/abis/SmartAccount.d.ts +16 -0
- package/lib/abis/SmartAccount.js +16 -0
- package/lib/abis/SmartAccount.js.map +1 -0
- package/lib/abis/TextResolver.d.ts +16 -0
- package/lib/abis/TextResolver.js +16 -0
- package/lib/abis/TextResolver.js.map +1 -0
- package/lib/abis/TokenizedVaultV2.d.ts +1354 -0
- package/lib/abis/TokenizedVaultV2.js +1028 -0
- package/lib/abis/TokenizedVaultV2.js.map +1 -0
- package/lib/abis/TokenizedVaultV2Receipt.d.ts +1568 -0
- package/lib/abis/TokenizedVaultV2Receipt.js +1061 -0
- package/lib/abis/TokenizedVaultV2Receipt.js.map +1 -0
- package/lib/abis/TokenizedVaultV2WhitelistedAssets.d.ts +274 -0
- package/lib/abis/TokenizedVaultV2WhitelistedAssets.js +167 -0
- package/lib/abis/TokenizedVaultV2WhitelistedAssets.js.map +1 -0
- package/lib/abis/UniversalResolverResolve.d.ts +69 -0
- package/lib/abis/UniversalResolverResolve.js +35 -0
- package/lib/abis/UniversalResolverResolve.js.map +1 -0
- package/lib/abis/UniversalSignatureValidator.d.ts +17 -0
- package/lib/abis/UniversalSignatureValidator.js +27 -0
- package/lib/abis/UniversalSignatureValidator.js.map +1 -0
- package/lib/abis/WrapperAdapter.d.ts +60 -0
- package/lib/abis/WrapperAdapter.js +150 -0
- package/lib/abis/WrapperAdapter.js.map +1 -0
- package/lib/abis/index.d.ts +19 -0
- package/lib/abis/index.js +36 -0
- package/lib/abis/index.js.map +1 -0
- package/lib/adapters/evm/getters.d.ts +4 -0
- package/lib/adapters/evm/getters.js +153 -0
- package/lib/adapters/evm/getters.js.map +1 -0
- package/lib/adapters/evm/index.d.ts +19 -0
- package/lib/adapters/evm/index.js +54 -0
- package/lib/adapters/evm/index.js.map +1 -0
- package/lib/adapters/evm/utils.d.ts +2 -0
- package/lib/adapters/evm/utils.js +43 -0
- package/lib/adapters/evm/utils.js.map +1 -0
- package/lib/adapters/solana/constants.d.ts +25 -0
- package/lib/adapters/solana/constants.js +32 -0
- package/lib/adapters/solana/constants.js.map +1 -0
- package/lib/adapters/solana/getters.d.ts +3 -0
- package/lib/adapters/solana/getters.js +135 -0
- package/lib/adapters/solana/getters.js.map +1 -0
- package/lib/adapters/solana/idl/vault-idl.d.ts +338 -0
- package/lib/adapters/solana/idl/vault-idl.js +1075 -0
- package/lib/adapters/solana/idl/vault-idl.js.map +1 -0
- package/lib/adapters/solana/index.d.ts +136 -0
- package/lib/adapters/solana/index.js +174 -0
- package/lib/adapters/solana/index.js.map +1 -0
- package/lib/adapters/solana/types.d.ts +58 -0
- package/lib/adapters/solana/types.js +3 -0
- package/lib/adapters/solana/types.js.map +1 -0
- package/lib/adapters/solana/utils.d.ts +104 -0
- package/lib/adapters/solana/utils.js +380 -0
- package/lib/adapters/solana/utils.js.map +1 -0
- package/lib/adapters/solana/vault.actions.d.ts +18 -0
- package/lib/adapters/solana/vault.actions.js +235 -0
- package/lib/adapters/solana/vault.actions.js.map +1 -0
- package/lib/adapters/sui/index.d.ts +0 -0
- package/lib/adapters/sui/index.js +1 -0
- package/lib/adapters/sui/index.js.map +1 -0
- package/lib/core/auth/index.d.ts +1 -0
- package/lib/core/auth/index.js +18 -0
- package/lib/core/auth/index.js.map +1 -0
- package/lib/{verify.js → core/auth/verify.js} +3 -2
- package/lib/core/auth/verify.js.map +1 -0
- package/lib/core/base.class.d.ts +33 -0
- package/lib/core/base.class.js +66 -0
- package/lib/core/base.class.js.map +1 -0
- package/lib/core/constants.d.ts +88 -0
- package/lib/core/constants.js +148 -0
- package/lib/core/constants.js.map +1 -0
- package/lib/core/core.helpers.d.ts +23 -0
- package/lib/core/core.helpers.js +210 -0
- package/lib/core/core.helpers.js.map +1 -0
- package/lib/core/fetcher.d.ts +22 -0
- package/lib/core/fetcher.js +358 -0
- package/lib/core/fetcher.js.map +1 -0
- package/lib/core/index.d.ts +7 -0
- package/lib/core/index.js +24 -0
- package/lib/core/index.js.map +1 -0
- package/lib/core/logger/index.d.ts +21 -0
- package/lib/core/logger/index.js +80 -0
- package/lib/core/logger/index.js.map +1 -0
- package/lib/core/logger/slack.d.ts +12 -0
- package/lib/core/logger/slack.js +49 -0
- package/lib/core/logger/slack.js.map +1 -0
- package/lib/core/read.actions.d.ts +3 -0
- package/lib/core/read.actions.js +39 -0
- package/lib/core/read.actions.js.map +1 -0
- package/lib/core/web3.helpers.d.ts +28 -0
- package/lib/core/web3.helpers.js +336 -0
- package/lib/core/web3.helpers.js.map +1 -0
- package/lib/core/write.actions.d.ts +9 -0
- package/lib/core/write.actions.js +51 -0
- package/lib/core/write.actions.js.map +1 -0
- package/lib/index.d.ts +4 -5
- package/lib/index.js +6 -7
- package/lib/index.js.map +1 -1
- package/lib/main.d.ts +82 -14
- package/lib/main.js +51 -17
- package/lib/main.js.map +1 -1
- package/lib/modules/vaults/constants.d.ts +63 -0
- package/lib/modules/vaults/constants.js +192 -0
- package/lib/modules/vaults/constants.js.map +1 -0
- package/lib/modules/vaults/fetcher.d.ts +101 -0
- package/lib/modules/vaults/fetcher.js +257 -0
- package/lib/modules/vaults/fetcher.js.map +1 -0
- package/lib/modules/vaults/getters.d.ts +47 -0
- package/lib/modules/vaults/getters.js +735 -0
- package/lib/modules/vaults/getters.js.map +1 -0
- package/lib/modules/vaults/index.d.ts +3 -0
- package/lib/modules/vaults/index.js +20 -0
- package/lib/modules/vaults/index.js.map +1 -0
- package/lib/modules/vaults/main.d.ts +95 -0
- package/lib/modules/vaults/main.js +494 -0
- package/lib/modules/vaults/main.js.map +1 -0
- package/lib/modules/vaults/read.actions.d.ts +3 -0
- package/lib/modules/vaults/read.actions.js +39 -0
- package/lib/modules/vaults/read.actions.js.map +1 -0
- package/lib/modules/vaults/utils.d.ts +15 -0
- package/lib/modules/vaults/utils.js +199 -0
- package/lib/modules/vaults/utils.js.map +1 -0
- package/lib/modules/vaults/vault.actions.d.ts +10 -0
- package/lib/modules/vaults/vault.actions.js +95 -0
- package/lib/modules/vaults/vault.actions.js.map +1 -0
- package/lib/modules/vaults/write.actions.d.ts +17 -0
- package/lib/modules/vaults/write.actions.js +143 -0
- package/lib/modules/vaults/write.actions.js.map +1 -0
- package/lib/services/coingecko/fetcher.d.ts +1 -0
- package/lib/services/coingecko/fetcher.js +36 -0
- package/lib/services/coingecko/fetcher.js.map +1 -0
- package/lib/services/coingecko/index.d.ts +2 -0
- package/lib/services/coingecko/index.js +19 -0
- package/lib/services/coingecko/index.js.map +1 -0
- package/lib/services/coingecko/utils.d.ts +1 -0
- package/lib/services/coingecko/utils.js +24 -0
- package/lib/services/coingecko/utils.js.map +1 -0
- package/lib/services/debank/fetcher.d.ts +2 -0
- package/lib/services/debank/fetcher.js +16 -0
- package/lib/services/debank/fetcher.js.map +1 -0
- package/lib/services/debank/index.d.ts +2 -0
- package/lib/services/debank/index.js +19 -0
- package/lib/services/debank/index.js.map +1 -0
- package/lib/services/debank/utils.d.ts +32 -0
- package/lib/services/debank/utils.js +229 -0
- package/lib/services/debank/utils.js.map +1 -0
- package/lib/services/subgraph/fetcher.d.ts +2 -0
- package/lib/services/subgraph/fetcher.js +58 -0
- package/lib/services/subgraph/fetcher.js.map +1 -0
- package/lib/services/subgraph/index.d.ts +2 -0
- package/lib/services/subgraph/index.js +19 -0
- package/lib/services/subgraph/index.js.map +1 -0
- package/lib/services/subgraph/vaults.d.ts +10 -0
- package/lib/services/subgraph/vaults.js +473 -0
- package/lib/services/subgraph/vaults.js.map +1 -0
- package/lib/types/index.d.ts +8 -0
- package/lib/types/index.js +24 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/pools.d.ts +141 -0
- package/lib/types/pools.js +3 -0
- package/lib/types/pools.js.map +1 -0
- package/lib/types/staking.d.ts +28 -0
- package/lib/types/staking.js +3 -0
- package/lib/types/staking.js.map +1 -0
- package/lib/types/subgraph.d.ts +45 -0
- package/lib/types/subgraph.js +3 -0
- package/lib/types/subgraph.js.map +1 -0
- package/lib/types/user.d.ts +1 -0
- package/lib/types/user.js +3 -0
- package/lib/types/user.js.map +1 -0
- package/lib/types/vaults.d.ts +170 -0
- package/lib/types/vaults.js +3 -0
- package/lib/types/vaults.js.map +1 -0
- package/lib/types/web3.d.ts +24 -0
- package/lib/types/web3.js +3 -0
- package/lib/types/web3.js.map +1 -0
- package/lib/types/webserver.d.ts +489 -0
- package/lib/types/webserver.js +3 -0
- package/lib/types/webserver.js.map +1 -0
- package/package.json +15 -10
- package/lib/verify.js.map +0 -1
- /package/lib/{verify.d.ts → core/auth/verify.d.ts} +0 -0
|
@@ -0,0 +1,735 @@
|
|
|
1
|
+
"use strict";
|
|
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getVault = getVault;
|
|
37
|
+
exports.getVaultLoans = getVaultLoans;
|
|
38
|
+
exports.getVaultAllocations = getVaultAllocations;
|
|
39
|
+
exports.getVaultAvailableRedemptions = getVaultAvailableRedemptions;
|
|
40
|
+
exports.getVaultRedemptionHistory = getVaultRedemptionHistory;
|
|
41
|
+
exports.getVaultPositions = getVaultPositions;
|
|
42
|
+
exports.getVaultApy = getVaultApy;
|
|
43
|
+
exports.getRewardsStakingPositions = getRewardsStakingPositions;
|
|
44
|
+
exports.getVaultTvl = getVaultTvl;
|
|
45
|
+
const abis_1 = require("../../abis");
|
|
46
|
+
const core_1 = require("../../core");
|
|
47
|
+
const ethers_1 = require("ethers");
|
|
48
|
+
const utils_1 = require("./utils");
|
|
49
|
+
const subgraph_1 = require("../../services/subgraph");
|
|
50
|
+
const constants_1 = require("./constants");
|
|
51
|
+
const debank_1 = require("../../services/debank");
|
|
52
|
+
const solana_1 = require("../../adapters/solana");
|
|
53
|
+
const EVM = __importStar(require("../../adapters/evm"));
|
|
54
|
+
const EthDater = require('ethereum-block-by-date');
|
|
55
|
+
async function getVault({ vault, loans = false, allocations = false, options, }) {
|
|
56
|
+
let returnedVault;
|
|
57
|
+
const vaultVersion = (0, utils_1.getVaultVersion)(vault);
|
|
58
|
+
try {
|
|
59
|
+
const tokenizedVault = (await (0, core_1.fetchTokenizedVaults)(vault))?.[0];
|
|
60
|
+
switch (vaultVersion) {
|
|
61
|
+
case 'sol-0': {
|
|
62
|
+
return await solana_1.Solana.getters.getSolanaVault(tokenizedVault, options);
|
|
63
|
+
}
|
|
64
|
+
case 'evm-2': {
|
|
65
|
+
returnedVault = await EVM.getEvmVaultV2(tokenizedVault, options);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
default: {
|
|
69
|
+
returnedVault = await EVM.getEvmVaultV1(tokenizedVault, options);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (returnedVault === null) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
core_1.Logger.log.error('getVault', err, { vault, vaultVersion });
|
|
79
|
+
throw new Error(`#getVault::${vault}::${vaultVersion}: ${err?.message}`);
|
|
80
|
+
}
|
|
81
|
+
if (!(0, constants_1.isBadVault)(vault)) {
|
|
82
|
+
if (loans) {
|
|
83
|
+
try {
|
|
84
|
+
returnedVault = {
|
|
85
|
+
...returnedVault,
|
|
86
|
+
loans: await getVaultLoans(returnedVault, options),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
core_1.Logger.log.error('getVault:loans', e, {
|
|
91
|
+
symbol: returnedVault.receipt.symbol,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (allocations) {
|
|
96
|
+
try {
|
|
97
|
+
const vaultAllocations = await getVaultAllocations(vault, options);
|
|
98
|
+
returnedVault = {
|
|
99
|
+
...returnedVault,
|
|
100
|
+
allocations: vaultAllocations,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
catch (e) {
|
|
104
|
+
core_1.Logger.log.error('getVault:allocations', e, {
|
|
105
|
+
symbol: returnedVault.receipt.symbol,
|
|
106
|
+
});
|
|
107
|
+
throw new Error('Failure fetching debank res');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return returnedVault;
|
|
112
|
+
}
|
|
113
|
+
async function getVaultLoans(vault, options) {
|
|
114
|
+
const vaultAddress = typeof vault === 'string' ? vault : vault.address;
|
|
115
|
+
const vaultVersion = (0, utils_1.getVaultVersion)(vaultAddress);
|
|
116
|
+
if (vaultVersion !== 'evm-0')
|
|
117
|
+
return [];
|
|
118
|
+
try {
|
|
119
|
+
let poolTotalSupply;
|
|
120
|
+
let poolDecimals;
|
|
121
|
+
const tokenizedVault = (await (0, core_1.fetchTokenizedVaults)(vaultAddress, options?.headers))?.[0];
|
|
122
|
+
const chainId = options?.chainId || tokenizedVault?.chain;
|
|
123
|
+
const provider = (0, core_1.createProvider)(options.rpcUrl);
|
|
124
|
+
const tokenizedVaultLoans = await (0, core_1.fetchTokenizedVaultLoans)(vaultAddress, chainId);
|
|
125
|
+
if (typeof vault === 'string') {
|
|
126
|
+
const poolContract = (0, core_1.createContract)({
|
|
127
|
+
provider,
|
|
128
|
+
address: vaultAddress,
|
|
129
|
+
abi: abis_1.ABI_LENDING_POOL_V2,
|
|
130
|
+
});
|
|
131
|
+
[poolTotalSupply, poolDecimals] = await Promise.all([
|
|
132
|
+
poolContract.totalSupply(),
|
|
133
|
+
(0, core_1.getDecimals)(provider, vaultAddress),
|
|
134
|
+
]);
|
|
135
|
+
poolTotalSupply = BigInt(poolTotalSupply).toString();
|
|
136
|
+
poolDecimals = Number(poolDecimals);
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
poolTotalSupply = vault.totalSupply.raw;
|
|
140
|
+
poolDecimals = vault.decimals;
|
|
141
|
+
}
|
|
142
|
+
const newLoans = (await Promise.all((Array.isArray(tokenizedVaultLoans) ? tokenizedVaultLoans : [])?.map(async (l) => {
|
|
143
|
+
const borrower = l.borrower;
|
|
144
|
+
const allocation = l.principal_amount /
|
|
145
|
+
Number((0, ethers_1.formatUnits)(poolTotalSupply, poolDecimals));
|
|
146
|
+
const loanFeeRate = await (0, core_1.getLoanOracleFeeRate)(provider, 'LOAN.REPAY.INTERESTS', l.address, chainId);
|
|
147
|
+
const loanApr = Number(l.apr || 0) / 100;
|
|
148
|
+
const loanAprAfterFees = loanApr * (1 - loanFeeRate / 100);
|
|
149
|
+
const isIdleCapital = constants_1.IDLE_CAPITAL_BORROWER_ADDRESS.includes(borrower);
|
|
150
|
+
const newLoanObj = {
|
|
151
|
+
vault: vaultAddress,
|
|
152
|
+
address: l.address,
|
|
153
|
+
lender: l.lender,
|
|
154
|
+
borrower: l.borrower,
|
|
155
|
+
state: l.state,
|
|
156
|
+
totalRepaid: l.total_repaid,
|
|
157
|
+
principalToken: l.principal_token,
|
|
158
|
+
principalAmount: l.principal_amount,
|
|
159
|
+
interestAmount: l.interest_amount,
|
|
160
|
+
upcomingPayment: {
|
|
161
|
+
amount: l.upcoming_payment.amount,
|
|
162
|
+
dueDate: l.upcoming_payment.due_date,
|
|
163
|
+
},
|
|
164
|
+
apr: loanAprAfterFees,
|
|
165
|
+
initialPrincipalAmount: l.initial_principal_amount,
|
|
166
|
+
deployedDate: l.deployed_date,
|
|
167
|
+
isIdleCapital,
|
|
168
|
+
paymentInterval: l.payment_interval,
|
|
169
|
+
allocation,
|
|
170
|
+
};
|
|
171
|
+
return newLoanObj;
|
|
172
|
+
}))).filter((l) => l !== undefined);
|
|
173
|
+
return newLoans;
|
|
174
|
+
}
|
|
175
|
+
catch (e) {
|
|
176
|
+
core_1.Logger.log.error('getVaultLoans', e, { vault });
|
|
177
|
+
throw new Error(`#getVaultLoans::${vault}:${e?.message}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
async function getVaultAllocations(vault, options) {
|
|
181
|
+
let protocolExposure = [];
|
|
182
|
+
let tokenExposure = [];
|
|
183
|
+
let cefiExposure = [];
|
|
184
|
+
let cefiBorrowerResponses = {};
|
|
185
|
+
let otcPositions = {};
|
|
186
|
+
let defiPerBorrower = {};
|
|
187
|
+
let exposurePerCategory = {
|
|
188
|
+
supplying: [],
|
|
189
|
+
borrowing: [],
|
|
190
|
+
wallet: [],
|
|
191
|
+
walletBalance: 0,
|
|
192
|
+
cefiBalance: 0,
|
|
193
|
+
defiBalance: 0,
|
|
194
|
+
lending: [],
|
|
195
|
+
loanBalance: 0,
|
|
196
|
+
};
|
|
197
|
+
let netValue = { value: 0 };
|
|
198
|
+
let debankErr = false;
|
|
199
|
+
let unfilteredTokens = [];
|
|
200
|
+
let uniqueBorrowers;
|
|
201
|
+
const tokenizedVault = (await (0, core_1.fetchTokenizedVaults)(vault, options?.headers))?.[0];
|
|
202
|
+
try {
|
|
203
|
+
const vaultVersion = (0, utils_1.getVaultVersion)(vault);
|
|
204
|
+
if (vaultVersion === 'evm-0') {
|
|
205
|
+
const loans = await getVaultLoans(vault, options);
|
|
206
|
+
uniqueBorrowers = [...new Set(loans.map((l) => l.borrower))];
|
|
207
|
+
}
|
|
208
|
+
else {
|
|
209
|
+
uniqueBorrowers = tokenizedVault.subaccounts.map((s) => s.address);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
catch (e) {
|
|
213
|
+
core_1.Logger.log.error('getVaultAllocations:borrowers', e);
|
|
214
|
+
}
|
|
215
|
+
if (vault.toLowerCase() === constants_1.VAULT_AgoraAUSD.toLowerCase()) {
|
|
216
|
+
const debankRes = await (0, debank_1.fetchDebankResponse)(constants_1.SUBACCOUNT_CALEB);
|
|
217
|
+
if (debankRes === false) {
|
|
218
|
+
debankErr = true;
|
|
219
|
+
}
|
|
220
|
+
(0, debank_1.parseVaultLevelDebank)(debankRes, protocolExposure, tokenExposure, constants_1.SUBACCOUNT_CALEB, exposurePerCategory, netValue);
|
|
221
|
+
unfilteredTokens = debankRes?.subaccount?.tokens;
|
|
222
|
+
const debankPerLoan = (0, debank_1.parseLoanLevelDebank)(debankRes);
|
|
223
|
+
defiPerBorrower[constants_1.SUBACCOUNT_CALEB] = debankPerLoan;
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
for (const borrower of uniqueBorrowers) {
|
|
227
|
+
try {
|
|
228
|
+
const debankRes = await (0, debank_1.fetchDebankResponse)(borrower);
|
|
229
|
+
if (debankRes === false) {
|
|
230
|
+
debankErr = true;
|
|
231
|
+
}
|
|
232
|
+
(0, debank_1.parseVaultLevelDebank)(debankRes, protocolExposure, tokenExposure, borrower, exposurePerCategory, netValue);
|
|
233
|
+
unfilteredTokens = debankRes?.subaccount?.tokens;
|
|
234
|
+
const debankPerLoan = (0, debank_1.parseLoanLevelDebank)(debankRes);
|
|
235
|
+
defiPerBorrower[borrower] = debankPerLoan;
|
|
236
|
+
}
|
|
237
|
+
catch (e) {
|
|
238
|
+
core_1.Logger.log.error('getVaultAllocations:debank', e, { borrower });
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
const cefiResponse = await (0, core_1.fetchAugustWithKey)(options.augustKey, core_1.WEBSERVER_ENDPOINTS.subaccount.cefi(borrower), { headers: options?.headers });
|
|
242
|
+
if (cefiResponse.status !== 200) {
|
|
243
|
+
console.error('#getVaultAllocations::cefi:', cefiResponse.status, cefiResponse.statusText);
|
|
244
|
+
}
|
|
245
|
+
if (cefiResponse.status === 200) {
|
|
246
|
+
const cefiRes = (await cefiResponse.json());
|
|
247
|
+
cefiBorrowerResponses[borrower] = cefiRes;
|
|
248
|
+
cefiExposure = cefiRes;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
catch (e) {
|
|
252
|
+
console.error('#getVaultAllocations::cefi:', e);
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
const otcResponse = await (0, core_1.fetchAugustWithKey)(options.augustKey, core_1.WEBSERVER_ENDPOINTS.subaccount.otc_positions(borrower), { headers: options?.headers });
|
|
256
|
+
if (otcResponse.status !== 200) {
|
|
257
|
+
console.error('#getVaultAllocations::otc:', otcResponse.status, otcResponse.statusText);
|
|
258
|
+
}
|
|
259
|
+
if (otcResponse.status === 200) {
|
|
260
|
+
const otcRes = (await otcResponse.json());
|
|
261
|
+
otcPositions[borrower] = otcRes;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
catch (e) {
|
|
265
|
+
console.error('#getVaultAllocations::otc:', e);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
const walletBalance = exposurePerCategory.wallet.reduce((sum, item) => sum + item.amount, 0);
|
|
270
|
+
const defiBalance = protocolExposure.reduce((sum, item) => sum + item.netUsdValue, 0);
|
|
271
|
+
const cefiBalance = cefiExposure.reduce((sum, item) => sum + item.value, 0);
|
|
272
|
+
exposurePerCategory.walletBalance = walletBalance;
|
|
273
|
+
exposurePerCategory.defiBalance = defiBalance;
|
|
274
|
+
exposurePerCategory.cefiBalance = cefiBalance;
|
|
275
|
+
netValue.value += cefiBalance;
|
|
276
|
+
if (debankErr) {
|
|
277
|
+
throw Error('failure to fetch debank response');
|
|
278
|
+
}
|
|
279
|
+
return {
|
|
280
|
+
defi: protocolExposure,
|
|
281
|
+
cefi: cefiExposure,
|
|
282
|
+
otc: otcPositions,
|
|
283
|
+
tokens: tokenExposure,
|
|
284
|
+
defiPerBorrower,
|
|
285
|
+
exposurePerCategory,
|
|
286
|
+
netValue: netValue.value,
|
|
287
|
+
unfilteredTokens: unfilteredTokens,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
async function getVaultAvailableRedemptions({ vault, wallet, options, }) {
|
|
291
|
+
try {
|
|
292
|
+
const provider = (0, core_1.createProvider)(options.rpcUrl);
|
|
293
|
+
const tokenizedVault = (await (0, core_1.fetchTokenizedVaults)(vault, options?.headers))?.[0];
|
|
294
|
+
if (!tokenizedVault) {
|
|
295
|
+
return { availableRedemptions: [], pendingRedemptions: [] };
|
|
296
|
+
}
|
|
297
|
+
const poolContract = (0, core_1.createContract)({
|
|
298
|
+
address: vault,
|
|
299
|
+
abi: abis_1.ABI_LENDING_POOL_V2,
|
|
300
|
+
provider,
|
|
301
|
+
});
|
|
302
|
+
const { withdrawalRequesteds, withdrawalProcesseds } = await (0, subgraph_1.getSubgraphAllWithdrawals)(vault, provider);
|
|
303
|
+
const decimals = await (0, core_1.getDecimals)(provider, vault);
|
|
304
|
+
const availableRedemptions = [];
|
|
305
|
+
const pendingRedemptions = [];
|
|
306
|
+
for (const ev of withdrawalRequesteds) {
|
|
307
|
+
if (!ev || typeof ev !== 'object') {
|
|
308
|
+
console.warn('#getVaultAvailableRedemptions: Skipping invalid event:', ev);
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (!ev.year || !ev.month || !ev.day || !ev.receiverAddr) {
|
|
312
|
+
console.warn('#getVaultAvailableRedemptions: Skipping event with missing required properties:', ev);
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
const fullDate = new Date(Number(ev.year), Number(ev.month) - 1, Number(ev.day));
|
|
316
|
+
const foundRedemptionAgainstClaim = withdrawalProcesseds.find((h) => new Date(h.processedOn).toDateString() === fullDate.toDateString() &&
|
|
317
|
+
h.receiverAddr === ev.receiverAddr);
|
|
318
|
+
if (wallet && (0, ethers_1.isAddress)(wallet)) {
|
|
319
|
+
if (ev?.receiverAddr?.toLowerCase() === wallet.toLowerCase()) {
|
|
320
|
+
const alreadyRedeemed = availableRedemptions.find((red) => BigInt(red.day.raw) === BigInt(ev.day) &&
|
|
321
|
+
BigInt(red.month.raw) === BigInt(ev.month) &&
|
|
322
|
+
BigInt(red.year.raw) === BigInt(ev.year));
|
|
323
|
+
if (!(foundRedemptionAgainstClaim && alreadyRedeemed)) {
|
|
324
|
+
try {
|
|
325
|
+
const trueClaimableAmount = await poolContract?.getClaimableAmountByReceiver?.(BigInt(ev.year), BigInt(ev.month), BigInt(ev.day), (0, ethers_1.getAddress)(wallet));
|
|
326
|
+
if (trueClaimableAmount > BigInt(0)) {
|
|
327
|
+
availableRedemptions.push({
|
|
328
|
+
id: ev.transactionHash_ || ev.id,
|
|
329
|
+
hash: ev.transactionHash_ || ev.id,
|
|
330
|
+
timestamp: Number(ev.timestamp_),
|
|
331
|
+
receiver: ev.receiverAddr,
|
|
332
|
+
day: (0, core_1.toNormalizedBn)(ev.day, 0),
|
|
333
|
+
month: (0, core_1.toNormalizedBn)(ev.month, 0),
|
|
334
|
+
year: (0, core_1.toNormalizedBn)(ev.year, 0),
|
|
335
|
+
amount: (0, core_1.toNormalizedBn)(trueClaimableAmount, decimals),
|
|
336
|
+
date: fullDate,
|
|
337
|
+
vault,
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
pendingRedemptions.push({
|
|
341
|
+
id: ev.transactionHash_ || ev.id,
|
|
342
|
+
hash: ev.transactionHash_ || ev.id,
|
|
343
|
+
timestamp: Number(ev.timestamp_),
|
|
344
|
+
receiver: ev.receiverAddr,
|
|
345
|
+
day: (0, core_1.toNormalizedBn)(ev.day, 0),
|
|
346
|
+
month: (0, core_1.toNormalizedBn)(ev.month, 0),
|
|
347
|
+
year: (0, core_1.toNormalizedBn)(ev.year, 0),
|
|
348
|
+
amount: (0, core_1.toNormalizedBn)(trueClaimableAmount || BigInt(0), decimals),
|
|
349
|
+
date: fullDate,
|
|
350
|
+
vault,
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
catch (contractError) {
|
|
354
|
+
console.error('#getVaultAvailableRedemptions: Contract call failed:', contractError);
|
|
355
|
+
pendingRedemptions.push({
|
|
356
|
+
id: ev.transactionHash_ || ev.id,
|
|
357
|
+
hash: ev.transactionHash_ || ev.id,
|
|
358
|
+
timestamp: Number(ev.timestamp_),
|
|
359
|
+
receiver: ev.receiverAddr,
|
|
360
|
+
day: (0, core_1.toNormalizedBn)(ev.day, 0),
|
|
361
|
+
month: (0, core_1.toNormalizedBn)(ev.month, 0),
|
|
362
|
+
year: (0, core_1.toNormalizedBn)(ev.year, 0),
|
|
363
|
+
amount: (0, core_1.toNormalizedBn)(BigInt(0), decimals),
|
|
364
|
+
date: fullDate,
|
|
365
|
+
vault,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
if (!!foundRedemptionAgainstClaim) {
|
|
373
|
+
availableRedemptions.push({
|
|
374
|
+
id: ev.transactionHash_ || ev.id,
|
|
375
|
+
hash: ev.transactionHash_ || ev.id,
|
|
376
|
+
timestamp: Number(ev.timestamp_),
|
|
377
|
+
receiver: ev.receiverAddr,
|
|
378
|
+
day: (0, core_1.toNormalizedBn)(ev.day, 0),
|
|
379
|
+
month: (0, core_1.toNormalizedBn)(ev.month, 0),
|
|
380
|
+
year: (0, core_1.toNormalizedBn)(ev.year, 0),
|
|
381
|
+
amount: (0, core_1.toNormalizedBn)(ev.assets || BigInt(0), decimals),
|
|
382
|
+
date: fullDate,
|
|
383
|
+
vault,
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
return { availableRedemptions, pendingRedemptions };
|
|
389
|
+
}
|
|
390
|
+
catch (e) {
|
|
391
|
+
core_1.Logger.log.error('getVaultAvailableRedemptions', e, { vault, wallet });
|
|
392
|
+
return { availableRedemptions: [], pendingRedemptions: [] };
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
async function getVaultRedemptionHistory({ vault, wallet, options, }) {
|
|
396
|
+
try {
|
|
397
|
+
const provider = (0, core_1.createProvider)(options.rpcUrl);
|
|
398
|
+
const tokenizedVault = (await (0, core_1.fetchTokenizedVaults)(vault, options?.headers))?.[0];
|
|
399
|
+
if (!tokenizedVault) {
|
|
400
|
+
return [];
|
|
401
|
+
}
|
|
402
|
+
const chainId = tokenizedVault.chain;
|
|
403
|
+
const poolContract = (0, core_1.createContract)({
|
|
404
|
+
address: vault,
|
|
405
|
+
abi: abis_1.ABI_LENDING_POOL_V2,
|
|
406
|
+
provider,
|
|
407
|
+
});
|
|
408
|
+
let logPromises = [];
|
|
409
|
+
const currentBlock = await provider.getBlockNumber();
|
|
410
|
+
let startingBlock = currentBlock - (0, core_1.determineBlockSkipInternal)(chainId);
|
|
411
|
+
let endBlock = currentBlock;
|
|
412
|
+
const cutoffBlock = currentBlock - (0, core_1.determineBlockCutoff)(chainId);
|
|
413
|
+
while (endBlock >= cutoffBlock) {
|
|
414
|
+
const logBloomsPromises = poolContract.queryFilter('WithdrawalProcessed', BigInt(startingBlock), BigInt(endBlock));
|
|
415
|
+
logPromises.push(logBloomsPromises);
|
|
416
|
+
startingBlock -= (0, core_1.determineBlockSkipInternal)(chainId);
|
|
417
|
+
endBlock -= (0, core_1.determineBlockSkipInternal)(chainId);
|
|
418
|
+
}
|
|
419
|
+
const logs = (await Promise.all(logPromises.flat())).flat();
|
|
420
|
+
const iface = new ethers_1.ethers.Interface([
|
|
421
|
+
'event WithdrawalProcessed (uint256 assetsAmount, uint256 processedOn, address receiverAddr, uint256 requestedOn)',
|
|
422
|
+
]);
|
|
423
|
+
const events = logs.map((log) => iface.parseLog(log));
|
|
424
|
+
const decimals = await (0, core_1.getDecimals)(provider, vault);
|
|
425
|
+
const redemptions = [];
|
|
426
|
+
events?.forEach((ev) => {
|
|
427
|
+
if (!ev?.args)
|
|
428
|
+
return;
|
|
429
|
+
const [assetsAmount, processedOn, receiverAddr, requestedOn] = ev.args;
|
|
430
|
+
if (wallet) {
|
|
431
|
+
if (String(receiverAddr)?.toLowerCase() === wallet.toLowerCase()) {
|
|
432
|
+
redemptions.push({
|
|
433
|
+
receiver: receiverAddr,
|
|
434
|
+
amount: (0, core_1.toNormalizedBn)(assetsAmount, decimals),
|
|
435
|
+
processed: new Date(Number(processedOn) * 1000),
|
|
436
|
+
requested: new Date(Number(requestedOn) * 1000),
|
|
437
|
+
vault,
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
redemptions.push({
|
|
443
|
+
receiver: receiverAddr,
|
|
444
|
+
amount: (0, core_1.toNormalizedBn)(assetsAmount, decimals),
|
|
445
|
+
processed: new Date(Number(processedOn) * 1000),
|
|
446
|
+
requested: new Date(Number(requestedOn) * 1000),
|
|
447
|
+
vault,
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
});
|
|
451
|
+
core_1.Logger.log.info('getVaultRedemptionHistory', redemptions.slice(0, 1));
|
|
452
|
+
return redemptions;
|
|
453
|
+
}
|
|
454
|
+
catch (e) {
|
|
455
|
+
core_1.Logger.log.error('getVaultRedemptionHistory', e, { vault, wallet });
|
|
456
|
+
return [];
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
async function getVaultPositions({ vault, wallet, solanaWallet, options, }) {
|
|
460
|
+
try {
|
|
461
|
+
const tokenizedVaults = await (0, core_1.fetchTokenizedVaults)(vault ? vault : undefined, options?.headers);
|
|
462
|
+
if (!tokenizedVaults || tokenizedVaults.length === 0) {
|
|
463
|
+
return [];
|
|
464
|
+
}
|
|
465
|
+
function renderStatus(_redemptions, _balance) {
|
|
466
|
+
if (_redemptions?.length)
|
|
467
|
+
return 'REDEEM';
|
|
468
|
+
if (BigInt(_balance.raw) > BigInt(0))
|
|
469
|
+
return 'STAKED';
|
|
470
|
+
return 'PENDING';
|
|
471
|
+
}
|
|
472
|
+
const promises = await Promise.all(tokenizedVaults.map(async (v) => {
|
|
473
|
+
try {
|
|
474
|
+
if (solana_1.Solana.utils.isSolanaAddress(v.address)) {
|
|
475
|
+
let balance = (0, core_1.toNormalizedBn)(0);
|
|
476
|
+
if (solana_1.Solana.utils.isSolanaAddress(solanaWallet)) {
|
|
477
|
+
const vaultStateRes = await options.solanaService.getVaultState(String(v.address), solana_1.Solana.constants.vaultIdl);
|
|
478
|
+
if (vaultStateRes &&
|
|
479
|
+
vaultStateRes?.vaultState?.shareMint) {
|
|
480
|
+
balance = (0, core_1.toNormalizedBn)(await options.solanaService.fetchUserShareBalance(solanaWallet, vaultStateRes?.vaultState?.shareMint));
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return {
|
|
484
|
+
vault,
|
|
485
|
+
status: renderStatus([], balance),
|
|
486
|
+
availableRedemptions: [],
|
|
487
|
+
pendingRedemptions: [],
|
|
488
|
+
redeemable: balance,
|
|
489
|
+
walletBalance: balance,
|
|
490
|
+
};
|
|
491
|
+
}
|
|
492
|
+
const provider = (0, core_1.createProvider)(options.rpcUrl);
|
|
493
|
+
const decimals = await (0, core_1.getDecimals)(provider, vault);
|
|
494
|
+
const vaultContract = (0, core_1.createContract)({
|
|
495
|
+
provider,
|
|
496
|
+
abi: abis_1.ABI_LENDING_POOL_V2,
|
|
497
|
+
address: v.address,
|
|
498
|
+
});
|
|
499
|
+
const bal = wallet && (0, ethers_1.isAddress)(wallet)
|
|
500
|
+
? await vaultContract.balanceOf(wallet)
|
|
501
|
+
: BigInt(0);
|
|
502
|
+
const balance = (0, core_1.toNormalizedBn)(bal || 0, decimals);
|
|
503
|
+
const { availableRedemptions, pendingRedemptions } = await getVaultAvailableRedemptions({
|
|
504
|
+
vault: v.address,
|
|
505
|
+
wallet,
|
|
506
|
+
options,
|
|
507
|
+
});
|
|
508
|
+
const aggregateAvailableRedemptions = availableRedemptions?.reduce((acc, curr) => acc + BigInt(curr.amount.raw), BigInt(0));
|
|
509
|
+
return {
|
|
510
|
+
vault,
|
|
511
|
+
status: renderStatus(availableRedemptions, balance),
|
|
512
|
+
availableRedemptions,
|
|
513
|
+
pendingRedemptions,
|
|
514
|
+
redeemable: (0, core_1.toNormalizedBn)(aggregateAvailableRedemptions, decimals),
|
|
515
|
+
walletBalance: balance,
|
|
516
|
+
};
|
|
517
|
+
}
|
|
518
|
+
catch (vaultError) {
|
|
519
|
+
console.error(`#getVaultPositions: Error processing vault ${v.address}:`, vaultError);
|
|
520
|
+
return {
|
|
521
|
+
vault,
|
|
522
|
+
status: 'PENDING',
|
|
523
|
+
availableRedemptions: [],
|
|
524
|
+
pendingRedemptions: [],
|
|
525
|
+
redeemable: (0, core_1.toNormalizedBn)(0),
|
|
526
|
+
walletBalance: (0, core_1.toNormalizedBn)(0),
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
}));
|
|
530
|
+
return promises.flat();
|
|
531
|
+
}
|
|
532
|
+
catch (e) {
|
|
533
|
+
core_1.Logger.log.error('getVaultPositions', e, { vault, wallet, solanaWallet });
|
|
534
|
+
throw new Error(`#getVaultPositions::${vault}:${e?.message}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
async function getVaultApy({ vault, options, historical, }) {
|
|
538
|
+
try {
|
|
539
|
+
if (!vault)
|
|
540
|
+
throw new Error('Vault input parameter is undefined.');
|
|
541
|
+
if (!historical) {
|
|
542
|
+
const tokenizedVault = (await (0, core_1.fetchTokenizedVaults)(vault, options?.headers))?.[0];
|
|
543
|
+
return tokenizedVault?.reported_apy?.apy || 0;
|
|
544
|
+
}
|
|
545
|
+
else {
|
|
546
|
+
function paramBuilder(params) {
|
|
547
|
+
const urlParams = new URLSearchParams();
|
|
548
|
+
if (!params)
|
|
549
|
+
return '';
|
|
550
|
+
if (params.daysAgo)
|
|
551
|
+
urlParams.append('days_ago', params.daysAgo.toString());
|
|
552
|
+
return urlParams.toString();
|
|
553
|
+
}
|
|
554
|
+
const historicalApy = await (0, core_1.fetchAugustPublic)(`${core_1.WEBSERVER_ENDPOINTS.public.tokenizedVault.historicalApy(vault)}${paramBuilder(historical)}`, {
|
|
555
|
+
headers: !!options?.headers
|
|
556
|
+
? options?.headers
|
|
557
|
+
: undefined,
|
|
558
|
+
});
|
|
559
|
+
if (!historicalApy.ok)
|
|
560
|
+
throw new Error('Failed to fetch historical apy.');
|
|
561
|
+
const historicalApyData = (await historicalApy.json());
|
|
562
|
+
return historicalApyData.data.values.forEach((val, index) => {
|
|
563
|
+
const respectiveDate = historicalApyData.data.labels[index];
|
|
564
|
+
return {
|
|
565
|
+
timestamp: new Date(respectiveDate).toISOString(),
|
|
566
|
+
value: val,
|
|
567
|
+
};
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
catch (e) {
|
|
572
|
+
core_1.Logger.log.error('getVaultApy', e, { vault });
|
|
573
|
+
throw new Error(`#getVaultApy::${vault}:${e?.message}`);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
async function getRewardsStakingPositions({ rpcUrl, wallet, coinGeckoKey, }) {
|
|
577
|
+
const REWARDS_CHAIN = 43114;
|
|
578
|
+
const REWARDS_SYMBOL = 'AVAX';
|
|
579
|
+
const REWARDS_DECIMALS = 18;
|
|
580
|
+
const REWARDS_NAME = 'Avalanche';
|
|
581
|
+
const APR_MULTIPLIER = 31536000;
|
|
582
|
+
const UP_AUSD_SYMBOL = 'upAUSD';
|
|
583
|
+
try {
|
|
584
|
+
const provider = (0, core_1.createProvider)(rpcUrl);
|
|
585
|
+
const chainId = await (0, core_1.getChainId)(provider);
|
|
586
|
+
if (chainId !== REWARDS_CHAIN) {
|
|
587
|
+
core_1.Logger.log.warn('getStakingPositions:unsupported_chain', chainId);
|
|
588
|
+
return [];
|
|
589
|
+
}
|
|
590
|
+
if (wallet && !(0, ethers_1.isAddress)(wallet)) {
|
|
591
|
+
core_1.Logger.log.warn('getStakingPositions:invalid_wallet', wallet);
|
|
592
|
+
return [];
|
|
593
|
+
}
|
|
594
|
+
const rewardDistributorAddresses = (0, utils_1.REWARD_DISTRIBUTOR_ADDRESS)(chainId);
|
|
595
|
+
const positions = await Promise.all(rewardDistributorAddresses.map(async (contract, i) => {
|
|
596
|
+
const rewardContract = (0, core_1.createContract)({
|
|
597
|
+
address: contract,
|
|
598
|
+
provider: provider,
|
|
599
|
+
abi: abis_1.ABI_REWARD_DISTRIBUTOR,
|
|
600
|
+
});
|
|
601
|
+
const totalStaked = await rewardContract.totalStaked();
|
|
602
|
+
const rewardsPerSecond = await rewardContract.rewardsPerSecond();
|
|
603
|
+
const stakingTokenCalls = ['decimals', 'symbol', 'name', 'totalSupply'];
|
|
604
|
+
const stakingTokenAddress = (await rewardContract.stakingToken());
|
|
605
|
+
const stakingTokenContract = (0, core_1.createContract)({
|
|
606
|
+
address: stakingTokenAddress,
|
|
607
|
+
provider: provider,
|
|
608
|
+
abi: abis_1.ABI_ERC20,
|
|
609
|
+
});
|
|
610
|
+
const [decimals, symbol, name] = await Promise.all(stakingTokenCalls.map((staking) => stakingTokenContract[staking]()));
|
|
611
|
+
let balance;
|
|
612
|
+
let earned;
|
|
613
|
+
if (wallet) {
|
|
614
|
+
const formattedWallet = (0, ethers_1.getAddress)(wallet);
|
|
615
|
+
balance = await rewardContract.balanceOf(formattedWallet);
|
|
616
|
+
earned = await rewardContract.earned(formattedWallet);
|
|
617
|
+
}
|
|
618
|
+
const rewardTokenPriceInUsd = await (0, core_1.fetchTokenPrice)(REWARDS_SYMBOL, null, coinGeckoKey);
|
|
619
|
+
const normalizedRewardTokenPrice = (0, core_1.toNormalizedBn)(rewardTokenPriceInUsd);
|
|
620
|
+
const normalizedTotalStakedInPool = (0, core_1.toNormalizedBn)(totalStaked, decimals);
|
|
621
|
+
const normalizedRedeemable = (0, core_1.toNormalizedBn)(earned, REWARDS_DECIMALS);
|
|
622
|
+
const normalizedRewardsPerSecond = (0, core_1.toNormalizedBn)(rewardsPerSecond, REWARDS_DECIMALS);
|
|
623
|
+
const normalizedTotalStakedBalance = (0, core_1.toNormalizedBn)(balance, decimals);
|
|
624
|
+
const rewardTokenPriceToMultiply = symbol === UP_AUSD_SYMBOL
|
|
625
|
+
? Number(normalizedRewardTokenPrice?.normalized)
|
|
626
|
+
: 1;
|
|
627
|
+
const STAKED_APR = ((Number(normalizedRewardsPerSecond?.normalized) *
|
|
628
|
+
APR_MULTIPLIER *
|
|
629
|
+
rewardTokenPriceToMultiply) /
|
|
630
|
+
(Number(normalizedTotalStakedInPool?.normalized) * Number(1))) *
|
|
631
|
+
100;
|
|
632
|
+
const MAX_APR = ((Number(normalizedRewardsPerSecond?.normalized) *
|
|
633
|
+
APR_MULTIPLIER *
|
|
634
|
+
rewardTokenPriceToMultiply) /
|
|
635
|
+
(Number(normalizedTotalStakedInPool?.normalized) * Number(1))) *
|
|
636
|
+
100;
|
|
637
|
+
const activePosition = {
|
|
638
|
+
id: String(i),
|
|
639
|
+
rewardToken: {
|
|
640
|
+
decimals: REWARDS_DECIMALS,
|
|
641
|
+
symbol: REWARDS_SYMBOL,
|
|
642
|
+
address: ethers_1.ZeroAddress,
|
|
643
|
+
chain: REWARDS_CHAIN,
|
|
644
|
+
redeemable: normalizedRedeemable,
|
|
645
|
+
usd: normalizedRewardTokenPrice,
|
|
646
|
+
name: REWARDS_NAME,
|
|
647
|
+
},
|
|
648
|
+
stakingToken: {
|
|
649
|
+
decimals: Number(decimals),
|
|
650
|
+
symbol: symbol,
|
|
651
|
+
address: stakingTokenAddress,
|
|
652
|
+
chain: REWARDS_CHAIN,
|
|
653
|
+
totalStaked: normalizedTotalStakedBalance,
|
|
654
|
+
usd: normalizedRewardTokenPrice,
|
|
655
|
+
totalSupply: normalizedTotalStakedInPool,
|
|
656
|
+
name: name,
|
|
657
|
+
},
|
|
658
|
+
rewardDistributor: contract,
|
|
659
|
+
rewardPerSecond: (0, core_1.toNormalizedBn)(rewardsPerSecond),
|
|
660
|
+
apy: STAKED_APR,
|
|
661
|
+
maxApy: MAX_APR,
|
|
662
|
+
chainId: REWARDS_CHAIN,
|
|
663
|
+
};
|
|
664
|
+
return activePosition;
|
|
665
|
+
}));
|
|
666
|
+
return positions;
|
|
667
|
+
}
|
|
668
|
+
catch (e) {
|
|
669
|
+
core_1.Logger.log.error('getStakingPositions', e, { wallet });
|
|
670
|
+
return [];
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
async function getVaultTvl({ vault, options, historical, }) {
|
|
674
|
+
try {
|
|
675
|
+
if (!vault)
|
|
676
|
+
throw new Error('Vault input parameter is undefined.');
|
|
677
|
+
const _vaultExists = (await (0, core_1.fetchTokenizedVaults)(vault))?.[0];
|
|
678
|
+
if (!_vaultExists) {
|
|
679
|
+
throw new Error(`Vault ${vault} not found in backend`);
|
|
680
|
+
}
|
|
681
|
+
if (!historical) {
|
|
682
|
+
const vaultRes = await getVault({
|
|
683
|
+
vault,
|
|
684
|
+
options,
|
|
685
|
+
loans: false,
|
|
686
|
+
allocations: false,
|
|
687
|
+
});
|
|
688
|
+
return vaultRes.totalSupply;
|
|
689
|
+
}
|
|
690
|
+
else {
|
|
691
|
+
if (typeof historical !== 'undefined' && !historical.order)
|
|
692
|
+
historical.order = 'desc';
|
|
693
|
+
if (typeof historical !== 'undefined' && !historical.interval)
|
|
694
|
+
historical.interval = 'days';
|
|
695
|
+
const provider = (0, core_1.createProvider)(options.rpcUrl);
|
|
696
|
+
const vaultContract = (0, core_1.createContract)({
|
|
697
|
+
address: vault,
|
|
698
|
+
abi: abis_1.ABI_LENDING_POOLS,
|
|
699
|
+
provider,
|
|
700
|
+
});
|
|
701
|
+
const vaultDecimals = await (0, core_1.getDecimals)(provider, vault);
|
|
702
|
+
const dater = new EthDater(provider);
|
|
703
|
+
const startDate = historical.daysAgo
|
|
704
|
+
? new Date(Date.now() - historical.daysAgo * 24 * 60 * 60 * 1000)
|
|
705
|
+
: new Date();
|
|
706
|
+
const blocks = await dater.getEvery(historical.interval, startDate.toUTCString(), new Date().toUTCString());
|
|
707
|
+
const orderedBlocks = historical.order === 'desc' ? (0, core_1.orderObjArrByDate)(blocks) : blocks;
|
|
708
|
+
const minAbi = ['function totalAssets() view returns (uint256)'];
|
|
709
|
+
const totalAssetsHistorical = await Promise.all(orderedBlocks?.map(async ({ block, date }) => {
|
|
710
|
+
const totalAssetsAtBlock = await provider.call({
|
|
711
|
+
to: vaultContract,
|
|
712
|
+
data: new ethers_1.Interface(minAbi).encodeFunctionData('totalAssets', []),
|
|
713
|
+
blockTag: block,
|
|
714
|
+
});
|
|
715
|
+
if (totalAssetsAtBlock === '0x')
|
|
716
|
+
return {
|
|
717
|
+
timestamp: (0, core_1.dateToUnix)(new Date(date)),
|
|
718
|
+
value: (0, core_1.toNormalizedBn)(0, 0),
|
|
719
|
+
};
|
|
720
|
+
const readableTotalAssetsAtBlock = new ethers_1.Interface(minAbi).decodeFunctionResult('totalAssets', totalAssetsAtBlock);
|
|
721
|
+
return {
|
|
722
|
+
timestamp: (0, core_1.dateToUnix)(new Date(date)),
|
|
723
|
+
value: (0, core_1.toNormalizedBn)(readableTotalAssetsAtBlock?.[0], vaultDecimals),
|
|
724
|
+
};
|
|
725
|
+
}));
|
|
726
|
+
core_1.Logger.log.info('getVaultTvl:historical', totalAssetsHistorical);
|
|
727
|
+
return totalAssetsHistorical;
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
catch (e) {
|
|
731
|
+
core_1.Logger.log.error('getVaultTvl', e, { vault });
|
|
732
|
+
throw new Error(`Failed to fetch TVL for ${vault}: ${e instanceof Error ? e.message : 'Unknown error'}`);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
//# sourceMappingURL=getters.js.map
|