Skip to content

Commit d946dba

Browse files
committed
refactor: remove type casts
1 parent a0fc2be commit d946dba

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

Diff for: apps/price_pusher/src/evm/evm.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ import {
44
ChainPriceListener,
55
PriceItem,
66
} from "../interface";
7-
import { addLeading0x, DurationInSeconds, removeLeading0x } from "../utils";
7+
import {
8+
addLeading0x,
9+
assertDefined,
10+
DurationInSeconds,
11+
removeLeading0x,
12+
} from "../utils";
813
import { PythAbi } from "./pyth-abi";
914
import { Logger } from "pino";
1015
import { isWsEndpoint } from "../utils";
@@ -53,8 +58,8 @@ type PythContract = GetContractReturnType<
5358

5459
export type SuperWalletClient<
5560
transport extends Transport = Transport,
56-
chain extends Chain | undefined = Chain | undefined,
57-
account extends Account | undefined = Account | undefined
61+
chain extends Chain | undefined = Chain,
62+
account extends Account | undefined = Account
5863
> = Client<
5964
transport,
6065
chain,
@@ -120,20 +125,20 @@ export class EvmPriceListener extends ChainPriceListener {
120125
private async startWatching() {
121126
this.pythContract.watchEvent.PriceFeedUpdate(
122127
{ id: this.priceItems.map((item) => addLeading0x(item.id)) },
123-
{ onLogs: this.onPriceFeedUpdate.bind(this) }
128+
{ strict: true, onLogs: this.onPriceFeedUpdate.bind(this) }
124129
);
125130
}
126131

127132
private onPriceFeedUpdate(
128133
logs: WatchContractEventOnLogsParameter<typeof PythAbi, "PriceFeedUpdate">
129134
) {
130135
for (const log of logs) {
131-
const priceId = removeLeading0x(log.args.id!);
136+
const priceId = removeLeading0x(assertDefined(log.args.id));
132137

133138
const priceInfo: PriceInfo = {
134-
conf: log.args.conf!.toString(),
135-
price: log.args.price!.toString(),
136-
publishTime: Number(log.args.publishTime!),
139+
conf: assertDefined(log.args.conf).toString(),
140+
price: assertDefined(log.args.price).toString(),
141+
publishTime: Number(assertDefined(log.args.publishTime)),
137142
};
138143

139144
this.logger.debug(
@@ -248,7 +253,7 @@ export class EvmPricePusher implements IPricePusher {
248253

249254
// Try to re-use the same nonce and increase the gas if the last tx is not landed yet.
250255
if (this.pusherAddress === undefined) {
251-
this.pusherAddress = this.client.account!.address;
256+
this.pusherAddress = this.client.account.address;
252257
}
253258

254259
const lastExecutedNonce =

Diff for: apps/price_pusher/src/utils.ts

+8
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,11 @@ export function verifyValidOption<
4646
option + " is not a valid option. Please choose between " + validOptions;
4747
throw new Error(errorString);
4848
}
49+
50+
export const assertDefined = <T>(value: T | undefined): T => {
51+
if (value === undefined) {
52+
throw new Error("Assertion failed: value was undefined");
53+
} else {
54+
return value;
55+
}
56+
};

0 commit comments

Comments
 (0)