@@ -12,7 +12,6 @@ import { DurationInSeconds } from "../utils";
1212import {
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+
2425type 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+ } ;
8793export 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