Skip to content

Commit

Permalink
refactor(apps/price_pusher): Use viem instead of web3 for evm pusher (#…
Browse files Browse the repository at this point in the history
…1829)

Web3 library is not very widely used anymore due to its complex and magical internal design. Some users were reporting issues like strange timeouts or unsupported RPC calls via web3 (and the deprecated HDWalletProvider by truffle that we use). This change refactors the code to use Viem. The experience with Viem is nice: it has strong types and good utilities. The error handling is also very well designed. The downside is that it has a steep learning curve to get it right.

This change refactors the code based on the PR reviews to make it simpler as well.

Lastly the version is bumped as a major release because the behaviour and logs have changed and it might affect production environments. It also signals the users to test it out properly before using it which is good because all the failure cases might not be handled.
  • Loading branch information
ali-bahjati authored Aug 21, 2024
1 parent 7fddbd6 commit 5c3be6a
Show file tree
Hide file tree
Showing 9 changed files with 1,064 additions and 270 deletions.
7 changes: 2 additions & 5 deletions apps/price_pusher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/price-pusher",
"version": "7.1.0",
"version": "8.0.0-alpha",
"description": "Pyth Price Pusher",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down Expand Up @@ -64,16 +64,13 @@
"@pythnetwork/pyth-sui-js": "workspace:*",
"@pythnetwork/solana-utils": "workspace:*",
"@solana/web3.js": "^1.93.0",
"@truffle/hdwallet-provider": "^2.1.3",
"@types/pino": "^7.0.5",
"aptos": "^1.8.5",
"jito-ts": "^3.0.1",
"joi": "^17.6.0",
"near-api-js": "^3.0.2",
"pino": "^9.2.0",
"web3": "^1.8.1",
"web3-core": "^1.8.1",
"web3-eth-contract": "^1.8.1",
"viem": "^2.19.4",
"yaml": "^2.1.1",
"yargs": "^17.5.1"
}
Expand Down
30 changes: 18 additions & 12 deletions apps/price_pusher/src/evm/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import * as options from "../options";
import { readPriceConfigFile } from "../price-config";
import { PythPriceListener } from "../pyth-price-listener";
import { Controller } from "../controller";
import { EvmPriceListener, EvmPricePusher, PythContractFactory } from "./evm";
import { EvmPriceListener, EvmPricePusher } from "./evm";
import { getCustomGasStation } from "./custom-gas-station";
import pino from "pino";
import { createClient } from "./super-wallet";
import { createPythContract } from "./pyth-contract";
import { isWsEndpoint } from "../utils";

export default {
command: "evm",
Expand Down Expand Up @@ -77,7 +80,7 @@ export default {
...options.priceServiceConnectionLogLevel,
...options.controllerLogLevel,
},
handler: function (argv: any) {
handler: async function (argv: any) {
// FIXME: type checks for this
const {
endpoint,
Expand Down Expand Up @@ -121,20 +124,22 @@ export default {
logger.child({ module: "PythPriceListener" })
);

const pythContractFactory = new PythContractFactory(
endpoint,
mnemonic,
pythContractAddress
);
const client = await createClient(endpoint, mnemonic);
const pythContract = createPythContract(client, pythContractAddress);

logger.info(
`Pushing updates from wallet address: ${pythContractFactory
.createWeb3PayerProvider()
.getAddress()}`
`Pushing updates from wallet address: ${client.account.address}`
);

// It is possible to watch the events in the non-ws endpoints, either by getFilter
// or by getLogs, but it is very expensive and our polling mechanism does it
// in a more efficient way. So we only do it with ws endpoints.
const watchEvents = isWsEndpoint(endpoint);

const evmListener = new EvmPriceListener(
pythContractFactory,
pythContract,
priceItems,
watchEvents,
logger.child({ module: "EvmPriceListener" }),
{
pollingFrequency,
Expand All @@ -148,7 +153,8 @@ export default {
);
const evmPusher = new EvmPricePusher(
priceServiceConnection,
pythContractFactory,
client,
pythContract,
logger.child({ module: "EvmPricePusher" }),
overrideGasPriceMultiplier,
overrideGasPriceMultiplierCap,
Expand Down
9 changes: 4 additions & 5 deletions apps/price_pusher/src/evm/custom-gas-station.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Web3 from "web3";
import {
CustomGasChainId,
TxSpeed,
Expand All @@ -7,8 +6,9 @@ import {
customGasChainIds,
} from "../utils";
import { Logger } from "pino";
import { parseGwei } from "viem";

type chainMethods = Record<CustomGasChainId, () => Promise<string | undefined>>;
type chainMethods = Record<CustomGasChainId, () => Promise<bigint | undefined>>;

export class CustomGasStation {
private chain: CustomGasChainId;
Expand All @@ -29,11 +29,10 @@ export class CustomGasStation {

private async fetchMaticMainnetGasPrice() {
try {
const res = await fetch("https://gasstation-mainnet.matic.network/v2");
const res = await fetch("https://gasstation.polygon.technology/v2");
const jsonRes = await res.json();
const gasPrice = jsonRes[this.speed].maxFee;
const gweiGasPrice = Web3.utils.toWei(gasPrice.toFixed(2), "Gwei");
return gweiGasPrice.toString();
return parseGwei(gasPrice.toFixed(2));
} catch (err) {
this.logger.error(
err,
Expand Down
Loading

0 comments on commit 5c3be6a

Please sign in to comment.