Skip to content
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
2 changes: 1 addition & 1 deletion price_pusher/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pythnetwork/pyth-price-pusher",
"version": "4.0.0",
"version": "4.1.0",
"description": "Pyth Price Pusher",
"homepage": "https://pyth.network",
"main": "lib/index.js",
Expand Down
59 changes: 51 additions & 8 deletions price_pusher/src/injective/injective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { DurationInSeconds } from "../utils";
import {
ChainGrpcAuthApi,
ChainGrpcWasmApi,
DEFAULT_STD_FEE,
MsgExecuteContract,
Msgs,
PrivateKey,
Expand All @@ -21,6 +20,8 @@ import {
createTransactionFromMsg,
} from "@injectivelabs/sdk-ts";

import { DEFAULT_GAS_PRICE } from "@injectivelabs/utils";

type PriceQueryResponse = {
price_feed: {
id: string;
Expand Down Expand Up @@ -84,34 +85,77 @@ export class InjectivePriceListener extends ChainPriceListener {
}
}

type InjectiveConfig = {
chainId: string;
gasMultiplier: number;
gasPrice: number;
};
export class InjectivePricePusher implements IPricePusher {
private wallet: PrivateKey;
private chainConfig: InjectiveConfig;

constructor(
private priceServiceConnection: PriceServiceConnection,
private pythContractAddress: string,
private grpcEndpoint: string,
mnemonic: string
mnemonic: string,
chainConfig?: Partial<InjectiveConfig>
) {
this.wallet = PrivateKey.fromMnemonic(mnemonic);

this.chainConfig = {
chainId: chainConfig?.chainId ?? "injective-888",
gasMultiplier: chainConfig?.gasMultiplier ?? 1.2,
gasPrice: chainConfig?.gasPrice ?? DEFAULT_GAS_PRICE,
};
}

private injectiveAddress(): string {
return this.wallet.toBech32();
}

private async signAndBroadcastMsg(
msg: Msgs,
fee = DEFAULT_STD_FEE
): Promise<TxResponse> {
private async signAndBroadcastMsg(msg: Msgs): Promise<TxResponse> {
const chainGrpcAuthApi = new ChainGrpcAuthApi(this.grpcEndpoint);
const account = await chainGrpcAuthApi.fetchAccount(
this.injectiveAddress()
);
const { txRaw: simulateTxRaw } = createTransactionFromMsg({
sequence: account.baseAccount.sequence,
accountNumber: account.baseAccount.accountNumber,
message: msg,
chainId: this.chainConfig.chainId,
pubKey: this.wallet.toPublicKey().toBase64(),
});

const txService = new TxGrpcClient(this.grpcEndpoint);
// simulation
const {
gasInfo: { gasUsed },
} = await txService.simulate(simulateTxRaw);

// simulation returns us the approximate gas used
// gas passed with the transaction should be more than that
// in order for it to be successfully executed
// this multiplier takes care of that
const fee = {
amount: [
{
denom: "inj",
amount: (
gasUsed *
this.chainConfig.gasPrice *
this.chainConfig.gasMultiplier
).toFixed(),
},
],
gas: (gasUsed * this.chainConfig.gasMultiplier).toFixed(),
};

const { signBytes, txRaw } = createTransactionFromMsg({
sequence: account.baseAccount.sequence,
accountNumber: account.baseAccount.accountNumber,
message: msg,
chainId: "injective-888",
chainId: this.chainConfig.chainId,
fee,
pubKey: this.wallet.toPublicKey().toBase64(),
});
Expand All @@ -121,7 +165,6 @@ export class InjectivePricePusher implements IPricePusher {
/** Append Signatures */
txRaw.setSignaturesList([sig]);

const txService = new TxGrpcClient(this.grpcEndpoint);
const txResponse = await txService.broadcast(txRaw);

return txResponse;
Expand Down