@@ -12,7 +12,6 @@ import { DurationInSeconds } from "../utils";
12
12
import {
13
13
ChainGrpcAuthApi ,
14
14
ChainGrpcWasmApi ,
15
- DEFAULT_STD_FEE ,
16
15
MsgExecuteContract ,
17
16
Msgs ,
18
17
PrivateKey ,
@@ -21,6 +20,8 @@ import {
21
20
createTransactionFromMsg ,
22
21
} from "@injectivelabs/sdk-ts" ;
23
22
23
+ import { DEFAULT_GAS_PRICE } from "@injectivelabs/utils" ;
24
+
24
25
type PriceQueryResponse = {
25
26
price_feed : {
26
27
id : string ;
@@ -84,34 +85,77 @@ export class InjectivePriceListener extends ChainPriceListener {
84
85
}
85
86
}
86
87
88
+ type InjectiveConfig = {
89
+ chainId : string ;
90
+ gasMultiplier : number ;
91
+ gasPrice : number ;
92
+ } ;
87
93
export class InjectivePricePusher implements IPricePusher {
88
94
private wallet : PrivateKey ;
95
+ private chainConfig : InjectiveConfig ;
96
+
89
97
constructor (
90
98
private priceServiceConnection : PriceServiceConnection ,
91
99
private pythContractAddress : string ,
92
100
private grpcEndpoint : string ,
93
- mnemonic : string
101
+ mnemonic : string ,
102
+ chainConfig ?: Partial < InjectiveConfig >
94
103
) {
95
104
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
+ } ;
96
111
}
97
112
98
113
private injectiveAddress ( ) : string {
99
114
return this . wallet . toBech32 ( ) ;
100
115
}
101
116
102
- private async signAndBroadcastMsg (
103
- msg : Msgs ,
104
- fee = DEFAULT_STD_FEE
105
- ) : Promise < TxResponse > {
117
+ private async signAndBroadcastMsg ( msg : Msgs ) : Promise < TxResponse > {
106
118
const chainGrpcAuthApi = new ChainGrpcAuthApi ( this . grpcEndpoint ) ;
107
119
const account = await chainGrpcAuthApi . fetchAccount (
108
120
this . injectiveAddress ( )
109
121
) ;
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
+
110
154
const { signBytes, txRaw } = createTransactionFromMsg ( {
111
155
sequence : account . baseAccount . sequence ,
112
156
accountNumber : account . baseAccount . accountNumber ,
113
157
message : msg ,
114
- chainId : "injective-888" ,
158
+ chainId : this . chainConfig . chainId ,
115
159
fee,
116
160
pubKey : this . wallet . toPublicKey ( ) . toBase64 ( ) ,
117
161
} ) ;
@@ -121,7 +165,6 @@ export class InjectivePricePusher implements IPricePusher {
121
165
/** Append Signatures */
122
166
txRaw . setSignaturesList ( [ sig ] ) ;
123
167
124
- const txService = new TxGrpcClient ( this . grpcEndpoint ) ;
125
168
const txResponse = await txService . broadcast ( txRaw ) ;
126
169
127
170
return txResponse ;
0 commit comments