Skip to content

Commit

Permalink
chore: remove cardano-cli ledger-state query, make AssetSupply.total …
Browse files Browse the repository at this point in the history
…optional

The benefit of this query is overshadowed by the resource demand. It's not a
supported interface, so was also always a risk for continual breaking changes.
The total ada supply will be restored if the information can be accessed via an
official API.

BREAKING CHANGE: AssetSupply.total is now an optional field, and will return null
  • Loading branch information
rhyslbw committed Mar 6, 2021
1 parent d07576b commit f86ef42
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 51 deletions.
2 changes: 1 addition & 1 deletion packages/api-cardano-db-hasura/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ type AssetBalance {
type AssetSupply {
circulating: String!
max: String!
total: String!
total: String
}

input AssetSupply_bool_exp {
Expand Down
9 changes: 0 additions & 9 deletions packages/api-cardano-db-hasura/src/CardanoCli.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { exec } from 'child_process'
import { Genesis, ShelleyProtocolParams } from './graphql_types'
import { Config } from './Config'
import { LedgerState } from './CardanoNodeClient'
import { knownEras, capitalizeFirstChar } from '@cardano-graphql/util'

export interface CardanoCliTip {
Expand All @@ -19,7 +18,6 @@ const isEraMismatch = (errorMessage: string, era: string): boolean => {
}

export interface CardanoCli {
getLedgerState(): Promise<LedgerState>,
getProtocolParams(): Promise<ProtocolParams>,
getTip(): Promise<CardanoCliTip>,
submitTransaction(filePath: string): Promise<void>
Expand Down Expand Up @@ -63,13 +61,6 @@ export function createCardanoCli (
})
}
return {
getLedgerState: () => query<LedgerState>(
'ledger-state',
{
jqOperation: '"{accountState: .nesEs.esAccountState, esNonMyopic: { rewardPot: .nesEs.esNonMyopic.rewardPotNM } }"',
withEraFlag: true
}
),
getProtocolParams: () => query<ProtocolParams>(
'protocol-parameters',
{
Expand Down
28 changes: 2 additions & 26 deletions packages/api-cardano-db-hasura/src/CardanoNodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@ import { CardanoCli } from './CardanoCli'
import fs from 'fs-extra'
import { AssetSupply, Transaction } from './graphql_types'
import pRetry from 'p-retry'
import util, { DataFetcher, knownEras } from '@cardano-graphql/util'
import util, { knownEras } from '@cardano-graphql/util'
import tempWrite from 'temp-write'
import { dummyLogger, Logger } from 'ts-log'
import { getHashOfSignedTransaction } from './util'

export type LedgerState = {
accountState: {
_reserves: string
_treasury: string
},
esNonMyopic: {
rewardPot: string
}
}

const fileTypeFromEra = (era: string) => {
switch (era) {
case 'mary' :
Expand All @@ -37,21 +27,12 @@ const isEraMismatch = (errorMessage: string): boolean =>
export class CardanoNodeClient {
readonly networkParams: string[]
public adaCirculatingSupply: AssetSupply['circulating']
public ledgerStateFetcher: DataFetcher<LedgerState>

constructor (
private cardanoCli: CardanoCli,
pollingInterval: number,
readonly lastConfiguredMajorVersion: number,
private logger: Logger = dummyLogger
) {
this.ledgerStateFetcher = new DataFetcher<LedgerState>(
'LedgerState',
this.cardanoCli.getLedgerState,
pollingInterval,
this.logger
)
}
) {}

public async getTip () {
const tip = await this.cardanoCli.getTip()
Expand Down Expand Up @@ -80,7 +61,6 @@ export class CardanoNodeClient {
this.logger
)
})
await this.ledgerStateFetcher.initialize()
}

public async isInCurrentEra () {
Expand All @@ -95,10 +75,6 @@ export class CardanoNodeClient {
return protocolVersion.major >= this.lastConfiguredMajorVersion
}

public async shutdown () {
await this.ledgerStateFetcher.shutdown()
}

public async submitTransaction (transaction: string): Promise<Transaction['hash']> {
for (const era of knownEras) {
const filePath = await tempWrite(`{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ query assets {
assetId
assetName
description
fingerprint
logo
name
unit
Expand Down
8 changes: 2 additions & 6 deletions packages/api-cardano-db-hasura/src/executableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
TimestampResolver,
URLResolver
} from 'graphql-scalars'
import BigNumber from 'bignumber.js'
import { CardanoNodeClient } from './CardanoNodeClient'
const GraphQLBigInt = require('graphql-bigint')

Expand All @@ -39,7 +38,7 @@ export const scalarResolvers = {
export async function buildSchema (
hasuraClient: HasuraClient,
genesis: Genesis,
cardanoNodeClient?: CardanoNodeClient
cardanoNodeClient: CardanoNodeClient
) {
const throwIfNotInCurrentEra = async (queryName: string) => {
if (!(await cardanoNodeClient.isInCurrentEra())) {
Expand Down Expand Up @@ -90,10 +89,7 @@ export async function buildSchema (
return {
supply: {
circulating: hasuraClient.adaCirculatingSupplyFetcher.value,
max: genesis.shelley.maxLovelaceSupply,
total: new BigNumber(genesis.shelley.maxLovelaceSupply)
.minus(new BigNumber(cardanoNodeClient.ledgerStateFetcher.value.accountState._reserves))
.toString()
max: genesis.shelley.maxLovelaceSupply
}
}
},
Expand Down

This file was deleted.

4 changes: 1 addition & 3 deletions packages/api-cardano-db-hasura/test/ada.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ describe('ada', () => {
const { ada } = result.data
const circulatingSupply = new BigNumber(ada.supply.circulating).toNumber()
const maxSupply = new BigNumber(ada.supply.max).toNumber()
const totalSupply = new BigNumber(ada.supply.total).toNumber()
expect(maxSupply).toEqual(genesis.shelley.maxLovelaceSupply)
expect(maxSupply).toBeGreaterThanOrEqual(circulatingSupply)
expect(maxSupply).toBeGreaterThanOrEqual(totalSupply)
expect(totalSupply).toBeGreaterThan(circulatingSupply)
expect(ada.supply.total).toBeNull()
})
})
3 changes: 2 additions & 1 deletion packages/api-cardano-db-hasura/test/assets.query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('assets', () => {
const { assets_aggregate, assets } = result.data
const { aggregate } = assets_aggregate
expect(aggregate.count).toBeDefined()
expect(assets).toMatchSnapshot()
expect(assets.length).toBeGreaterThan(0)
expect(assets[0].fingerprint.slice(0, 5)).toBe('asset')
})
})
8 changes: 7 additions & 1 deletion packages/api-cardano-db-hasura/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import path from 'path'
import { readSecrets } from '@src/util'
import { Config } from '@src/Config'
import { Genesis } from '@src/graphql_types'
import { createCardanoCli } from '@src/CardanoCli'
import { CardanoNodeClient } from '@src/CardanoNodeClient'

export async function buildClient (
apiUri: string,
Expand All @@ -35,6 +37,10 @@ export async function buildClient (
})
return client
} else {
const cardanoNodeClient = new CardanoNodeClient(
createCardanoCli(path.resolve('..', '..', '..', 'bin', 'cardano-cli'), genesis.shelley, 'jq'),
genesis.shelley.protocolParams.protocolVersion.major
)
const hasuraClient = new HasuraClient('hasura', hasuraUri, 1000 * 60 * 5)
const db = new Db({
...{ host: 'localhost', port: dbPort },
Expand All @@ -43,7 +49,7 @@ export async function buildClient (
await db.init({
onDbSetup: hasuraClient.applySchemaAndMetadata.bind(hasuraClient)
})
const schema = await buildSchema(hasuraClient, genesis)
const schema = await buildSchema(hasuraClient, genesis, cardanoNodeClient)
return utilDev.createIntegrationClient(schema)
}
}
1 change: 0 additions & 1 deletion packages/server/src/CompleteApiServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export async function CompleteApiServer (
if (config.cardanoCliPath !== undefined) {
cardanoNodeClient = new CardanoNodeClient(
createCardanoCli(config.cardanoCliPath, genesis.shelley, config.jqPath),
1000 * 60 * 10,
genesis.shelley.protocolParams.protocolVersion.major,
logger
)
Expand Down

0 comments on commit f86ef42

Please sign in to comment.