Skip to content

Commit

Permalink
New exit queue position logic (#41)
Browse files Browse the repository at this point in the history
* [new-exit-queue-position-logic] replace claimExitedAssets on calculateExitedAssets

* [new-exit-queue-position-logic] add new allocator actions
  • Loading branch information
Cast0001 authored Dec 5, 2023
1 parent 7f662da commit 40999c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion 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.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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ describe('parseExitRequests function', () => {

const input: ParseExitRequestsInput = {
contracts,
options: { network },
totalShares: 1000n,
options: { network },
userAddress: ZeroAddress,
vaultAddress: ZeroAddress,
provider: getMockProvider(9999999999),
Expand Down Expand Up @@ -58,12 +58,12 @@ describe('parseExitRequests function', () => {
{
claimedAssets: 30n,
claimedShares: 50n,
newPositionTicket: 10n,
leftShares: 10n,
},
{
claimedAssets: 1n,
claimedShares: 100n,
newPositionTicket: 20n,
leftShares: 20n,
},
])
.mockResolvedValueOnce([
Expand All @@ -87,7 +87,7 @@ describe('parseExitRequests function', () => {
positionTicket: 'positionTicket-2',
},
],
total: 100n,
total: 131n,
withdrawable: 31n,
})
})
Expand Down Expand Up @@ -141,7 +141,7 @@ describe('parseExitRequests function', () => {
])
.mockResolvedValueOnce([
{
newPositionTicket: 10n,
leftShares: 10n,
claimedShares: 50n,
claimedAssets: 30n,
},
Expand All @@ -156,7 +156,7 @@ describe('parseExitRequests function', () => {
timestamp: '123456',
positionTicket: 'positionTicket-2',
} ],
total: 50n,
total: 80n,
withdrawable: 30n,
})
})
Expand All @@ -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 } ])

Expand Down Expand Up @@ -200,7 +200,7 @@ describe('parseExitRequests function', () => {
[ 1n ],
])
.mockResolvedValueOnce([
{ newPositionTicket: 10n, claimedShares: 1000n, claimedAssets: 30n },
{ leftShares: 10n, claimedShares: 1000n, claimedAssets: 30n },
])
.mockResolvedValueOnce([])

Expand All @@ -219,7 +219,7 @@ describe('parseExitRequests function', () => {
positionTicket: 'positionTicket-2',
},
],
total: 0n,
total: 30n,
withdrawable: 30n,
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,8 +28,8 @@ type ParseExitRequestsOutput = {
}

type ExitedAssetsResponse = Array<{
newPositionTicket: bigint,
claimedShares: bigint,
leftShares: bigint
claimedShares: bigint
claimedAssets: bigint
}>

Expand Down Expand Up @@ -100,55 +100,57 @@ const parseExitRequests = async (values: ParseExitRequestsInput): Promise<ParseE
...commonMulticallParams,
request: {
params: claims.map(({ positionTicket, exitQueueIndex, timestamp }) => ({
method: 'claimExitedAssets',
args: [ positionTicket, timestamp, exitQueueIndex ],
method: 'calculateExitedAssets',
args: [ userAddress, positionTicket, timestamp, exitQueueIndex ],
})),
callStatic: true,
},
}) || []
}
else {
const result = await vaultMulticall<Array<{ assets: bigint }>>({
...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<Array<{ assets: bigint }>>({
if (totalLeftShares > 0) {
const result = await vaultMulticall<Array<{ assets: bigint }>>({
...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,
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ export enum AllocatorActionType {
Migrated = 'Migrated',
Deposited = 'Deposited',
VaultCreated = 'VaultCreated',
OsTokenMinted = 'OsTokenMinted',
OsTokenBurned = 'OsTokenBurned',
OsTokenRedeemed = 'OsTokenRedeemed',
OsTokenLiquidated = 'OsTokenLiquidated',
ExitedAssetsClaimed = 'ExitedAssetsClaimed',
}

Expand Down

0 comments on commit 40999c8

Please sign in to comment.