@injectivelabs/wallet-core 1.16.38 → 1.16.39-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +1016 -0
- package/dist/cjs/index.d.cts +288 -0
- package/dist/cjs/package.json +2 -2
- package/dist/esm/index.d.ts +288 -3
- package/dist/esm/index.js +1012 -3
- package/dist/esm/package.json +2 -2
- package/package.json +46 -47
- package/dist/cjs/broadcaster/MsgBroadcaster.d.ts +0 -137
- package/dist/cjs/broadcaster/MsgBroadcaster.js +0 -920
- package/dist/cjs/broadcaster/Web3Broadcaster.d.ts +0 -30
- package/dist/cjs/broadcaster/Web3Broadcaster.js +0 -32
- package/dist/cjs/broadcaster/index.d.ts +0 -3
- package/dist/cjs/broadcaster/index.js +0 -19
- package/dist/cjs/broadcaster/types.d.ts +0 -56
- package/dist/cjs/broadcaster/types.js +0 -13
- package/dist/cjs/index.d.ts +0 -3
- package/dist/cjs/index.js +0 -19
- package/dist/cjs/strategy/BaseWalletStrategy.d.ts +0 -58
- package/dist/cjs/strategy/BaseWalletStrategy.js +0 -176
- package/dist/cjs/strategy/index.d.ts +0 -2
- package/dist/cjs/strategy/index.js +0 -8
- package/dist/cjs/utils/index.d.ts +0 -1
- package/dist/cjs/utils/index.js +0 -17
- package/dist/cjs/utils/tx.d.ts +0 -1
- package/dist/cjs/utils/tx.js +0 -11
- package/dist/esm/broadcaster/MsgBroadcaster.d.ts +0 -137
- package/dist/esm/broadcaster/MsgBroadcaster.js +0 -916
- package/dist/esm/broadcaster/Web3Broadcaster.d.ts +0 -30
- package/dist/esm/broadcaster/Web3Broadcaster.js +0 -28
- package/dist/esm/broadcaster/index.d.ts +0 -3
- package/dist/esm/broadcaster/index.js +0 -3
- package/dist/esm/broadcaster/types.d.ts +0 -56
- package/dist/esm/broadcaster/types.js +0 -10
- package/dist/esm/strategy/BaseWalletStrategy.d.ts +0 -58
- package/dist/esm/strategy/BaseWalletStrategy.js +0 -173
- package/dist/esm/strategy/index.d.ts +0 -2
- package/dist/esm/strategy/index.js +0 -2
- package/dist/esm/utils/index.d.ts +0 -1
- package/dist/esm/utils/index.js +0 -1
- package/dist/esm/utils/tx.d.ts +0 -1
- package/dist/esm/utils/tx.js +0 -7
|
@@ -1,916 +0,0 @@
|
|
|
1
|
-
import { EvmChainId } from '@injectivelabs/ts-types';
|
|
2
|
-
import { isTestnet, isMainnet, getNetworkInfo, getNetworkEndpoints, } from '@injectivelabs/networks';
|
|
3
|
-
import { sleep, getStdFee, toBigNumber, DEFAULT_GAS_PRICE, DEFAULT_BLOCK_TIMEOUT_HEIGHT, DEFAULT_BLOCK_TIME_IN_SECONDS, } from '@injectivelabs/utils';
|
|
4
|
-
import { WalletException, GeneralException, isThrownException, UnspecifiedErrorCode, ChainCosmosErrorCode, TransactionException, TransactionChainErrorModule, } from '@injectivelabs/exceptions';
|
|
5
|
-
import { Wallet, isCosmosWallet, WalletDeviceType, isEvmBrowserWallet, isEip712V2OnlyWallet, createEip712StdSignDoc, isCosmosAminoOnlyWallet, getEthereumSignerAddress, getInjectiveSignerAddress, } from '@injectivelabs/wallet-base';
|
|
6
|
-
import { TxGrpcApi, hexToBuff, PublicKey, SIGN_DIRECT, hexToBase64, ofacWallets, SIGN_EIP712, SIGN_EIP712_V2, ChainGrpcAuthApi, createTxRawEIP712, createTransaction, ChainGrpcTxFeesApi, getAminoStdSignDoc, getEip712TypedData, createWeb3Extension, getEip712TypedDataV2, IndexerGrpcWeb3GwApi, ChainGrpcTendermintApi, getGasPriceBasedOnMessage, createTxRawFromSigResponse, recoverTypedSignaturePubKey, createTransactionWithSigners, } from '@injectivelabs/sdk-ts';
|
|
7
|
-
import { checkIfTxRunOutOfGas } from '../utils/index.js';
|
|
8
|
-
import { WalletStrategyEmitterEventType } from './types.js';
|
|
9
|
-
const getEthereumWalletPubKey = async ({ pubKey, eip712TypedData, signature, }) => {
|
|
10
|
-
if (pubKey) {
|
|
11
|
-
return pubKey;
|
|
12
|
-
}
|
|
13
|
-
const recoveredPubKey = await recoverTypedSignaturePubKey(
|
|
14
|
-
// TODO: fix type
|
|
15
|
-
eip712TypedData, signature);
|
|
16
|
-
return hexToBase64(recoveredPubKey);
|
|
17
|
-
};
|
|
18
|
-
const defaultRetriesConfig = () => ({
|
|
19
|
-
[`${TransactionChainErrorModule.CosmosSdk}-${ChainCosmosErrorCode.ErrMempoolIsFull}`]: {
|
|
20
|
-
retries: 0,
|
|
21
|
-
maxRetries: 10,
|
|
22
|
-
timeout: 1000,
|
|
23
|
-
},
|
|
24
|
-
});
|
|
25
|
-
/**
|
|
26
|
-
* This class is used to broadcast transactions
|
|
27
|
-
* using the WalletStrategy as a handler
|
|
28
|
-
* for the sign/broadcast flow of the transactions
|
|
29
|
-
*
|
|
30
|
-
* Mainly used for building UI products
|
|
31
|
-
*/
|
|
32
|
-
export class MsgBroadcaster {
|
|
33
|
-
options;
|
|
34
|
-
walletStrategy;
|
|
35
|
-
endpoints;
|
|
36
|
-
chainId;
|
|
37
|
-
txTimeout = DEFAULT_BLOCK_TIMEOUT_HEIGHT;
|
|
38
|
-
simulateTx = true;
|
|
39
|
-
txTimeoutOnFeeDelegation = false;
|
|
40
|
-
evmChainId;
|
|
41
|
-
gasBufferCoefficient = 1.2;
|
|
42
|
-
retriesOnError = defaultRetriesConfig();
|
|
43
|
-
httpHeaders;
|
|
44
|
-
constructor(options) {
|
|
45
|
-
const networkInfo = getNetworkInfo(options.network);
|
|
46
|
-
this.options = options;
|
|
47
|
-
this.simulateTx =
|
|
48
|
-
options.simulateTx !== undefined ? options.simulateTx : true;
|
|
49
|
-
this.txTimeout = options.txTimeout || DEFAULT_BLOCK_TIMEOUT_HEIGHT;
|
|
50
|
-
this.txTimeoutOnFeeDelegation =
|
|
51
|
-
options.txTimeoutOnFeeDelegation !== undefined
|
|
52
|
-
? options.txTimeoutOnFeeDelegation
|
|
53
|
-
: true;
|
|
54
|
-
this.gasBufferCoefficient = options.gasBufferCoefficient || 1.2;
|
|
55
|
-
this.chainId = options.chainId || networkInfo.chainId;
|
|
56
|
-
this.evmChainId = options.evmChainId || networkInfo.evmChainId;
|
|
57
|
-
this.endpoints = options.endpoints || getNetworkEndpoints(options.network);
|
|
58
|
-
this.walletStrategy = options.walletStrategy;
|
|
59
|
-
this.httpHeaders = options.httpHeaders;
|
|
60
|
-
}
|
|
61
|
-
setOptions(options) {
|
|
62
|
-
this.simulateTx = options.simulateTx || this.simulateTx;
|
|
63
|
-
this.txTimeout = options.txTimeout || this.txTimeout;
|
|
64
|
-
this.txTimeoutOnFeeDelegation =
|
|
65
|
-
options.txTimeoutOnFeeDelegation || this.txTimeoutOnFeeDelegation;
|
|
66
|
-
}
|
|
67
|
-
async getEvmChainId() {
|
|
68
|
-
const { walletStrategy } = this;
|
|
69
|
-
if (!isEvmBrowserWallet(walletStrategy.wallet)) {
|
|
70
|
-
return this.evmChainId;
|
|
71
|
-
}
|
|
72
|
-
const mainnetEvmIds = [
|
|
73
|
-
EvmChainId.Mainnet,
|
|
74
|
-
EvmChainId.MainnetEvm,
|
|
75
|
-
];
|
|
76
|
-
const testnetEvmIds = [
|
|
77
|
-
EvmChainId.Sepolia,
|
|
78
|
-
EvmChainId.TestnetEvm,
|
|
79
|
-
];
|
|
80
|
-
const devnetEvmIds = [
|
|
81
|
-
EvmChainId.Sepolia,
|
|
82
|
-
EvmChainId.DevnetEvm,
|
|
83
|
-
EvmChainId.MainnetEvm,
|
|
84
|
-
EvmChainId.TestnetEvm,
|
|
85
|
-
];
|
|
86
|
-
try {
|
|
87
|
-
const chainId = await walletStrategy.getEthereumChainId();
|
|
88
|
-
if (!chainId) {
|
|
89
|
-
return this.evmChainId;
|
|
90
|
-
}
|
|
91
|
-
const evmChainId = parseInt(chainId, 16);
|
|
92
|
-
if (isNaN(evmChainId)) {
|
|
93
|
-
return this.evmChainId;
|
|
94
|
-
}
|
|
95
|
-
if ((isMainnet(this.options.network) &&
|
|
96
|
-
!mainnetEvmIds.includes(evmChainId)) ||
|
|
97
|
-
(isTestnet(this.options.network) &&
|
|
98
|
-
!testnetEvmIds.includes(evmChainId)) ||
|
|
99
|
-
(!isMainnet(this.options.network) &&
|
|
100
|
-
!isTestnet(this.options.network) &&
|
|
101
|
-
!devnetEvmIds.includes(evmChainId))) {
|
|
102
|
-
throw new WalletException(new Error('Your selected network is incorrect'));
|
|
103
|
-
}
|
|
104
|
-
return evmChainId;
|
|
105
|
-
}
|
|
106
|
-
catch (e) {
|
|
107
|
-
throw new WalletException(e);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
/**
|
|
111
|
-
* Broadcasting the transaction using the client
|
|
112
|
-
* side approach for both cosmos and ethereum native wallets
|
|
113
|
-
*
|
|
114
|
-
* @param tx
|
|
115
|
-
* @returns {string} transaction hash
|
|
116
|
-
*/
|
|
117
|
-
async broadcast(tx) {
|
|
118
|
-
const { walletStrategy } = this;
|
|
119
|
-
const txWithAddresses = {
|
|
120
|
-
...tx,
|
|
121
|
-
ethereumAddress: getEthereumSignerAddress(tx.injectiveAddress),
|
|
122
|
-
injectiveAddress: getInjectiveSignerAddress(tx.injectiveAddress),
|
|
123
|
-
};
|
|
124
|
-
if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
|
|
125
|
-
throw new GeneralException(new Error('You cannot execute this transaction'));
|
|
126
|
-
}
|
|
127
|
-
try {
|
|
128
|
-
return isCosmosWallet(walletStrategy.wallet)
|
|
129
|
-
? await this.broadcastDirectSign(txWithAddresses)
|
|
130
|
-
: isEip712V2OnlyWallet(walletStrategy.wallet)
|
|
131
|
-
? await this.broadcastEip712V2(txWithAddresses)
|
|
132
|
-
: await this.broadcastEip712(txWithAddresses);
|
|
133
|
-
}
|
|
134
|
-
catch (e) {
|
|
135
|
-
const error = e;
|
|
136
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionFail);
|
|
137
|
-
if (isThrownException(error)) {
|
|
138
|
-
throw error;
|
|
139
|
-
}
|
|
140
|
-
throw new TransactionException(new Error(error));
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Broadcasting the transaction using the client
|
|
145
|
-
* side approach for both cosmos and ethereum native wallets
|
|
146
|
-
* Note: using EIP712_V2 for Ethereum wallets
|
|
147
|
-
*
|
|
148
|
-
* @param tx
|
|
149
|
-
* @returns {string} transaction hash
|
|
150
|
-
*/
|
|
151
|
-
async broadcastV2(tx) {
|
|
152
|
-
const { walletStrategy } = this;
|
|
153
|
-
const txWithAddresses = {
|
|
154
|
-
...tx,
|
|
155
|
-
ethereumAddress: getEthereumSignerAddress(tx.injectiveAddress),
|
|
156
|
-
injectiveAddress: getInjectiveSignerAddress(tx.injectiveAddress),
|
|
157
|
-
};
|
|
158
|
-
if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
|
|
159
|
-
throw new GeneralException(new Error('You cannot execute this transaction'));
|
|
160
|
-
}
|
|
161
|
-
try {
|
|
162
|
-
return isCosmosWallet(walletStrategy.wallet)
|
|
163
|
-
? await this.broadcastDirectSign(txWithAddresses)
|
|
164
|
-
: await this.broadcastEip712V2(txWithAddresses);
|
|
165
|
-
}
|
|
166
|
-
catch (e) {
|
|
167
|
-
const error = e;
|
|
168
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionFail);
|
|
169
|
-
if (isThrownException(error)) {
|
|
170
|
-
throw error;
|
|
171
|
-
}
|
|
172
|
-
throw new TransactionException(new Error(error));
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Broadcasting the transaction using the feeDelegation
|
|
177
|
-
* support approach for both cosmos and ethereum native wallets
|
|
178
|
-
*
|
|
179
|
-
* @param tx
|
|
180
|
-
* @returns {string} transaction hash
|
|
181
|
-
*/
|
|
182
|
-
async broadcastWithFeeDelegation(tx) {
|
|
183
|
-
const { walletStrategy } = this;
|
|
184
|
-
const txWithAddresses = {
|
|
185
|
-
...tx,
|
|
186
|
-
ethereumAddress: getEthereumSignerAddress(tx.injectiveAddress),
|
|
187
|
-
injectiveAddress: getInjectiveSignerAddress(tx.injectiveAddress),
|
|
188
|
-
};
|
|
189
|
-
if (ofacWallets.includes(txWithAddresses.ethereumAddress)) {
|
|
190
|
-
throw new GeneralException(new Error('You cannot execute this transaction'));
|
|
191
|
-
}
|
|
192
|
-
try {
|
|
193
|
-
return isCosmosWallet(walletStrategy.wallet)
|
|
194
|
-
? await this.broadcastDirectSignWithFeeDelegation(txWithAddresses)
|
|
195
|
-
: await this.broadcastEip712WithFeeDelegation(txWithAddresses);
|
|
196
|
-
}
|
|
197
|
-
catch (e) {
|
|
198
|
-
const error = e;
|
|
199
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionFail);
|
|
200
|
-
if (isThrownException(error)) {
|
|
201
|
-
throw error;
|
|
202
|
-
}
|
|
203
|
-
throw new TransactionException(new Error(error));
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
/**
|
|
207
|
-
* Prepare/sign/broadcast transaction using
|
|
208
|
-
* Ethereum native wallets on the client side.
|
|
209
|
-
*
|
|
210
|
-
* Note: Gas estimation not available
|
|
211
|
-
*
|
|
212
|
-
* @param tx The transaction that needs to be broadcasted
|
|
213
|
-
* @returns transaction hash
|
|
214
|
-
*/
|
|
215
|
-
async broadcastEip712(tx) {
|
|
216
|
-
const { chainId, endpoints, walletStrategy, txTimeout: txTimeoutInBlocks, } = this;
|
|
217
|
-
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
218
|
-
const evmChainId = await this.getEvmChainId();
|
|
219
|
-
if (!evmChainId) {
|
|
220
|
-
throw new GeneralException(new Error('Please provide evmChainId'));
|
|
221
|
-
}
|
|
222
|
-
/** Account Details * */
|
|
223
|
-
const { baseAccount, latestHeight } = await this.fetchAccountAndBlockDetails(tx.injectiveAddress);
|
|
224
|
-
const timeoutHeight = toBigNumber(latestHeight).plus(txTimeoutInBlocks);
|
|
225
|
-
const txTimeoutTimeInSeconds = txTimeoutInBlocks * DEFAULT_BLOCK_TIME_IN_SECONDS;
|
|
226
|
-
const txTimeoutTimeInMilliSeconds = txTimeoutTimeInSeconds * 1000;
|
|
227
|
-
const gas = (tx.gas?.gas || getGasPriceBasedOnMessage(msgs)).toString();
|
|
228
|
-
let stdFee = getStdFee({ ...tx.gas, gas });
|
|
229
|
-
/**
|
|
230
|
-
* Account has not been created on chain
|
|
231
|
-
* and we cannot simulate the transaction
|
|
232
|
-
* to estimate the gas
|
|
233
|
-
**/
|
|
234
|
-
if (!baseAccount.pubKey) {
|
|
235
|
-
stdFee = await this.getStdFeeWithDynamicBaseFee(stdFee);
|
|
236
|
-
}
|
|
237
|
-
else {
|
|
238
|
-
const { stdFee: simulatedStdFee } = await this.getTxWithSignersAndStdFee({
|
|
239
|
-
chainId,
|
|
240
|
-
signMode: SIGN_EIP712,
|
|
241
|
-
memo: tx.memo,
|
|
242
|
-
message: msgs,
|
|
243
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
244
|
-
signers: {
|
|
245
|
-
pubKey: baseAccount.pubKey.key,
|
|
246
|
-
accountNumber: baseAccount.accountNumber,
|
|
247
|
-
sequence: baseAccount.sequence,
|
|
248
|
-
},
|
|
249
|
-
fee: stdFee,
|
|
250
|
-
});
|
|
251
|
-
stdFee = simulatedStdFee;
|
|
252
|
-
}
|
|
253
|
-
/** EIP712 for signing on Ethereum wallets */
|
|
254
|
-
const eip712TypedData = getEip712TypedData({
|
|
255
|
-
msgs,
|
|
256
|
-
fee: stdFee,
|
|
257
|
-
tx: {
|
|
258
|
-
memo: tx.memo,
|
|
259
|
-
accountNumber: baseAccount.accountNumber.toString(),
|
|
260
|
-
sequence: baseAccount.sequence.toString(),
|
|
261
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
262
|
-
chainId,
|
|
263
|
-
},
|
|
264
|
-
evmChainId,
|
|
265
|
-
});
|
|
266
|
-
/** Signing on Ethereum */
|
|
267
|
-
const signature = await walletStrategy.signEip712TypedData(JSON.stringify(eip712TypedData), tx.ethereumAddress, { txTimeout: txTimeoutTimeInSeconds });
|
|
268
|
-
const pubKeyOrSignatureDerivedPubKey = await getEthereumWalletPubKey({
|
|
269
|
-
pubKey: baseAccount.pubKey?.key,
|
|
270
|
-
eip712TypedData,
|
|
271
|
-
signature,
|
|
272
|
-
});
|
|
273
|
-
/** Preparing the transaction for client broadcasting */
|
|
274
|
-
const { txRaw } = createTransaction({
|
|
275
|
-
message: msgs,
|
|
276
|
-
memo: tx.memo,
|
|
277
|
-
signMode: SIGN_EIP712,
|
|
278
|
-
fee: stdFee,
|
|
279
|
-
pubKey: pubKeyOrSignatureDerivedPubKey,
|
|
280
|
-
sequence: baseAccount.sequence,
|
|
281
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
282
|
-
accountNumber: baseAccount.accountNumber,
|
|
283
|
-
chainId,
|
|
284
|
-
});
|
|
285
|
-
const web3Extension = createWeb3Extension({
|
|
286
|
-
evmChainId,
|
|
287
|
-
});
|
|
288
|
-
const txRawEip712 = createTxRawEIP712(txRaw, web3Extension);
|
|
289
|
-
/** Append Signatures */
|
|
290
|
-
txRawEip712.signatures = [hexToBuff(signature)];
|
|
291
|
-
const response = await walletStrategy.sendTransaction(txRawEip712, {
|
|
292
|
-
chainId,
|
|
293
|
-
endpoints,
|
|
294
|
-
txTimeout: txTimeoutInBlocks,
|
|
295
|
-
address: tx.injectiveAddress,
|
|
296
|
-
});
|
|
297
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastEnd);
|
|
298
|
-
return await new TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash, txTimeoutTimeInMilliSeconds);
|
|
299
|
-
}
|
|
300
|
-
/**
|
|
301
|
-
* Prepare/sign/broadcast transaction using
|
|
302
|
-
* Ethereum native wallets on the client side.
|
|
303
|
-
*
|
|
304
|
-
* Note: Gas estimation not available
|
|
305
|
-
*
|
|
306
|
-
* @param tx The transaction that needs to be broadcasted
|
|
307
|
-
* @returns transaction hash
|
|
308
|
-
*/
|
|
309
|
-
async broadcastEip712V2(tx) {
|
|
310
|
-
const { chainId, endpoints, walletStrategy, txTimeout: txTimeoutInBlocks, } = this;
|
|
311
|
-
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
312
|
-
const evmChainId = await this.getEvmChainId();
|
|
313
|
-
if (!evmChainId) {
|
|
314
|
-
throw new GeneralException(new Error('Please provide evmChainId'));
|
|
315
|
-
}
|
|
316
|
-
/** Account Details * */
|
|
317
|
-
const { baseAccount, latestHeight } = await this.fetchAccountAndBlockDetails(tx.injectiveAddress);
|
|
318
|
-
const timeoutHeight = toBigNumber(latestHeight).plus(txTimeoutInBlocks);
|
|
319
|
-
const txTimeoutTimeInSeconds = txTimeoutInBlocks * DEFAULT_BLOCK_TIME_IN_SECONDS;
|
|
320
|
-
const txTimeoutTimeInMilliSeconds = txTimeoutTimeInSeconds * 1000;
|
|
321
|
-
const gas = (tx.gas?.gas || getGasPriceBasedOnMessage(msgs)).toString();
|
|
322
|
-
let stdFee = getStdFee({ ...tx.gas, gas });
|
|
323
|
-
/**
|
|
324
|
-
* Account has not been created on chain
|
|
325
|
-
* and we cannot simulate the transaction
|
|
326
|
-
* to estimate the gas
|
|
327
|
-
**/
|
|
328
|
-
if (!baseAccount.pubKey) {
|
|
329
|
-
stdFee = await this.getStdFeeWithDynamicBaseFee(stdFee);
|
|
330
|
-
}
|
|
331
|
-
else {
|
|
332
|
-
const { stdFee: simulatedStdFee } = await this.getTxWithSignersAndStdFee({
|
|
333
|
-
chainId,
|
|
334
|
-
signMode: SIGN_EIP712_V2,
|
|
335
|
-
memo: tx.memo,
|
|
336
|
-
message: msgs,
|
|
337
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
338
|
-
signers: {
|
|
339
|
-
pubKey: baseAccount.pubKey.key,
|
|
340
|
-
sequence: baseAccount.sequence,
|
|
341
|
-
accountNumber: baseAccount.accountNumber,
|
|
342
|
-
},
|
|
343
|
-
fee: stdFee,
|
|
344
|
-
});
|
|
345
|
-
stdFee = simulatedStdFee;
|
|
346
|
-
}
|
|
347
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationStart);
|
|
348
|
-
/** EIP712 for signing on Ethereum wallets */
|
|
349
|
-
const eip712TypedData = getEip712TypedDataV2({
|
|
350
|
-
msgs,
|
|
351
|
-
fee: stdFee,
|
|
352
|
-
tx: {
|
|
353
|
-
memo: tx.memo,
|
|
354
|
-
accountNumber: baseAccount.accountNumber.toString(),
|
|
355
|
-
sequence: baseAccount.sequence.toString(),
|
|
356
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
357
|
-
chainId,
|
|
358
|
-
},
|
|
359
|
-
evmChainId,
|
|
360
|
-
});
|
|
361
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationEnd);
|
|
362
|
-
/** Signing on Ethereum */
|
|
363
|
-
const signature = await walletStrategy.signEip712TypedData(JSON.stringify(eip712TypedData), tx.ethereumAddress, { txTimeout: txTimeoutTimeInSeconds });
|
|
364
|
-
const pubKeyOrSignatureDerivedPubKey = await getEthereumWalletPubKey({
|
|
365
|
-
pubKey: baseAccount.pubKey?.key,
|
|
366
|
-
eip712TypedData,
|
|
367
|
-
signature,
|
|
368
|
-
});
|
|
369
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastStart);
|
|
370
|
-
const { txRaw } = createTransaction({
|
|
371
|
-
message: msgs,
|
|
372
|
-
memo: tx.memo,
|
|
373
|
-
signMode: SIGN_EIP712_V2,
|
|
374
|
-
fee: stdFee,
|
|
375
|
-
pubKey: pubKeyOrSignatureDerivedPubKey,
|
|
376
|
-
sequence: baseAccount.sequence,
|
|
377
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
378
|
-
accountNumber: baseAccount.accountNumber,
|
|
379
|
-
chainId,
|
|
380
|
-
});
|
|
381
|
-
const web3Extension = createWeb3Extension({
|
|
382
|
-
evmChainId,
|
|
383
|
-
});
|
|
384
|
-
const txRawEip712 = createTxRawEIP712(txRaw, web3Extension);
|
|
385
|
-
/** Append Signatures */
|
|
386
|
-
txRawEip712.signatures = [hexToBuff(signature)];
|
|
387
|
-
const response = await walletStrategy.sendTransaction(txRawEip712, {
|
|
388
|
-
chainId,
|
|
389
|
-
endpoints,
|
|
390
|
-
txTimeout: txTimeoutInBlocks,
|
|
391
|
-
address: tx.injectiveAddress,
|
|
392
|
-
});
|
|
393
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastEnd);
|
|
394
|
-
return await new TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash, txTimeoutTimeInMilliSeconds);
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* Prepare/sign/broadcast transaction using
|
|
398
|
-
* Ethereum native wallets using the Web3Gateway.
|
|
399
|
-
*
|
|
400
|
-
* @param tx The transaction that needs to be broadcasted
|
|
401
|
-
* @returns transaction hash
|
|
402
|
-
*/
|
|
403
|
-
async broadcastEip712WithFeeDelegation(tx) {
|
|
404
|
-
const { endpoints, simulateTx, httpHeaders, walletStrategy, txTimeoutOnFeeDelegation, txTimeout: txTimeoutInBlocks, } = this;
|
|
405
|
-
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
406
|
-
const web3Msgs = msgs.map((msg) => msg.toWeb3());
|
|
407
|
-
const evmChainId = await this.getEvmChainId();
|
|
408
|
-
if (!evmChainId) {
|
|
409
|
-
throw new GeneralException(new Error('Please provide evmChainId'));
|
|
410
|
-
}
|
|
411
|
-
const transactionApi = new IndexerGrpcWeb3GwApi(endpoints.web3gw || endpoints.indexer);
|
|
412
|
-
if (httpHeaders) {
|
|
413
|
-
transactionApi.setMetadata(httpHeaders);
|
|
414
|
-
}
|
|
415
|
-
const txTimeoutTimeInSeconds = txTimeoutInBlocks * DEFAULT_BLOCK_TIME_IN_SECONDS;
|
|
416
|
-
const txTimeoutTimeInMilliSeconds = txTimeoutTimeInSeconds * 1000;
|
|
417
|
-
let timeoutHeight = undefined;
|
|
418
|
-
if (txTimeoutOnFeeDelegation) {
|
|
419
|
-
const latestBlock = await new ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
420
|
-
const latestHeight = latestBlock.header.height;
|
|
421
|
-
timeoutHeight = toBigNumber(latestHeight)
|
|
422
|
-
.plus(txTimeoutInBlocks)
|
|
423
|
-
.toNumber();
|
|
424
|
-
}
|
|
425
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationStart);
|
|
426
|
-
const prepareTxResponse = await transactionApi.prepareTxRequest({
|
|
427
|
-
timeoutHeight,
|
|
428
|
-
memo: tx.memo,
|
|
429
|
-
message: web3Msgs,
|
|
430
|
-
address: tx.ethereumAddress,
|
|
431
|
-
chainId: evmChainId,
|
|
432
|
-
gasLimit: getGasPriceBasedOnMessage(msgs),
|
|
433
|
-
estimateGas: simulateTx,
|
|
434
|
-
});
|
|
435
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationEnd);
|
|
436
|
-
const signature = await walletStrategy.signEip712TypedData(prepareTxResponse.data, tx.ethereumAddress, { txTimeout: txTimeoutTimeInSeconds });
|
|
437
|
-
const broadcast = async () => await transactionApi.broadcastTxRequest({
|
|
438
|
-
signature,
|
|
439
|
-
message: web3Msgs,
|
|
440
|
-
txResponse: prepareTxResponse,
|
|
441
|
-
chainId: evmChainId,
|
|
442
|
-
});
|
|
443
|
-
try {
|
|
444
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastStart);
|
|
445
|
-
const response = await broadcast();
|
|
446
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastEnd);
|
|
447
|
-
return await new TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash, txTimeoutTimeInMilliSeconds);
|
|
448
|
-
}
|
|
449
|
-
catch (e) {
|
|
450
|
-
const error = e;
|
|
451
|
-
if (isThrownException(error)) {
|
|
452
|
-
const exception = error;
|
|
453
|
-
/**
|
|
454
|
-
* First MsgExec transaction with a PrivateKey wallet
|
|
455
|
-
* always runs out of gas for some reason, temporary solution
|
|
456
|
-
* to just broadcast the transaction twice
|
|
457
|
-
**/
|
|
458
|
-
if (walletStrategy.wallet === Wallet.PrivateKey &&
|
|
459
|
-
checkIfTxRunOutOfGas(exception)) {
|
|
460
|
-
/** Account Details * */
|
|
461
|
-
const accountDetails = await new ChainGrpcAuthApi(endpoints.grpc).fetchAccount(tx.injectiveAddress);
|
|
462
|
-
const { baseAccount } = accountDetails;
|
|
463
|
-
/** We only do it on the first account tx fail */
|
|
464
|
-
if (baseAccount.sequence > 1) {
|
|
465
|
-
throw e;
|
|
466
|
-
}
|
|
467
|
-
return await this.broadcastEip712WithFeeDelegation(tx);
|
|
468
|
-
}
|
|
469
|
-
return await this.retryOnException(exception, broadcast);
|
|
470
|
-
}
|
|
471
|
-
throw e;
|
|
472
|
-
}
|
|
473
|
-
}
|
|
474
|
-
/**
|
|
475
|
-
* Prepare/sign/broadcast transaction using
|
|
476
|
-
* Cosmos native wallets on the client side.
|
|
477
|
-
*
|
|
478
|
-
* @param tx The transaction that needs to be broadcasted
|
|
479
|
-
* @returns transaction hash
|
|
480
|
-
*/
|
|
481
|
-
async broadcastDirectSign(tx) {
|
|
482
|
-
const { chainId, endpoints, walletStrategy, txTimeout: txTimeoutInBlocks, } = this;
|
|
483
|
-
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
484
|
-
/**
|
|
485
|
-
* When using Ledger with Keplr/Leap we have
|
|
486
|
-
* to send EIP712 to sign on Keplr/Leap
|
|
487
|
-
*/
|
|
488
|
-
if ([Wallet.Keplr, Wallet.Leap].includes(walletStrategy.getWallet())) {
|
|
489
|
-
const walletDeviceType = await walletStrategy.getWalletDeviceType();
|
|
490
|
-
const isLedgerConnected = walletDeviceType === WalletDeviceType.Hardware;
|
|
491
|
-
if (isLedgerConnected) {
|
|
492
|
-
return this.experimentalBroadcastWalletThroughLedger(tx);
|
|
493
|
-
}
|
|
494
|
-
}
|
|
495
|
-
const { baseAccount, latestHeight } = await this.fetchAccountAndBlockDetails(tx.injectiveAddress);
|
|
496
|
-
const timeoutHeight = toBigNumber(latestHeight).plus(txTimeoutInBlocks);
|
|
497
|
-
const txTimeoutTimeInSeconds = txTimeoutInBlocks * DEFAULT_BLOCK_TIME_IN_SECONDS;
|
|
498
|
-
const txTimeoutTimeInMilliSeconds = txTimeoutTimeInSeconds * 1000;
|
|
499
|
-
const signMode = isCosmosAminoOnlyWallet(walletStrategy.wallet)
|
|
500
|
-
? SIGN_EIP712
|
|
501
|
-
: SIGN_DIRECT;
|
|
502
|
-
const pubKey = await walletStrategy.getPubKey(tx.injectiveAddress);
|
|
503
|
-
const gas = (tx.gas?.gas || getGasPriceBasedOnMessage(msgs)).toString();
|
|
504
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationStart);
|
|
505
|
-
/** Prepare the Transaction * */
|
|
506
|
-
const { txRaw } = await this.getTxWithSignersAndStdFee({
|
|
507
|
-
chainId,
|
|
508
|
-
signMode,
|
|
509
|
-
memo: tx.memo,
|
|
510
|
-
message: msgs,
|
|
511
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
512
|
-
signers: {
|
|
513
|
-
pubKey,
|
|
514
|
-
accountNumber: baseAccount.accountNumber,
|
|
515
|
-
sequence: baseAccount.sequence,
|
|
516
|
-
},
|
|
517
|
-
fee: getStdFee({ ...tx.gas, gas }),
|
|
518
|
-
});
|
|
519
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationEnd);
|
|
520
|
-
/** Ledger using Cosmos app only allows signing amino docs */
|
|
521
|
-
if (isCosmosAminoOnlyWallet(walletStrategy.wallet)) {
|
|
522
|
-
const aminoSignDoc = getAminoStdSignDoc({
|
|
523
|
-
...tx,
|
|
524
|
-
...baseAccount,
|
|
525
|
-
msgs,
|
|
526
|
-
chainId,
|
|
527
|
-
gas: gas || tx.gas?.gas?.toString(),
|
|
528
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
529
|
-
});
|
|
530
|
-
const signResponse = await walletStrategy.signAminoCosmosTransaction({
|
|
531
|
-
signDoc: aminoSignDoc,
|
|
532
|
-
address: tx.injectiveAddress,
|
|
533
|
-
});
|
|
534
|
-
txRaw.signatures = [
|
|
535
|
-
Buffer.from(signResponse.signature.signature, 'base64'),
|
|
536
|
-
];
|
|
537
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastStart);
|
|
538
|
-
const response = await walletStrategy.sendTransaction(txRaw, {
|
|
539
|
-
chainId,
|
|
540
|
-
endpoints,
|
|
541
|
-
address: tx.injectiveAddress,
|
|
542
|
-
txTimeout: txTimeoutInBlocks,
|
|
543
|
-
});
|
|
544
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastEnd);
|
|
545
|
-
return await new TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash, txTimeoutTimeInMilliSeconds);
|
|
546
|
-
}
|
|
547
|
-
const directSignResponse = (await walletStrategy.signCosmosTransaction({
|
|
548
|
-
txRaw,
|
|
549
|
-
chainId,
|
|
550
|
-
address: tx.injectiveAddress,
|
|
551
|
-
accountNumber: baseAccount.accountNumber,
|
|
552
|
-
}));
|
|
553
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastStart);
|
|
554
|
-
const response = await walletStrategy.sendTransaction(directSignResponse, {
|
|
555
|
-
chainId,
|
|
556
|
-
endpoints,
|
|
557
|
-
txTimeout: txTimeoutInBlocks,
|
|
558
|
-
address: tx.injectiveAddress,
|
|
559
|
-
});
|
|
560
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastEnd);
|
|
561
|
-
return await new TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash, txTimeoutTimeInMilliSeconds);
|
|
562
|
-
}
|
|
563
|
-
/**
|
|
564
|
-
* We use this method only when we want to broadcast a transaction using Ledger on Keplr/Leap for Injective
|
|
565
|
-
*
|
|
566
|
-
* Note: Gas estimation not available
|
|
567
|
-
* @param tx the transaction that needs to be broadcasted
|
|
568
|
-
*/
|
|
569
|
-
async experimentalBroadcastWalletThroughLedger(tx) {
|
|
570
|
-
const { chainId, endpoints, evmChainId, simulateTx, walletStrategy, txTimeout: txTimeoutInBlocks, } = this;
|
|
571
|
-
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
572
|
-
/**
|
|
573
|
-
* We can only use this method
|
|
574
|
-
* when Ledger is connected through Keplr
|
|
575
|
-
*/
|
|
576
|
-
if ([Wallet.Keplr, Wallet.Leap].includes(walletStrategy.getWallet())) {
|
|
577
|
-
const walletDeviceType = await walletStrategy.getWalletDeviceType();
|
|
578
|
-
const isLedgerConnected = walletDeviceType === WalletDeviceType.Hardware;
|
|
579
|
-
if (!isLedgerConnected) {
|
|
580
|
-
throw new GeneralException(new Error(`This method can only be used when Ledger is connected through ${walletStrategy.getWallet()}`));
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
if (!evmChainId) {
|
|
584
|
-
throw new GeneralException(new Error('Please provide evmChainId'));
|
|
585
|
-
}
|
|
586
|
-
const cosmosWallet = walletStrategy.getCosmosWallet(chainId);
|
|
587
|
-
const { baseAccount, latestHeight } = await this.fetchAccountAndBlockDetails(tx.injectiveAddress);
|
|
588
|
-
const timeoutHeight = toBigNumber(latestHeight).plus(txTimeoutInBlocks);
|
|
589
|
-
const pubKey = await walletStrategy.getPubKey();
|
|
590
|
-
const gas = (tx.gas?.gas || getGasPriceBasedOnMessage(msgs)).toString();
|
|
591
|
-
/** EIP712 for signing on Ethereum wallets */
|
|
592
|
-
const eip712TypedData = getEip712TypedData({
|
|
593
|
-
msgs,
|
|
594
|
-
fee: await this.getStdFeeWithDynamicBaseFee({ ...tx.gas, gas }),
|
|
595
|
-
tx: {
|
|
596
|
-
chainId,
|
|
597
|
-
memo: tx.memo,
|
|
598
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
599
|
-
sequence: baseAccount.sequence.toString(),
|
|
600
|
-
accountNumber: baseAccount.accountNumber.toString(),
|
|
601
|
-
},
|
|
602
|
-
evmChainId,
|
|
603
|
-
});
|
|
604
|
-
const aminoSignResponse = await cosmosWallet.signEIP712CosmosTx({
|
|
605
|
-
eip712: eip712TypedData,
|
|
606
|
-
signDoc: createEip712StdSignDoc({
|
|
607
|
-
...tx,
|
|
608
|
-
...baseAccount,
|
|
609
|
-
msgs,
|
|
610
|
-
chainId,
|
|
611
|
-
gas: gas || tx.gas?.gas?.toString(),
|
|
612
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
613
|
-
}),
|
|
614
|
-
});
|
|
615
|
-
/**
|
|
616
|
-
* Create TxRaw from the signed tx that we
|
|
617
|
-
* get as a response in case the user changed the fee/memo
|
|
618
|
-
* on the Keplr popup
|
|
619
|
-
*/
|
|
620
|
-
const { txRaw } = createTransaction({
|
|
621
|
-
pubKey,
|
|
622
|
-
message: msgs,
|
|
623
|
-
memo: aminoSignResponse.signed.memo,
|
|
624
|
-
signMode: SIGN_EIP712,
|
|
625
|
-
fee: aminoSignResponse.signed.fee,
|
|
626
|
-
sequence: parseInt(aminoSignResponse.signed.sequence, 10),
|
|
627
|
-
timeoutHeight: parseInt(aminoSignResponse.signed.timeout_height, 10),
|
|
628
|
-
accountNumber: parseInt(aminoSignResponse.signed.account_number, 10),
|
|
629
|
-
chainId,
|
|
630
|
-
});
|
|
631
|
-
/** Preparing the transaction for client broadcasting */
|
|
632
|
-
const web3Extension = createWeb3Extension({
|
|
633
|
-
evmChainId,
|
|
634
|
-
});
|
|
635
|
-
const txRawEip712 = createTxRawEIP712(txRaw, web3Extension);
|
|
636
|
-
if (simulateTx) {
|
|
637
|
-
await this.simulateTxRaw(txRawEip712);
|
|
638
|
-
}
|
|
639
|
-
/** Append Signatures */
|
|
640
|
-
const signatureBuff = Buffer.from(aminoSignResponse.signature.signature, 'base64');
|
|
641
|
-
txRawEip712.signatures = [signatureBuff];
|
|
642
|
-
/** Broadcast the transaction */
|
|
643
|
-
const response = await new TxGrpcApi(endpoints.grpc).broadcast(txRawEip712, { txTimeout: txTimeoutInBlocks });
|
|
644
|
-
if (response.code !== 0) {
|
|
645
|
-
throw new TransactionException(new Error(response.rawLog), {
|
|
646
|
-
code: UnspecifiedErrorCode,
|
|
647
|
-
contextCode: response.code,
|
|
648
|
-
contextModule: response.codespace,
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
return response;
|
|
652
|
-
}
|
|
653
|
-
/**
|
|
654
|
-
* Prepare/sign/broadcast transaction using
|
|
655
|
-
* Cosmos native wallets using the Web3Gateway.
|
|
656
|
-
*
|
|
657
|
-
* @param tx The transaction that needs to be broadcasted
|
|
658
|
-
* @returns transaction hash
|
|
659
|
-
*/
|
|
660
|
-
async broadcastDirectSignWithFeeDelegation(tx) {
|
|
661
|
-
const { options, chainId, endpoints, httpHeaders, walletStrategy, txTimeoutOnFeeDelegation, txTimeout: txTimeoutInBlocks, } = this;
|
|
662
|
-
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
663
|
-
/**
|
|
664
|
-
* We can only use this method when Keplr is connected
|
|
665
|
-
* with ledger
|
|
666
|
-
*/
|
|
667
|
-
if (walletStrategy.getWallet() === Wallet.Keplr) {
|
|
668
|
-
const walletDeviceType = await walletStrategy.getWalletDeviceType();
|
|
669
|
-
const isLedgerConnectedOnKeplr = walletDeviceType === WalletDeviceType.Hardware;
|
|
670
|
-
if (isLedgerConnectedOnKeplr) {
|
|
671
|
-
throw new GeneralException(new Error('Keplr + Ledger is not available with fee delegation. Connect with Ledger directly.'));
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
const cosmosWallet = walletStrategy.getCosmosWallet(chainId);
|
|
675
|
-
const canDisableCosmosGasCheck = [Wallet.Keplr, Wallet.OWallet].includes(walletStrategy.wallet);
|
|
676
|
-
const feePayerPubKey = await this.fetchFeePayerPubKey(options.feePayerPubKey);
|
|
677
|
-
const feePayerPublicKey = PublicKey.fromBase64(feePayerPubKey);
|
|
678
|
-
const feePayer = feePayerPublicKey.toAddress().address;
|
|
679
|
-
/** Account Details * */
|
|
680
|
-
const { baseAccount, latestHeight } = await this.fetchAccountAndBlockDetails(tx.injectiveAddress);
|
|
681
|
-
const chainGrpcAuthApi = new ChainGrpcAuthApi(endpoints.grpc);
|
|
682
|
-
if (httpHeaders) {
|
|
683
|
-
chainGrpcAuthApi.setMetadata(httpHeaders);
|
|
684
|
-
}
|
|
685
|
-
const feePayerAccountDetails = await chainGrpcAuthApi.fetchAccount(feePayer);
|
|
686
|
-
const { baseAccount: feePayerBaseAccount } = feePayerAccountDetails;
|
|
687
|
-
const timeoutHeight = toBigNumber(latestHeight).plus(txTimeoutOnFeeDelegation
|
|
688
|
-
? txTimeoutInBlocks
|
|
689
|
-
: DEFAULT_BLOCK_TIMEOUT_HEIGHT);
|
|
690
|
-
const txTimeoutTimeInSeconds = txTimeoutInBlocks * DEFAULT_BLOCK_TIME_IN_SECONDS;
|
|
691
|
-
const txTimeoutTimeInMilliSeconds = txTimeoutTimeInSeconds * 1000;
|
|
692
|
-
const pubKey = await walletStrategy.getPubKey();
|
|
693
|
-
const gas = (tx.gas?.gas || getGasPriceBasedOnMessage(msgs)).toString();
|
|
694
|
-
/** Prepare the Transaction * */
|
|
695
|
-
const { txRaw } = await this.getTxWithSignersAndStdFee({
|
|
696
|
-
chainId,
|
|
697
|
-
memo: tx.memo,
|
|
698
|
-
message: msgs,
|
|
699
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
700
|
-
signers: [
|
|
701
|
-
{
|
|
702
|
-
pubKey,
|
|
703
|
-
accountNumber: baseAccount.accountNumber,
|
|
704
|
-
sequence: baseAccount.sequence,
|
|
705
|
-
},
|
|
706
|
-
{
|
|
707
|
-
pubKey: feePayerPublicKey.toBase64(),
|
|
708
|
-
accountNumber: feePayerBaseAccount.accountNumber,
|
|
709
|
-
sequence: feePayerBaseAccount.sequence,
|
|
710
|
-
},
|
|
711
|
-
],
|
|
712
|
-
fee: getStdFee({ ...tx.gas, gas, payer: feePayer }),
|
|
713
|
-
});
|
|
714
|
-
// Temporary remove tx gas check because Keplr doesn't recognize feePayer
|
|
715
|
-
if (canDisableCosmosGasCheck && cosmosWallet.disableGasCheck) {
|
|
716
|
-
cosmosWallet.disableGasCheck(chainId);
|
|
717
|
-
}
|
|
718
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationStart);
|
|
719
|
-
const directSignResponse = (await walletStrategy.signCosmosTransaction({
|
|
720
|
-
txRaw,
|
|
721
|
-
chainId,
|
|
722
|
-
address: tx.injectiveAddress,
|
|
723
|
-
accountNumber: baseAccount.accountNumber,
|
|
724
|
-
}));
|
|
725
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionPreparationEnd);
|
|
726
|
-
const transactionApi = new IndexerGrpcWeb3GwApi(endpoints.web3gw || endpoints.indexer);
|
|
727
|
-
if (httpHeaders) {
|
|
728
|
-
transactionApi.setMetadata(httpHeaders);
|
|
729
|
-
}
|
|
730
|
-
const broadcast = async () => await transactionApi.broadcastCosmosTxRequest({
|
|
731
|
-
address: tx.injectiveAddress,
|
|
732
|
-
txRaw: createTxRawFromSigResponse(directSignResponse),
|
|
733
|
-
signature: directSignResponse.signature.signature,
|
|
734
|
-
pubKey: directSignResponse.signature.pub_key || {
|
|
735
|
-
value: pubKey,
|
|
736
|
-
type: '/injective.crypto.v1beta1.ethsecp256k1.PubKey',
|
|
737
|
-
},
|
|
738
|
-
});
|
|
739
|
-
try {
|
|
740
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastStart);
|
|
741
|
-
const response = await broadcast();
|
|
742
|
-
walletStrategy.emit(WalletStrategyEmitterEventType.TransactionBroadcastEnd);
|
|
743
|
-
// Re-enable tx gas check removed above
|
|
744
|
-
if (canDisableCosmosGasCheck && cosmosWallet.enableGasCheck) {
|
|
745
|
-
cosmosWallet.enableGasCheck(chainId);
|
|
746
|
-
}
|
|
747
|
-
return await new TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash, txTimeoutTimeInMilliSeconds);
|
|
748
|
-
}
|
|
749
|
-
catch (e) {
|
|
750
|
-
const error = e;
|
|
751
|
-
if (isThrownException(error)) {
|
|
752
|
-
const exception = error;
|
|
753
|
-
return await this.retryOnException(exception, broadcast);
|
|
754
|
-
}
|
|
755
|
-
throw e;
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
/**
|
|
759
|
-
* Fetch the fee payer's pub key from the web3 gateway
|
|
760
|
-
*
|
|
761
|
-
* Returns a base64 version of it
|
|
762
|
-
*/
|
|
763
|
-
async fetchFeePayerPubKey(existingFeePayerPubKey) {
|
|
764
|
-
if (existingFeePayerPubKey) {
|
|
765
|
-
return existingFeePayerPubKey;
|
|
766
|
-
}
|
|
767
|
-
const { endpoints, httpHeaders } = this;
|
|
768
|
-
const transactionApi = new IndexerGrpcWeb3GwApi(endpoints.web3gw || endpoints.indexer);
|
|
769
|
-
if (httpHeaders) {
|
|
770
|
-
transactionApi.setMetadata(httpHeaders);
|
|
771
|
-
}
|
|
772
|
-
const response = await transactionApi.fetchFeePayer();
|
|
773
|
-
if (!response.feePayerPubKey) {
|
|
774
|
-
throw new GeneralException(new Error('Please provide a feePayerPubKey'));
|
|
775
|
-
}
|
|
776
|
-
if (response.feePayerPubKey.key.startsWith('0x') ||
|
|
777
|
-
response.feePayerPubKey.key.length === 66) {
|
|
778
|
-
return Buffer.from(response.feePayerPubKey.key, 'hex').toString('base64');
|
|
779
|
-
}
|
|
780
|
-
return response.feePayerPubKey.key;
|
|
781
|
-
}
|
|
782
|
-
async getStdFeeWithDynamicBaseFee(args) {
|
|
783
|
-
const client = new ChainGrpcTxFeesApi(this.endpoints.grpc);
|
|
784
|
-
let baseFee = DEFAULT_GAS_PRICE;
|
|
785
|
-
try {
|
|
786
|
-
const response = await client.fetchEipBaseFee();
|
|
787
|
-
baseFee = Number(response?.baseFee || DEFAULT_GAS_PRICE);
|
|
788
|
-
}
|
|
789
|
-
catch { }
|
|
790
|
-
if (!args) {
|
|
791
|
-
return getStdFee(baseFee ? { gasPrice: baseFee } : {});
|
|
792
|
-
}
|
|
793
|
-
if (typeof args === 'string') {
|
|
794
|
-
return getStdFee({
|
|
795
|
-
...(baseFee && {
|
|
796
|
-
gasPrice: toBigNumber(baseFee).toFixed(),
|
|
797
|
-
}),
|
|
798
|
-
gas: args,
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
return getStdFee({
|
|
802
|
-
...args,
|
|
803
|
-
...(baseFee && {
|
|
804
|
-
gasPrice: toBigNumber(baseFee).toFixed(),
|
|
805
|
-
}),
|
|
806
|
-
});
|
|
807
|
-
}
|
|
808
|
-
/**
|
|
809
|
-
* In case we don't want to simulate the transaction
|
|
810
|
-
* we get the gas limit based on the message type.
|
|
811
|
-
*
|
|
812
|
-
* If we want to simulate the transaction we set the
|
|
813
|
-
* gas limit based on the simulation and add a small multiplier
|
|
814
|
-
* to be safe (factor of 1.2 as default)
|
|
815
|
-
*/
|
|
816
|
-
async getTxWithSignersAndStdFee(args) {
|
|
817
|
-
const { simulateTx } = this;
|
|
818
|
-
if (!simulateTx) {
|
|
819
|
-
return {
|
|
820
|
-
...createTransactionWithSigners(args),
|
|
821
|
-
stdFee: await this.getStdFeeWithDynamicBaseFee(args.fee),
|
|
822
|
-
};
|
|
823
|
-
}
|
|
824
|
-
const result = await this.simulateTxWithSigners(args);
|
|
825
|
-
if (!result.gasInfo?.gasUsed) {
|
|
826
|
-
return {
|
|
827
|
-
...createTransactionWithSigners(args),
|
|
828
|
-
stdFee: await this.getStdFeeWithDynamicBaseFee(args.fee),
|
|
829
|
-
};
|
|
830
|
-
}
|
|
831
|
-
const stdGasFee = {
|
|
832
|
-
...(await this.getStdFeeWithDynamicBaseFee({
|
|
833
|
-
...getStdFee(args.fee),
|
|
834
|
-
gas: toBigNumber(result.gasInfo.gasUsed)
|
|
835
|
-
.times(this.gasBufferCoefficient)
|
|
836
|
-
.toFixed(),
|
|
837
|
-
})),
|
|
838
|
-
};
|
|
839
|
-
return {
|
|
840
|
-
...createTransactionWithSigners({
|
|
841
|
-
...args,
|
|
842
|
-
fee: stdGasFee,
|
|
843
|
-
}),
|
|
844
|
-
stdFee: stdGasFee,
|
|
845
|
-
};
|
|
846
|
-
}
|
|
847
|
-
/**
|
|
848
|
-
* Create TxRaw and simulate it
|
|
849
|
-
*/
|
|
850
|
-
async simulateTxRaw(txRaw) {
|
|
851
|
-
const { endpoints, httpHeaders } = this;
|
|
852
|
-
txRaw.signatures = [new Uint8Array(0)];
|
|
853
|
-
const client = new TxGrpcApi(endpoints.grpc);
|
|
854
|
-
if (httpHeaders) {
|
|
855
|
-
client.setMetadata(httpHeaders);
|
|
856
|
-
}
|
|
857
|
-
const simulationResponse = await client.simulate(txRaw);
|
|
858
|
-
return simulationResponse;
|
|
859
|
-
}
|
|
860
|
-
/**
|
|
861
|
-
* Create TxRaw and simulate it
|
|
862
|
-
*/
|
|
863
|
-
async simulateTxWithSigners(args) {
|
|
864
|
-
const { endpoints, httpHeaders } = this;
|
|
865
|
-
const { txRaw } = createTransactionWithSigners(args);
|
|
866
|
-
txRaw.signatures = Array(Array.isArray(args.signers) ? args.signers.length : 1).fill(new Uint8Array(0));
|
|
867
|
-
const client = new TxGrpcApi(endpoints.grpc);
|
|
868
|
-
if (httpHeaders) {
|
|
869
|
-
client.setMetadata(httpHeaders);
|
|
870
|
-
}
|
|
871
|
-
const simulationResponse = await client.simulate(txRaw);
|
|
872
|
-
return simulationResponse;
|
|
873
|
-
}
|
|
874
|
-
async retryOnException(exception, retryLogic) {
|
|
875
|
-
const errorsToRetry = Object.keys(this.retriesOnError);
|
|
876
|
-
const errorKey = `${exception.contextModule}-${exception.contextCode}`;
|
|
877
|
-
if (!errorsToRetry.includes(errorKey)) {
|
|
878
|
-
throw exception;
|
|
879
|
-
}
|
|
880
|
-
const retryConfig = this.retriesOnError[errorKey];
|
|
881
|
-
if (retryConfig.retries >= retryConfig.maxRetries) {
|
|
882
|
-
this.retriesOnError = defaultRetriesConfig();
|
|
883
|
-
throw exception;
|
|
884
|
-
}
|
|
885
|
-
await sleep(retryConfig.timeout);
|
|
886
|
-
try {
|
|
887
|
-
retryConfig.retries += 1;
|
|
888
|
-
return await retryLogic();
|
|
889
|
-
}
|
|
890
|
-
catch (e) {
|
|
891
|
-
const error = e;
|
|
892
|
-
if (isThrownException(error)) {
|
|
893
|
-
return this.retryOnException(error, retryLogic);
|
|
894
|
-
}
|
|
895
|
-
throw e;
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
async fetchAccountAndBlockDetails(address) {
|
|
899
|
-
const { endpoints, httpHeaders } = this;
|
|
900
|
-
const chainClient = new ChainGrpcAuthApi(endpoints.grpc);
|
|
901
|
-
const tendermintClient = new ChainGrpcTendermintApi(endpoints.grpc);
|
|
902
|
-
if (httpHeaders) {
|
|
903
|
-
chainClient.setMetadata(httpHeaders);
|
|
904
|
-
tendermintClient.setMetadata(httpHeaders);
|
|
905
|
-
}
|
|
906
|
-
const accountDetails = await chainClient.fetchAccount(address);
|
|
907
|
-
const { baseAccount } = accountDetails;
|
|
908
|
-
const latestBlock = await tendermintClient.fetchLatestBlock();
|
|
909
|
-
const latestHeight = latestBlock.header.height;
|
|
910
|
-
return {
|
|
911
|
-
baseAccount,
|
|
912
|
-
latestHeight,
|
|
913
|
-
accountDetails,
|
|
914
|
-
};
|
|
915
|
-
}
|
|
916
|
-
}
|