Skip to content

Commit

Permalink
refactor: use native_resource_details to find LSUs and Claim NFTs
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Oct 1, 2024
1 parent baebc01 commit d74a381
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,11 @@ import { callApi } from '@api/_deprecated/gateway'
import { andThen, pipe } from 'ramda'
import { handleGatewayResult } from '../../../../utils'
import { getStringMetadata } from '@api/utils/metadata'
import type { EntityType } from '@common/ret'
import { http } from '@common/http'
import { hasValidatorMetadataSet } from '@api/utils/entities/component/validator'
import { verifyStakeUnit } from '@api/utils/entities/resource/fungible/stake-unit'
import { verifyClaimNft } from '@api/utils/entities/resource/non-fungible/claim-nft-collection'
import { transformUnknownResource } from '@api/utils/entities/resource'
import type { PoolUnit } from '@api/utils/entities/resource/fungible/pool-unit'

const ERROR_MSG = 'Failed to load resource data.'

const getEntityTypes = async (
addresses: string[]
): Promise<{ [address: string]: EntityType }> =>
http.post('/api/ret/entity-type', {
addresses
})

const getEntityDetails = (stateVersion?: number) => (addresses: string[]) =>
pipe(
() =>
callApi(
'getEntityDetailsVaultAggregated',
addresses,
{
dappTwoWayLinks: true,
nativeResourceDetails: true
},
stateVersion
? {
state_version: stateVersion
}
: undefined
),
handleGatewayResult((_) => ERROR_MSG)
)()

const getRedeemableTokens = async (poolUnit: PoolUnit) => {
const pool = poolUnit.metadata.expected.pool!.typed.value

Expand All @@ -71,15 +40,13 @@ export const load: LayoutLoad = async ({ params }) => {

const resource = await getLookupEntity(params.resource)

const isValidStakeUnit = await verifyStakeUnit(resource)
const isValidClaimNft = await verifyClaimNft(resource)
const isValidStakeUnit = verifyStakeUnit(resource)
const isValidClaimNft = verifyClaimNft(resource)

if (hasValidatorMetadataSet(resource)) {
if (isValidStakeUnit) {
throw redirect(308, `/stake_unit/${encodeURIComponent(params.resource)}`)
} else if (isValidClaimNft) {
throw redirect(308, `/claim_nft/${encodeURIComponent(params.resource)}`)
}
if (isValidStakeUnit) {
throw redirect(308, `/stake_unit/${encodeURIComponent(params.resource)}`)
} else if (isValidClaimNft) {
throw redirect(308, `/claim_nft/${encodeURIComponent(params.resource)}`)
}
const transformedResource = await transformUnknownResource(resource)

Expand Down
14 changes: 0 additions & 14 deletions packages/ui/src/api/utils/entities/component/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type {
import {
getEnumStringMetadata,
transformMetadata,
getStringMetadata,
createSystemMetadata,
createStandardMetadata
} from '../../metadata'
Expand Down Expand Up @@ -515,16 +514,3 @@ export const getValidators = <
withStakeUnits
)
)

export const getValidatorMetadataValue = (
entity: StateEntityDetailsVaultResponseItem
) => getStringMetadata('validator')(entity.metadata)

export const hasValidatorMetadataSet = (
entity: StateEntityDetailsVaultResponseItem
) => {
const validatorAddress = getValidatorMetadataValue(entity)
const isValidatorAddress = validatorAddress.startsWith('validator_')

return isValidatorAddress
}
44 changes: 4 additions & 40 deletions packages/ui/src/api/utils/entities/resource/fungible/stake-unit.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
import { callApi } from '@api/_deprecated/gateway'
import type { _Entity } from '../..'
import {
getStringMetadata,
transformMetadata,
createSystemMetadata
} from '../../../metadata'
import { transformMetadata, createSystemMetadata } from '../../../metadata'
import type { StateEntityDetailsVaultResponseItem } from '@common/gateway-sdk'
import type { FungibleResource } from '.'
import { ok } from 'neverthrow'
import { getValidatorMetadataValue } from '../../component/validator'
import { getPoolUnitMetadataValue } from './pool-unit'

const systemMetadata = createSystemMetadata({
validator: 'GlobalAddress'
Expand Down Expand Up @@ -41,34 +33,6 @@ export const resourceToStakeUnit = (resource: FungibleResource): StakeUnit => {
}
}

export const isStakeUnit = async (
entity: StateEntityDetailsVaultResponseItem
) => {
const validator = await getStringMetadata('validator')(entity.metadata)
if (!validator) return false
const validatorEntityResult = await callApi(
'getEntityDetailsVaultAggregated',
[validator]
)
return validatorEntityResult.match(
(validatorEntity) =>
!!getStringMetadata('pool_unit')(validatorEntity[0].metadata),
() => false
)
}

const getEntityDetails = (address: string) =>
callApi('getEntityDetailsVaultAggregated', [address])

export const verifyStakeUnit = async (
entity: StateEntityDetailsVaultResponseItem
) => {
const validatorMetadataValue = getValidatorMetadataValue(entity)
if (!validatorMetadataValue) return false
const result = await ok(validatorMetadataValue)
.asyncAndThen(getEntityDetails)
.map(([entity]) => getPoolUnitMetadataValue(entity))
.map((poolUnit) => poolUnit === entity.address)

return result.isOk() && result.value === true
}
export const verifyStakeUnit = (entity: StateEntityDetailsVaultResponseItem) =>
entity.details?.type === 'FungibleResource' &&
entity.details.native_resource_details?.kind === 'ValidatorLiquidStakeUnit'
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { createSystemMetadata, getStringMetadata } from '@api/utils/metadata'
import type { Resource, standardMetadata } from '..'
import type { StateEntityDetailsVaultResponseItem } from '@common/gateway-sdk'
import { callApi } from '@api/gateway'
import { ok } from 'neverthrow'
import {
getValidatorMetadataValue,
type Validator,
type ValidatorListItem
} from '../../component/validator'

import type { DefaultNonFungibleResource } from '.'
import { transformMetadata } from '@api/utils/metadata'

Expand All @@ -29,9 +24,6 @@ export const getClaimNftMetadataValue = (
entity: StateEntityDetailsVaultResponseItem
) => getStringMetadata('claim_nft')(entity.metadata)

const getEntityDetails = (address: string) =>
callApi('getEntityDetailsVaultAggregated', [address])

export const isClaimNftCollection = (
resourceEntity: StateEntityDetailsVaultResponseItem
) => {
Expand Down Expand Up @@ -63,15 +55,6 @@ export const resourceToClaimNftCollection = (
}
})

export const verifyClaimNft = async (
entity: StateEntityDetailsVaultResponseItem
) => {
const validatorMetadataValue = getValidatorMetadataValue(entity)
if (!validatorMetadataValue) return false
const result = await ok(validatorMetadataValue)
.asyncAndThen(getEntityDetails)
.map(([entity]) => getClaimNftMetadataValue(entity))
.map((claimNft) => claimNft === entity.address)

return result.isOk() && result.value === true
}
export const verifyClaimNft = (entity: StateEntityDetailsVaultResponseItem) =>
entity.details?.type === 'NonFungibleResource' &&
entity.details.native_resource_details?.kind === 'ValidatorClaimNft'

0 comments on commit d74a381

Please sign in to comment.