Skip to content
This repository has been archived by the owner on Apr 3, 2023. It is now read-only.

added custom-gas-station for chains that require different gas price querying #91

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 34 additions & 0 deletions pyth-evm-price-pusher/src/custom-gas-station.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Web3 from "web3";
import {
CustomGasChainId,
TxSpeed,
verifyValidOption,
txSpeeds,
customGasChainIds,
} from "./utils";

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

export class CustomGasStation {
private chain: CustomGasChainId;
private speed: TxSpeed;
private chainMethods: chainMethods = {
137: this.fetchMaticMainnetGasPrice.bind(this),
};
constructor(chain: number, speed: string) {
this.speed = verifyValidOption(speed, txSpeeds);
this.chain = verifyValidOption(chain, customGasChainIds);
}

async getCustomGasPrice() {
return this.chainMethods[this.chain]();
}

private async fetchMaticMainnetGasPrice() {
const res = await fetch("https://gasstation-mainnet.matic.network/v2");
const jsonRes = await res.json();
const gasPrice = jsonRes[this.speed].maxFee;
const gweiGasPrice = Web3.utils.toWei(gasPrice.toFixed(2), "Gwei");
return gweiGasPrice.toString();
}
}
25 changes: 24 additions & 1 deletion pyth-evm-price-pusher/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { PythPriceListener } from "./pyth-price-listener";
import fs from "fs";
import { readPriceConfigFile } from "./price-config";
import { PythContractFactory } from "./pyth-contract-factory";
import { CustomGasStation } from "./custom-gas-station";

const argv = yargs(hideBin(process.argv))
.option("evm-endpoint", {
Expand Down Expand Up @@ -63,6 +64,18 @@ const argv = yargs(hideBin(process.argv))
required: false,
default: 5,
})
.option("custom-gas-station", {
description:
"If using a custom gas station, chainId of custom gas station to use",
type: "number",
required: false,
})
.option("tx-speed", {
description:
"txSpeed for custom gas station. choose between 'slow'|'standard'|'fast'",
type: "string",
required: false,
})
.help()
.alias("help", "h")
.parserConfiguration({
Expand Down Expand Up @@ -101,6 +114,15 @@ async function run() {

const pythPriceListener = new PythPriceListener(connection, priceConfigs);

let customGasStation: CustomGasStation | undefined;

if (argv.customGasStation && argv.txSpeed) {
customGasStation = new CustomGasStation(
argv.customGasStation,
argv.txSpeed
);
}

const handler = new Pusher(
connection,
pythContractFactory,
Expand All @@ -109,7 +131,8 @@ async function run() {
priceConfigs,
{
cooldownDuration: argv.cooldownDuration,
}
},
customGasStation
);

await evmPriceListener.start();
Expand Down
11 changes: 8 additions & 3 deletions pyth-evm-price-pusher/src/pusher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Contract } from "web3-eth-contract";
import { PriceConfig } from "./price-config";
import { TransactionReceipt } from "ethereum-protocol";
import { PythContractFactory } from "./pyth-contract-factory";
import { CustomGasStation } from "./custom-gas-station";

export class Pusher {
private connection: EvmPriceServiceConnection;
Expand All @@ -16,8 +17,8 @@ export class Pusher {
private targetPriceListener: PriceListener;
private sourcePriceListener: PriceListener;
private priceConfigs: PriceConfig[];

private cooldownDuration: DurationInSeconds;
private customGasStation?: CustomGasStation;

constructor(
connection: EvmPriceServiceConnection,
Expand All @@ -27,7 +28,8 @@ export class Pusher {
priceConfigs: PriceConfig[],
config: {
cooldownDuration: DurationInSeconds;
}
},
customGasStation?: CustomGasStation
) {
this.connection = connection;
this.targetPriceListener = targetPriceListener;
Expand All @@ -38,6 +40,7 @@ export class Pusher {

this.pythContractFactory = pythContractFactory;
this.pythContract = this.pythContractFactory.createPythContractWithPayer();
this.customGasStation = customGasStation;
}

async start() {
Expand Down Expand Up @@ -96,13 +99,15 @@ export class Pusher {
.call();
console.log(`Update fee: ${updateFee}`);

const gasPrice = await this.customGasStation?.getCustomGasPrice();

this.pythContract.methods
.updatePriceFeedsIfNecessary(
priceFeedUpdateData,
priceIds,
pubTimesToPush
)
.send({ value: updateFee })
.send({ value: updateFee, gasPrice })
.on("transactionHash", (hash: string) => {
console.log(`Successful. Tx hash: ${hash}`);
})
Expand Down
16 changes: 16 additions & 0 deletions pyth-evm-price-pusher/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { HexString } from "@pythnetwork/pyth-evm-js";

export type PctNumber = number;
export type DurationInSeconds = number;
export const txSpeeds = ["slow", "standard", "fast"] as const;
export type TxSpeed = typeof txSpeeds[number];
export const customGasChainIds = [137] as const;
export type CustomGasChainId = typeof customGasChainIds[number];

export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -31,3 +35,15 @@ export function isWsEndpoint(endpoint: string): boolean {

return false;
}

export function verifyValidOption<
options extends Readonly<Array<any>>,
validOption extends options[number]
>(option: any, validOptions: options) {
if (validOptions.includes(option)) {
return option as validOption;
}
const errorString =
option + " is not a valid option. Please choose between " + validOptions;
throw new Error(errorString);
}