Skip to content

Commit

Permalink
feat: add StakePool.status and a few other minor improvements to Stak…
Browse files Browse the repository at this point in the history
…ePool types
  • Loading branch information
mkazlauskas committed Nov 5, 2021
1 parent 61f81f0 commit 1405d05
Show file tree
Hide file tree
Showing 11 changed files with 403 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Cardano } from '@cardano-sdk/core';
import { Directive, Field, ObjectType, registerEnumType } from 'type-graphql';

enum PoolStatus {
enum ExtendedPoolStatus {
active = 'active',
retired = 'retired',
offline = 'offline',
experimental = 'experimental',
private = 'private'
}

registerEnumType(PoolStatus, { name: 'PoolStatus' });
registerEnumType(ExtendedPoolStatus, { name: 'ExtendedPoolStatus' });

@ObjectType()
export class ITNVerification implements Cardano.ITNVerification {
Expand Down Expand Up @@ -62,11 +62,11 @@ export class ExtendedStakePoolMetadataFields implements Cardano.ExtendedStakePoo
id: string;
@Field({ nullable: true })
country?: string;
@Field(() => PoolStatus, {
@Field(() => ExtendedPoolStatus, {
description: 'active | retired | offline | experimental | private',
nullable: true
})
status?: Cardano.PoolStatus;
status?: Cardano.ExtendedPoolStatus;
@Field(() => PoolContactData, { nullable: true })
contact?: Cardano.PoolContactData;
@Field(() => ThePoolsMediaAssets, { nullable: true })
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/* eslint-disable no-use-before-define */
import { BigIntsAsStrings, coinDescription, percentageDescription } from '../../util';
import { Cardano } from '@cardano-sdk/core';
import { Directive, Field, Float, Int, ObjectType, createUnionType } from 'type-graphql';
import { Directive, Field, Float, Int, ObjectType, createUnionType, registerEnumType } from 'type-graphql';
import { ExtendedStakePoolMetadataFields } from './ExtendedStakePoolMetadataFields';

enum StakePoolStatus {
active = 'active',
retired = 'retired',
retiring = 'retiring'
}

registerEnumType(StakePoolStatus, { name: 'StakePoolStatus' });

// This is not in ./ExtendedStakePoolMetadata to avoid circular import
@ObjectType()
export class ExtendedStakePoolMetadata implements Cardano.ExtendedStakePoolMetadata {
Expand Down Expand Up @@ -66,17 +74,15 @@ export class StakePoolMetadataJson implements Cardano.PoolMetadata {
}

@ObjectType()
export class RelayByName implements Cardano.ByName {
type: 'singlehost-by-name';
export class RelayByName implements Omit<Cardano.RelayByName, '__typename'> {
@Field()
hostname: string;
@Field(() => Int, { nullable: true })
port: number;
}

@ObjectType()
export class RelayByAddress implements Cardano.ByAddress {
type: 'singlehost-by-address';
export class RelayByAddress implements Omit<Cardano.RelayByAddress, '__typename'> {
@Field(() => String, { nullable: true })
ipv4?: string;
@Field(() => String, { nullable: true })
Expand All @@ -85,12 +91,23 @@ export class RelayByAddress implements Cardano.ByAddress {
port?: number;
}

@ObjectType()
export class RelayByNameMultihost implements Omit<Cardano.RelayByNameMultihost, '__typename'> {
type: 'multihost-by-name';
@Field()
dnsName: string;
}

const Relay = createUnionType({
name: 'SearchResult',
// function that returns tuple of object types classes,
resolveType: (value) => ('hostname' in value ? RelayByName : RelayByAddress),
resolveType: (value) => {
if ('hostname' in value) return RelayByName;
if ('dnsName' in value) return RelayByNameMultihost;
return RelayByAddress;
},
// the name of the GraphQL union
types: () => [RelayByName, RelayByAddress] as const
types: () => [RelayByName, RelayByAddress, RelayByNameMultihost] as const
});

@ObjectType()
Expand Down Expand Up @@ -138,11 +155,15 @@ export class StakePool implements BigIntsAsStrings<Cardano.StakePool> {
id: string;
@Field()
hexId: string;
@Field(() => StakePoolStatus, {
description: 'active | retired | retiring'
})
status: Cardano.StakePoolStatus;
@Field({ description: coinDescription })
pledge: string;
@Field({ description: coinDescription })
cost: string;
@Field(() => Float)
@Field(() => Fraction)
margin: Fraction;
@Field(() => StakePoolMetrics)
metrics: BigIntsAsStrings<Cardano.StakePoolMetrics>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,9 @@ export const createGraphQLStakePoolSearchProvider = (
].filter(isNotNil);
return responseStakePools.map((responseStakePool) => {
const stakePool = util.replaceNullsWithUndefineds(responseStakePool);
const metadata = stakePool.metadata;
const ext = metadata?.ext;
return {
...stakePool,
cost: BigInt(stakePool.cost),
// TODO: Rebuild sdk.ts and convert 'margin' and 'relays' to updated types
margin: {} as Cardano.Fraction,
metadata: metadata
? {
...metadata,
ext: ext
? {
...ext,
pool: {
...ext.pool,
status: ext.pool.status as unknown as Cardano.PoolStatus
}
}
: undefined
}
: undefined,
metrics: {
...stakePool.metrics!,
livePledge: BigInt(stakePool.metrics.livePledge),
Expand All @@ -56,8 +38,7 @@ export const createGraphQLStakePoolSearchProvider = (
live: BigInt(stakePool.metrics.stake.live)
}
},
pledge: BigInt(stakePool.pledge),
relays: []
pledge: BigInt(stakePool.pledge)
};
});
} catch (error) {
Expand Down
9 changes: 8 additions & 1 deletion packages/cardano-graphql/src/operations/stakePools.graphql
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
fragment allStakePoolFields on StakePool {
id
hexId
status
owners
cost
margin
margin {
numerator
denominator
}
vrf
relays {
__typename
Expand All @@ -16,6 +20,9 @@ fragment allStakePoolFields on StakePool {
ipv6
port
}
... on RelayByNameMultihost {
dnsName
}
}
rewardAccount
pledge
Expand Down
Loading

0 comments on commit 1405d05

Please sign in to comment.