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

Polyfill #84

Closed
wants to merge 11 commits into from
Prev Previous commit
Next Next commit
Change to Number.parseInt for sense of unity
  • Loading branch information
joon9823 committed Sep 13, 2024
commit a0de75119788abc1aeeb86bc81725bc6b7be7e55
4 changes: 2 additions & 2 deletions src/client/lcd/api/ForwardingAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class ForwardingAPI extends BaseAPI {
params
)
.then((d) => ({
num_of_accounts: parseInt(d.num_of_accounts),
num_of_forwards: parseInt(d.num_of_forwards),
num_of_accounts: Number.parseInt(d.num_of_accounts),
num_of_forwards: Number.parseInt(d.num_of_forwards),
total_forwarded: Coin.fromData(d.total_forwarded),
}))
Comment on lines 39 to 45
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding error handling for parsing operations.

While the current implementation works well for valid input, it might be beneficial to add error handling for cases where the parsed strings are not valid numbers. This would make the code more robust and prevent potential runtime errors.

Here's a suggested implementation with error handling:

public async stats(
  channel: string,
  params: APIParams = {}
): Promise<ForwardingStats> {
  return this.c
    .get<ForwardingStats.Data>(
      `/noble/forwarding/v1/stats/${channel}`,
      params
    )
    .then((d) => {
      const num_of_accounts = Number.parseInt(d.num_of_accounts, 10);
      const num_of_forwards = Number.parseInt(d.num_of_forwards, 10);
      
      if (isNaN(num_of_accounts) || isNaN(num_of_forwards)) {
        throw new Error('Invalid data: num_of_accounts or num_of_forwards is not a number');
      }

      return {
        num_of_accounts,
        num_of_forwards,
        total_forwarded: Coin.fromData(d.total_forwarded),
      };
    });
}

This implementation adds checks for NaN values after parsing and throws an error if invalid data is encountered.

}
Expand Down
4 changes: 2 additions & 2 deletions src/client/lcd/api/OpchildAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ export class OpchildAPI extends BaseAPI {
.get<{
next_l1_sequence: string
}>(`/opinit/opchild/v1/next_l1_sequence`, params)
.then((d) => parseInt(d.next_l1_sequence))
.then((d) => Number.parseInt(d.next_l1_sequence))
}

public async nextL2Sequence(params: APIParams = {}): Promise<number> {
return this.c
.get<{
next_l2_sequence: string
}>(`/opinit/opchild/v1/next_l2_sequence`, params)
.then((d) => parseInt(d.next_l2_sequence))
.then((d) => Number.parseInt(d.next_l2_sequence))
}

public async baseDenom(
Expand Down
2 changes: 1 addition & 1 deletion src/client/lcd/api/OphostAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ export class OphostAPI extends BaseAPI {
.get<{
next_l1_sequence: string
}>(`/opinit/ophost/v1/bridges/${bridgeId}/next_l1_sequence`, params)
.then((d) => parseInt(d.next_l1_sequence))
.then((d) => Number.parseInt(d.next_l1_sequence))
}

public async batchInfos(
Expand Down
4 changes: 2 additions & 2 deletions src/core/ibc/core/client/Height.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class Height extends JSONSerializable<
public static fromAmino(data: Height.Amino): Height {
const { revision_number, revision_height } = data
return new Height(
parseInt(revision_number ?? '0'),
parseInt(revision_height ?? '0')
Number.parseInt(revision_number ?? '0'),
Number.parseInt(revision_height ?? '0')
)
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/ibc/core/client/msgs/tendermint/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ export class PartSetHeader extends JSONSerializable<

public static fromData(data: PartSetHeader.Data): PartSetHeader {
const { total, hash } = data
return new PartSetHeader(parseInt(total), hash)
return new PartSetHeader(Number.parseInt(total), hash)
}

public toData(): PartSetHeader.Data {
Expand Down