From 40999c8f1cbdbbf9c1ecd8ea54657e79cf72a25d Mon Sep 17 00:00:00 2001 From: CAst Date: Tue, 5 Dec 2023 19:05:10 +0500 Subject: [PATCH] New exit queue position logic (#41) * [new-exit-queue-position-logic] replace claimExitedAssets on calculateExitedAssets * [new-exit-queue-position-logic] add new allocator actions --- README.md | 2 +- .../parseExitRequests.spec.ts | 20 +++--- .../parseExitRequests.ts | 66 ++++++++++--------- src/utils/enums.ts | 4 ++ 4 files changed, 49 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index ef0d29a1..4b0bbc00 100644 --- a/README.md +++ b/README.md @@ -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.4.1-blue) +![Version](https://img.shields.io/badge/version-1.4.2-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) diff --git a/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.spec.ts b/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.spec.ts index 8fbaf6d1..0efbbb94 100644 --- a/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.spec.ts +++ b/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.spec.ts @@ -25,8 +25,8 @@ describe('parseExitRequests function', () => { const input: ParseExitRequestsInput = { contracts, - options: { network }, totalShares: 1000n, + options: { network }, userAddress: ZeroAddress, vaultAddress: ZeroAddress, provider: getMockProvider(9999999999), @@ -58,12 +58,12 @@ describe('parseExitRequests function', () => { { claimedAssets: 30n, claimedShares: 50n, - newPositionTicket: 10n, + leftShares: 10n, }, { claimedAssets: 1n, claimedShares: 100n, - newPositionTicket: 20n, + leftShares: 20n, }, ]) .mockResolvedValueOnce([ @@ -87,7 +87,7 @@ describe('parseExitRequests function', () => { positionTicket: 'positionTicket-2', }, ], - total: 100n, + total: 131n, withdrawable: 31n, }) }) @@ -141,7 +141,7 @@ describe('parseExitRequests function', () => { ]) .mockResolvedValueOnce([ { - newPositionTicket: 10n, + leftShares: 10n, claimedShares: 50n, claimedAssets: 30n, }, @@ -156,7 +156,7 @@ describe('parseExitRequests function', () => { timestamp: '123456', positionTicket: 'positionTicket-2', } ], - total: 50n, + total: 80n, withdrawable: 30n, }) }) @@ -168,8 +168,8 @@ describe('parseExitRequests function', () => { [ 1n ], ]) .mockResolvedValueOnce([ - { newPositionTicket: 0n, claimedShares: 0n, claimedAssets: 0n }, - { newPositionTicket: 0n, claimedShares: 0n, claimedAssets: 0n }, + { leftShares: 0n, claimedShares: 0n, claimedAssets: 0n }, + { leftShares: 0n, claimedShares: 0n, claimedAssets: 0n }, ]) .mockResolvedValueOnce([ { assets: 0n } ]) @@ -200,7 +200,7 @@ describe('parseExitRequests function', () => { [ 1n ], ]) .mockResolvedValueOnce([ - { newPositionTicket: 10n, claimedShares: 1000n, claimedAssets: 30n }, + { leftShares: 10n, claimedShares: 1000n, claimedAssets: 30n }, ]) .mockResolvedValueOnce([]) @@ -219,7 +219,7 @@ describe('parseExitRequests function', () => { positionTicket: 'positionTicket-2', }, ], - total: 0n, + total: 30n, withdrawable: 30n, }) }) diff --git a/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.ts b/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.ts index 968504d0..b5d5bfa6 100644 --- a/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.ts +++ b/src/methods/vault/requests/getExitQueuePositions/parseExitRequests.ts @@ -6,8 +6,8 @@ export type ParseExitRequestsInput = { provider: StakeWise.Provider options: StakeWise.Options userAddress: string - vaultAddress: string totalShares: bigint + vaultAddress: string exitRequests: Array<{ positionTicket: string totalShares: string @@ -28,8 +28,8 @@ type ParseExitRequestsOutput = { } type ExitedAssetsResponse = Array<{ - newPositionTicket: bigint, - claimedShares: bigint, + leftShares: bigint + claimedShares: bigint claimedAssets: bigint }> @@ -100,55 +100,57 @@ const parseExitRequests = async (values: ParseExitRequestsInput): Promise ({ - method: 'claimExitedAssets', - args: [ positionTicket, timestamp, exitQueueIndex ], + method: 'calculateExitedAssets', + args: [ userAddress, positionTicket, timestamp, exitQueueIndex ], })), callStatic: true, }, }) || [] } + else { + const result = await vaultMulticall>({ + ...commonMulticallParams, + request: { + params: [ { method: 'convertToAssets', args: [ totalShares ] } ], + callStatic: true, + }, + }) - let remainingShares = totalShares, - totalExitingAssets = 0n, - withdrawableAssets = 0n - - exitedAssetsResponse.forEach(({ newPositionTicket, claimedShares, claimedAssets }, index) => { - // "claimedAssets" is the value in ETH that user will take after claim. - // We can get the sum of these values to display how much ETH the user will get - - withdrawableAssets = withdrawableAssets + claimedAssets + // If there are no positions with an index greater than 0 or their timestamp has failed the 24 hour check. + // Then we can use totalShares from the subgraph to show total + return { + positions: [], + withdrawable: 0n, + total: result[0]?.assets || 0n, + } + } - const prevPositionTicket = claims[index].positionTicket - const item = exitRequests.find((item) => item.positionTicket === prevPositionTicket) + let withdrawableAssets = 0n, + totalLeftShares = 0n, + totalLeftAssets = 0n - // Check whether not all the shares have been exited - if (newPositionTicket > 0) { - // Subtract claimed shares from the remaining shares - remainingShares = remainingShares - claimedShares - } - else { - // If the next position ID is 0, then we will take all tokens from it - remainingShares = remainingShares - BigInt(item?.totalShares || 0) - } + exitedAssetsResponse.forEach(({ leftShares, claimedAssets }) => { + totalLeftShares += leftShares + withdrawableAssets += claimedAssets }) - if (remainingShares > 0) { - // If there are remaining shares, we must calculate how many assets are still queuing - // Remember - Shares are VLT tokens and Assets are ETH tokens - const remainingAssets = await vaultMulticall>({ + if (totalLeftShares > 0) { + const result = await vaultMulticall>({ ...commonMulticallParams, request: { - params: [ { method: 'convertToAssets', args: [ remainingShares ] } ], + params: [ { method: 'convertToAssets', args: [ totalLeftShares ] } ], callStatic: true, }, }) - totalExitingAssets = remainingAssets[0]?.assets || 0n + totalLeftAssets = result[0]?.assets || 0n } + const total = withdrawableAssets + totalLeftAssets + return { + total, positions: claims, - total: totalExitingAssets, withdrawable: withdrawableAssets, } } diff --git a/src/utils/enums.ts b/src/utils/enums.ts index 55cc8e40..dbd385f7 100644 --- a/src/utils/enums.ts +++ b/src/utils/enums.ts @@ -10,6 +10,10 @@ export enum AllocatorActionType { Migrated = 'Migrated', Deposited = 'Deposited', VaultCreated = 'VaultCreated', + OsTokenMinted = 'OsTokenMinted', + OsTokenBurned = 'OsTokenBurned', + OsTokenRedeemed = 'OsTokenRedeemed', + OsTokenLiquidated = 'OsTokenLiquidated', ExitedAssetsClaimed = 'ExitedAssetsClaimed', }