[price-pusher] Simulate injective tx (#689)

* simulate tx to calculate gas

* update package version

* injective class: flexible
This commit is contained in:
Dev Kalra 2023-03-15 14:33:25 +05:30 committed by GitHub
parent 156fb23f24
commit e81bfdf428
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 9 deletions

View File

@ -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",

View File

@ -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;
@ -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(),
});
@ -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;