diff --git a/README.md b/README.md index 37853d74..47a77eea 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,167 @@ +

+ StakeWise Logo +

+ # StakeWise V3 SDK +The official SDK for seamlessly pulling data from the StakeWise platform. It is a wrapper over graphql queries and contract queries. Also included in the package are our basic contracts, which will be connected to your provider if needed + +[![Unit Tests](https://img.shields.io/badge/Unit%20Tests-passing-brightgreen)](LINK_TO_YOUR_TEST_RESULTS) +[![Linting](https://img.shields.io/badge/Linting-passing-brightgreen)](LINK_TO_YOUR_LINTING_RESULTS) +[![Latest Version](https://img.shields.io/npm/v/YOUR_NPM_PACKAGE_NAME/latest)](https://www.npmjs.com/package/YOUR_NPM_PACKAGE_NAME) +[![Bundle Size](https://img.shields.io/bundlephobia/minzip/YOUR_NPM_PACKAGE_NAME)](https://www.npmjs.com/package/YOUR_NPM_PACKAGE_NAME) + + + +## Table of Contents +- [Requirements](#requirements) +- [Usage](#usage) +- [API](#api) + +--- +## Requirements +Installed ethers 6.6.7+ is required This package is not included in the library (peerDependencies are used), but must be installed in your application. In the future we will try to update the ethers library with the latest version in a timely manner. If you are using ethers 5 or less, there are 2 solutions: +1. Upgrade to ethers 6, it is much more convenient. +2. You can install ethers 6 version separately, but it will expand your bandle: + +`npm i ethers-6@npm:ethers@6.6.7` + +Now you have ethers of your version and ethers-6. Now we can swap the use of imports within our library. If you are using webpack, you can implement it this way: + +```typescript +webpackConfig.plugins.push( + new webpack.NormalModuleReplacementPlugin( + /ethers$/m, (resource) => { + const isStakeWise = /@stakewise\/v3-sdk/.test(resource.context) + + if (isStakeWise) { + resource.request = resource.request.replace(/ethers/, 'ethers-6') + } + } + ) +) +``` +You can do something similar for other builders as well + +## Usage + +Install SDK +```bash +npm i @stakewise/v3-sdk +``` + +Create SDK instance + +```typescript +import { StakeWiseSDK, Network } from '@stakewise/v3-sdk' + +const sdk = new StakeWiseSDK({ network: Network.Mainnet }) + +``` +#### SDK Constructor Arguments: + +| Name | Type | Type | Description | +|------|------|-------------|---------| +| network | `Network` | **Require** | Chain id | +| provider | `any` | **Optional** | You can provide your implementation of the provender for ethers | +| endpoints.subgraph | `string` | **Optional** | stakewise sbugraph url | +| endpoints.web3 | `number` | **Require** | Your url for connect to blockchian | +| endpoints.api | `string` | **Optional** | stakewise backend url | + +## API + +### `sdk.requests.getAllocatorActions()` + +#### Description: + +Get a list of interactions with the vault. + +#### Arguments: + +| Name | Type | Type | Description | +|------|------|-------------|---------| +| vaultAddress | `string` | **Require** | - | +| userAddress | `string` | **Optional** | If a user address is specified, the query will look for events for that address and the vault address only | +| types | `AllocatorActionType` | **Require** | Event types can be found in `enum AllocatorActionType` which you can import from the library | +| limit | `number` | **Require** | To implement pagination | +| skip | `number` | **Require** | To implement pagination | + +#### Returns: + +```ts +type Output = Array<{ + actionType: AllocatorActionType + createdAt: number + assets: string + link: string + id: string +}> +``` + +| Name | Description | +|------|-------------| +| `id` | Event identifier | +| `assets` | Transaction amount | +| `createdAt` | Transaction date | +| `actionType` | Type of action | +| `link` | Transaction link (etherscan/blockscout) | + +#### Example: + +```ts +import { AllocatorActionType } from '@stakewise/v3-sdk' + +sdk.requests.getAllocatorActions({ + skip: 0, + limit: 20, + userAddress: '0x...', + vaultAddress: '0x...', + types: [ + AllocatorActionType.Redeemed, + AllocatorActionType.Deposited, + AllocatorActionType.VaultCreated, + AllocatorActionType.ExitedAssetsClaimed, + ], +}) +``` + +### `sdk.requests.getDaySnapshots()` + +#### Description: + +TVL and APY snapshots for the vault. With the help of this data it is possible to build a chart. + +#### Arguments: + +| Name | Type | Type | Description | +|------|------|-------------|---------| +| unixStartOfDay | `number` | **Require** | Time to start | +| vaultAddress | `string` | **Require** | - | + +#### Returns: + +```ts +type DaySnapshot = { + APY: number + TVL: string +} + +type Output = { + days: Record + first: DaySnapshot +} +``` + +| Name | Description | +|------|-------------| +| `days` | The result of the query on your parameters, is returned as an object where the keys are timestamps | +| `first` | We always send the very first saved snapshot regardless of the request parameters, this helps for rendering the chart | + +#### Example: -### Before start -Call `npm run prepare`. It will prepare all packages for -further development: -- Install common dependencies -- Install dependencies for all packages -- WIP: Generate types for abis +```ts +sdk.requests.getDaySnapshots({ + vaultAddress: '0x...', + unixStartOfDay: 1695730032793, +}) +``` diff --git a/jest.config.ts b/jest.config.ts index 7d46cbd7..82140b57 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -22,11 +22,13 @@ const config: Config = { '^graphql/(.*)': '/graphql/$1', '^graphql$': '/graphql/index.ts', - '^requests/(.*)': '/requests/$1', - '^requests$': '/requests/index.ts', - '^requests/methods': '/requests/methods/indes.ts', + '^methods/(.*)': '/methods/$1', + '^methods$': '/methods/index.ts', + '^helpers/(.*)': '/helpers/$1', '^helpers$': '/helpers/index.ts', + + '^contracts/(.*)': '/contracts/$1', '^contracts$': '/contracts/index.ts', }, } diff --git a/package-lock.json b/package-lock.json index 3f524e7a..f33c6ff2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@stakewise/v3-sdk", - "version": "1.0.0", + "version": "1.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@stakewise/v3-sdk", - "version": "1.0.0", + "version": "1.0.1", "license": "AGPL-3.0-only", "dependencies": { "bignumber.js": "9.1.2" @@ -22582,9 +22582,9 @@ "dev": true }, "node_modules/graphql": { - "version": "16.8.0", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-16.8.0.tgz", - "integrity": "sha512-0oKGaR+y3qcS5mCu1vb7KG+a89vjn06C7Ihq/dDl3jA+A8B3TKomvi3CiEcVLJQGalbu8F52LxkOym7U5sSfbg==", + "version": "16.8.1", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-16.8.1.tgz", + "integrity": "sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==", "dev": true, "peer": true, "engines": { diff --git a/package.json b/package.json index c9597af9..aed9568c 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,12 @@ { - "version": "1.0.0", + "version": "1.0.1", "sideEffects": false, "main": "dist/index.js", + "module": "dist/index.js", "types": "dist/index.d.ts", "license": "AGPL-3.0-only", "name": "@stakewise/v3-sdk", "typings": "dist/index.d.ts", - "module": "dist/index.esm.js", - "browser": "dist/index.umd.js", "description": "StakeWise v3 SDK", "homepage": "https://app.stakewise.io", "scripts": { diff --git a/rollup.config.ts b/rollup.config.ts index a5db3c80..f5e8883b 100644 --- a/rollup.config.ts +++ b/rollup.config.ts @@ -15,21 +15,6 @@ const config: RollupOptions[] = [ { input: 'src/index.ts', output: [ - { - name: pkg.name, - file: pkg.browser, - sourcemap: true, - format: 'umd', - globals: { - ethers: 'ethers', - }, - }, - { - name: pkg.name, - file: pkg.main, - sourcemap: true, - format: 'cjs', - }, { name: pkg.name, file: pkg.module, diff --git a/src/StakeWiseSDK.ts b/src/StakeWiseSDK.ts index 3a41bd15..b99f271c 100644 --- a/src/StakeWiseSDK.ts +++ b/src/StakeWiseSDK.ts @@ -1,23 +1,61 @@ +import methods from 'methods' import { JsonRpcProvider } from 'ethers' -import { configs, apiUrls } from 'helpers' -import { createContracts } from 'contracts' -import { createRequests, getHealthFactor, getRewardsPerYear } from 'requests' +import { configs, apiUrls, Network } from 'helpers' +import { createContracts, createRatesContracts, vaultMulticall } from 'contracts' +type VaultMulticallInput = Pick[0], 'request' | 'userAddress' | 'vaultAddress'> + class StakeWiseSDK { - readonly requests: StakeWise.Requests + readonly utils: StakeWise.Utils + readonly options: StakeWise.Options + readonly vault: StakeWise.VaultMethods readonly contracts: StakeWise.Contracts + readonly osToken: StakeWise.OsTokenMethods + readonly rateContracts: StakeWise.RateContracts constructor(options: StakeWise.Options) { const config = configs[options.network] const provider = options.provider || new JsonRpcProvider(apiUrls.getWeb3Url(options)) + this.options = options + this.rateContracts = this.#initRateContracts() this.contracts = createContracts({ provider, config }) - this.requests = createRequests({ options, contracts: this.contracts }) + + const argsForMethods = { options, contracts: this.contracts } + + this.utils = methods.createUtils(argsForMethods) + this.vault = methods.createVaultMethods(argsForMethods) + this.osToken = methods.createOsTokenMethods(argsForMethods) + } + + #initRateContracts() { + // Contracts to get the exchange prices for tokens are on the mainnet + const mainnetApiUrl = configs[Network.Mainnet].network.url + const provider = new JsonRpcProvider(mainnetApiUrl) + + return createRatesContracts(provider) } - getHealthFactor = getHealthFactor - getRewardsPerYear = getRewardsPerYear + // Temporary method + vaultMulticall({ userAddress, vaultAddress, request }: VaultMulticallInput) { + return vaultMulticall({ + vaultContract: this.contracts.helpers.createVaultContract(vaultAddress), + keeperContract: this.contracts.base.keeper, + options: this.options, + vaultAddress, + userAddress, + request, + }) + } + + get network() { + return this.options.network + } + + get provider() { + return this.options.provider + } } diff --git a/src/contracts/abis/VestingEscrowAbi.json b/src/contracts/abis/VestingEscrowAbi.json deleted file mode 100644 index d43b01c9..00000000 --- a/src/contracts/abis/VestingEscrowAbi.json +++ /dev/null @@ -1 +0,0 @@ -[{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claimedAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"endTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"startTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"vestedAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}] diff --git a/src/contracts/abis/VestingEscrowFactoryAbi.json b/src/contracts/abis/VestingEscrowFactoryAbi.json deleted file mode 100644 index df3894e5..00000000 --- a/src/contracts/abis/VestingEscrowFactoryAbi.json +++ /dev/null @@ -1 +0,0 @@ -[{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"admin","type":"address"},{"indexed":true,"internalType":"address","name":"token","type":"address"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"address","name":"beneficiary","type":"address"},{"indexed":false,"internalType":"address","name":"escrow","type":"address"},{"indexed":false,"internalType":"uint256","name":"totalAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"startTime","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"endTime","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"cliffLength","type":"uint256"}],"name":"VestingEscrowCreated","type":"event"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"total","type":"uint256"}],"stateMutability":"view","type":"function"}] diff --git a/src/contracts/abis/index.ts b/src/contracts/abis/index.ts index 45d6191f..6545ddf2 100644 --- a/src/contracts/abis/index.ts +++ b/src/contracts/abis/index.ts @@ -1,11 +1,9 @@ import RewardSplitterFactoryAbi from './RewardSplitterFactoryAbi.json' -import VestingEscrowFactoryAbi from './VestingEscrowFactoryAbi.json' import Erc20PrivateVaultAbi from './Erc20PrivateVaultAbi.json' import MintTokenConfigAbi from './MintTokenConfigAbi.json' import VaultsRegistryAbi from './VaultsRegistryAbi.json' import RewardSplitterAbi from './RewardSplitterAbi.json' import V2RewardTokenAbi from './V2RewardTokenAbi.json' -import VestingEscrowAbi from './VestingEscrowAbi.json' import PrivateVaultAbi from './PrivateVaultAbi.json' import VaultFactoryAbi from './VaultFactoryAbi.json' import PriceOracleAbi from './PriceOracleAbi.json' @@ -22,13 +20,11 @@ import Erc20Abi from './Erc20Abi.json' export { RewardSplitterFactoryAbi, - VestingEscrowFactoryAbi, Erc20PrivateVaultAbi, MintTokenConfigAbi, VaultsRegistryAbi, RewardSplitterAbi, V2RewardTokenAbi, - VestingEscrowAbi, PrivateVaultAbi, VaultFactoryAbi, PriceOracleAbi, diff --git a/src/contracts/createContracts.ts b/src/contracts/createContracts.ts index f59bbb35..8126adda 100644 --- a/src/contracts/createContracts.ts +++ b/src/contracts/createContracts.ts @@ -11,24 +11,20 @@ import { PriceOracleAbi, PrivateVaultAbi, VaultFactoryAbi, - VestingEscrowAbi, V2RewardTokenAbi, VaultsRegistryAbi, RewardSplitterAbi, MintTokenConfigAbi, Erc20PrivateVaultAbi, - VestingEscrowFactoryAbi, RewardSplitterFactoryAbi, } from './abis' import type { RewardSplitterFactoryAbi as RewardSplitterFactoryType, - VestingEscrowFactoryAbi as VestingEscrowFactoryType, MintTokenConfigAbi as MintTokenConfigType, VaultsRegistryAbi as VaultsRegistryType, RewardSplitterAbi as RewardSplitterType, V2RewardTokenAbi as V2RewardTokenType, - VestingEscrowAbi as VestingEscrowType, VaultFactoryAbi as VaultFactoryType, PrivateVaultAbi as PrivateVaultType, Erc20VaultAbi as Erc20VaultAbiType, @@ -57,12 +53,6 @@ const getMulticall = (provider: Provider, config: StakeWise.Config) => createCon provider ) -const getVestingEscrowFactory = (provider: Provider, config: StakeWise.Config) => createContract( - config.addresses.factories.vestingEscrow, - VestingEscrowFactoryAbi, - provider -) - const getVaultFactory = (provider: Provider, address: string) => createContract( address, VaultFactoryAbi, @@ -128,7 +118,6 @@ export const createContracts = (input: CreateContractsInput) => { createErc20: (address: string) => createContract(address, Erc20Abi, provider), createVaultContract: (address: string) => createContract(address, VaultAbi, provider), createErc20VaultContract: (address: string) => createContract(address, Erc20VaultAbi, provider), - createVestingEscrowDirect: (address: string) => createContract(address, VestingEscrowAbi, provider), createPrivateVaultContract: (address: string) => createContract(address, PrivateVaultAbi, provider), createRewardSplitterContract: (address: string) => createContract(address, RewardSplitterAbi, provider), createErc20PrivateVaultContract: (address: string) => createContract(address, Erc20PrivateVaultAbi, provider), @@ -146,7 +135,6 @@ export const createContracts = (input: CreateContractsInput) => { V2RewardToken: getV2RewardToken(provider, config), }, factories: { - vestingEscrow: getVestingEscrowFactory(provider, config), vault: getVaultFactory(provider, config.addresses.factories.vault), erc20Vault: getVaultFactory(provider, config.addresses.factories.erc20Vault), privateVault: getVaultFactory(provider, config.addresses.factories.privateVault), diff --git a/src/contracts/createRatesContracts.ts b/src/contracts/createRatesContracts.ts new file mode 100644 index 00000000..d081fe9c --- /dev/null +++ b/src/contracts/createRatesContracts.ts @@ -0,0 +1,49 @@ +import type { Provider } from 'ethers' +import { Contract } from 'ethers' + +import { UsdRateAbi, EthRateAbi } from './abis' +import type { UsdRateAbi as UsdRateType, EthRateAbi as EthRateType } from './types' + + +const createUsdRateContract = (address: string, provider: Provider) => { + const contract = new Contract(address, UsdRateAbi, provider) as unknown + + return contract as UsdRateType +} + +const createEthRateContract = (address: string, provider: Provider) => { + const contract = new Contract(address, EthRateAbi, provider) as unknown + + return contract as EthRateType +} + +type CreateRatesContractsOutput = { + USD: { + ETH: StakeWise.ABI.UsdRate + EUR: StakeWise.ABI.UsdRate + GBP: StakeWise.ABI.UsdRate + } + ETH: { + GNO: StakeWise.ABI.EthRate + xDAI: StakeWise.ABI.EthRate + } +} + +const createRatesContracts = (provider: Provider): CreateRatesContractsOutput => { + const ETH = createUsdRateContract('0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419', provider) + const EUR = createUsdRateContract('0xb49f677943BC038e9857d61E7d053CaA2C1734C1', provider) + const GBP = createUsdRateContract('0x5c0Ab2d9b5a7ed9f470386e82BB36A3613cDd4b5', provider) + + const GNO = createEthRateContract('0xA614953dF476577E90dcf4e3428960e221EA4727', provider) + const xDAI = createEthRateContract('0x773616E4d11A78F511299002da57A0a94577F1f4', provider) + + const result = { + USD: { ETH, EUR, GBP }, + ETH: { GNO, xDAI }, + } + + return result +} + + +export default createRatesContracts diff --git a/src/contracts/index.ts b/src/contracts/index.ts index b07528fc..4d374776 100644 --- a/src/contracts/index.ts +++ b/src/contracts/index.ts @@ -1,3 +1,4 @@ +export { default as createRatesContracts } from './createRatesContracts' export { default as createContracts } from './createContracts' export { default as vaultMulticall } from './vaultMulticall' export { default as multicall } from './multicall' diff --git a/src/contracts/vaultMulticall.ts b/src/contracts/vaultMulticall.ts index 7c44cba5..2a40bf34 100644 --- a/src/contracts/vaultMulticall.ts +++ b/src/contracts/vaultMulticall.ts @@ -1,4 +1,4 @@ -import getHarvestParams from 'requests/methods/getHarvestParams' +import getHarvestParams from 'methods/vault/requests/getHarvestParams' import { VoidSigner, JsonRpcProvider } from 'ethers' import { configs } from 'helpers' diff --git a/src/graphql/subgraph/index.ts b/src/graphql/subgraph/index.ts index 948f0633..1ba85aa2 100644 --- a/src/graphql/subgraph/index.ts +++ b/src/graphql/subgraph/index.ts @@ -1,7 +1,6 @@ export * as vault from './vault' export * as osToken from './osToken' export * as exitQueue from './exitQueue' -export * as vaultsCount from './vaultsCount' export * as daySnapshots from './daySnapshots' export * as transactions from './transactions' export * as allocatorActions from './allocatorActions' diff --git a/src/graphql/subgraph/vault/vaultQuery.graphql b/src/graphql/subgraph/vault/vaultQuery.graphql index 5e465333..940fab88 100644 --- a/src/graphql/subgraph/vault/vaultQuery.graphql +++ b/src/graphql/subgraph/vault/vaultQuery.graphql @@ -3,7 +3,6 @@ query Vault($address: ID!) { address: id performance: score admin - proof isErc20 imageUrl verified @@ -20,14 +19,8 @@ query Vault($address: ID!) { whitelister keysManager tokenSymbol - rewardsRoot - proofReward - totalShares feeRecipient - queuedShares validatorsRoot - unclaimedAssets - metadataIpfsHash avgRewardPerAsset } privateVaultAccounts( diff --git a/src/graphql/subgraph/vaultsCount/index.ts b/src/graphql/subgraph/vaultsCount/index.ts deleted file mode 100644 index 4c9daf47..00000000 --- a/src/graphql/subgraph/vaultsCount/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { fetchVaultsCountQuery } from './vaultsCountQuery.graphql' -export type { VaultsCountQueryPayload, VaultsCountQueryVariables } from './vaultsCountQuery.graphql' diff --git a/src/graphql/subgraph/vaultsCount/vaultsCountQuery.graphql b/src/graphql/subgraph/vaultsCount/vaultsCountQuery.graphql deleted file mode 100644 index 5a03557c..00000000 --- a/src/graphql/subgraph/vaultsCount/vaultsCountQuery.graphql +++ /dev/null @@ -1,13 +0,0 @@ -query VaultsCount($address: Bytes) { - all: network(id: 0) { - vaultsTotal - } - deposits: vaults(where: { - or: [ - { allocators_: { address: $address } } - { exitRequests_: { owner: $address } } - ] - }) { - id - } -} diff --git a/src/helpers/configs/gnosis.ts b/src/helpers/configs/gnosis.ts index 3a46f735..d4e28c56 100644 --- a/src/helpers/configs/gnosis.ts +++ b/src/helpers/configs/gnosis.ts @@ -1,3 +1,5 @@ +import { ZeroAddress } from 'ethers' + import tokens from '../constants/tokens' @@ -26,25 +28,25 @@ export default { addresses: { tokens: { swise: '0xfdA94F056346d2320d4B5E468D6Ad099b2277746', - mintToken: '', + mintToken: ZeroAddress, v2RewardToken: '0x6aC78efae880282396a335CA2F79863A1e6831D4', v2StakedToken: '0xA4eF9Da5BA71Cc0D2e5E877a910A37eC43420445', }, base: { - keeper: '', - priceOracle: '', - vaultsRegistry: '', - sharedMevEscrow: '', - mintTokenConfig: '', + keeper: ZeroAddress, + priceOracle: ZeroAddress, + vaultsRegistry: ZeroAddress, + sharedMevEscrow: ZeroAddress, + mintTokenConfig: ZeroAddress, multicall: '0xb5b692a88BDFc81ca69dcB1d924f59f0413A602a', - rewardSplitterFactory: '', + rewardSplitterFactory: ZeroAddress, }, factories: { - vault: '', - erc20Vault: '', - privateVault: '', - erc20PrivateVault: '', - vestingEscrow: '', + vault: ZeroAddress, + erc20Vault: ZeroAddress, + privateVault: ZeroAddress, + vestingEscrow: ZeroAddress, + erc20PrivateVault: ZeroAddress, }, }, tokens: { diff --git a/src/helpers/configs/mainnet.ts b/src/helpers/configs/mainnet.ts index 3b06c0c7..987233dc 100644 --- a/src/helpers/configs/mainnet.ts +++ b/src/helpers/configs/mainnet.ts @@ -1,3 +1,5 @@ +import { ZeroAddress } from 'ethers' + import tokens from '../constants/tokens' @@ -26,24 +28,24 @@ export default { addresses: { tokens: { swise: '0x48C3399719B582dD63eB5AADf12A40B4C3f52FA2', - mintToken: '', + mintToken: ZeroAddress, v2RewardToken: '0x20BC832ca081b91433ff6c17f85701B6e92486c5', v2StakedToken: '0xFe2e637202056d30016725477c5da089Ab0A043A', }, base: { - keeper: '', - priceOracle: '', - vaultsRegistry: '', - sharedMevEscrow: '', - mintTokenConfig: '', + keeper: ZeroAddress, + priceOracle: ZeroAddress, + vaultsRegistry: ZeroAddress, + sharedMevEscrow: ZeroAddress, + mintTokenConfig: ZeroAddress, multicall: '0xeefBa1e63905eF1D7ACbA5a8513c70307C1cE441', - rewardSplitterFactory: '', + rewardSplitterFactory: ZeroAddress, }, factories: { - vault: '', - erc20Vault: '', - privateVault: '', - erc20PrivateVault: '', + vault: ZeroAddress, + erc20Vault: ZeroAddress, + privateVault: ZeroAddress, + erc20PrivateVault: ZeroAddress, vestingEscrow: '0x7B910cc3D4B42FEFF056218bD56d7700E4ea7dD5', }, }, diff --git a/src/helpers/enums.ts b/src/helpers/enums.ts index a6c2eb3f..b0538147 100644 --- a/src/helpers/enums.ts +++ b/src/helpers/enums.ts @@ -6,6 +6,7 @@ export enum Network { export enum AllocatorActionType { Redeemed = 'Redeemed', + Migrated = 'Migrated', Deposited = 'Deposited', VaultCreated = 'VaultCreated', ExitedAssetsClaimed = 'ExitedAssetsClaimed', diff --git a/src/methods/index.ts b/src/methods/index.ts new file mode 100644 index 00000000..8d3c70b6 --- /dev/null +++ b/src/methods/index.ts @@ -0,0 +1,40 @@ +import * as osToken from './osToken' +import * as vault from './vault' +import * as utils from './utils' + + +type Methods = typeof utils | typeof osToken | typeof vault + +type CheckArgs> = [ keyof Obj ] extends [ never ] ? [] : [ Obj ] + +type ModifyRequests> = { + [K in keyof T]: (...values: CheckArgs[0], 'options' | 'contracts'>>) => ReturnType +} + +type CreateMethodsParams = { + options: StakeWise.Options + contracts: StakeWise.Contracts +} + +type CreateMethodsOutput = ModifyRequests + +const createRequests = (methods: T, params: CreateMethodsParams): CreateMethodsOutput => ( + Object.keys(methods).reduce((acc, method) => { + const fn = methods[method as keyof typeof methods] as (values: unknown) => unknown + + return { + ...acc, + [method]: (values: unknown) => fn({ ...(values || {}), ...params }), + } + }, {} as CreateMethodsOutput) +) + + +const methods = { + createUtils: (params: CreateMethodsParams) => createRequests(utils, params), + createVaultMethods: (params: CreateMethodsParams) => createRequests(vault, params), + createOsTokenMethods: (params: CreateMethodsParams) => createRequests(osToken, params), +} + + +export default methods diff --git a/src/requests/methods/getBurnAmount.spec.ts b/src/methods/osToken/getBurnAmount.spec.ts similarity index 62% rename from src/requests/methods/getBurnAmount.spec.ts rename to src/methods/osToken/getBurnAmount.spec.ts index 981d8358..dc5f740c 100644 --- a/src/requests/methods/getBurnAmount.spec.ts +++ b/src/methods/osToken/getBurnAmount.spec.ts @@ -18,6 +18,7 @@ describe('getBurnAmount', () => { it('should calculate the correct burn amount', async () => { const mockedShares = 100n + const contracts = { tokens: { mintToken: { @@ -30,15 +31,13 @@ describe('getBurnAmount', () => { ltvPercent: 8000n, mintedAssets: 100n, stakedAssets: 1000n, - newStakedAssets: 900n, + newStakedAssets: 1900n, contracts: contracts as any, } - const expectedBurnAmount = (input.stakedAssets - input.newStakedAssets) * input.ltvPercent / 10_000n - const result = await getBurnAmount(input) - expect(result).toBe(expectedBurnAmount > input.mintedAssets ? input.mintedAssets : expectedBurnAmount) + expect(result).toBe(100n) }) it('should return 0 if assetsToBurn is less than or equal to 0', async () => { @@ -62,27 +61,4 @@ describe('getBurnAmount', () => { expect(result).toBe(0n) }) - - it('should return mintedAssets if assetsToBurn is greater than mintedAssets', async () => { - const mockedShares = 100n - const contracts = { - tokens: { - mintToken: { - convertToShares: jest.fn().mockResolvedValue(mockedShares), - }, - }, - } - - const input = { - ltvPercent: 8000n, - mintedAssets: 50n, - stakedAssets: 1000n, - newStakedAssets: 500n, - contracts: contracts as any, - } - - const result = await getBurnAmount(input) - - expect(result).toBe(input.mintedAssets) - }) }) diff --git a/src/requests/methods/getBurnAmount.ts b/src/methods/osToken/getBurnAmount.ts similarity index 96% rename from src/requests/methods/getBurnAmount.ts rename to src/methods/osToken/getBurnAmount.ts index 4b0a11ae..b2df4d55 100644 --- a/src/requests/methods/getBurnAmount.ts +++ b/src/methods/osToken/getBurnAmount.ts @@ -25,7 +25,7 @@ const getBurnAmount = async (values: GetBurnAmountInput) => { const sharesToBurn = await contracts.tokens.mintToken.convertToShares(assetsResult) - return sharesToBurn + return sharesToBurn || 0n } return 0n diff --git a/src/requests/methods/getOsTokenData.ts b/src/methods/osToken/getOsTokenData.ts similarity index 100% rename from src/requests/methods/getOsTokenData.ts rename to src/methods/osToken/getOsTokenData.ts diff --git a/src/requests/helpers/getHealthFactor.spec.ts b/src/methods/osToken/helpers/getHealthFactor.spec.ts similarity index 100% rename from src/requests/helpers/getHealthFactor.spec.ts rename to src/methods/osToken/helpers/getHealthFactor.spec.ts diff --git a/src/requests/helpers/getHealthFactor.ts b/src/methods/osToken/helpers/getHealthFactor.ts similarity index 100% rename from src/requests/helpers/getHealthFactor.ts rename to src/methods/osToken/helpers/getHealthFactor.ts diff --git a/src/methods/osToken/index.ts b/src/methods/osToken/index.ts new file mode 100644 index 00000000..5d313fba --- /dev/null +++ b/src/methods/osToken/index.ts @@ -0,0 +1,5 @@ +export { default as getBaseData } from './getOsTokenData' +export { default as getBurnAmount } from './getBurnAmount' +export { default as getAPY } from './requests/getOsTokenAPY' +export { default as getPosition } from './requests/getOsTokenPosition' +export { default as getHealthFactor } from './helpers/getHealthFactor' diff --git a/src/requests/methods/getMaxMintShares.spec.ts b/src/methods/osToken/requests/getMaxMintShares.spec.ts similarity index 100% rename from src/requests/methods/getMaxMintShares.spec.ts rename to src/methods/osToken/requests/getMaxMintShares.spec.ts diff --git a/src/requests/methods/getMaxMintShares.ts b/src/methods/osToken/requests/getMaxMintShares.ts similarity index 100% rename from src/requests/methods/getMaxMintShares.ts rename to src/methods/osToken/requests/getMaxMintShares.ts diff --git a/src/requests/methods/getOsTokenAPY/calculateAPY.ts b/src/methods/osToken/requests/getOsTokenAPY/calculateAPY.ts similarity index 100% rename from src/requests/methods/getOsTokenAPY/calculateAPY.ts rename to src/methods/osToken/requests/getOsTokenAPY/calculateAPY.ts diff --git a/src/requests/methods/getOsTokenAPY/fetchOsTokenSnapshots.ts b/src/methods/osToken/requests/getOsTokenAPY/fetchOsTokenSnapshots.ts similarity index 100% rename from src/requests/methods/getOsTokenAPY/fetchOsTokenSnapshots.ts rename to src/methods/osToken/requests/getOsTokenAPY/fetchOsTokenSnapshots.ts diff --git a/src/requests/methods/getOsTokenAPY/index.ts b/src/methods/osToken/requests/getOsTokenAPY/index.ts similarity index 100% rename from src/requests/methods/getOsTokenAPY/index.ts rename to src/methods/osToken/requests/getOsTokenAPY/index.ts diff --git a/src/requests/methods/getOsTokenPosition/getOsTokenPositionShares.ts b/src/methods/osToken/requests/getOsTokenPosition/getOsTokenPositionShares.ts similarity index 100% rename from src/requests/methods/getOsTokenPosition/getOsTokenPositionShares.ts rename to src/methods/osToken/requests/getOsTokenPosition/getOsTokenPositionShares.ts diff --git a/src/requests/methods/getOsTokenPosition/index.ts b/src/methods/osToken/requests/getOsTokenPosition/index.ts similarity index 100% rename from src/requests/methods/getOsTokenPosition/index.ts rename to src/methods/osToken/requests/getOsTokenPosition/index.ts diff --git a/src/requests/helpers/getRewardsPerYear.spec.ts b/src/methods/utils/getRewardsPerYear.spec.ts similarity index 95% rename from src/requests/helpers/getRewardsPerYear.spec.ts rename to src/methods/utils/getRewardsPerYear.spec.ts index f0482203..598b6dda 100644 --- a/src/requests/helpers/getRewardsPerYear.spec.ts +++ b/src/methods/utils/getRewardsPerYear.spec.ts @@ -22,9 +22,6 @@ describe('getRewardsPerYear', () => { const result = await getRewardsPerYear(input) - console.log('result', result) - console.log('expectedReward', expectedReward) - expect(result).toBe(expectedReward) }) diff --git a/src/requests/helpers/getRewardsPerYear.ts b/src/methods/utils/getRewardsPerYear.ts similarity index 100% rename from src/requests/helpers/getRewardsPerYear.ts rename to src/methods/utils/getRewardsPerYear.ts diff --git a/src/requests/methods/getSwiseUsdPrice.ts b/src/methods/utils/getSwiseUsdPrice.ts similarity index 100% rename from src/requests/methods/getSwiseUsdPrice.ts rename to src/methods/utils/getSwiseUsdPrice.ts diff --git a/src/requests/helpers/index.ts b/src/methods/utils/index.ts similarity index 50% rename from src/requests/helpers/index.ts rename to src/methods/utils/index.ts index 5fbfb556..000d9156 100644 --- a/src/requests/helpers/index.ts +++ b/src/methods/utils/index.ts @@ -1,2 +1,2 @@ -export { default as getHealthFactor } from './getHealthFactor' export { default as getRewardsPerYear } from './getRewardsPerYear' +export { default as getSwiseUsdPrice } from './getSwiseUsdPrice' diff --git a/src/methods/vault/index.ts b/src/methods/vault/index.ts new file mode 100644 index 00000000..7b2d11cb --- /dev/null +++ b/src/methods/vault/index.ts @@ -0,0 +1,9 @@ +export { default as getVault } from './requests/getVault' +export { default as getExitQueue } from './requests/getExitQueue' +export { default as getValidators } from './requests/getValidators' +export { default as getTransactions } from './requests/getTransactions' +export { default as getDaySnapshots } from './requests/getDaySnapshots' +export { default as getStakeBalance } from './requests/getStakeBalance' +export { default as getWithdrawData } from './requests/getWithdrawData' +export { default as getHarvestParams } from './requests/getHarvestParams' +export { default as getAllocatorActions } from './requests/getAllocatorActions' diff --git a/src/requests/methods/getAllocatorActions/index.ts b/src/methods/vault/requests/getAllocatorActions/index.ts similarity index 92% rename from src/requests/methods/getAllocatorActions/index.ts rename to src/methods/vault/requests/getAllocatorActions/index.ts index b7ad53be..06f5948b 100644 --- a/src/requests/methods/getAllocatorActions/index.ts +++ b/src/methods/vault/requests/getAllocatorActions/index.ts @@ -9,7 +9,7 @@ import modifyAllocatorActions from './modifyAllocatorActions' type GetAllocatorActionsInput = { options: StakeWise.Options userAddress?: string - types: AllocatorActionType[] + types?: AllocatorActionType[] skip: AllocatorActionsQueryVariables['skip'] limit: AllocatorActionsQueryVariables['first'] vaultAddress: AllocatorActionsQueryVariables['where']['address'] @@ -24,9 +24,9 @@ const getAllocatorActions = async (input: GetAllocatorActionsInput) => { skip, first: limit, where: { - address: userAddress, actionType_in: types, vault_: { id: vaultAddress.toLowerCase() }, + address: userAddress ? userAddress.toLowerCase() : undefined, } as AllocatorActionsQueryVariables['where'], }, modifyResult: (data: AllocatorActionsQueryPayload) => modifyAllocatorActions({ data, network: options.network }), diff --git a/src/requests/methods/getAllocatorActions/modifyAllocatorActions.spec.ts b/src/methods/vault/requests/getAllocatorActions/modifyAllocatorActions.spec.ts similarity index 100% rename from src/requests/methods/getAllocatorActions/modifyAllocatorActions.spec.ts rename to src/methods/vault/requests/getAllocatorActions/modifyAllocatorActions.spec.ts diff --git a/src/requests/methods/getAllocatorActions/modifyAllocatorActions.ts b/src/methods/vault/requests/getAllocatorActions/modifyAllocatorActions.ts similarity index 100% rename from src/requests/methods/getAllocatorActions/modifyAllocatorActions.ts rename to src/methods/vault/requests/getAllocatorActions/modifyAllocatorActions.ts index 814917bf..05c9e068 100644 --- a/src/requests/methods/getAllocatorActions/modifyAllocatorActions.ts +++ b/src/methods/vault/requests/getAllocatorActions/modifyAllocatorActions.ts @@ -1,5 +1,5 @@ -import { Network, configs } from 'helpers' import { formatEther } from 'ethers' +import { Network, configs } from 'helpers' import { AllocatorActionsQueryPayload } from 'graphql/subgraph/allocatorActions' import { ModifiedAllocatorActions } from './types' diff --git a/src/requests/methods/getAllocatorActions/types.ts b/src/methods/vault/requests/getAllocatorActions/types.ts similarity index 100% rename from src/requests/methods/getAllocatorActions/types.ts rename to src/methods/vault/requests/getAllocatorActions/types.ts diff --git a/src/requests/methods/getDaySnapshots/index.ts b/src/methods/vault/requests/getDaySnapshots/index.ts similarity index 100% rename from src/requests/methods/getDaySnapshots/index.ts rename to src/methods/vault/requests/getDaySnapshots/index.ts diff --git a/src/requests/methods/getDaySnapshots/modifyDaySnapshots.spec.ts b/src/methods/vault/requests/getDaySnapshots/modifyDaySnapshots.spec.ts similarity index 88% rename from src/requests/methods/getDaySnapshots/modifyDaySnapshots.spec.ts rename to src/methods/vault/requests/getDaySnapshots/modifyDaySnapshots.spec.ts index b0bf8085..fe42d920 100644 --- a/src/requests/methods/getDaySnapshots/modifyDaySnapshots.spec.ts +++ b/src/methods/vault/requests/getDaySnapshots/modifyDaySnapshots.spec.ts @@ -32,25 +32,25 @@ describe('modifyDaySnapshot and modifyDaySnapshots functions', () => { expect(result).toEqual({ APY: Number(daySnapshot.rewardPerAsset) * 365 * 100, - TVL: '344.379922475148628745', + TVL: 344.37992247514865, }) }) it('should correctly modify multiple day snapshots', () => { const expectedResult = { - days: [ - { + days: { + 1694908800: { APY: Number(sampleInput.daySnapshots[0].rewardPerAsset) * 365 * 100, - TVL: '344.379922475148628745', + TVL: 344.37992247514865, }, - { + 1694995200: { APY: Number(sampleInput.daySnapshots[1].rewardPerAsset) * 365 * 100, - TVL: '344.382187878289278175', + TVL: 344.3821878782893, }, - ], + }, first: { APY: Number(sampleInput.firstSnapshots[0].rewardPerAsset) * 365 * 100, - TVL: '344.379922475148628745', + TVL: 344.37992247514865, }, } diff --git a/src/requests/methods/getDaySnapshots/modifyDaySnapshots.ts b/src/methods/vault/requests/getDaySnapshots/modifyDaySnapshots.ts similarity index 64% rename from src/requests/methods/getDaySnapshots/modifyDaySnapshots.ts rename to src/methods/vault/requests/getDaySnapshots/modifyDaySnapshots.ts index 7a8fdad0..a9be2f3f 100644 --- a/src/requests/methods/getDaySnapshots/modifyDaySnapshots.ts +++ b/src/methods/vault/requests/getDaySnapshots/modifyDaySnapshots.ts @@ -4,18 +4,22 @@ import { DaySnapshotsQueryPayload } from 'graphql/subgraph/daySnapshots' import { ModifiedDaySnapshots } from './types' -export const modifyDaySnapshot = (daySnapshot: DaySnapshotsQueryPayload['daySnapshots'][number]) => { +export const modifyDaySnapshot = (daySnapshot: Omit) => { const apyValue = Number(daySnapshot.rewardPerAsset) || 0 const tvlValue = daySnapshot.totalAssets || '0' return { APY: apyValue * 365 * 100, - TVL: formatEther(tvlValue), + TVL: Number(formatEther(tvlValue)), } } const modifyDaySnapshots = (input: DaySnapshotsQueryPayload): ModifiedDaySnapshots => { - const days = input.daySnapshots.map(modifyDaySnapshot) + const days = input.daySnapshots.reduce((acc, { date, ...rest }) => { + acc[date] = modifyDaySnapshot(rest) + + return acc + }, {} as ModifiedDaySnapshots['days']) const first = input.firstSnapshots[0] ? modifyDaySnapshot(input.firstSnapshots[0]) diff --git a/src/requests/methods/getDaySnapshots/types.ts b/src/methods/vault/requests/getDaySnapshots/types.ts similarity index 67% rename from src/requests/methods/getDaySnapshots/types.ts rename to src/methods/vault/requests/getDaySnapshots/types.ts index 40c85023..089b885f 100644 --- a/src/requests/methods/getDaySnapshots/types.ts +++ b/src/methods/vault/requests/getDaySnapshots/types.ts @@ -1,9 +1,9 @@ type DaySnapshot = { APY: number - TVL: string + TVL: number } export type ModifiedDaySnapshots = { - days: DaySnapshot[] + days: Record first: DaySnapshot | null } diff --git a/src/requests/methods/getExitQueue/fetchExitQueuePositions.ts b/src/methods/vault/requests/getExitQueue/fetchExitQueuePositions.ts similarity index 100% rename from src/requests/methods/getExitQueue/fetchExitQueuePositions.ts rename to src/methods/vault/requests/getExitQueue/fetchExitQueuePositions.ts diff --git a/src/requests/methods/getExitQueue/index.ts b/src/methods/vault/requests/getExitQueue/index.ts similarity index 100% rename from src/requests/methods/getExitQueue/index.ts rename to src/methods/vault/requests/getExitQueue/index.ts diff --git a/src/requests/methods/getExitQueue/parseExitRequests.spec.ts b/src/methods/vault/requests/getExitQueue/parseExitRequests.spec.ts similarity index 98% rename from src/requests/methods/getExitQueue/parseExitRequests.spec.ts rename to src/methods/vault/requests/getExitQueue/parseExitRequests.spec.ts index e29ecf53..52ab154f 100644 --- a/src/requests/methods/getExitQueue/parseExitRequests.spec.ts +++ b/src/methods/vault/requests/getExitQueue/parseExitRequests.spec.ts @@ -2,7 +2,7 @@ import { Network, configs } from 'helpers' import { vaultMulticall } from 'contracts' import { JsonRpcProvider, ZeroAddress } from 'ethers' -import createContracts from '../../../contracts/createContracts' +import createContracts from 'contracts/createContracts' import parseExitRequests, { ParseExitRequestsInput } from './parseExitRequests' diff --git a/src/requests/methods/getExitQueue/parseExitRequests.ts b/src/methods/vault/requests/getExitQueue/parseExitRequests.ts similarity index 100% rename from src/requests/methods/getExitQueue/parseExitRequests.ts rename to src/methods/vault/requests/getExitQueue/parseExitRequests.ts diff --git a/src/requests/methods/getHarvestParams.ts b/src/methods/vault/requests/getHarvestParams.ts similarity index 100% rename from src/requests/methods/getHarvestParams.ts rename to src/methods/vault/requests/getHarvestParams.ts diff --git a/src/requests/methods/getStakeBalance.ts b/src/methods/vault/requests/getStakeBalance.ts similarity index 100% rename from src/requests/methods/getStakeBalance.ts rename to src/methods/vault/requests/getStakeBalance.ts diff --git a/src/requests/methods/getTransactions.ts b/src/methods/vault/requests/getTransactions.ts similarity index 100% rename from src/requests/methods/getTransactions.ts rename to src/methods/vault/requests/getTransactions.ts diff --git a/src/methods/vault/requests/getValidators/index.ts b/src/methods/vault/requests/getValidators/index.ts new file mode 100644 index 00000000..cec59b7f --- /dev/null +++ b/src/methods/vault/requests/getValidators/index.ts @@ -0,0 +1,27 @@ +import { apiUrls } from 'helpers' +import { backend } from 'graphql' +import { VaultValidatorsQueryVariables, VaultValidatorsQueryPayload } from 'graphql/backend/vault' + +import type { ModifiedValidators } from './types' +import modifyValidators from './modifyValidators' + + +type GetValidatorsInput = { + options: StakeWise.Options + vaultAddress: VaultValidatorsQueryVariables['address'] +} + +const getValidators = async (input: GetValidatorsInput) => { + const { options, vaultAddress } = input + + const data = await backend.vault.fetchVaultValidatorsQuery({ + url: apiUrls.getBackendUrl(options), + variables: { address: vaultAddress.toLowerCase() }, + modifyResult: (data: VaultValidatorsQueryPayload) => modifyValidators({ data, network: options.network }), + }) + + return data +} + + +export default getValidators diff --git a/src/methods/vault/requests/getValidators/modifyValidators.spec.ts b/src/methods/vault/requests/getValidators/modifyValidators.spec.ts new file mode 100644 index 00000000..66b1c786 --- /dev/null +++ b/src/methods/vault/requests/getValidators/modifyValidators.spec.ts @@ -0,0 +1,43 @@ +import { VaultValidatorsQueryPayload } from 'graphql/backend/vault' +import { Network, configs } from 'helpers' +import { formatEther } from 'ethers' + +import modifyValidators from './modifyValidators' + + +describe('modifyValidators', () => { + it('should modify validators data', () => { + const mockData: VaultValidatorsQueryPayload = { + vaults: [ + { + validators: [ + { + apy: '5.50', + earned: '2', + publicKey: 'testPublicKey', + createdAt: '2023-05-05T12:00:00Z', + }, + ], + }, + ], + } + const mockNetwork = Network.Mainnet + + const expectedOutput = [ + { + apy: '5.50', + createdAt: new Date('2023-05-05T12:00:00Z').getTime(), + earned: formatEther('2'), + publicKey: 'testPublicKey', + link: `${configs[mockNetwork].pages.beaconchain}/validator/testPublicKey`, + }, + ] + + const result = modifyValidators({ + data: mockData, + network: mockNetwork, + }) + + expect(result).toEqual(expectedOutput) + }) +}) diff --git a/src/methods/vault/requests/getValidators/modifyValidators.ts b/src/methods/vault/requests/getValidators/modifyValidators.ts new file mode 100644 index 00000000..0c36c45b --- /dev/null +++ b/src/methods/vault/requests/getValidators/modifyValidators.ts @@ -0,0 +1,31 @@ +import { VaultValidatorsQueryPayload } from 'graphql/backend/vault' +import { Network, configs } from 'helpers' +import { formatEther } from 'ethers' + +import type { ModifiedValidators } from './types' + + +type ModifyValidatorsInput = { + data: VaultValidatorsQueryPayload + network: Network +} + +const modifyValidators = (input: ModifyValidatorsInput): ModifiedValidators => { + const { data, network } = input + const validators = data?.vaults?.[0]?.validators || [] + + return validators.map((validator) => { + const { apy, createdAt, earned, publicKey } = validator + + return { + publicKey, + apy: Number(apy).toFixed(2), + createdAt: new Date(createdAt).getTime(), + earned: formatEther(String(earned || 0)), + link: `${configs[network].pages.beaconchain}/validator/${publicKey}`, + } + }) +} + + +export default modifyValidators diff --git a/src/methods/vault/requests/getValidators/types.ts b/src/methods/vault/requests/getValidators/types.ts new file mode 100644 index 00000000..60ec092e --- /dev/null +++ b/src/methods/vault/requests/getValidators/types.ts @@ -0,0 +1,9 @@ +import { VaultValidatorsQueryPayload } from 'graphql/backend/vault' + +// earned: string, createdAt: string, publicKey: string, apy: string +type AllocatorActionsPayload = Pick + +export type ModifiedValidators = Array diff --git a/src/requests/methods/getVault/index.ts b/src/methods/vault/requests/getVault/index.ts similarity index 100% rename from src/requests/methods/getVault/index.ts rename to src/methods/vault/requests/getVault/index.ts diff --git a/src/requests/methods/getVault/modifyVault.spec.ts b/src/methods/vault/requests/getVault/modifyVault.spec.ts similarity index 90% rename from src/requests/methods/getVault/modifyVault.spec.ts rename to src/methods/vault/requests/getVault/modifyVault.spec.ts index 8bf1de58..4737e454 100644 --- a/src/requests/methods/getVault/modifyVault.spec.ts +++ b/src/methods/vault/requests/getVault/modifyVault.spec.ts @@ -10,32 +10,25 @@ const network = Network.Goerli describe('modifyVault', () => { const mockVaultQueryPayload: VaultQueryPayload = { vault: { - proof: [], isErc20: true, verified: true, feePercent: 200, isPrivate: false, - proofReward: '10', performance: '10', - totalShares: '100', - queuedShares: '50', tokenSymbol: 'mTKN', imageUrl: 'mockUrl', - unclaimedAssets: '5', tokenName: 'mockToken', - rewardsRoot: 'mockRoot', createdAt: '1693395816', displayName: 'Mock Vault', avgRewardPerAsset: '0.01', totalAssets: '150000000000', capacity: '1000000000000000', - metadataIpfsHash: 'mockHash', - whitelister: 'mockWhitelister', validatorsRoot: 'mockValidators', description: 'This is a mock vault', admin: '0xeefffd4c23d2e8c845870e273861e7d60df49663', address: '0xeefffd4c23d2e8c845870e273861e7d60df49663', mevEscrow: '0xeefffd4c23d2e8c845870e273861e7d60df49663', + whitelister: '0xeefffd4c23d2e8c845870e273861e7d60df49663', keysManager: '0xeefffd4c23d2e8c845870e273861e7d60df49663', feeRecipient: '0xeefffd4c23d2e8c845870e273861e7d60df49663', }, @@ -48,29 +41,22 @@ describe('modifyVault', () => { it('should correctly transform the vault data', () => { const expectedModifiedVault = { apy: 365, - proof: [], isErc20: true, feePercent: 2, verified: true, isPrivate: false, - proofReward: '10', capacity: '0.001', - totalShares: '100', - queuedShares: '50', tokenSymbol: 'mTKN', imageUrl: 'mockUrl', - unclaimedAssets: '5', tokenName: 'mockToken', isSmoothingPool: false, - rewardsRoot: 'mockRoot', createdAt: 1693395816000, displayName: 'Mock Vault', totalAssets: '0.00000015', - metadataIpfsHash: 'mockHash', - whitelister: 'mockWhitelister', validatorsRoot: 'mockValidators', description: 'This is a mock vault', vaultAdmin: '0xeEFFFD4C23D2E8c845870e273861e7d60Df49663', + whitelister: '0xeEFFFD4C23D2E8c845870e273861e7d60Df49663', vaultAddress: '0xeEFFFD4C23D2E8c845870e273861e7d60Df49663', feeRecipient: '0xeEFFFD4C23D2E8c845870e273861e7d60Df49663', mevRecipient: '0xeEFFFD4C23D2E8c845870e273861e7d60Df49663', diff --git a/src/requests/methods/getVault/modifyVault.ts b/src/methods/vault/requests/getVault/modifyVault.ts similarity index 95% rename from src/requests/methods/getVault/modifyVault.ts rename to src/methods/vault/requests/getVault/modifyVault.ts index b3d36df2..5f6d9819 100644 --- a/src/requests/methods/getVault/modifyVault.ts +++ b/src/methods/vault/requests/getVault/modifyVault.ts @@ -42,6 +42,7 @@ const modifyVault = (input: ModifyVaultInput): ModifiedVault => { feeRecipient: getAddress(feeRecipient), vaultKeysManager: getAddress(keysManager), apy: Number(avgRewardPerAsset) * 365 * 100, + whitelister: vault.whitelister ? getAddress(vault.whitelister) : '', whitelist: privateVaultAccounts.map(({ createdAt, address }) => ({ createdAt: Number(createdAt) * 1000, address: getAddress(address), diff --git a/src/requests/methods/getVault/types.ts b/src/methods/vault/requests/getVault/types.ts similarity index 100% rename from src/requests/methods/getVault/types.ts rename to src/methods/vault/requests/getVault/types.ts diff --git a/src/requests/methods/getWithdrawData/getAvailableAssets.ts b/src/methods/vault/requests/getWithdrawData/getAvailableAssets.ts similarity index 100% rename from src/requests/methods/getWithdrawData/getAvailableAssets.ts rename to src/methods/vault/requests/getWithdrawData/getAvailableAssets.ts diff --git a/src/requests/methods/getWithdrawData/getMaxWithdraw.spec.ts b/src/methods/vault/requests/getWithdrawData/getMaxWithdraw.spec.ts similarity index 100% rename from src/requests/methods/getWithdrawData/getMaxWithdraw.spec.ts rename to src/methods/vault/requests/getWithdrawData/getMaxWithdraw.spec.ts diff --git a/src/requests/methods/getWithdrawData/getMaxWithdraw.ts b/src/methods/vault/requests/getWithdrawData/getMaxWithdraw.ts similarity index 100% rename from src/requests/methods/getWithdrawData/getMaxWithdraw.ts rename to src/methods/vault/requests/getWithdrawData/getMaxWithdraw.ts diff --git a/src/requests/methods/getWithdrawData/index.ts b/src/methods/vault/requests/getWithdrawData/index.ts similarity index 100% rename from src/requests/methods/getWithdrawData/index.ts rename to src/methods/vault/requests/getWithdrawData/index.ts diff --git a/src/requests/createRequests.ts b/src/requests/createRequests.ts deleted file mode 100644 index 445b23dc..00000000 --- a/src/requests/createRequests.ts +++ /dev/null @@ -1,31 +0,0 @@ -import * as methods from './methods' - - -type Methods = typeof methods - -type CheckArgs> = [ keyof Obj ] extends [ never ] ? [] : [ Obj ] - -type ModifyRequests> = { - [K in keyof T]: (...values: CheckArgs[0], 'options' | 'contracts'>>) => ReturnType -} - -type CreateRequestsInput = { - options: StakeWise.Options - contracts: StakeWise.Contracts -} - -type CreateRequestsOutput = ModifyRequests - -const createRequests = (input: CreateRequestsInput): CreateRequestsOutput => ( - Object.keys(methods).reduce((acc, method) => { - const fn = methods[method as keyof typeof methods] - - return { - ...acc, - [method]: (values: unknown) => fn({ ...(values || {}), ...input } as any), - } - }, {} as CreateRequestsOutput) -) - - -export default createRequests diff --git a/src/requests/index.ts b/src/requests/index.ts deleted file mode 100644 index 436c9952..00000000 --- a/src/requests/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from './helpers' -export { default as createRequests } from './createRequests' diff --git a/src/requests/methods/getVaultValidators.ts b/src/requests/methods/getVaultValidators.ts deleted file mode 100644 index 339d9ed6..00000000 --- a/src/requests/methods/getVaultValidators.ts +++ /dev/null @@ -1,23 +0,0 @@ -import { apiUrls } from 'helpers' -import { backend } from 'graphql' -import { VaultValidatorsQueryVariables } from 'graphql/backend/vault' - - -type GetVaultValidatorsInput = { - options: StakeWise.Options - vaultAddress: VaultValidatorsQueryVariables['address'] -} - -const getVaultValidators = async (input: GetVaultValidatorsInput) => { - const { options, vaultAddress } = input - - const data = await backend.vault.fetchVaultValidatorsQuery({ - url: apiUrls.getBackendUrl(options), - variables: { address: vaultAddress.toLowerCase() }, - }) - - return data?.vaults?.[0]?.validators || [] -} - - -export default getVaultValidators diff --git a/src/requests/methods/getVaultsCount.ts b/src/requests/methods/getVaultsCount.ts deleted file mode 100644 index fd4bbcff..00000000 --- a/src/requests/methods/getVaultsCount.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { apiUrls } from 'helpers' -import { subgraph } from 'graphql' -import { ZeroAddress } from 'ethers' - - -type GetVaultsCountInput = { - userAddress?: string - options: StakeWise.Options -} - -const getVaultsCount = async (input: GetVaultsCountInput) => { - const { options, userAddress } = input - - const data = await subgraph.vaultsCount.fetchVaultsCountQuery({ - url: apiUrls.getSubgraphqlUrl(options), - variables: { - address: userAddress?.toLowerCase() || ZeroAddress, - }, - }) - - return { - all: data?.all?.vaultsTotal || 0, - deposits: data?.deposits?.length || 0, - } -} - - -export default getVaultsCount diff --git a/src/requests/methods/index.ts b/src/requests/methods/index.ts deleted file mode 100644 index 572e26dc..00000000 --- a/src/requests/methods/index.ts +++ /dev/null @@ -1,17 +0,0 @@ -export { default as getVault } from './getVault' -export { default as getExitQueue } from './getExitQueue' -export { default as getOsTokenAPY } from './getOsTokenAPY' -export { default as getBurnAmount } from './getBurnAmount' -export { default as getOsTokenData } from './getOsTokenData' -export { default as getVaultsCount } from './getVaultsCount' -export { default as getTransactions } from './getTransactions' -export { default as getDaySnapshots } from './getDaySnapshots' -export { default as getStakeBalance } from './getStakeBalance' -export { default as getWithdrawData } from './getWithdrawData' -export { default as getHarvestParams } from './getHarvestParams' -export { default as getSwiseUsdPrice } from './getSwiseUsdPrice' -export { default as getMaxMintShares } from './getMaxMintShares' -export { default as getRewardsPerYear } from './getRewardsPerYear' -export { default as getVaultValidators } from './getVaultValidators' -export { default as getOsTokenPosition } from './getOsTokenPosition' -export { default as getAllocatorActions } from './getAllocatorActions' diff --git a/src/types/global.ts b/src/types/global.ts index bc749b1d..359a375e 100644 --- a/src/types/global.ts +++ b/src/types/global.ts @@ -1,23 +1,69 @@ -import { createContracts } from 'contracts' +import { createContracts, createRatesContracts } from 'contracts' import { Network, configs } from 'helpers' -import { createRequests } from 'requests' -import type { Provider } from 'ethers' +import methods from 'methods' + +import type { + VaultAbi, + Erc20Abi, + KeeperAbi, + UsdRateAbi, + EthRateAbi, + MulticallAbi, + MintTokenAbi, + Erc20VaultAbi, + SwiseTokenAbi, + PriceOracleAbi, + VaultFactoryAbi, + PrivateVaultAbi, + V2RewardTokenAbi, + VaultsRegistryAbi, + RewardSplitterAbi, + MintTokenConfigAbi, + Erc20PrivateVaultAbi, + RewardSplitterFactoryAbi, +} from '../contracts/types' declare global { + namespace StakeWise { type Config = typeof configs[Network] - type Requests = ReturnType type Contracts = ReturnType + type RateContracts = ReturnType + + type Utils = ReturnType + type VaultMethods = ReturnType + type OsTokenMethods = ReturnType type Options = { network: Network - provider?: Provider + provider?: any endpoints?: { api?: string web3?: string subgraph?: string - }, + } + } + + namespace ABI { + type Vault = VaultAbi + type Keeper = KeeperAbi + type UsdRate = UsdRateAbi + type EthRate = EthRateAbi + type Erc20Token = Erc20Abi + type Multicall = MulticallAbi + type MintToken = MintTokenAbi + type Erc20Vault = Erc20VaultAbi + type SwiseToken = SwiseTokenAbi + type PriceOracle = PriceOracleAbi + type PrivateVault = PrivateVaultAbi + type VaultFactory = VaultFactoryAbi + type V2RewardToken = V2RewardTokenAbi + type RewardSplitter = RewardSplitterAbi + type VaultsRegistry = VaultsRegistryAbi + type MintTokenConfig = MintTokenConfigAbi + type Erc20PrivateVault = Erc20PrivateVaultAbi + type RewardSplitterFactory = RewardSplitterFactoryAbi } } }