Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/minimist-1.2.8
Browse files Browse the repository at this point in the history
  • Loading branch information
Cast0001 authored Oct 3, 2023
2 parents ae0a3e2 + 5dfb949 commit 8c0be5a
Show file tree
Hide file tree
Showing 72 changed files with 545 additions and 276 deletions.
170 changes: 164 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,167 @@
<p align="center">
<img src="https://app.stakewise.io/logo512.png" alt="StakeWise Logo" width="200">
</p>

# 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:[email protected]`

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<number, DaySnapshot>
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,
})
```
8 changes: 5 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ const config: Config = {
'^graphql/(.*)': '<rootDir>/graphql/$1',
'^graphql$': '<rootDir>/graphql/index.ts',

'^requests/(.*)': '<rootDir>/requests/$1',
'^requests$': '<rootDir>/requests/index.ts',
'^requests/methods': '<rootDir>/requests/methods/indes.ts',
'^methods/(.*)': '<rootDir>/methods/$1',
'^methods$': '<rootDir>/methods/index.ts',

'^helpers/(.*)': '<rootDir>/helpers/$1',
'^helpers$': '<rootDir>/helpers/index.ts',

'^contracts/(.*)': '<rootDir>/contracts/$1',
'^contracts$': '<rootDir>/contracts/index.ts',
},
}
Expand Down
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
15 changes: 0 additions & 15 deletions rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 45 additions & 7 deletions src/StakeWiseSDK.ts
Original file line number Diff line number Diff line change
@@ -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<Parameters<typeof vaultMulticall>[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<T extends unknown>({ userAddress, vaultAddress, request }: VaultMulticallInput) {
return vaultMulticall<T>({
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
}
}


Expand Down
1 change: 0 additions & 1 deletion src/contracts/abis/VestingEscrowAbi.json

This file was deleted.

1 change: 0 additions & 1 deletion src/contracts/abis/VestingEscrowFactoryAbi.json

This file was deleted.

4 changes: 0 additions & 4 deletions src/contracts/abis/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -22,13 +20,11 @@ import Erc20Abi from './Erc20Abi.json'

export {
RewardSplitterFactoryAbi,
VestingEscrowFactoryAbi,
Erc20PrivateVaultAbi,
MintTokenConfigAbi,
VaultsRegistryAbi,
RewardSplitterAbi,
V2RewardTokenAbi,
VestingEscrowAbi,
PrivateVaultAbi,
VaultFactoryAbi,
PriceOracleAbi,
Expand Down
Loading

0 comments on commit 8c0be5a

Please sign in to comment.