Skip to content

Commit

Permalink
refactor: recognize pool_units based on native resource details
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Oct 1, 2024
1 parent b953b0b commit baebc01
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 107 deletions.
35 changes: 4 additions & 31 deletions apps/dashboard/src/lib/summary/summary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,30 +46,6 @@ export type LayoutDataStakeInfo = {
totalReadyToClaim: BigNumber
}

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

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

const getPoolUnitData =
(
stateVersion: number,
Expand Down Expand Up @@ -303,13 +279,10 @@ export const produceSummary = (
const poolData = pipe(
() => Promise.all([account, fungibleResources, ledgerState]),
andThen(([account, fungibles, { stateVersion }]) =>
pipe(() => {
return getPoolUnits(
fungibles,
getEntityTypes,
getEntityDetailsFn(stateVersion)
)
}, andThen(getPoolUnitData(stateVersion, account)))()
pipe(
() => getPoolUnits(fungibles),
andThen(getPoolUnitData(stateVersion, account))
)()
)
)()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ export const load: LayoutLoad = async ({ params }) => {
throw redirect(308, `/claim_nft/${encodeURIComponent(params.resource)}`)
}
}
const transformedResource = await transformUnknownResource(
resource,
getEntityTypes,
getEntityDetails()
)
const transformedResource = await transformUnknownResource(resource)

const redeemableTokens =
transformedResource.type === 'poolUnit'
Expand Down
1 change: 0 additions & 1 deletion packages/ui/src/api/_deprecated/utils/entities/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
type GetEntityDetailsFn
} from './pool-unit'
import { type AuthInfo, isAllowed } from '../../../utils/auth'
import { err, ok } from 'neverthrow'

type _Resource<T extends 'fungible' | 'non-fungible'> = _Entity<
'resource',
Expand Down
68 changes: 12 additions & 56 deletions packages/ui/src/api/utils/entities/resource/fungible/pool-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ export const resourceToPoolUnit = (resource: FungibleResource): PoolUnit => ({
}
})

const getPoolAddress = (resource: FungibleResource) => {
const poolMetadataEntry = getMetadataItem('pool')({
items: resource.metadata.all
})
if (poolMetadataEntry?.value.typed.type === 'GlobalAddress')
return poolMetadataEntry?.value.typed.value
}

export const hasPoolMetadataSet = (resource: FungibleResource) => {
const poolMetadataEntry = getMetadataItem('pool')({
items: resource.metadata.all
Expand Down Expand Up @@ -111,55 +103,19 @@ export const verify2WayLinking =
const getPoolAddresses = (resources: FungibleResourceWithPoolAddress[]) =>
resources.map(({ poolAddress }) => poolAddress)

const filterByEntityType =
(validEntityTypes: Set<string>, getEntityTypesFn: GetEntityTypesFn) =>
async (resources: FungibleResourceWithPoolAddress[]) =>
pipe(
getPoolAddresses,
getEntityTypesFn,
andThen((entityTypes) =>
resources.filter(({ poolAddress }) => {
const poolEntityType = entityTypes[poolAddress]
return poolEntityType && validEntityTypes.has(poolEntityType)
})
export const getPoolUnits = (resources: FungibleResource[]) => {
return Promise.resolve(
resources
.filter((resource) =>
[
'TwoResourcePoolUnit',
'OneResourcePoolUnit',
'MultiResourcePoolUnit'
].includes(resource.nativeResourceDetails?.kind || '')
)
)(resources)

const extendWithPoolAddress = (resources: FungibleResource[]) =>
resources.map((resource) => ({
...resource,
poolAddress: getPoolAddress(resource)!
}))

export const verifyPoolUnit =
(
getEntityTypesFn: GetEntityTypesFn,
getEntityDetailsFn: GetEntityDetailsFn
) =>
(resources: FungibleResource[]) =>
pipe(
(resources: FungibleResource[]) => resources.filter(hasPoolMetadataSet),
extendWithPoolAddress,
filterByEntityType(
new Set([
'GlobalOneResourcePool',
'GlobalTwoResourcePool',
'GlobalMultiResourcePool'
]),
getEntityTypesFn
),
andThen(verify2WayLinking(getEntityDetailsFn))
)(resources)

export const getPoolUnits = (
resources: FungibleResource[],
getEntityTypesFn: GetEntityTypesFn,
getEntityDetailsFn: GetEntityDetailsFn
) =>
pipe(
verifyPoolUnit(getEntityTypesFn, getEntityDetailsFn),
andThen((resources) => resources.map(resourceToPoolUnit))
)(resources)
.map((resource) => resourceToPoolUnit(resource))
)
}

export const getPoolUnitMetadataValue = (
entity: StateEntityDetailsVaultResponseItem
Expand Down
17 changes: 3 additions & 14 deletions packages/ui/src/api/utils/entities/resource/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,7 @@ import type {
import { getBehaviors, type Behavior } from './behaviors'
import { getAuthInfo } from '@api/utils/auth'
import { transformFungibleResource } from './fungible'
import {
getPoolUnits,
hasPoolMetadataSet,
type GetEntityTypesFn,
type GetEntityDetailsFn
} from './fungible/pool-unit'
import { getPoolUnits, hasPoolMetadataSet } from './fungible/pool-unit'
import { transformNonFungibleResource } from './non-fungible'
import {
createStandardMetadata,
Expand Down Expand Up @@ -80,18 +75,12 @@ export const transformResource = <Metadata extends ExpectedMetadata>(
)()

export const transformUnknownResource = async (
entity: StateEntityDetailsVaultResponseItem,
getEntityTypesFn: GetEntityTypesFn,
getEntityDetailsFn: GetEntityDetailsFn
entity: StateEntityDetailsVaultResponseItem
) => {
if (entity.details?.type === 'FungibleResource') {
const fungible = transformFungibleResource(entity)
if (hasPoolMetadataSet(fungible)) {
return getPoolUnits(
[fungible],
getEntityTypesFn,
getEntityDetailsFn
).then((res) => res[0] ?? fungible)
return getPoolUnits([fungible]).then((res) => res[0] ?? fungible)
}
return fungible
}
Expand Down

0 comments on commit baebc01

Please sign in to comment.