Skip to content

refactor(apps/price_pusher): Use viem instead of web3 for evm pusher #1829

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

Merged
merged 8 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading