Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abort request #72

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,41 @@ const sdk = new StakeWiseSDK({ network: Network.Mainnet })
## Quick Links

##### Request table:
| **Vault** | **osToken** | **Utils** |
|-------------------------------------------------------------------|-------------|------|
| [sdk.vault.getStakerActions](#sdkvaultgetstakeractions) | [sdk.osToken.getBurnAmount](#sdkostokengetburnamount) | [sdk.utils.getSwiseUsdPrice](#sdkutilsgetswiseusdprice) |
| [sdk.vault.getSnapshots](#sdkvaultgetsnapshots) | [sdk.osToken.getHealthFactor](#sdkostokengethealthfactor) | [sdk.utils.getTransactions](#sdkutilsgettransactions) |
| [sdk.vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [sdk.osToken.getAPY](#sdkostokengetapy) |
| [sdk.vault.getValidators](#sdkvaultgetvalidators) | [sdk.osToken.getPosition](#sdkostokengetposition) |
| [sdk.vault.getVault](#sdkvaultgetvault) | [sdk.osToken.getMaxMint](#sdkostokengetmaxmint) |
| [sdk.vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [sdk.osToken.getBaseData](#sdkostokengetbasedata) |
| **Vault** | **osToken** | **Utils** |
|-------------------------------------------------------------------|-----------------------------------------------------------------|------|
| [sdk.vault.getStakerActions](#sdkvaultgetstakeractions) | [sdk.osToken.getBurnAmount](#sdkostokengetburnamount) | [sdk.utils.getSwiseUsdPrice](#sdkutilsgetswiseusdprice) |
| [sdk.vault.getSnapshots](#sdkvaultgetsnapshots) | [sdk.osToken.getHealthFactor](#sdkostokengethealthfactor) | [sdk.utils.getTransactions](#sdkutilsgettransactions) |
| [sdk.vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [sdk.osToken.getAPY](#sdkostokengetapy) |
| [sdk.vault.getValidators](#sdkvaultgetvalidators) | [sdk.osToken.getPosition](#sdkostokengetposition) |
| [sdk.vault.getVault](#sdkvaultgetvault) | [sdk.osToken.getMaxMint](#sdkostokengetmaxmint) |
| [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.getScorePercentiles](#sdkvaultgetscorepercentiles)
| [sdk.vault.getUserRewards](#sdkvaultgetuserrewards) | [sdk.vault.getScorePercentiles](#sdkvaultgetscorepercentiles)
| [sdk.vault.getWhitelist](#sdkvaultgetwhitelist)
| [sdk.vault.getBlocklist](#sdkvaultgetblocklist)

All of these methods (except synchronous getHealthFactor) return a promise that can be
aborted by invoking the `abort()` function.

If `abort()` is invoked, the in-progress promise will not be resolved or rejected, and the
network request will be canceled.
If the promise has already been resolved or rejected, invoking `abort()` will not have any effect.

Using `abort()` can be beneficial when querying lists such as `whitelist` or `blocklist`. If we
are retrieving the list based on a filter string from user input, even with debounced requests,
the user might continue typing and modify the filter after the initial request is sent. In such
cases, a second request may be initiated. To prevent fetching data from the first request, we
can call `abort()`.

```ts
const promise = sdk.vault.getWhitelist({
vaultAddress: '0x...',
})

promise.abort()
```

##### Table of transactions:
| **Vault** | **osToken** |
|------|------|
Expand Down
3 changes: 2 additions & 1 deletion src/methods/osToken/helpers/getBurnAmount.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { validateArgs } from '../../../utils'
import { wrapAbortPromise } from '../../../modules/gql-module'


type GetBurnAmountInput = {
Expand Down Expand Up @@ -37,4 +38,4 @@ const getBurnAmount = async (values: GetBurnAmountInput) => {
}


export default getBurnAmount
export default wrapAbortPromise<GetBurnAmountInput, bigint>(getBurnAmount)
5 changes: 4 additions & 1 deletion src/methods/osToken/requests/getAssetsFromShares.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { wrapAbortPromise } from '../../../modules/gql-module'


type GetAssetsFromSharesInput = {
contracts: StakeWise.Contracts
amount: bigint
Expand All @@ -12,4 +15,4 @@ const getAssetsFromShares = async (values: GetAssetsFromSharesInput) => {
}


export default getAssetsFromShares
export default wrapAbortPromise<GetAssetsFromSharesInput, bigint>(getAssetsFromShares)
29 changes: 15 additions & 14 deletions src/methods/osToken/requests/getAvgRewardsPerSecond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,33 @@ export type FetchOsTokenSnapshotsInput = {
options: StakeWise.Options
}

const getAvgRewardsPerSecond = async (input: FetchOsTokenSnapshotsInput) => {
const getAvgRewardsPerSecond = (input: FetchOsTokenSnapshotsInput) => {
const { options } = input

const data = await graphql.subgraph.osToken.fetchOsTokenRewardPerSecondQuery({
return graphql.subgraph.osToken.fetchOsTokenRewardPerSecondQuery({
url: apiUrls.getSubgraphqlUrl(options),
variables: {
first: 14,
orderBy: 'createdAt',
orderDirection: 'desc',
},
})

let rewardPerSecond = 0n
modifyResult: (data) => {
let rewardPerSecond = 0n

const count = BigInt(data.osTokenSnapshots.length || 0)
const count = BigInt(data.osTokenSnapshots.length || 0)

if (count) {
const sum = data.osTokenSnapshots.reduce(
(acc, { avgRewardPerSecond }) => acc + BigInt(avgRewardPerSecond),
0n
)
if (count) {
const sum = data.osTokenSnapshots.reduce(
(acc, { avgRewardPerSecond }) => acc + BigInt(avgRewardPerSecond),
0n
)

rewardPerSecond = sum / count
}
rewardPerSecond = sum / count
}

return rewardPerSecond
return rewardPerSecond
},
})
}


Expand Down
3 changes: 2 additions & 1 deletion src/methods/osToken/requests/getMaxMint.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { constants, validateArgs } from '../../../utils'
import { wrapAbortPromise } from '../../../modules/gql-module'


type GetMaxMintInput = {
Expand Down Expand Up @@ -33,4 +34,4 @@ const getMaxMint = async (values: GetMaxMintInput) => {
}


export default getMaxMint
export default wrapAbortPromise<GetMaxMintInput, bigint>(getMaxMint)
7 changes: 3 additions & 4 deletions src/methods/osToken/requests/getOsTokenAPY.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ export type FetchOsTokenSnapshotsInput = {
options: StakeWise.Options
}

const getOsTokenAPY = async (input: FetchOsTokenSnapshotsInput) => {
const getOsTokenAPY = (input: FetchOsTokenSnapshotsInput) => {
const { options } = input

const data = await graphql.subgraph.osToken.fetchOsTokenApyQuery({
return graphql.subgraph.osToken.fetchOsTokenApyQuery({
url: apiUrls.getSubgraphqlUrl(options),
modifyResult: (data) => data?.osTokens?.[0].apy || '0',
})

return data?.osTokens?.[0].apy || '0'
}


Expand Down
8 changes: 7 additions & 1 deletion src/methods/osToken/requests/getOsTokenData.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { constants, BigDecimal } from '../../../utils'
import { wrapAbortPromise } from '../../../modules/gql-module'


export type GetOsTokenDataInput = {
Expand All @@ -11,6 +12,11 @@ export type OsTokenDataMulticallReturnType = {
thresholdPercent: bigint
}

type Output = {
rate: string
ltvPercent: bigint
thresholdPercent: bigint
}

const getOsTokenData = async (input: GetOsTokenDataInput) => {
const { contracts } = input
Expand Down Expand Up @@ -53,4 +59,4 @@ const getOsTokenData = async (input: GetOsTokenDataInput) => {
}


export default getOsTokenData
export default wrapAbortPromise<GetOsTokenDataInput, Output>(getOsTokenData)
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ type GetOsTokenPositionSharesInput = {
options: StakeWise.Options
}

const getOsTokenPositionShares = async (values: GetOsTokenPositionSharesInput) => {
const getOsTokenPositionShares = (values: GetOsTokenPositionSharesInput) => {
const { options, vaultAddress, userAddress } = values

const result = await graphql.subgraph.osToken.fetchOsTokenPositionsQuery({
return graphql.subgraph.osToken.fetchOsTokenPositionsQuery({
url: apiUrls.getSubgraphqlUrl(options),
variables: {
vaultAddress,
address: userAddress,
},
modifyResult: (data) => BigInt(data?.osTokenPositions?.[0]?.shares || 0),
})

return BigInt(result?.osTokenPositions?.[0]?.shares || 0)
}


Expand Down
48 changes: 24 additions & 24 deletions src/methods/osToken/requests/getOsTokenPosition/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,35 @@ type GetOsTokenPositionInput = {
contracts: StakeWise.Contracts
}

const getOsTokenPosition = async (values: GetOsTokenPositionInput) => {
const getOsTokenPosition = (values: GetOsTokenPositionInput) => {
const { options, contracts, vaultAddress, userAddress, stakedAssets, thresholdPercent } = values

validateArgs.address({ vaultAddress, userAddress })
validateArgs.bigint({ stakedAssets, thresholdPercent })

const vaultContract = contracts.helpers.createVault(vaultAddress)

const gqlMintedShares = await getOsTokenPositionShares({ options, vaultAddress, userAddress })
const mintedShares = await vaultContract.osTokenPositions(userAddress)

const [ mintedAssets, feePercent ] = await Promise.all([
contracts.base.mintTokenController.convertToAssets(mintedShares),
contracts.base.mintTokenController.feePercent(),
])

const protocolFeePercent = feePercent / 100n
const healthFactor = getHealthFactor({ mintedAssets, stakedAssets, thresholdPercent })

return {
minted: {
assets: mintedAssets,
shares: mintedShares,
fee: mintedShares - gqlMintedShares,
},
healthFactor,
protocolFeePercent,
}
return getOsTokenPositionShares({ options, vaultAddress, userAddress })
.then(async (gqlMintedShares) => {
const vaultContract = contracts.helpers.createVault(vaultAddress)
const mintedShares = await vaultContract.osTokenPositions(userAddress)

const [ mintedAssets, feePercent ] = await Promise.all([
contracts.base.mintTokenController.convertToAssets(mintedShares),
contracts.base.mintTokenController.feePercent(),
])

const protocolFeePercent = feePercent / 100n
const healthFactor = getHealthFactor({ mintedAssets, stakedAssets, thresholdPercent })

return {
minted: {
assets: mintedAssets,
shares: mintedShares,
fee: mintedShares - gqlMintedShares,
},
healthFactor,
protocolFeePercent,
}
})
}


export default getOsTokenPosition
5 changes: 4 additions & 1 deletion src/methods/osToken/requests/getSharesFromAssets.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { wrapAbortPromise } from '../../../modules/gql-module'


type GetSharesFromAssetsInput = {
contracts: StakeWise.Contracts
amount: bigint
Expand All @@ -12,4 +15,4 @@ const getSharesFromAssets = async (values: GetSharesFromAssetsInput) => {
}


export default getSharesFromAssets
export default wrapAbortPromise<GetSharesFromAssetsInput, bigint>(getSharesFromAssets)
7 changes: 3 additions & 4 deletions src/methods/utils/getSwiseUsdPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ type GetSwiseUsdPriceInput = {
options: StakeWise.Options
}

const getSwiseUsdPrice = async (input: GetSwiseUsdPriceInput) => {
const getSwiseUsdPrice = (input: GetSwiseUsdPriceInput) => {
const { options } = input

const data = await graphql.backend.swise.fetchSwiseStatsQuery({
return graphql.backend.swise.fetchSwiseStatsQuery({
url: apiUrls.getBackendUrl(options),
modifyResult: (data) => data?.swiseStats?.price || '0',
})

return data?.swiseStats?.price || '0'
}


Expand Down
7 changes: 3 additions & 4 deletions src/methods/utils/getTransactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@ type GetTransactionsInput = {
options: StakeWise.Options
}

const getTransactions = async (input: GetTransactionsInput) => {
const getTransactions = (input: GetTransactionsInput) => {
const { options, hash } = input

validateArgs.string({ hash })

const data = await graphql.subgraph.transactions.fetchTransactionsQuery({
return graphql.subgraph.transactions.fetchTransactionsQuery({
url: apiUrls.getSubgraphqlUrl(options),
variables: {
where: {
id: hash,
},
} as TransactionsQueryVariables,
modifyResult: (data) => data?.transactions || [],
})

return data?.transactions || []
}


Expand Down
9 changes: 1 addition & 8 deletions src/methods/vault/requests/getBlocklist/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ type GetBlocklistInput = {
options: StakeWise.Options
}

type GetBlocklistOutput = {
blocklist: {
createdAt: number
address: string
}[]
}

const getBlocklist = async (input: GetBlocklistInput): Promise<GetBlocklistOutput> => {
const getBlocklist = (input: GetBlocklistInput) => {
const { vaultAddress, orderDirection, search, limit, skip, options } = input

validateArgs.address({ vaultAddress })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,17 @@ export type FetchExitQueuePositionsInput = {
userAddress: string
}

const fetchExitQueuePositions = async (values: FetchExitQueuePositionsInput) => {
const fetchExitQueuePositions = (values: FetchExitQueuePositionsInput) => {
const { options, vaultAddress, userAddress } = values

const data = await graphql.subgraph.exitQueue.fetchExitQueueQuery({
return graphql.subgraph.exitQueue.fetchExitQueueQuery({
url: apiUrls.getSubgraphqlUrl(options),
variables: {
vault: vaultAddress.toLowerCase(),
receiver: userAddress.toLowerCase(),
},
modifyResult: (data) => data?.exitRequests || [],
})

return data?.exitRequests || []
}


Expand Down
37 changes: 18 additions & 19 deletions src/methods/vault/requests/getExitQueuePositions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,24 @@ const getExitQueuePositions = async (input: GetExitQueuePositionsInput) => {

validateArgs.address({ vaultAddress, userAddress })

const data = await fetchExitQueuePositions({ options, vaultAddress, userAddress })

if (!data) {
return mock
}

const totalShares = data.reduce((acc, { totalShares }) => acc + BigInt(totalShares), 0n)

const exitQueue = await parseExitRequests({
options,
provider,
contracts,
userAddress,
totalShares,
vaultAddress,
exitRequests: data,
})

return exitQueue
return fetchExitQueuePositions({ options, vaultAddress, userAddress })
.then((data) => {
if (!data) {
return mock
}

const totalShares = data.reduce((acc, { totalShares }) => acc + BigInt(totalShares), 0n)

return parseExitRequests({
options,
provider,
contracts,
userAddress,
totalShares,
vaultAddress,
exitRequests: data,
})
})
}


Expand Down
Loading
Loading