Skip to content

Commit

Permalink
Merge pull request #51 from Concordium/type-guard-cleanup
Browse files Browse the repository at this point in the history
Remove type guards for versions that are subsets of others
  • Loading branch information
shjortConcordium authored May 11, 2022
2 parents 29282da + ff63158 commit ddb2649
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,21 +637,19 @@ const passiveDelegationStatus = await client.getPoolStatus(blockHash);
## getRewardStatus
Retrieves the current amount of funds in the system at a specific block, and the state of the special accounts.
Protocol version 4 expanded the amount of information in the response, so one should check the type to access that.
This information includes information about the payday and total amount of funds staked.
```js
const blockHash = "7f7409679e53875567e2ae812c9fcefe90ced8961d08554756f42bf268a42749";

const rewardStatus = await client.getRewardStatus(blockHash);
```
Protocol version 4 expanded the amount of information in the response, so one should check the type to access that.
This information includes information about the payday and total amount of funds staked.
```js
if (isRewardStatusV1(rewardStatus)) {
const nextPaydayTime = rewardStatus.nextPaydayTime;
...
} else if (isRewardStatusV0(rewardStatus)) {
// This status is for a block from protocol version <= 3, so it will only have limited information
...
}
...
```
## Check block for transfers with memo
Expand Down
7 changes: 1 addition & 6 deletions src/blockSummaryHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Authorizations,
AuthorizationsV0,
AuthorizationsV1,
BlockSummary,
BlockSummaryV0,
Expand All @@ -22,10 +21,6 @@ import {
export const isAuthorizationsV1 = (a: Authorizations): a is AuthorizationsV1 =>
(a as AuthorizationsV1).timeParameters !== undefined;

export function isAuthorizationsV0(a: Authorizations): a is AuthorizationsV0 {
return (a as AuthorizationsV1).timeParameters === undefined;
}

export const isChainParametersV1 = (
cp: ChainParameters
): cp is ChainParametersV1 =>
Expand All @@ -40,7 +35,7 @@ export const isKeysV1 = (k: Keys): k is KeysV1 =>
isAuthorizationsV1(k.level2Keys);

export const isKeysV0 = (k: Keys): k is KeysV0 =>
isAuthorizationsV0(k.level2Keys);
!isAuthorizationsV1(k.level2Keys);

export const isUpdateQueuesV1 = (uq: UpdateQueues): uq is UpdateQueuesV1 =>
(uq as UpdateQueuesV1).timeParameters !== undefined;
Expand Down
6 changes: 1 addition & 5 deletions src/rewardStatusHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { RewardStatus, RewardStatusV0, RewardStatusV1 } from './types';
import { RewardStatus, RewardStatusV1 } from './types';

export function isRewardStatusV1(rs: RewardStatus): rs is RewardStatusV1 {
return rs.protocolVersion !== undefined && rs.protocolVersion > 3n;
}

export function isRewardStatusV0(rs: RewardStatus): rs is RewardStatusV0 {
return rs.protocolVersion === undefined || rs.protocolVersion <= 3n;
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2120,6 +2120,11 @@ [email protected]:
resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.15.8.tgz#5f3948905e4951c867d6bc143f385a80e2a39efe"
integrity sha512-2jtfdqTaSxk0cuBJBtTTWsot4WtR9RVr2rXg7x7OoqiuOKopPrwXpM1G4dXIkLcUNRh3RKzz76C8IOkksZSeOw==

google-protobuf@^3.20.1:
version "3.20.1"
resolved "https://registry.yarnpkg.com/google-protobuf/-/google-protobuf-3.20.1.tgz#1b255c2b59bcda7c399df46c65206aa3c7a0ce8b"
integrity sha512-XMf1+O32FjYIV3CYu6Tuh5PNbfNEU5Xu22X+Xkdb/DUexFlCzhvv7d5Iirm4AOwn8lv4al1YvIhzGrg2j9Zfzw==

graceful-fs@^4.2.4:
version "4.2.8"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a"
Expand Down

0 comments on commit ddb2649

Please sign in to comment.