From 847df764e1887b275647c8fb0ec5565fd4ebcf56 Mon Sep 17 00:00:00 2001 From: 0xfirefist Date: Tue, 14 Mar 2023 23:15:56 +0530 Subject: [PATCH 1/3] simulate tx to calculate gas --- price_pusher/src/injective/injective.ts | 42 +++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/price_pusher/src/injective/injective.ts b/price_pusher/src/injective/injective.ts index 4e177e3b36..d461300c07 100644 --- a/price_pusher/src/injective/injective.ts +++ b/price_pusher/src/injective/injective.ts @@ -12,7 +12,6 @@ import { DurationInSeconds } from "../utils"; import { ChainGrpcAuthApi, ChainGrpcWasmApi, - DEFAULT_STD_FEE, MsgExecuteContract, Msgs, PrivateKey, @@ -21,6 +20,8 @@ import { createTransactionFromMsg, } from "@injectivelabs/sdk-ts"; +import { DEFAULT_GAS_PRICE } from "@injectivelabs/utils"; + type PriceQueryResponse = { price_feed: { id: string; @@ -99,14 +100,44 @@ export class InjectivePricePusher implements IPricePusher { return this.wallet.toBech32(); } - private async signAndBroadcastMsg( - msg: Msgs, - fee = DEFAULT_STD_FEE - ): Promise { + private async signAndBroadcastMsg(msg: Msgs): Promise { 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: "injective-888", + 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 EXTRA_GAS_MULTIPLIER = 1.2; + const fee = { + amount: [ + { + denom: "inj", + amount: ( + gasUsed * + DEFAULT_GAS_PRICE * + EXTRA_GAS_MULTIPLIER + ).toFixed(), + }, + ], + gas: (gasUsed * EXTRA_GAS_MULTIPLIER).toFixed(), + }; + const { signBytes, txRaw } = createTransactionFromMsg({ sequence: account.baseAccount.sequence, accountNumber: account.baseAccount.accountNumber, @@ -121,7 +152,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; From 1f94f746301f7b790a19324ebfb3ad84d6c52bf3 Mon Sep 17 00:00:00 2001 From: 0xfirefist Date: Tue, 14 Mar 2023 23:17:31 +0530 Subject: [PATCH 2/3] update package version --- price_pusher/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/price_pusher/package.json b/price_pusher/package.json index a5629d5562..0176c79698 100644 --- a/price_pusher/package.json +++ b/price_pusher/package.json @@ -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", From 16425364d571b960b07ebef58003aa891e3c021f Mon Sep 17 00:00:00 2001 From: 0xfirefist Date: Wed, 15 Mar 2023 14:23:57 +0530 Subject: [PATCH 3/3] injective class: flexible --- price_pusher/src/injective/injective.ts | 27 ++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/price_pusher/src/injective/injective.ts b/price_pusher/src/injective/injective.ts index d461300c07..7837cec7ee 100644 --- a/price_pusher/src/injective/injective.ts +++ b/price_pusher/src/injective/injective.ts @@ -85,15 +85,29 @@ 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 ) { 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 { @@ -109,7 +123,7 @@ export class InjectivePricePusher implements IPricePusher { sequence: account.baseAccount.sequence, accountNumber: account.baseAccount.accountNumber, message: msg, - chainId: "injective-888", + chainId: this.chainConfig.chainId, pubKey: this.wallet.toPublicKey().toBase64(), }); @@ -123,26 +137,25 @@ export class InjectivePricePusher implements IPricePusher { // 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 EXTRA_GAS_MULTIPLIER = 1.2; const fee = { amount: [ { denom: "inj", amount: ( gasUsed * - DEFAULT_GAS_PRICE * - EXTRA_GAS_MULTIPLIER + this.chainConfig.gasPrice * + this.chainConfig.gasMultiplier ).toFixed(), }, ], - gas: (gasUsed * EXTRA_GAS_MULTIPLIER).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(), });