Skip to content

Commit e81bfdf

Browse files
author
Dev Kalra
authored
[price-pusher] Simulate injective tx (#689)
* simulate tx to calculate gas * update package version * injective class: flexible
1 parent 156fb23 commit e81bfdf

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

price_pusher/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pythnetwork/pyth-price-pusher",
3-
"version": "4.0.0",
3+
"version": "4.1.0",
44
"description": "Pyth Price Pusher",
55
"homepage": "https://pyth.network",
66
"main": "lib/index.js",

price_pusher/src/injective/injective.ts

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { DurationInSeconds } from "../utils";
1212
import {
1313
ChainGrpcAuthApi,
1414
ChainGrpcWasmApi,
15-
DEFAULT_STD_FEE,
1615
MsgExecuteContract,
1716
Msgs,
1817
PrivateKey,
@@ -21,6 +20,8 @@ import {
2120
createTransactionFromMsg,
2221
} from "@injectivelabs/sdk-ts";
2322

23+
import { DEFAULT_GAS_PRICE } from "@injectivelabs/utils";
24+
2425
type PriceQueryResponse = {
2526
price_feed: {
2627
id: string;
@@ -84,34 +85,77 @@ export class InjectivePriceListener extends ChainPriceListener {
8485
}
8586
}
8687

88+
type InjectiveConfig = {
89+
chainId: string;
90+
gasMultiplier: number;
91+
gasPrice: number;
92+
};
8793
export class InjectivePricePusher implements IPricePusher {
8894
private wallet: PrivateKey;
95+
private chainConfig: InjectiveConfig;
96+
8997
constructor(
9098
private priceServiceConnection: PriceServiceConnection,
9199
private pythContractAddress: string,
92100
private grpcEndpoint: string,
93-
mnemonic: string
101+
mnemonic: string,
102+
chainConfig?: Partial<InjectiveConfig>
94103
) {
95104
this.wallet = PrivateKey.fromMnemonic(mnemonic);
105+
106+
this.chainConfig = {
107+
chainId: chainConfig?.chainId ?? "injective-888",
108+
gasMultiplier: chainConfig?.gasMultiplier ?? 1.2,
109+
gasPrice: chainConfig?.gasPrice ?? DEFAULT_GAS_PRICE,
110+
};
96111
}
97112

98113
private injectiveAddress(): string {
99114
return this.wallet.toBech32();
100115
}
101116

102-
private async signAndBroadcastMsg(
103-
msg: Msgs,
104-
fee = DEFAULT_STD_FEE
105-
): Promise<TxResponse> {
117+
private async signAndBroadcastMsg(msg: Msgs): Promise<TxResponse> {
106118
const chainGrpcAuthApi = new ChainGrpcAuthApi(this.grpcEndpoint);
107119
const account = await chainGrpcAuthApi.fetchAccount(
108120
this.injectiveAddress()
109121
);
122+
const { txRaw: simulateTxRaw } = createTransactionFromMsg({
123+
sequence: account.baseAccount.sequence,
124+
accountNumber: account.baseAccount.accountNumber,
125+
message: msg,
126+
chainId: this.chainConfig.chainId,
127+
pubKey: this.wallet.toPublicKey().toBase64(),
128+
});
129+
130+
const txService = new TxGrpcClient(this.grpcEndpoint);
131+
// simulation
132+
const {
133+
gasInfo: { gasUsed },
134+
} = await txService.simulate(simulateTxRaw);
135+
136+
// simulation returns us the approximate gas used
137+
// gas passed with the transaction should be more than that
138+
// in order for it to be successfully executed
139+
// this multiplier takes care of that
140+
const fee = {
141+
amount: [
142+
{
143+
denom: "inj",
144+
amount: (
145+
gasUsed *
146+
this.chainConfig.gasPrice *
147+
this.chainConfig.gasMultiplier
148+
).toFixed(),
149+
},
150+
],
151+
gas: (gasUsed * this.chainConfig.gasMultiplier).toFixed(),
152+
};
153+
110154
const { signBytes, txRaw } = createTransactionFromMsg({
111155
sequence: account.baseAccount.sequence,
112156
accountNumber: account.baseAccount.accountNumber,
113157
message: msg,
114-
chainId: "injective-888",
158+
chainId: this.chainConfig.chainId,
115159
fee,
116160
pubKey: this.wallet.toPublicKey().toBase64(),
117161
});
@@ -121,7 +165,6 @@ export class InjectivePricePusher implements IPricePusher {
121165
/** Append Signatures */
122166
txRaw.setSignaturesList([sig]);
123167

124-
const txService = new TxGrpcClient(this.grpcEndpoint);
125168
const txResponse = await txService.broadcast(txRaw);
126169

127170
return txResponse;

0 commit comments

Comments
 (0)