@pythnetwork/price-pusher
Version:
Pyth Price Pusher
161 lines (160 loc) • 7.62 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const hermes_client_1 = require("@pythnetwork/hermes-client");
const options = __importStar(require("../options"));
const price_config_1 = require("../price-config");
const fs_1 = __importDefault(require("fs"));
const pyth_price_listener_1 = require("../pyth-price-listener");
const controller_1 = require("../controller");
const sui_1 = require("./sui");
const ed25519_1 = require("@mysten/sui/keypairs/ed25519");
const pino_1 = __importDefault(require("pino"));
const utils_1 = require("../utils");
const metrics_1 = require("../metrics");
const balance_tracker_1 = require("./balance-tracker");
const client_1 = require("@mysten/sui/client");
exports.default = {
command: "sui",
describe: "Run price pusher for sui. Most of the arguments below are" +
"network specific, so there's one set of values for mainnet and" +
" another for testnet. See config.sui.mainnet.sample.json for the " +
"appropriate values for your network. ",
builder: {
endpoint: {
description: "RPC endpoint URL for sui. The pusher will periodically" +
"poll for updates. The polling interval is configurable via the " +
"`polling-frequency` command-line argument.",
type: "string",
required: true,
},
"pyth-state-id": {
description: "Pyth State Id. Can be found here" +
"https://docs.pyth.network/documentation/pythnet-price-feeds/sui",
type: "string",
required: true,
},
"wormhole-state-id": {
description: "Wormhole State Id. Can be found here" +
"https://docs.pyth.network/documentation/pythnet-price-feeds/sui",
type: "string",
required: true,
},
"num-gas-objects": {
description: "Number of gas objects in the pool.",
type: "number",
required: true,
default: 30,
},
"ignore-gas-objects": {
description: "Gas objects to ignore when merging gas objects on startup -- use this for locked objects.",
type: "array",
required: false,
default: [],
},
"gas-budget": {
description: "Gas budget for each price update",
type: "number",
required: true,
default: 500_000_000,
},
"account-index": {
description: "Index of the account to use derived by the mnemonic",
type: "number",
required: true,
default: 0,
},
...options.priceConfigFile,
...options.priceServiceEndpoint,
...options.mnemonicFile,
...options.pollingFrequency,
...options.pushingFrequency,
...options.logLevel,
...options.controllerLogLevel,
...options.enableMetrics,
...options.metricsPort,
},
handler: async function (argv) {
const { endpoint, priceConfigFile, priceServiceEndpoint, mnemonicFile, pushingFrequency, pollingFrequency, pythStateId, wormholeStateId, numGasObjects, ignoreGasObjects, gasBudget, accountIndex, logLevel, controllerLogLevel, enableMetrics, metricsPort, } = argv;
const logger = (0, pino_1.default)({ level: logLevel });
const priceConfigs = (0, price_config_1.readPriceConfigFile)(priceConfigFile);
const hermesClient = new hermes_client_1.HermesClient(priceServiceEndpoint);
const mnemonic = fs_1.default.readFileSync(mnemonicFile, "utf-8").trim();
const keypair = ed25519_1.Ed25519Keypair.deriveKeypair(mnemonic, `m/44'/784'/${accountIndex}'/0'/0'`);
const suiAddress = keypair.getPublicKey().toSuiAddress();
logger.info(`Pushing updates from wallet address: ${suiAddress}`);
let priceItems = priceConfigs.map(({ id, alias }) => ({ id, alias }));
// Better to filter out invalid price items before creating the pyth listener
const { existingPriceItems, invalidPriceItems } = await (0, utils_1.filterInvalidPriceItems)(hermesClient, priceItems);
if (invalidPriceItems.length > 0) {
logger.error(`Invalid price id submitted for: ${invalidPriceItems
.map(({ alias }) => alias)
.join(", ")}`);
}
priceItems = existingPriceItems;
// Initialize metrics if enabled
let metrics;
if (enableMetrics) {
metrics = new metrics_1.PricePusherMetrics(logger.child({ module: "Metrics" }));
metrics.start(metricsPort);
logger.info(`Metrics server started on port ${metricsPort}`);
}
const pythListener = new pyth_price_listener_1.PythPriceListener(hermesClient, priceItems, logger.child({ module: "PythPriceListener" }));
const suiClient = new client_1.SuiClient({ url: endpoint });
const suiListener = new sui_1.SuiPriceListener(pythStateId, wormholeStateId, endpoint, priceItems, logger.child({ module: "SuiPriceListener" }), { pollingFrequency });
const suiPusher = await sui_1.SuiPricePusher.createWithAutomaticGasPool(hermesClient, logger.child({ module: "SuiPricePusher" }), pythStateId, wormholeStateId, endpoint, keypair, gasBudget, numGasObjects, ignoreGasObjects);
const controller = new controller_1.Controller(priceConfigs, pythListener, suiListener, suiPusher, logger.child({ module: "Controller" }, { level: controllerLogLevel }), {
pushingFrequency,
metrics,
});
// Create and start the balance tracker if metrics are enabled
if (metrics) {
const balanceTracker = (0, balance_tracker_1.createSuiBalanceTracker)({
client: suiClient,
address: suiAddress,
network: "sui",
updateInterval: pushingFrequency,
metrics,
logger,
});
// Start the balance tracker
await balanceTracker.start();
}
controller.start();
},
};