Skip to content

Commit

Permalink
New withdraw (#30)
Browse files Browse the repository at this point in the history
* [new-withdraw] change logic

* [new-withdraw] fix getMaxWithdraw

* [new-withdraw] change parseExitRequests

* [new-withdraw] change getUserRewards

* [new-withdraw] change vaultsRegistry address

* [new-withdraw] up version
  • Loading branch information
Cast0001 authored Oct 31, 2023
1 parent e142b1d commit f329c0b
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 190 deletions.
62 changes: 7 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

The official SDK designed for effortless data retrieval from the StakeWise platform. This SDK provides a streamlined interface over GraphQL requests and contract interactions.

![Version](https://img.shields.io/badge/version-1.2.7-blue)
![Version](https://img.shields.io/badge/version-1.2.8-blue)
![Unit Tests](https://github.com/stakewise/v3-sdk/actions/workflows/unit-tests.yml/badge.svg)
![GitHub issues](https://img.shields.io/github/issues-raw/stakewise/v3-sdk)
![GitHub pull requests](https://img.shields.io/github/issues-pr-raw/stakewise/v3-sdk)
Expand Down Expand Up @@ -76,11 +76,10 @@ const sdk = new StakeWiseSDK({ network: Network.Mainnet })
| [sdk.vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [sdk.osToken.getAPY](#sdkostokengetapy) | [sdk.utils.getTransactions](#sdkutilsgettransactions) |
| [sdk.vault.getValidators](#sdkvaultgetvalidators) | [sdk.osToken.getPosition](#sdkostokengetposition) |
| [sdk.vault.getVault](#sdkvaultgetvault) | [sdk.osToken.getMaxMint](#sdkostokengetmaxmint) |
| [sdk.vault.getWithdrawData](#sdkvaultgetwithdrawdata) | [sdk.osToken.getBaseData](#sdkostokengetbasedata) |
| [sdk.vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [sdk.osToken.getBaseData](#sdkostokengetbasedata) |
| [sdk.vault.getHarvestParams](#sdkvaultgetharvestparams) | [sdk.osToken.getSharesFromAssets](#sdkostokengetsharesfromassets) |
| [sdk.vault.getStakeBalance](#sdkvaultgetstakebalance) | [sdk.osToken.getAssetsFromShares](#sdkostokengetassetsfromshares) |
|[sdk.vault.getUserRewards](#sdkvaultgetuserrewards)|
|[sdk.vault.getCollateralized](#sdkvaultgetcollateralized)|

##### Table of transactions:
| **Vault** | **osToken** |
Expand Down Expand Up @@ -383,45 +382,33 @@ type Output = {
await sdk.vault.getVault({ vaultAddress: '0x...' })
```
---
### `sdk.vault.getWithdrawData`
### `sdk.vault.getMaxWithdraw`
#### Description:
Withdrawal details
How much a user can withdraw
#### Arguments:
| Name | Type | Type | Info |
|------|------|-------------|-------|
| ltvPercent | `bigint` | **Require** | [sdk.osToken.getBaseData](#sdkostokengetbasedata) |
| userAddress | `string` | **Require** | - |
| vaultAddress | `string` | **Require** | - |
| mintedAssets | `bigint` | **Require** | [sdk.osToken.getPosition](#sdkostokengetposition) |
| stakedAssets | `bigint` | **Require** | [sdk.vault.getStakeBalance](#sdkvaultgetstakebalance) |
#### Returns:
```ts
type Output = {
availableAssets: bigint
maxWithdrawAssets: bigint
}
type Output = bigint
```
| Name | Description |
|------|-------------|
| `availableAssets` | Available for withdrawal instantly |
| `maxWithdrawAssets` | Maximum available for withdrawal |
#### Example:
```ts
await sdk.vault.getWithdrawData({
await sdk.vault.getMaxWithdraw({
ltvPercent: 0n,
mintedAssets: 0n,
stakedAssets: 0n,
userAddress: '0x...',
vaultAddress: '0x...',
})
```
---
Expand Down Expand Up @@ -484,31 +471,6 @@ await sdk.vault.getStakeBalance({
})
```
---
### `sdk.vault.getCollateralized`
#### Description:
Checks if the vault validators have been run and if so, returns true
#### Arguments:
| Name | Type | Type |
|------|------|-------------|
| vaultAddress | `string` | **Require** |
#### Returns:
```ts
type Output = boolean
```
#### Example:
```ts
await sdk.vault.getCollateralized({
vaultAddress: '0x...',
})
```
## API-osToken
### `sdk.osToken.getBurnAmount`
Expand Down Expand Up @@ -878,7 +840,6 @@ Withdrawal of funds from a vault
| Name | Type | Type | Description |
|------|------|-------------|---------|
| assets | `bigint` | **Require** | Withdraw amount |
| availableAssets | `string` | **Require** | [sdk.vault.getWithdrawData](#sdkvaultgetwithdrawdata) |
| userAddress | `string` | **Require** | - |
| vaultAddress | `string` | **Require** | - |

Expand All @@ -905,22 +866,14 @@ const osToken = await sdk.osToken.getPosition({
thresholdPercent,
})

const { availableAssets, maxWithdrawAssets } = sdk.vault.getWithdrawData({
const maxWithdrawAssets = await sdk.vault.getMaxWithdraw({
mintedAssets: osToken.minted.assets,
stakedAssets: stake.assets,
vaultAddress: '0x...',
userAddress: '0x...',
ltvPercent,
})

if (amountAssets > maxWithdrawAssets) {
// There is a withdrawal restriction if you have an osToken.
// If you withdraw all funds, your osToken position will become unhealthy.
// Contracts control this, but it's good to block cases like this in the UI.

const burnAmount = await burn.calculateBurn(stake.assets)

// burnAmount is the value that is responsible for figuring out how many osToken must be burned to withdraw all your funds.

return
}
Expand All @@ -929,7 +882,6 @@ const params = {
vaultAddress: '0x...',
userAddress: '0x...',
assets: amountAssets,
availableAssets,
}

// Send transaction
Expand Down
8 changes: 4 additions & 4 deletions src/methods/osToken/helpers/getBurnAmount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ describe('getBurnAmount', () => {
const mockedShares = 100n

const contracts = {
tokens: {
mintToken: {
base: {
mintTokenController: {
convertToShares: jest.fn().mockResolvedValue(mockedShares),
},
},
Expand All @@ -42,8 +42,8 @@ describe('getBurnAmount', () => {

it('should return 0 if assetsToBurn is less than or equal to 0', async () => {
const contracts = {
tokens: {
mintToken: {
base: {
mintTokenController: {
convertToShares: jest.fn(),
},
},
Expand Down
4 changes: 2 additions & 2 deletions src/methods/osToken/requests/getMaxMint.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ describe('getMaxMint', () => {

beforeEach(() => {
mockContracts = {
tokens: {
mintToken: {
base: {
mintTokenController: {
avgRewardPerSecond: jest.fn(),
convertToShares: jest.fn(),
},
Expand Down
6 changes: 2 additions & 4 deletions src/methods/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import getVault from './requests/getVault'
import getValidators from './requests/getValidators'
import getUserRewards from './requests/getUserRewards'
import getMaxWithdraw from './requests/getMaxWithdraw'
import getDaySnapshots from './requests/getDaySnapshots'
import getStakeBalance from './requests/getStakeBalance'
import getWithdrawData from './requests/getWithdrawData'
import getHarvestParams from './requests/getHarvestParams'
import getStakerActions from './requests/getStakerActions'
import getCollateralized from './requests/getCollateralized'
import getExitQueuePositions from './requests/getExitQueuePositions'

// Transactions
Expand All @@ -21,12 +20,11 @@ export default {
getVault,
getValidators,
getUserRewards,
getMaxWithdraw,
getDaySnapshots,
getStakeBalance,
getWithdrawData,
getHarvestParams,
getStakerActions,
getCollateralized,
getExitQueuePositions,
},
transactions: {
Expand Down
20 changes: 0 additions & 20 deletions src/methods/vault/requests/getCollateralized.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/methods/vault/requests/getExitQueuePositions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ type GetExitQueuePositionsInput = FetchExitQueuePositionsInput & {
}

const mock = {
data: [],
total: 0n,
positions: [],
withdrawable: 0n,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('parseExitRequests function', () => {
expect(vaultMulticall).toHaveBeenCalledTimes(3)

expect(result).toEqual({
data: [
positions: [
{ exitQueueIndex: 1n, positionTicket: 'positionTicket-1' },
{ exitQueueIndex: 2n, positionTicket: 'positionTicket-2' },
],
Expand All @@ -84,7 +84,7 @@ describe('parseExitRequests function', () => {
const result = await parseExitRequests(input)

expect(result).toEqual({
data: [],
positions: [],
total: 50n,
withdrawable: 0n,
})
Expand All @@ -108,7 +108,7 @@ describe('parseExitRequests function', () => {
const result = await parseExitRequests(input)

expect(result).toEqual({
data: [ { exitQueueIndex: 1n, positionTicket: 'positionTicket-2' } ],
positions: [ { exitQueueIndex: 1n, positionTicket: 'positionTicket-2' } ],
total: 50n,
withdrawable: 30n,
})
Expand All @@ -129,7 +129,7 @@ describe('parseExitRequests function', () => {
const result = await parseExitRequests(input)

expect(result).toEqual({
data: [
positions: [
{ exitQueueIndex: 0n, positionTicket: 'positionTicket-1' },
{ exitQueueIndex: 1n, positionTicket: 'positionTicket-2' },
],
Expand All @@ -152,7 +152,7 @@ describe('parseExitRequests function', () => {
const result = await parseExitRequests(input)

expect(result).toEqual({
data: [
positions: [
{ exitQueueIndex: 0n, positionTicket: 'positionTicket-1' },
{ exitQueueIndex: 1n, positionTicket: 'positionTicket-2' },
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type Position = {

type ParseExitRequestsOutput = {
total: bigint
data: Position[]
withdrawable: bigint
positions: Position[]
}

type ExitedAssetsResponse = Array<{
Expand Down Expand Up @@ -127,7 +127,7 @@ const parseExitRequests = async (values: ParseExitRequestsInput): Promise<ParseE
}

return {
data: claims,
positions: claims,
total: totalExitingAssets,
withdrawable: withdrawableAssets,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('getMaxWithdraw function', () => {
mintedAssets: 1000n,
stakedAssets: 2000n,
contracts: {
tokens: {
mintToken: {
base: {
mintTokenController: {
avgRewardPerSecond: jest.fn(),
},
},
Expand All @@ -25,8 +25,8 @@ describe('getMaxWithdraw function', () => {
it('should consider rewardPerSecond for locked assets', async () => {
const mockedReward = 10n
const contracts = {
tokens: {
mintToken: {
base: {
mintTokenController: {
avgRewardPerSecond: jest.fn().mockResolvedValue(mockedReward),
},
},
Expand All @@ -50,8 +50,8 @@ describe('getMaxWithdraw function', () => {
it('should return 0 if locked assets exceed staked assets', async () => {
const mockedReward = 10n
const contracts = {
tokens: {
mintToken: {
base: {
mintTokenController: {
avgRewardPerSecond: jest.fn().mockResolvedValue(mockedReward),
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { parseEther } from 'ethers'

import { BigDecimal, constants } from '../../../../utils'
import { BigDecimal, constants } from '../../../utils'


type GetMaxWithdrawInput = {
Expand Down
2 changes: 1 addition & 1 deletion src/methods/vault/requests/getUserRewards/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const getUserRewards = async (input: GetUserRewardsInput) => {
modifyResult: modifyUserRewards,
})

return data
return data?.days || []
}


Expand Down
16 changes: 13 additions & 3 deletions src/methods/vault/requests/getVault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ import modifyVault from './modifyVault'

type GetVaultInput = {
options: StakeWise.Options
contracts: StakeWise.Contracts
vaultAddress: VaultQueryVariables['address']
}

const getVault = async (input: GetVaultInput) => {
const { vaultAddress, options } = input
type GetVaultOutput = ModifiedVault & {
isCollateralized: boolean
}

const getVault = async (input: GetVaultInput): Promise<GetVaultOutput> => {
const { contracts, options, vaultAddress } = input

validateArgs.address({ vaultAddress })

Expand All @@ -23,7 +28,12 @@ const getVault = async (input: GetVaultInput) => {
modifyResult: (data: VaultQueryPayload) => modifyVault({ data, network: options.network }),
})

return data
const isCollateralized = await contracts.base.keeper.isCollateralized(vaultAddress)

return {
...data,
isCollateralized,
}
}


Expand Down
Loading

0 comments on commit f329c0b

Please sign in to comment.