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.
ali-bahjati authored Aug 21, 2024
1 parent 7fddbd6 commit 5c3be6a
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",
@@ -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"
}
30 changes: 18 additions & 12 deletions apps/price_pusher/src/evm/command.ts
Original file line number Diff line number Diff line change
@@ -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",
@@ -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,
@@ -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,
@@ -148,7 +153,8 @@ export default {
);
const evmPusher = new EvmPricePusher(
priceServiceConnection,
pythContractFactory,
client,
pythContract,
logger.child({ module: "EvmPricePusher" }),
overrideGasPriceMultiplier,
overrideGasPriceMultiplierCap,
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,
@@ -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;
@@ -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,
435 changes: 215 additions & 220 deletions apps/price_pusher/src/evm/evm.ts

Large diffs are not rendered by default.

660 changes: 660 additions & 0 deletions apps/price_pusher/src/evm/pyth-abi.ts

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions apps/price_pusher/src/evm/pyth-contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getContract, Address, GetContractReturnType } from "viem";
import { PythAbi } from "./pyth-abi";
import { SuperWalletClient } from "./super-wallet";

export type PythContract = GetContractReturnType<
typeof PythAbi,
SuperWalletClient
>;

export const createPythContract = (
client: SuperWalletClient,
address: Address
): PythContract =>
getContract({
client,
abi: PythAbi,
address,
});
71 changes: 71 additions & 0 deletions apps/price_pusher/src/evm/super-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
createPublicClient,
createWalletClient,
defineChain,
http,
webSocket,
Account,
Chain,
publicActions,
Client,
RpcSchema,
WalletActions,
PublicActions,
WebSocketTransport,
HttpTransport,
Transport,
} from "viem";
import { mnemonicToAccount } from "viem/accounts";
import * as chains from "viem/chains";
import { isWsEndpoint } from "../utils";

const UNKNOWN_CHAIN_CONFIG = {
name: "Unknown",
nativeCurrency: {
name: "Unknown",
symbol: "Unknown",
decimals: 18,
},
rpcUrls: {
default: {
http: [],
},
},
};

export type SuperWalletClient = Client<
Transport,
Chain,
Account,
RpcSchema,
PublicActions<Transport, Chain, Account> & WalletActions<Chain, Account>
>;

// Get the transport based on the endpoint
const getTransport = (endpoint: string): WebSocketTransport | HttpTransport =>
isWsEndpoint(endpoint) ? webSocket(endpoint) : http(endpoint);

// Get the chain corresponding to the chainId. If the chain is not found, it will return
// an unknown chain which should work fine in most of the cases. We might need to update
// the viem package to support new chains if they don't work as expected with the unknown
// chain.
const getChainById = (chainId: number): Chain =>
Object.values(chains).find((chain) => chain.id === chainId) ||
defineChain({ id: chainId, ...UNKNOWN_CHAIN_CONFIG });

export const createClient = async (
endpoint: string,
mnemonic: string
): Promise<SuperWalletClient> => {
const transport = getTransport(endpoint);

const chainId = await createPublicClient({
transport,
}).getChainId();

return createWalletClient({
transport,
account: mnemonicToAccount(mnemonic),
chain: getChainById(chainId),
}).extend(publicActions);
};
19 changes: 13 additions & 6 deletions apps/price_pusher/src/utils.ts
Original file line number Diff line number Diff line change
@@ -18,12 +18,11 @@ export function removeLeading0x(id: HexString): HexString {
return id;
}

export function addLeading0x(id: HexString): HexString {
if (id.startsWith("0x")) {
return id;
}
return "0x" + id;
}
export const addLeading0x = (id: HexString): `0x${string}` =>
hasLeading0x(id) ? id : `0x${id}`;

const hasLeading0x = (input: string): input is `0x${string}` =>
input.startsWith("0x");

export function isWsEndpoint(endpoint: string): boolean {
const url = new URL(endpoint);
@@ -47,3 +46,11 @@ export function verifyValidOption<
option + " is not a valid option. Please choose between " + validOptions;
throw new Error(errorString);
}

export const assertDefined = <T>(value: T | undefined): T => {
if (value === undefined) {
throw new Error("Assertion failed: value was undefined");
} else {
return value;
}
};
85 changes: 63 additions & 22 deletions pnpm-lock.yaml

0 comments on commit 5c3be6a

Please sign in to comment.