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

[issue-334] Dynamically calc Byron epochs length #338

Merged
merged 8 commits into from
Aug 4, 2020
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ yarn-error.log
.env
generated/
result*
schema.graphql
22 changes: 2 additions & 20 deletions .graphqlconfig
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
{
"name": "Cardano GraphQL - Cardano DB Hasura Schema",
"schemaPath": "node_modules/@cardano-graphql/client-ts/api/cardano-db-hasura/schema.graphql",
"extensions": {
"endpoints": {
"Mainnet GraphQL Endpoint": {
"url": "https://cardano-graphql-mainnet.daedalus-operations.com",
"headers": {
"user-agent": "JS GraphQL"
},
"introspect": false
},
"Testnet GraphQL Endpoint": {
"url": "https://cardano-graphql-testnet.daedalus-operations.com",
"headers": {
"user-agent": "JS GraphQL"
},
"introspect": false
}
}
}
"name": "Cardano GraphQL Schema",
"schemaPath": "./schema.graphql"
}
4 changes: 1 addition & 3 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
overwrite: true
schema:
- ./node_modules/@cardano-graphql/client-ts/api/cardano-db-hasura/schema.graphql
- ./node_modules/@cardano-graphql/client-ts/api/genesis/schema.graphql
schema: schema.graphql
documents: "source/**/*.graphql"
generates:
generated/typings/graphql-schema.d.ts:
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"test": "jest",
"test:e2e": "cypress run",
"test:e2e:dev": "cypress open",
"generate:graphql-typings": "graphql-codegen",
"generate:graphql-schema": "node source/utils/generateGraphQLSchema.js",
"generate:graphql-typings": "yarn generate:graphql-schema && graphql-codegen",
"export": "next export source --outdir build/static",
"static:build": "NODE_ENV=production yarn build && yarn export",
"static:serve": "serve ./build/static",
Expand All @@ -32,7 +33,6 @@
}
},
"dependencies": {
"@cardano-graphql/client-ts": "2.0.0",
"@types/react-addons-css-transition-group": "15.0.5",
"browser-update": "3.3.9",
"cardano-js": "0.3.0",
Expand Down Expand Up @@ -71,10 +71,15 @@
"@babel/plugin-transform-react-display-name": "7.8.3",
"@babel/plugin-transform-react-jsx-self": "7.9.0",
"@babel/plugin-transform-react-jsx-source": "7.9.0",
"@cardano-graphql/client-ts": "2.0.0",
"@cypress/webpack-preprocessor": "4.1.1",
"@graphql-codegen/cli": "1.2.0",
"@graphql-codegen/typescript": "1.2.0",
"@graphql-codegen/typescript-operations": "1.2.0",
"@graphql-tools/load-files": "6.0.15",
"@graphql-tools/merge": "6.0.15",
"@graphql-tools/schema": "6.0.15",
"@graphql-tools/utils": "6.0.15",
"@prefresh/next": "0.3.1",
"@storybook/addon-actions": "5.1.11",
"@storybook/addon-info": "5.1.11",
Expand Down Expand Up @@ -131,7 +136,7 @@
"serve": "11.1.0",
"ts-jest": "26.1.1",
"ts-loader": "6.0.4",
"ts-node": "7.0.1",
"ts-node": "8.10.2",
"tslint": "5.12.0",
"tslint-config-prettier": "1.17.0",
"typescript": "3.9.6",
Expand Down
5 changes: 4 additions & 1 deletion source/features/epochs/api/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ export const epochOverviewTransformer = (
output: Currency.Util.lovelacesToAda(e.output),
percentage:
e.number === n.currentEpoch ? n.currentEpochPercentageComplete : 100,
slotsCount: !!e.blocks[0].protocolVersion ? n.shelleyEpochLength : 21600,
slotsCount:
(!!e.blocks[0].protocolVersion
? n.shelleyEpochLength
: n.byronSlotsPerEpoch) || 21600,
startedAt: new Date(e.startedAt),
};
};
11 changes: 0 additions & 11 deletions source/features/network-info/api/cardanoDynamic.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

query cardanoDynamic {
cardano {
tip {
Expand All @@ -11,14 +10,4 @@ query cardanoDynamic {
number
}
}
genesis {
byron {
protocolConsts {
k
}
}
shelley {
epochLength
}
}
}
6 changes: 5 additions & 1 deletion source/features/network-info/api/cardanoStatic.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

query cardanoStatic {
genesis {
byron {
protocolConsts {
k
}
}
shelley {
epochLength
systemStart
Expand Down
16 changes: 11 additions & 5 deletions source/features/network-info/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import { NetworkInfoActions } from './index';
export class NetworkInfoStore extends Store {
@observable public blockHeight: number;
@observable public currentEpoch: number;
@observable public shelleyEpochLength: number;
@observable public isShelleyEra: boolean;
@observable public lastSlotFilled: number;
@observable public lastBlockTime: Date;
@observable public startTime: Date;
// Epoch lengths for various Cardano eras
@observable public byronSlotsPerEpoch?: number;
@observable public shelleyEpochLength?: number;
@observable public slotsPerPresentEpoch: number;

private readonly networkInfoApi: NetworkInfoApi;
Expand Down Expand Up @@ -69,13 +71,15 @@ export class NetworkInfoStore extends Store {
if (result) {
const { cardano } = result;
const { currentEpoch, tip } = cardano;
const fallbackslotsPerPresentEpoch = 21600;
runInAction(() => {
this.isShelleyEra = !!tip.protocolVersion;
this.slotsPerPresentEpoch = this.isShelleyEra ? this.shelleyEpochLength || fallbackslotsPerPresentEpoch : fallbackslotsPerPresentEpoch;
this.slotsPerPresentEpoch =
(this.isShelleyEra
? this.shelleyEpochLength
: this.byronSlotsPerEpoch) || 21600;
this.blockHeight = tip.number || 0;
this.currentEpoch = currentEpoch.number;
this.lastSlotFilled = tip.slotInEpoch || 0;
this.lastSlotFilled = tip.slotInEpoch || 0;
this.lastBlockTime = new Date(tip.forgedAt);
});
}
Expand All @@ -85,7 +89,9 @@ export class NetworkInfoStore extends Store {
const result = await this.networkInfoApi.fetchStatic.execute({});
if (result) {
const { genesis } = result;
this.shelleyEpochLength = genesis.shelley?.epochLength || 21600
const k = genesis.byron?.protocolConsts?.k;
this.byronSlotsPerEpoch = k != null ? k * 10 : undefined;
this.shelleyEpochLength = genesis.shelley?.epochLength ?? undefined;
// if (genesis.networkName !== environment.CARDANO.NETWORK) {
// throw new Error(
// `Cardano GraphQL is connected to ${cardano.networkName}, whereas the web app is expecting ${environment.CARDANO.NETWORK}. The instance of Cardano GraphQL needs to be configured to match our expected environment.`
Expand Down
17 changes: 17 additions & 0 deletions source/utils/generateGraphQLSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const fs = require('fs');
const path = require('path');
const { loadFilesSync } = require('@graphql-tools/load-files');
const { mergeTypeDefs } = require('@graphql-tools/merge');
const { printSchemaWithDirectives } = require('@graphql-tools/utils');
const { makeExecutableSchema } = require('@graphql-tools/schema');

const cardanoDbHasuraSchema = loadFilesSync('./node_modules/@cardano-graphql/client-ts/api/cardano-db-hasura/schema.graphql');
const genesisSchema = loadFilesSync('./node_modules/@cardano-graphql/client-ts/api/genesis/schema.graphql');
const mergedTypes = mergeTypeDefs([cardanoDbHasuraSchema, genesisSchema], {
throwOnConflict: true
});

const executableSchema = makeExecutableSchema({ typeDefs: [mergedTypes] });
const schema = printSchemaWithDirectives(executableSchema);

fs.writeFileSync(path.join(__dirname, '../../schema.graphql'), schema);
Loading