@injectivelabs/wallet-core 1.14.41-alpha.1 → 1.14.41-alpha.3
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/broadcaster/MsgBroadcaster.d.ts.map +1 -1
- package/dist/cjs/broadcaster/MsgBroadcaster.js +601 -600
- package/dist/cjs/broadcaster/MsgBroadcaster.js.map +1 -1
- package/dist/cjs/broadcaster/Web3Broadcaster.js +24 -15
- package/dist/cjs/broadcaster/Web3Broadcaster.js.map +1 -1
- package/dist/cjs/strategy/BaseWalletStrategy.js +91 -61
- package/dist/cjs/strategy/BaseWalletStrategy.js.map +1 -1
- package/dist/esm/broadcaster/MsgBroadcaster.d.ts.map +1 -1
- package/dist/esm/broadcaster/MsgBroadcaster.js.map +1 -1
- package/package.json +16 -11
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
2
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
12
|
exports.MsgBroadcaster = void 0;
|
|
4
13
|
const sdk_ts_1 = require("@injectivelabs/sdk-ts");
|
|
@@ -28,17 +37,12 @@ const defaultRetriesConfig = () => ({
|
|
|
28
37
|
* Mainly used for building UI products
|
|
29
38
|
*/
|
|
30
39
|
class MsgBroadcaster {
|
|
31
|
-
options;
|
|
32
|
-
walletStrategy;
|
|
33
|
-
endpoints;
|
|
34
|
-
chainId;
|
|
35
|
-
txTimeout = utils_1.DEFAULT_BLOCK_TIMEOUT_HEIGHT;
|
|
36
|
-
simulateTx = true;
|
|
37
|
-
txTimeoutOnFeeDelegation = false;
|
|
38
|
-
ethereumChainId;
|
|
39
|
-
gasBufferCoefficient = 1.2;
|
|
40
|
-
retriesOnError = defaultRetriesConfig();
|
|
41
40
|
constructor(options) {
|
|
41
|
+
this.txTimeout = utils_1.DEFAULT_BLOCK_TIMEOUT_HEIGHT;
|
|
42
|
+
this.simulateTx = true;
|
|
43
|
+
this.txTimeoutOnFeeDelegation = false;
|
|
44
|
+
this.gasBufferCoefficient = 1.2;
|
|
45
|
+
this.retriesOnError = defaultRetriesConfig();
|
|
42
46
|
const networkInfo = (0, networks_1.getNetworkInfo)(options.network);
|
|
43
47
|
this.options = options;
|
|
44
48
|
this.simulateTx =
|
|
@@ -68,30 +72,28 @@ class MsgBroadcaster {
|
|
|
68
72
|
* @param tx
|
|
69
73
|
* @returns {string} transaction hash
|
|
70
74
|
*/
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
throw new exceptions_1.TransactionException(new Error(error));
|
|
94
|
-
}
|
|
75
|
+
broadcast(tx) {
|
|
76
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
77
|
+
const { walletStrategy } = this;
|
|
78
|
+
const txWithAddresses = Object.assign(Object.assign({}, tx), { ethereumAddress: (0, wallet_base_1.getEthereumSignerAddress)(tx.injectiveAddress), injectiveAddress: (0, wallet_base_1.getInjectiveSignerAddress)(tx.injectiveAddress) });
|
|
79
|
+
if (sdk_ts_1.ofacWallets.includes(txWithAddresses.ethereumAddress)) {
|
|
80
|
+
throw new exceptions_1.GeneralException(new Error('You cannot execute this transaction'));
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
return (0, wallet_base_1.isCosmosWallet)(walletStrategy.wallet)
|
|
84
|
+
? yield this.broadcastCosmos(txWithAddresses)
|
|
85
|
+
: (0, wallet_base_1.isEip712V2OnlyWallet)(walletStrategy.wallet)
|
|
86
|
+
? yield this.broadcastWeb3V2(txWithAddresses)
|
|
87
|
+
: yield this.broadcastWeb3(txWithAddresses);
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
const error = e;
|
|
91
|
+
if ((0, exceptions_1.isThrownException)(error)) {
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
throw new exceptions_1.TransactionException(new Error(error));
|
|
95
|
+
}
|
|
96
|
+
});
|
|
95
97
|
}
|
|
96
98
|
/**
|
|
97
99
|
* Broadcasting the transaction using the client
|
|
@@ -101,28 +103,26 @@ class MsgBroadcaster {
|
|
|
101
103
|
* @param tx
|
|
102
104
|
* @returns {string} transaction hash
|
|
103
105
|
*/
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
throw new exceptions_1.TransactionException(new Error(error));
|
|
125
|
-
}
|
|
106
|
+
broadcastV2(tx) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const { walletStrategy } = this;
|
|
109
|
+
const txWithAddresses = Object.assign(Object.assign({}, tx), { ethereumAddress: (0, wallet_base_1.getEthereumSignerAddress)(tx.injectiveAddress), injectiveAddress: (0, wallet_base_1.getInjectiveSignerAddress)(tx.injectiveAddress) });
|
|
110
|
+
if (sdk_ts_1.ofacWallets.includes(txWithAddresses.ethereumAddress)) {
|
|
111
|
+
throw new exceptions_1.GeneralException(new Error('You cannot execute this transaction'));
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
return (0, wallet_base_1.isCosmosWallet)(walletStrategy.wallet)
|
|
115
|
+
? yield this.broadcastCosmos(txWithAddresses)
|
|
116
|
+
: yield this.broadcastWeb3V2(txWithAddresses);
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
const error = e;
|
|
120
|
+
if ((0, exceptions_1.isThrownException)(error)) {
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
throw new exceptions_1.TransactionException(new Error(error));
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
126
|
}
|
|
127
127
|
/**
|
|
128
128
|
* Broadcasting the transaction using the feeDelegation
|
|
@@ -131,28 +131,26 @@ class MsgBroadcaster {
|
|
|
131
131
|
* @param tx
|
|
132
132
|
* @returns {string} transaction hash
|
|
133
133
|
*/
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
throw new exceptions_1.TransactionException(new Error(error));
|
|
155
|
-
}
|
|
134
|
+
broadcastWithFeeDelegation(tx) {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
const { walletStrategy } = this;
|
|
137
|
+
const txWithAddresses = Object.assign(Object.assign({}, tx), { ethereumAddress: (0, wallet_base_1.getEthereumSignerAddress)(tx.injectiveAddress), injectiveAddress: (0, wallet_base_1.getInjectiveSignerAddress)(tx.injectiveAddress) });
|
|
138
|
+
if (sdk_ts_1.ofacWallets.includes(txWithAddresses.ethereumAddress)) {
|
|
139
|
+
throw new exceptions_1.GeneralException(new Error('You cannot execute this transaction'));
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
return (0, wallet_base_1.isCosmosWallet)(walletStrategy.wallet)
|
|
143
|
+
? yield this.broadcastCosmosWithFeeDelegation(txWithAddresses)
|
|
144
|
+
: yield this.broadcastWeb3WithFeeDelegation(txWithAddresses);
|
|
145
|
+
}
|
|
146
|
+
catch (e) {
|
|
147
|
+
const error = e;
|
|
148
|
+
if ((0, exceptions_1.isThrownException)(error)) {
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
throw new exceptions_1.TransactionException(new Error(error));
|
|
152
|
+
}
|
|
153
|
+
});
|
|
156
154
|
}
|
|
157
155
|
/**
|
|
158
156
|
* Prepare/sign/broadcast transaction using
|
|
@@ -163,85 +161,88 @@ class MsgBroadcaster {
|
|
|
163
161
|
* @param tx The transaction that needs to be broadcasted
|
|
164
162
|
* @returns transaction hash
|
|
165
163
|
*/
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
164
|
+
broadcastWeb3(tx) {
|
|
165
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
166
|
+
var _a, _b;
|
|
167
|
+
const { chainId, txTimeout, endpoints, ethereumChainId, walletStrategy } = this;
|
|
168
|
+
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
169
|
+
if (!ethereumChainId) {
|
|
170
|
+
throw new exceptions_1.GeneralException(new Error('Please provide ethereumChainId'));
|
|
171
|
+
}
|
|
172
|
+
/** Account Details * */
|
|
173
|
+
const accountDetails = yield new sdk_ts_1.ChainGrpcAuthApi(endpoints.grpc).fetchAccount(tx.injectiveAddress);
|
|
174
|
+
const { baseAccount } = accountDetails;
|
|
175
|
+
/** Block Details */
|
|
176
|
+
const latestBlock = yield new sdk_ts_1.ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
177
|
+
const latestHeight = latestBlock.header.height;
|
|
178
|
+
const timeoutHeight = new utils_1.BigNumberInBase(latestHeight).plus(txTimeout);
|
|
179
|
+
const gas = (((_a = tx.gas) === null || _a === void 0 ? void 0 : _a.gas) || (0, sdk_ts_1.getGasPriceBasedOnMessage)(msgs)).toString();
|
|
180
|
+
let stdFee = (0, utils_1.getStdFee)(Object.assign(Object.assign({}, tx.gas), { gas }));
|
|
181
|
+
/**
|
|
182
|
+
* Account has been created on chain
|
|
183
|
+
* and we can simulate the transaction
|
|
184
|
+
* to estimate the gas
|
|
185
|
+
**/
|
|
186
|
+
if (baseAccount.pubKey) {
|
|
187
|
+
const { stdFee: simulatedStdFee } = yield this.getTxWithSignersAndStdFee({
|
|
188
|
+
chainId,
|
|
189
|
+
signMode: sdk_ts_1.SIGN_EIP712,
|
|
190
|
+
memo: tx.memo,
|
|
191
|
+
message: msgs,
|
|
192
|
+
timeoutHeight: timeoutHeight.toNumber(),
|
|
193
|
+
signers: {
|
|
194
|
+
pubKey: baseAccount.pubKey.key,
|
|
195
|
+
accountNumber: baseAccount.accountNumber,
|
|
196
|
+
sequence: baseAccount.sequence,
|
|
197
|
+
},
|
|
198
|
+
fee: stdFee,
|
|
199
|
+
});
|
|
200
|
+
stdFee = simulatedStdFee;
|
|
201
|
+
}
|
|
202
|
+
/** EIP712 for signing on Ethereum wallets */
|
|
203
|
+
const eip712TypedData = (0, sdk_ts_1.getEip712TypedData)({
|
|
204
|
+
msgs,
|
|
198
205
|
fee: stdFee,
|
|
206
|
+
tx: {
|
|
207
|
+
memo: tx.memo,
|
|
208
|
+
accountNumber: baseAccount.accountNumber.toString(),
|
|
209
|
+
sequence: baseAccount.sequence.toString(),
|
|
210
|
+
timeoutHeight: timeoutHeight.toFixed(),
|
|
211
|
+
chainId,
|
|
212
|
+
},
|
|
213
|
+
ethereumChainId,
|
|
199
214
|
});
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
215
|
+
/** Signing on Ethereum */
|
|
216
|
+
const signature = yield walletStrategy.signEip712TypedData(JSON.stringify(eip712TypedData), tx.ethereumAddress);
|
|
217
|
+
const pubKeyOrSignatureDerivedPubKey = getEthereumWalletPubKey({
|
|
218
|
+
pubKey: (_b = baseAccount.pubKey) === null || _b === void 0 ? void 0 : _b.key,
|
|
219
|
+
eip712TypedData,
|
|
220
|
+
signature,
|
|
221
|
+
});
|
|
222
|
+
/** Preparing the transaction for client broadcasting */
|
|
223
|
+
const { txRaw } = (0, sdk_ts_1.createTransaction)({
|
|
224
|
+
message: msgs,
|
|
207
225
|
memo: tx.memo,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
226
|
+
signMode: sdk_ts_1.SIGN_EIP712,
|
|
227
|
+
fee: stdFee,
|
|
228
|
+
pubKey: pubKeyOrSignatureDerivedPubKey,
|
|
229
|
+
sequence: baseAccount.sequence,
|
|
230
|
+
timeoutHeight: timeoutHeight.toNumber(),
|
|
231
|
+
accountNumber: baseAccount.accountNumber,
|
|
211
232
|
chainId,
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
memo: tx.memo,
|
|
226
|
-
signMode: sdk_ts_1.SIGN_EIP712,
|
|
227
|
-
fee: stdFee,
|
|
228
|
-
pubKey: pubKeyOrSignatureDerivedPubKey,
|
|
229
|
-
sequence: baseAccount.sequence,
|
|
230
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
231
|
-
accountNumber: baseAccount.accountNumber,
|
|
232
|
-
chainId,
|
|
233
|
-
});
|
|
234
|
-
const web3Extension = (0, sdk_ts_1.createWeb3Extension)({
|
|
235
|
-
ethereumChainId,
|
|
236
|
-
});
|
|
237
|
-
const txRawEip712 = (0, sdk_ts_1.createTxRawEIP712)(txRaw, web3Extension);
|
|
238
|
-
/** Append Signatures */
|
|
239
|
-
txRawEip712.signatures = [(0, sdk_ts_1.hexToBuff)(signature)];
|
|
240
|
-
return walletStrategy.sendTransaction(txRawEip712, {
|
|
241
|
-
chainId,
|
|
242
|
-
endpoints,
|
|
243
|
-
txTimeout,
|
|
244
|
-
address: tx.injectiveAddress,
|
|
233
|
+
});
|
|
234
|
+
const web3Extension = (0, sdk_ts_1.createWeb3Extension)({
|
|
235
|
+
ethereumChainId,
|
|
236
|
+
});
|
|
237
|
+
const txRawEip712 = (0, sdk_ts_1.createTxRawEIP712)(txRaw, web3Extension);
|
|
238
|
+
/** Append Signatures */
|
|
239
|
+
txRawEip712.signatures = [(0, sdk_ts_1.hexToBuff)(signature)];
|
|
240
|
+
return walletStrategy.sendTransaction(txRawEip712, {
|
|
241
|
+
chainId,
|
|
242
|
+
endpoints,
|
|
243
|
+
txTimeout,
|
|
244
|
+
address: tx.injectiveAddress,
|
|
245
|
+
});
|
|
245
246
|
});
|
|
246
247
|
}
|
|
247
248
|
/**
|
|
@@ -253,84 +254,87 @@ class MsgBroadcaster {
|
|
|
253
254
|
* @param tx The transaction that needs to be broadcasted
|
|
254
255
|
* @returns transaction hash
|
|
255
256
|
*/
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
257
|
+
broadcastWeb3V2(tx) {
|
|
258
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
+
var _a, _b;
|
|
260
|
+
const { walletStrategy, chainId, txTimeout, endpoints, ethereumChainId } = this;
|
|
261
|
+
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
262
|
+
if (!ethereumChainId) {
|
|
263
|
+
throw new exceptions_1.GeneralException(new Error('Please provide ethereumChainId'));
|
|
264
|
+
}
|
|
265
|
+
/** Account Details * */
|
|
266
|
+
const accountDetails = yield new sdk_ts_1.ChainGrpcAuthApi(endpoints.grpc).fetchAccount(tx.injectiveAddress);
|
|
267
|
+
const { baseAccount } = accountDetails;
|
|
268
|
+
/** Block Details */
|
|
269
|
+
const latestBlock = yield new sdk_ts_1.ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
270
|
+
const latestHeight = latestBlock.header.height;
|
|
271
|
+
const timeoutHeight = new utils_1.BigNumberInBase(latestHeight).plus(txTimeout);
|
|
272
|
+
const gas = (((_a = tx.gas) === null || _a === void 0 ? void 0 : _a.gas) || (0, sdk_ts_1.getGasPriceBasedOnMessage)(msgs)).toString();
|
|
273
|
+
let stdFee = (0, utils_1.getStdFee)(Object.assign(Object.assign({}, tx.gas), { gas }));
|
|
274
|
+
/**
|
|
275
|
+
* Account has been created on chain
|
|
276
|
+
* and we can simulate the transaction
|
|
277
|
+
* to estimate the gas
|
|
278
|
+
**/
|
|
279
|
+
if (baseAccount.pubKey) {
|
|
280
|
+
const { stdFee: simulatedStdFee } = yield this.getTxWithSignersAndStdFee({
|
|
281
|
+
chainId,
|
|
282
|
+
signMode: sdk_ts_1.SIGN_EIP712_V2,
|
|
283
|
+
memo: tx.memo,
|
|
284
|
+
message: msgs,
|
|
285
|
+
timeoutHeight: timeoutHeight.toNumber(),
|
|
286
|
+
signers: {
|
|
287
|
+
pubKey: baseAccount.pubKey.key,
|
|
288
|
+
accountNumber: baseAccount.accountNumber,
|
|
289
|
+
sequence: baseAccount.sequence,
|
|
290
|
+
},
|
|
291
|
+
fee: stdFee,
|
|
292
|
+
});
|
|
293
|
+
stdFee = simulatedStdFee;
|
|
294
|
+
}
|
|
295
|
+
/** EIP712 for signing on Ethereum wallets */
|
|
296
|
+
const eip712TypedData = (0, sdk_ts_1.getEip712TypedDataV2)({
|
|
297
|
+
msgs,
|
|
288
298
|
fee: stdFee,
|
|
299
|
+
tx: {
|
|
300
|
+
memo: tx.memo,
|
|
301
|
+
accountNumber: baseAccount.accountNumber.toString(),
|
|
302
|
+
sequence: baseAccount.sequence.toString(),
|
|
303
|
+
timeoutHeight: timeoutHeight.toFixed(),
|
|
304
|
+
chainId,
|
|
305
|
+
},
|
|
306
|
+
ethereumChainId,
|
|
307
|
+
});
|
|
308
|
+
/** Signing on Ethereum */
|
|
309
|
+
const signature = yield walletStrategy.signEip712TypedData(JSON.stringify(eip712TypedData), tx.ethereumAddress);
|
|
310
|
+
const pubKeyOrSignatureDerivedPubKey = getEthereumWalletPubKey({
|
|
311
|
+
pubKey: (_b = baseAccount.pubKey) === null || _b === void 0 ? void 0 : _b.key,
|
|
312
|
+
eip712TypedData,
|
|
313
|
+
signature,
|
|
289
314
|
});
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
/** EIP712 for signing on Ethereum wallets */
|
|
293
|
-
const eip712TypedData = (0, sdk_ts_1.getEip712TypedDataV2)({
|
|
294
|
-
msgs,
|
|
295
|
-
fee: stdFee,
|
|
296
|
-
tx: {
|
|
315
|
+
const { txRaw } = (0, sdk_ts_1.createTransaction)({
|
|
316
|
+
message: msgs,
|
|
297
317
|
memo: tx.memo,
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
318
|
+
signMode: sdk_ts_1.SIGN_EIP712_V2,
|
|
319
|
+
fee: stdFee,
|
|
320
|
+
pubKey: pubKeyOrSignatureDerivedPubKey,
|
|
321
|
+
sequence: baseAccount.sequence,
|
|
322
|
+
timeoutHeight: timeoutHeight.toNumber(),
|
|
323
|
+
accountNumber: baseAccount.accountNumber,
|
|
301
324
|
chainId,
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
signMode: sdk_ts_1.SIGN_EIP712_V2,
|
|
316
|
-
fee: stdFee,
|
|
317
|
-
pubKey: pubKeyOrSignatureDerivedPubKey,
|
|
318
|
-
sequence: baseAccount.sequence,
|
|
319
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
320
|
-
accountNumber: baseAccount.accountNumber,
|
|
321
|
-
chainId,
|
|
322
|
-
});
|
|
323
|
-
const web3Extension = (0, sdk_ts_1.createWeb3Extension)({
|
|
324
|
-
ethereumChainId,
|
|
325
|
-
});
|
|
326
|
-
const txRawEip712 = (0, sdk_ts_1.createTxRawEIP712)(txRaw, web3Extension);
|
|
327
|
-
/** Append Signatures */
|
|
328
|
-
txRawEip712.signatures = [(0, sdk_ts_1.hexToBuff)(signature)];
|
|
329
|
-
return walletStrategy.sendTransaction(txRawEip712, {
|
|
330
|
-
chainId,
|
|
331
|
-
endpoints,
|
|
332
|
-
txTimeout,
|
|
333
|
-
address: tx.injectiveAddress,
|
|
325
|
+
});
|
|
326
|
+
const web3Extension = (0, sdk_ts_1.createWeb3Extension)({
|
|
327
|
+
ethereumChainId,
|
|
328
|
+
});
|
|
329
|
+
const txRawEip712 = (0, sdk_ts_1.createTxRawEIP712)(txRaw, web3Extension);
|
|
330
|
+
/** Append Signatures */
|
|
331
|
+
txRawEip712.signatures = [(0, sdk_ts_1.hexToBuff)(signature)];
|
|
332
|
+
return walletStrategy.sendTransaction(txRawEip712, {
|
|
333
|
+
chainId,
|
|
334
|
+
endpoints,
|
|
335
|
+
txTimeout,
|
|
336
|
+
address: tx.injectiveAddress,
|
|
337
|
+
});
|
|
334
338
|
});
|
|
335
339
|
}
|
|
336
340
|
/**
|
|
@@ -340,66 +344,70 @@ class MsgBroadcaster {
|
|
|
340
344
|
* @param tx The transaction that needs to be broadcasted
|
|
341
345
|
* @returns transaction hash
|
|
342
346
|
*/
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
.
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
347
|
+
broadcastWeb3WithFeeDelegation(tx) {
|
|
348
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
349
|
+
const { txTimeout, endpoints, simulateTx, walletStrategy, ethereumChainId, txTimeoutOnFeeDelegation, } = this;
|
|
350
|
+
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
351
|
+
const web3Msgs = msgs.map((msg) => msg.toWeb3());
|
|
352
|
+
if (!ethereumChainId) {
|
|
353
|
+
throw new exceptions_1.GeneralException(new Error('Please provide ethereumChainId'));
|
|
354
|
+
}
|
|
355
|
+
const transactionApi = new sdk_ts_1.IndexerGrpcWeb3GwApi(endpoints.web3gw || endpoints.indexer);
|
|
356
|
+
let timeoutHeight = undefined;
|
|
357
|
+
if (txTimeoutOnFeeDelegation) {
|
|
358
|
+
const latestBlock = yield new sdk_ts_1.ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
359
|
+
const latestHeight = latestBlock.header.height;
|
|
360
|
+
timeoutHeight = new utils_1.BigNumberInBase(latestHeight)
|
|
361
|
+
.plus(txTimeout)
|
|
362
|
+
.toNumber();
|
|
363
|
+
}
|
|
364
|
+
const prepareTxResponse = yield transactionApi.prepareTxRequest({
|
|
365
|
+
timeoutHeight,
|
|
366
|
+
memo: tx.memo,
|
|
367
|
+
message: web3Msgs,
|
|
368
|
+
address: tx.ethereumAddress,
|
|
369
|
+
chainId: ethereumChainId,
|
|
370
|
+
gasLimit: (0, sdk_ts_1.getGasPriceBasedOnMessage)(msgs),
|
|
371
|
+
estimateGas: simulateTx,
|
|
372
|
+
});
|
|
373
|
+
const signature = yield walletStrategy.signEip712TypedData(prepareTxResponse.data, tx.ethereumAddress);
|
|
374
|
+
const broadcast = () => __awaiter(this, void 0, void 0, function* () {
|
|
375
|
+
return yield transactionApi.broadcastTxRequest({
|
|
376
|
+
signature,
|
|
377
|
+
message: web3Msgs,
|
|
378
|
+
txResponse: prepareTxResponse,
|
|
379
|
+
chainId: ethereumChainId,
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
try {
|
|
383
|
+
const response = yield broadcast();
|
|
384
|
+
return yield new sdk_ts_1.TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash);
|
|
385
|
+
}
|
|
386
|
+
catch (e) {
|
|
387
|
+
const error = e;
|
|
388
|
+
if ((0, exceptions_1.isThrownException)(error)) {
|
|
389
|
+
const exception = error;
|
|
390
|
+
/**
|
|
391
|
+
* First MsgExec transaction with a PrivateKey wallet
|
|
392
|
+
* always runs out of gas for some reason, temporary solution
|
|
393
|
+
* to just broadcast the transaction twice
|
|
394
|
+
**/
|
|
395
|
+
if (walletStrategy.wallet === wallet_base_1.Wallet.PrivateKey &&
|
|
396
|
+
(0, tx_js_1.checkIfTxRunOutOfGas)(exception)) {
|
|
397
|
+
/** Account Details * */
|
|
398
|
+
const accountDetails = yield new sdk_ts_1.ChainGrpcAuthApi(endpoints.grpc).fetchAccount(tx.injectiveAddress);
|
|
399
|
+
const { baseAccount } = accountDetails;
|
|
400
|
+
/** We only do it on the first account tx fail */
|
|
401
|
+
if (baseAccount.sequence > 1) {
|
|
402
|
+
throw e;
|
|
403
|
+
}
|
|
404
|
+
return yield this.broadcastWeb3WithFeeDelegation(tx);
|
|
396
405
|
}
|
|
397
|
-
return
|
|
406
|
+
return yield this.retryOnException(exception, broadcast);
|
|
398
407
|
}
|
|
399
|
-
|
|
408
|
+
throw e;
|
|
400
409
|
}
|
|
401
|
-
|
|
402
|
-
}
|
|
410
|
+
});
|
|
403
411
|
}
|
|
404
412
|
/**
|
|
405
413
|
* Prepare/sign/broadcast transaction using
|
|
@@ -408,81 +416,78 @@ class MsgBroadcaster {
|
|
|
408
416
|
* @param tx The transaction that needs to be broadcasted
|
|
409
417
|
* @returns transaction hash
|
|
410
418
|
*/
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
memo: tx.memo,
|
|
442
|
-
message: msgs,
|
|
443
|
-
timeoutHeight: timeoutHeight.toNumber(),
|
|
444
|
-
signers: {
|
|
445
|
-
pubKey,
|
|
446
|
-
accountNumber: baseAccount.accountNumber,
|
|
447
|
-
sequence: baseAccount.sequence,
|
|
448
|
-
},
|
|
449
|
-
fee: (0, utils_1.getStdFee)({ ...tx.gas, gas }),
|
|
450
|
-
});
|
|
451
|
-
/** Ledger using Cosmos app only allows signing amino docs */
|
|
452
|
-
if ((0, wallet_base_1.isCosmosAminoOnlyWallet)(walletStrategy.wallet)) {
|
|
453
|
-
const aminoSignDoc = (0, sdk_ts_1.getAminoStdSignDoc)({
|
|
454
|
-
...tx,
|
|
455
|
-
...baseAccount,
|
|
456
|
-
msgs,
|
|
419
|
+
broadcastCosmos(tx) {
|
|
420
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
421
|
+
var _a, _b, _c;
|
|
422
|
+
const { walletStrategy, txTimeout, endpoints, chainId } = this;
|
|
423
|
+
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
424
|
+
/**
|
|
425
|
+
* When using Ledger with Keplr/Leap we have
|
|
426
|
+
* to send EIP712 to sign on Keplr/Leap
|
|
427
|
+
*/
|
|
428
|
+
if ([wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.Leap].includes(walletStrategy.getWallet())) {
|
|
429
|
+
const walletDeviceType = yield walletStrategy.getWalletDeviceType();
|
|
430
|
+
const isLedgerConnected = walletDeviceType === wallet_base_1.WalletDeviceType.Hardware;
|
|
431
|
+
if (isLedgerConnected) {
|
|
432
|
+
return this.experimentalBroadcastWalletThroughLedger(tx);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/** Account Details * */
|
|
436
|
+
const accountDetails = yield new sdk_ts_1.ChainGrpcAuthApi(endpoints.grpc).fetchAccount(tx.injectiveAddress);
|
|
437
|
+
const { baseAccount } = accountDetails;
|
|
438
|
+
/** Block Details */
|
|
439
|
+
const latestBlock = yield new sdk_ts_1.ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
440
|
+
const latestHeight = latestBlock.header.height;
|
|
441
|
+
const timeoutHeight = new utils_1.BigNumberInBase(latestHeight).plus(txTimeout);
|
|
442
|
+
const signMode = (0, wallet_base_1.isCosmosAminoOnlyWallet)(walletStrategy.wallet)
|
|
443
|
+
? sdk_ts_1.SIGN_EIP712
|
|
444
|
+
: sdk_ts_1.SIGN_DIRECT;
|
|
445
|
+
const pubKey = yield walletStrategy.getPubKey(tx.injectiveAddress);
|
|
446
|
+
const gas = (((_a = tx.gas) === null || _a === void 0 ? void 0 : _a.gas) || (0, sdk_ts_1.getGasPriceBasedOnMessage)(msgs)).toString();
|
|
447
|
+
/** Prepare the Transaction * */
|
|
448
|
+
const { txRaw } = yield this.getTxWithSignersAndStdFee({
|
|
457
449
|
chainId,
|
|
458
|
-
|
|
459
|
-
|
|
450
|
+
signMode,
|
|
451
|
+
memo: tx.memo,
|
|
452
|
+
message: msgs,
|
|
453
|
+
timeoutHeight: timeoutHeight.toNumber(),
|
|
454
|
+
signers: {
|
|
455
|
+
pubKey,
|
|
456
|
+
accountNumber: baseAccount.accountNumber,
|
|
457
|
+
sequence: baseAccount.sequence,
|
|
458
|
+
},
|
|
459
|
+
fee: (0, utils_1.getStdFee)(Object.assign(Object.assign({}, tx.gas), { gas })),
|
|
460
460
|
});
|
|
461
|
-
|
|
462
|
-
|
|
461
|
+
/** Ledger using Cosmos app only allows signing amino docs */
|
|
462
|
+
if ((0, wallet_base_1.isCosmosAminoOnlyWallet)(walletStrategy.wallet)) {
|
|
463
|
+
const aminoSignDoc = (0, sdk_ts_1.getAminoStdSignDoc)(Object.assign(Object.assign(Object.assign({}, tx), baseAccount), { msgs,
|
|
464
|
+
chainId, gas: gas || ((_c = (_b = tx.gas) === null || _b === void 0 ? void 0 : _b.gas) === null || _c === void 0 ? void 0 : _c.toString()), timeoutHeight: timeoutHeight.toFixed() }));
|
|
465
|
+
const signResponse = yield walletStrategy.signAminoCosmosTransaction({
|
|
466
|
+
signDoc: aminoSignDoc,
|
|
467
|
+
address: tx.injectiveAddress,
|
|
468
|
+
});
|
|
469
|
+
txRaw.signatures = [
|
|
470
|
+
Buffer.from(signResponse.signature.signature, 'base64'),
|
|
471
|
+
];
|
|
472
|
+
return walletStrategy.sendTransaction(txRaw, {
|
|
473
|
+
chainId,
|
|
474
|
+
endpoints,
|
|
475
|
+
txTimeout,
|
|
476
|
+
address: tx.injectiveAddress,
|
|
477
|
+
});
|
|
478
|
+
}
|
|
479
|
+
const directSignResponse = (yield walletStrategy.signCosmosTransaction({
|
|
480
|
+
txRaw,
|
|
481
|
+
chainId,
|
|
463
482
|
address: tx.injectiveAddress,
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
];
|
|
468
|
-
return walletStrategy.sendTransaction(txRaw, {
|
|
483
|
+
accountNumber: baseAccount.accountNumber,
|
|
484
|
+
}));
|
|
485
|
+
return walletStrategy.sendTransaction(directSignResponse, {
|
|
469
486
|
chainId,
|
|
470
487
|
endpoints,
|
|
471
488
|
txTimeout,
|
|
472
489
|
address: tx.injectiveAddress,
|
|
473
490
|
});
|
|
474
|
-
}
|
|
475
|
-
const directSignResponse = (await walletStrategy.signCosmosTransaction({
|
|
476
|
-
txRaw,
|
|
477
|
-
chainId,
|
|
478
|
-
address: tx.injectiveAddress,
|
|
479
|
-
accountNumber: baseAccount.accountNumber,
|
|
480
|
-
}));
|
|
481
|
-
return walletStrategy.sendTransaction(directSignResponse, {
|
|
482
|
-
chainId,
|
|
483
|
-
endpoints,
|
|
484
|
-
txTimeout,
|
|
485
|
-
address: tx.injectiveAddress,
|
|
486
491
|
});
|
|
487
492
|
}
|
|
488
493
|
/**
|
|
@@ -491,94 +496,91 @@ class MsgBroadcaster {
|
|
|
491
496
|
* Note: Gas estimation not available
|
|
492
497
|
* @param tx the transaction that needs to be broadcasted
|
|
493
498
|
*/
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
tx: {
|
|
526
|
-
memo: tx.memo,
|
|
527
|
-
accountNumber: baseAccount.accountNumber.toString(),
|
|
528
|
-
sequence: baseAccount.sequence.toString(),
|
|
529
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
530
|
-
chainId,
|
|
531
|
-
},
|
|
532
|
-
ethereumChainId,
|
|
533
|
-
});
|
|
534
|
-
const aminoSignResponse = await cosmosWallet.signEIP712CosmosTx({
|
|
535
|
-
eip712: eip712TypedData,
|
|
536
|
-
signDoc: (0, wallet_base_1.createEip712StdSignDoc)({
|
|
537
|
-
...tx,
|
|
538
|
-
...baseAccount,
|
|
499
|
+
experimentalBroadcastWalletThroughLedger(tx) {
|
|
500
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
501
|
+
var _a, _b, _c;
|
|
502
|
+
const { chainId, txTimeout, endpoints, simulateTx, walletStrategy, ethereumChainId, } = this;
|
|
503
|
+
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
504
|
+
/**
|
|
505
|
+
* We can only use this method
|
|
506
|
+
* when Ledger is connected through Keplr
|
|
507
|
+
*/
|
|
508
|
+
if ([wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.Leap].includes(walletStrategy.getWallet())) {
|
|
509
|
+
const walletDeviceType = yield walletStrategy.getWalletDeviceType();
|
|
510
|
+
const isLedgerConnected = walletDeviceType === wallet_base_1.WalletDeviceType.Hardware;
|
|
511
|
+
if (!isLedgerConnected) {
|
|
512
|
+
throw new exceptions_1.GeneralException(new Error(`This method can only be used when Ledger is connected through ${walletStrategy.getWallet()}`));
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
if (!ethereumChainId) {
|
|
516
|
+
throw new exceptions_1.GeneralException(new Error('Please provide ethereumChainId'));
|
|
517
|
+
}
|
|
518
|
+
const cosmosWallet = walletStrategy.getCosmosWallet(chainId);
|
|
519
|
+
/** Account Details * */
|
|
520
|
+
const accountDetails = yield new sdk_ts_1.ChainGrpcAuthApi(endpoints.grpc).fetchAccount(tx.injectiveAddress);
|
|
521
|
+
const { baseAccount } = accountDetails;
|
|
522
|
+
/** Block Details */
|
|
523
|
+
const latestBlock = yield new sdk_ts_1.ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
524
|
+
const latestHeight = latestBlock.header.height;
|
|
525
|
+
const timeoutHeight = new utils_1.BigNumberInBase(latestHeight).plus(txTimeout);
|
|
526
|
+
const pubKey = yield walletStrategy.getPubKey();
|
|
527
|
+
const gas = (((_a = tx.gas) === null || _a === void 0 ? void 0 : _a.gas) || (0, sdk_ts_1.getGasPriceBasedOnMessage)(msgs)).toString();
|
|
528
|
+
/** EIP712 for signing on Ethereum wallets */
|
|
529
|
+
const eip712TypedData = (0, sdk_ts_1.getEip712TypedData)({
|
|
539
530
|
msgs,
|
|
531
|
+
fee: (0, utils_1.getStdFee)(Object.assign(Object.assign({}, tx.gas), { gas })),
|
|
532
|
+
tx: {
|
|
533
|
+
memo: tx.memo,
|
|
534
|
+
accountNumber: baseAccount.accountNumber.toString(),
|
|
535
|
+
sequence: baseAccount.sequence.toString(),
|
|
536
|
+
timeoutHeight: timeoutHeight.toFixed(),
|
|
537
|
+
chainId,
|
|
538
|
+
},
|
|
539
|
+
ethereumChainId,
|
|
540
|
+
});
|
|
541
|
+
const aminoSignResponse = yield cosmosWallet.signEIP712CosmosTx({
|
|
542
|
+
eip712: eip712TypedData,
|
|
543
|
+
signDoc: (0, wallet_base_1.createEip712StdSignDoc)(Object.assign(Object.assign(Object.assign({}, tx), baseAccount), { msgs,
|
|
544
|
+
chainId, gas: gas || ((_c = (_b = tx.gas) === null || _b === void 0 ? void 0 : _b.gas) === null || _c === void 0 ? void 0 : _c.toString()), timeoutHeight: timeoutHeight.toFixed() })),
|
|
545
|
+
});
|
|
546
|
+
/**
|
|
547
|
+
* Create TxRaw from the signed tx that we
|
|
548
|
+
* get as a response in case the user changed the fee/memo
|
|
549
|
+
* on the Keplr popup
|
|
550
|
+
*/
|
|
551
|
+
const { txRaw } = (0, sdk_ts_1.createTransaction)({
|
|
552
|
+
pubKey,
|
|
553
|
+
message: msgs,
|
|
554
|
+
memo: aminoSignResponse.signed.memo,
|
|
555
|
+
signMode: sdk_ts_1.SIGN_EIP712,
|
|
556
|
+
fee: aminoSignResponse.signed.fee,
|
|
557
|
+
sequence: parseInt(aminoSignResponse.signed.sequence, 10),
|
|
558
|
+
timeoutHeight: parseInt(aminoSignResponse.signed.timeout_height, 10),
|
|
559
|
+
accountNumber: parseInt(aminoSignResponse.signed.account_number, 10),
|
|
540
560
|
chainId,
|
|
541
|
-
gas: gas || tx.gas?.gas?.toString(),
|
|
542
|
-
timeoutHeight: timeoutHeight.toFixed(),
|
|
543
|
-
}),
|
|
544
|
-
});
|
|
545
|
-
/**
|
|
546
|
-
* Create TxRaw from the signed tx that we
|
|
547
|
-
* get as a response in case the user changed the fee/memo
|
|
548
|
-
* on the Keplr popup
|
|
549
|
-
*/
|
|
550
|
-
const { txRaw } = (0, sdk_ts_1.createTransaction)({
|
|
551
|
-
pubKey,
|
|
552
|
-
message: msgs,
|
|
553
|
-
memo: aminoSignResponse.signed.memo,
|
|
554
|
-
signMode: sdk_ts_1.SIGN_EIP712,
|
|
555
|
-
fee: aminoSignResponse.signed.fee,
|
|
556
|
-
sequence: parseInt(aminoSignResponse.signed.sequence, 10),
|
|
557
|
-
timeoutHeight: parseInt(aminoSignResponse.signed.timeout_height, 10),
|
|
558
|
-
accountNumber: parseInt(aminoSignResponse.signed.account_number, 10),
|
|
559
|
-
chainId,
|
|
560
|
-
});
|
|
561
|
-
/** Preparing the transaction for client broadcasting */
|
|
562
|
-
const web3Extension = (0, sdk_ts_1.createWeb3Extension)({
|
|
563
|
-
ethereumChainId,
|
|
564
|
-
});
|
|
565
|
-
const txRawEip712 = (0, sdk_ts_1.createTxRawEIP712)(txRaw, web3Extension);
|
|
566
|
-
if (simulateTx) {
|
|
567
|
-
await this.simulateTxRaw(txRawEip712);
|
|
568
|
-
}
|
|
569
|
-
/** Append Signatures */
|
|
570
|
-
const signatureBuff = Buffer.from(aminoSignResponse.signature.signature, 'base64');
|
|
571
|
-
txRawEip712.signatures = [signatureBuff];
|
|
572
|
-
/** Broadcast the transaction */
|
|
573
|
-
const response = await new sdk_ts_1.TxGrpcApi(endpoints.grpc).broadcast(txRawEip712, { txTimeout });
|
|
574
|
-
if (response.code !== 0) {
|
|
575
|
-
throw new exceptions_1.TransactionException(new Error(response.rawLog), {
|
|
576
|
-
code: exceptions_1.UnspecifiedErrorCode,
|
|
577
|
-
contextCode: response.code,
|
|
578
|
-
contextModule: response.codespace,
|
|
579
561
|
});
|
|
580
|
-
|
|
581
|
-
|
|
562
|
+
/** Preparing the transaction for client broadcasting */
|
|
563
|
+
const web3Extension = (0, sdk_ts_1.createWeb3Extension)({
|
|
564
|
+
ethereumChainId,
|
|
565
|
+
});
|
|
566
|
+
const txRawEip712 = (0, sdk_ts_1.createTxRawEIP712)(txRaw, web3Extension);
|
|
567
|
+
if (simulateTx) {
|
|
568
|
+
yield this.simulateTxRaw(txRawEip712);
|
|
569
|
+
}
|
|
570
|
+
/** Append Signatures */
|
|
571
|
+
const signatureBuff = Buffer.from(aminoSignResponse.signature.signature, 'base64');
|
|
572
|
+
txRawEip712.signatures = [signatureBuff];
|
|
573
|
+
/** Broadcast the transaction */
|
|
574
|
+
const response = yield new sdk_ts_1.TxGrpcApi(endpoints.grpc).broadcast(txRawEip712, { txTimeout });
|
|
575
|
+
if (response.code !== 0) {
|
|
576
|
+
throw new exceptions_1.TransactionException(new Error(response.rawLog), {
|
|
577
|
+
code: exceptions_1.UnspecifiedErrorCode,
|
|
578
|
+
contextCode: response.code,
|
|
579
|
+
contextModule: response.codespace,
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
return response;
|
|
583
|
+
});
|
|
582
584
|
}
|
|
583
585
|
/**
|
|
584
586
|
* Prepare/sign/broadcast transaction using
|
|
@@ -587,114 +589,121 @@ class MsgBroadcaster {
|
|
|
587
589
|
* @param tx The transaction that needs to be broadcasted
|
|
588
590
|
* @returns transaction hash
|
|
589
591
|
*/
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
592
|
+
broadcastCosmosWithFeeDelegation(tx) {
|
|
593
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
594
|
+
var _a;
|
|
595
|
+
const { options, chainId, txTimeout, endpoints, walletStrategy, txTimeoutOnFeeDelegation, } = this;
|
|
596
|
+
const msgs = Array.isArray(tx.msgs) ? tx.msgs : [tx.msgs];
|
|
597
|
+
/**
|
|
598
|
+
* We can only use this method when Keplr is connected
|
|
599
|
+
* with ledger
|
|
600
|
+
*/
|
|
601
|
+
if (walletStrategy.getWallet() === wallet_base_1.Wallet.Keplr) {
|
|
602
|
+
const walletDeviceType = yield walletStrategy.getWalletDeviceType();
|
|
603
|
+
const isLedgerConnectedOnKeplr = walletDeviceType === wallet_base_1.WalletDeviceType.Hardware;
|
|
604
|
+
if (isLedgerConnectedOnKeplr) {
|
|
605
|
+
throw new exceptions_1.GeneralException(new Error('Keplr + Ledger is not available with fee delegation. Connect with Ledger directly.'));
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
const cosmosWallet = walletStrategy.getCosmosWallet(chainId);
|
|
609
|
+
const canDisableCosmosGasCheck = [wallet_base_1.Wallet.Keplr, wallet_base_1.Wallet.OWallet].includes(walletStrategy.wallet);
|
|
610
|
+
const feePayerPubKey = yield this.fetchFeePayerPubKey(options.feePayerPubKey);
|
|
611
|
+
const feePayerPublicKey = sdk_ts_1.PublicKey.fromBase64(feePayerPubKey);
|
|
612
|
+
const feePayer = feePayerPublicKey.toAddress().address;
|
|
613
|
+
/** Account Details * */
|
|
614
|
+
const chainGrpcAuthApi = new sdk_ts_1.ChainGrpcAuthApi(endpoints.grpc);
|
|
615
|
+
const accountDetails = yield chainGrpcAuthApi.fetchAccount(tx.injectiveAddress);
|
|
616
|
+
const feePayerAccountDetails = yield chainGrpcAuthApi.fetchAccount(feePayer);
|
|
617
|
+
const { baseAccount } = accountDetails;
|
|
618
|
+
const { baseAccount: feePayerBaseAccount } = feePayerAccountDetails;
|
|
619
|
+
/** Block Details */
|
|
620
|
+
const latestBlock = yield new sdk_ts_1.ChainGrpcTendermintApi(endpoints.grpc).fetchLatestBlock();
|
|
621
|
+
const latestHeight = latestBlock.header.height;
|
|
622
|
+
const timeoutHeight = new utils_1.BigNumberInBase(latestHeight).plus(txTimeoutOnFeeDelegation ? txTimeout : utils_1.DEFAULT_BLOCK_TIMEOUT_HEIGHT);
|
|
623
|
+
const pubKey = yield walletStrategy.getPubKey();
|
|
624
|
+
const gas = (((_a = tx.gas) === null || _a === void 0 ? void 0 : _a.gas) || (0, sdk_ts_1.getGasPriceBasedOnMessage)(msgs)).toString();
|
|
625
|
+
/** Prepare the Transaction * */
|
|
626
|
+
const { txRaw } = yield this.getTxWithSignersAndStdFee({
|
|
627
|
+
chainId,
|
|
628
|
+
memo: tx.memo,
|
|
629
|
+
message: msgs,
|
|
630
|
+
timeoutHeight: timeoutHeight.toNumber(),
|
|
631
|
+
signers: [
|
|
632
|
+
{
|
|
633
|
+
pubKey,
|
|
634
|
+
accountNumber: baseAccount.accountNumber,
|
|
635
|
+
sequence: baseAccount.sequence,
|
|
636
|
+
},
|
|
637
|
+
{
|
|
638
|
+
pubKey: feePayerPublicKey.toBase64(),
|
|
639
|
+
accountNumber: feePayerBaseAccount.accountNumber,
|
|
640
|
+
sequence: feePayerBaseAccount.sequence,
|
|
641
|
+
},
|
|
642
|
+
],
|
|
643
|
+
fee: (0, utils_1.getStdFee)(Object.assign(Object.assign({}, tx.gas), { gas, payer: feePayer })),
|
|
644
|
+
});
|
|
645
|
+
// Temporary remove tx gas check because Keplr doesn't recognize feePayer
|
|
646
|
+
if (canDisableCosmosGasCheck && cosmosWallet.disableGasCheck) {
|
|
647
|
+
cosmosWallet.disableGasCheck(chainId);
|
|
648
|
+
}
|
|
649
|
+
const directSignResponse = (yield walletStrategy.signCosmosTransaction({
|
|
650
|
+
txRaw,
|
|
651
|
+
chainId,
|
|
652
|
+
address: tx.injectiveAddress,
|
|
653
|
+
accountNumber: baseAccount.accountNumber,
|
|
654
|
+
}));
|
|
655
|
+
const transactionApi = new sdk_ts_1.IndexerGrpcWeb3GwApi(endpoints.web3gw || endpoints.indexer);
|
|
656
|
+
const broadcast = () => __awaiter(this, void 0, void 0, function* () {
|
|
657
|
+
return yield transactionApi.broadcastCosmosTxRequest({
|
|
658
|
+
address: tx.injectiveAddress,
|
|
659
|
+
txRaw: (0, sdk_ts_1.createTxRawFromSigResponse)(directSignResponse),
|
|
660
|
+
signature: directSignResponse.signature.signature,
|
|
661
|
+
pubKey: directSignResponse.signature.pub_key || {
|
|
662
|
+
value: pubKey,
|
|
663
|
+
type: '/injective.crypto.v1beta1.ethsecp256k1.PubKey',
|
|
664
|
+
},
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
try {
|
|
668
|
+
const response = yield broadcast();
|
|
669
|
+
// Re-enable tx gas check removed above
|
|
670
|
+
if (canDisableCosmosGasCheck && cosmosWallet.enableGasCheck) {
|
|
671
|
+
cosmosWallet.enableGasCheck(chainId);
|
|
672
|
+
}
|
|
673
|
+
return yield new sdk_ts_1.TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash);
|
|
674
|
+
}
|
|
675
|
+
catch (e) {
|
|
676
|
+
const error = e;
|
|
677
|
+
if ((0, exceptions_1.isThrownException)(error)) {
|
|
678
|
+
const exception = error;
|
|
679
|
+
return yield this.retryOnException(exception, broadcast);
|
|
680
|
+
}
|
|
681
|
+
throw e;
|
|
682
|
+
}
|
|
660
683
|
});
|
|
661
|
-
try {
|
|
662
|
-
const response = await broadcast();
|
|
663
|
-
// Re-enable tx gas check removed above
|
|
664
|
-
if (canDisableCosmosGasCheck && cosmosWallet.enableGasCheck) {
|
|
665
|
-
cosmosWallet.enableGasCheck(chainId);
|
|
666
|
-
}
|
|
667
|
-
return await new sdk_ts_1.TxGrpcApi(endpoints.grpc).fetchTxPoll(response.txHash);
|
|
668
|
-
}
|
|
669
|
-
catch (e) {
|
|
670
|
-
const error = e;
|
|
671
|
-
if ((0, exceptions_1.isThrownException)(error)) {
|
|
672
|
-
const exception = error;
|
|
673
|
-
return await this.retryOnException(exception, broadcast);
|
|
674
|
-
}
|
|
675
|
-
throw e;
|
|
676
|
-
}
|
|
677
684
|
}
|
|
678
685
|
/**
|
|
679
686
|
* Fetch the fee payer's pub key from the web3 gateway
|
|
680
687
|
*
|
|
681
688
|
* Returns a base64 version of it
|
|
682
689
|
*/
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
response.feePayerPubKey.key.
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
690
|
+
fetchFeePayerPubKey(existingFeePayerPubKey) {
|
|
691
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
692
|
+
if (existingFeePayerPubKey) {
|
|
693
|
+
return existingFeePayerPubKey;
|
|
694
|
+
}
|
|
695
|
+
const { endpoints } = this;
|
|
696
|
+
const transactionApi = new sdk_ts_1.IndexerGrpcWeb3GwApi(endpoints.web3gw || endpoints.indexer);
|
|
697
|
+
const response = yield transactionApi.fetchFeePayer();
|
|
698
|
+
if (!response.feePayerPubKey) {
|
|
699
|
+
throw new exceptions_1.GeneralException(new Error('Please provide a feePayerPubKey'));
|
|
700
|
+
}
|
|
701
|
+
if (response.feePayerPubKey.key.startsWith('0x') ||
|
|
702
|
+
response.feePayerPubKey.key.length === 66) {
|
|
703
|
+
return Buffer.from(response.feePayerPubKey.key, 'hex').toString('base64');
|
|
704
|
+
}
|
|
705
|
+
return response.feePayerPubKey.key;
|
|
706
|
+
});
|
|
698
707
|
}
|
|
699
708
|
/**
|
|
700
709
|
* In case we don't want to simulate the transaction
|
|
@@ -704,79 +713,71 @@ class MsgBroadcaster {
|
|
|
704
713
|
* gas limit based on the simulation and add a small multiplier
|
|
705
714
|
* to be safe (factor of 1.2 as default)
|
|
706
715
|
*/
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
stdFee: (0, utils_1.getStdFee)(args.fee)
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
stdFee: (0, utils_1.getStdFee)(args.fee),
|
|
720
|
-
};
|
|
721
|
-
}
|
|
722
|
-
const stdGasFee = {
|
|
723
|
-
...(0, utils_1.getStdFee)({
|
|
724
|
-
...(0, utils_1.getStdFee)(args.fee),
|
|
725
|
-
gas: new utils_1.BigNumberInBase(result.gasInfo.gasUsed)
|
|
716
|
+
getTxWithSignersAndStdFee(args) {
|
|
717
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
718
|
+
var _a;
|
|
719
|
+
const { simulateTx } = this;
|
|
720
|
+
if (!simulateTx) {
|
|
721
|
+
return Object.assign(Object.assign({}, (0, sdk_ts_1.createTransactionWithSigners)(args)), { stdFee: (0, utils_1.getStdFee)(args.fee) });
|
|
722
|
+
}
|
|
723
|
+
const result = yield this.simulateTxWithSigners(args);
|
|
724
|
+
if (!((_a = result.gasInfo) === null || _a === void 0 ? void 0 : _a.gasUsed)) {
|
|
725
|
+
return Object.assign(Object.assign({}, (0, sdk_ts_1.createTransactionWithSigners)(args)), { stdFee: (0, utils_1.getStdFee)(args.fee) });
|
|
726
|
+
}
|
|
727
|
+
const stdGasFee = Object.assign({}, (0, utils_1.getStdFee)(Object.assign(Object.assign({}, (0, utils_1.getStdFee)(args.fee)), { gas: new utils_1.BigNumberInBase(result.gasInfo.gasUsed)
|
|
726
728
|
.times(this.gasBufferCoefficient)
|
|
727
|
-
.toFixed()
|
|
728
|
-
}),
|
|
729
|
-
};
|
|
730
|
-
return {
|
|
731
|
-
...(0, sdk_ts_1.createTransactionWithSigners)({
|
|
732
|
-
...args,
|
|
733
|
-
fee: stdGasFee,
|
|
734
|
-
}),
|
|
735
|
-
stdFee: stdGasFee,
|
|
736
|
-
};
|
|
729
|
+
.toFixed() })));
|
|
730
|
+
return Object.assign(Object.assign({}, (0, sdk_ts_1.createTransactionWithSigners)(Object.assign(Object.assign({}, args), { fee: stdGasFee }))), { stdFee: stdGasFee });
|
|
731
|
+
});
|
|
737
732
|
}
|
|
738
733
|
/**
|
|
739
734
|
* Create TxRaw and simulate it
|
|
740
735
|
*/
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
736
|
+
simulateTxRaw(txRaw) {
|
|
737
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
738
|
+
const { endpoints } = this;
|
|
739
|
+
txRaw.signatures = [new Uint8Array(0)];
|
|
740
|
+
const simulationResponse = yield new sdk_ts_1.TxGrpcApi(endpoints.grpc).simulate(txRaw);
|
|
741
|
+
return simulationResponse;
|
|
742
|
+
});
|
|
746
743
|
}
|
|
747
744
|
/**
|
|
748
745
|
* Create TxRaw and simulate it
|
|
749
746
|
*/
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
747
|
+
simulateTxWithSigners(args) {
|
|
748
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
749
|
+
const { endpoints } = this;
|
|
750
|
+
const { txRaw } = (0, sdk_ts_1.createTransactionWithSigners)(args);
|
|
751
|
+
txRaw.signatures = Array(Array.isArray(args.signers) ? args.signers.length : 1).fill(new Uint8Array(0));
|
|
752
|
+
const simulationResponse = yield new sdk_ts_1.TxGrpcApi(endpoints.grpc).simulate(txRaw);
|
|
753
|
+
return simulationResponse;
|
|
754
|
+
});
|
|
756
755
|
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
756
|
+
retryOnException(exception, retryLogic) {
|
|
757
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
758
|
+
const errorsToRetry = Object.keys(this.retriesOnError);
|
|
759
|
+
const errorKey = `${exception.contextModule}-${exception.contextCode}`;
|
|
760
|
+
if (!errorsToRetry.includes(errorKey)) {
|
|
761
|
+
throw exception;
|
|
762
|
+
}
|
|
763
|
+
const retryConfig = this.retriesOnError[errorKey];
|
|
764
|
+
if (retryConfig.retries >= retryConfig.maxRetries) {
|
|
765
|
+
this.retriesOnError = defaultRetriesConfig();
|
|
766
|
+
throw exception;
|
|
767
|
+
}
|
|
768
|
+
yield (0, utils_1.sleep)(retryConfig.timeout);
|
|
769
|
+
try {
|
|
770
|
+
retryConfig.retries += 1;
|
|
771
|
+
return yield retryLogic();
|
|
772
|
+
}
|
|
773
|
+
catch (e) {
|
|
774
|
+
const error = e;
|
|
775
|
+
if ((0, exceptions_1.isThrownException)(error)) {
|
|
776
|
+
return this.retryOnException(error, retryLogic);
|
|
777
|
+
}
|
|
778
|
+
throw e;
|
|
779
|
+
}
|
|
780
|
+
});
|
|
780
781
|
}
|
|
781
782
|
}
|
|
782
783
|
exports.MsgBroadcaster = MsgBroadcaster;
|