@ark-us/wasmx-stargate 0.0.10 → 0.0.12
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/LICENSE +674 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +5 -1
- package/build/index.js.map +1 -1
- package/build/modules/wasmx/aminomessages.d.ts +3 -3
- package/build/modules/wasmx/aminomessages.js +4 -4
- package/build/modules/wasmx/aminomessages.js.map +1 -1
- package/build/modules/wasmx/utils.d.ts +11 -0
- package/build/modules/wasmx/utils.js +43 -0
- package/build/modules/wasmx/utils.js.map +1 -0
- package/build/signingwasmxclient.js +14 -16
- package/build/signingwasmxclient.js.map +1 -1
- package/build/tendermintclient.d.ts +4 -2
- package/build/tendermintclient.js +8 -1
- package/build/tendermintclient.js.map +1 -1
- package/build/wasmxclient.d.ts +3 -4
- package/build/wasmxclient.js +4 -33
- package/build/wasmxclient.js.map +1 -1
- package/package.json +10 -10
- package/.DS_Store +0 -0
- package/.eslintignore +0 -5
- package/.eslintrc.js +0 -108
- package/.gitignore +0 -5
- package/build/encoding.spec.d.ts +0 -1
- package/build/encoding.spec.js +0 -78
- package/build/encoding.spec.js.map +0 -1
- package/build/modules/wasmx/aminomessages.spec.d.ts +0 -1
- package/build/modules/wasmx/aminomessages.spec.js +0 -242
- package/build/modules/wasmx/aminomessages.spec.js.map +0 -1
- package/jasmine-testrunner.js +0 -38
- package/karma.conf.js +0 -54
- package/src/encoding.spec.ts +0 -99
- package/src/encoding.ts +0 -21
- package/src/index.ts +0 -30
- package/src/modules/index.ts +0 -20
- package/src/modules/wasmx/aminomessages.spec.ts +0 -258
- package/src/modules/wasmx/aminomessages.ts +0 -177
- package/src/modules/wasmx/messages.ts +0 -54
- package/src/modules/wasmx/queries.ts +0 -141
- package/src/signingwasmxclient.ts +0 -660
- package/src/tendermintclient.ts +0 -7
- package/src/wasmxclient.ts +0 -494
- package/tsconfig.eslint.json +0 -9
- package/tsconfig.json +0 -12
- package/typedoc.js +0 -11
- package/webpack.web.config.js +0 -37
- package/yarn-error.log +0 -4205
- package/yarn.lock +0 -4145
|
@@ -1,660 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/naming-convention */
|
|
2
|
-
import { encodeSecp256k1Pubkey, makeSignDoc as makeSignDocAmino } from "@cosmjs/amino";
|
|
3
|
-
import { sha256 } from "@cosmjs/crypto";
|
|
4
|
-
import { fromBase64, toHex, toUtf8 } from "@cosmjs/encoding";
|
|
5
|
-
import { Int53, Uint53 } from "@cosmjs/math";
|
|
6
|
-
import {
|
|
7
|
-
EncodeObject,
|
|
8
|
-
encodePubkey,
|
|
9
|
-
isOfflineDirectSigner,
|
|
10
|
-
makeAuthInfoBytes,
|
|
11
|
-
makeSignDoc,
|
|
12
|
-
OfflineSigner,
|
|
13
|
-
Registry,
|
|
14
|
-
TxBodyEncodeObject,
|
|
15
|
-
} from "@cosmjs/proto-signing";
|
|
16
|
-
import {
|
|
17
|
-
AminoTypes,
|
|
18
|
-
calculateFee,
|
|
19
|
-
Coin,
|
|
20
|
-
createBankAminoConverters,
|
|
21
|
-
defaultRegistryTypes as defaultStargateTypes,
|
|
22
|
-
DeliverTxResponse,
|
|
23
|
-
Event,
|
|
24
|
-
GasPrice,
|
|
25
|
-
isDeliverTxFailure,
|
|
26
|
-
logs,
|
|
27
|
-
MsgDelegateEncodeObject,
|
|
28
|
-
MsgSendEncodeObject,
|
|
29
|
-
MsgUndelegateEncodeObject,
|
|
30
|
-
MsgWithdrawDelegatorRewardEncodeObject,
|
|
31
|
-
SignerData,
|
|
32
|
-
StdFee,
|
|
33
|
-
} from "@cosmjs/stargate";
|
|
34
|
-
import { HttpEndpoint, Tendermint34Client } from "@cosmjs/tendermint-rpc";
|
|
35
|
-
import {TendermintClient} from './tendermintclient';
|
|
36
|
-
import { assert, assertDefined } from "@cosmjs/utils";
|
|
37
|
-
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
|
|
38
|
-
import { MsgDelegate, MsgUndelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx";
|
|
39
|
-
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
|
|
40
|
-
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
|
|
41
|
-
import {
|
|
42
|
-
MsgExecuteContract,
|
|
43
|
-
MsgInstantiateContract,
|
|
44
|
-
MsgStoreCode,
|
|
45
|
-
MsgDeployCode,
|
|
46
|
-
CodeMetadata,
|
|
47
|
-
} from "@ark-us/wasmxjs";
|
|
48
|
-
import Long from 'long';
|
|
49
|
-
import pako from "pako";
|
|
50
|
-
|
|
51
|
-
import { WasmXClient } from "./wasmxclient";
|
|
52
|
-
import {
|
|
53
|
-
createWasmAminoConverters,
|
|
54
|
-
JsonObject,
|
|
55
|
-
MsgExecuteContractEncodeObject,
|
|
56
|
-
MsgInstantiateContractEncodeObject,
|
|
57
|
-
MsgStoreCodeEncodeObject,
|
|
58
|
-
MsgDeployCodeEncodeObject,
|
|
59
|
-
wasmTypes,
|
|
60
|
-
} from "./modules";
|
|
61
|
-
|
|
62
|
-
export interface UploadResult {
|
|
63
|
-
/** Size of the original wasm code in bytes */
|
|
64
|
-
readonly originalSize: number;
|
|
65
|
-
/** A hex encoded sha256 checksum of the original wasm code (that is stored on chain) */
|
|
66
|
-
readonly originalChecksum: string;
|
|
67
|
-
/** Size of the compressed wasm code in bytes */
|
|
68
|
-
readonly compressedSize: number;
|
|
69
|
-
/** A hex encoded sha256 checksum of the compressed wasm code (that stored in the transaction) */
|
|
70
|
-
readonly compressedChecksum: string;
|
|
71
|
-
/** The ID of the code asigned by the chain */
|
|
72
|
-
readonly codeId: number;
|
|
73
|
-
readonly logs: readonly logs.Log[];
|
|
74
|
-
/** Block height in which the transaction is included */
|
|
75
|
-
readonly height: number;
|
|
76
|
-
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
|
|
77
|
-
readonly transactionHash: string;
|
|
78
|
-
readonly events: readonly Event[];
|
|
79
|
-
readonly gasWanted: number;
|
|
80
|
-
readonly gasUsed: number;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
export interface DeployResult {
|
|
84
|
-
readonly checksum: string;
|
|
85
|
-
/** The ID of the code asigned by the chain */
|
|
86
|
-
readonly codeId: number;
|
|
87
|
-
readonly contractAddress: string;
|
|
88
|
-
readonly logs: readonly logs.Log[];
|
|
89
|
-
/** Block height in which the transaction is included */
|
|
90
|
-
readonly height: number;
|
|
91
|
-
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
|
|
92
|
-
readonly transactionHash: string;
|
|
93
|
-
readonly events: readonly Event[];
|
|
94
|
-
readonly gasWanted: number;
|
|
95
|
-
readonly gasUsed: number;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* The options of an .instantiate() call.
|
|
100
|
-
* All properties are optional.
|
|
101
|
-
*/
|
|
102
|
-
export interface InstantiateOptions {
|
|
103
|
-
readonly memo?: string;
|
|
104
|
-
/**
|
|
105
|
-
* The funds that are transferred from the sender to the newly created contract.
|
|
106
|
-
* The funds are transferred as part of the message execution after the contract address is
|
|
107
|
-
* created and before the instantiation message is executed by the contract.
|
|
108
|
-
*
|
|
109
|
-
* Only native tokens are supported.
|
|
110
|
-
*/
|
|
111
|
-
readonly funds?: readonly Coin[];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface InstantiateResult {
|
|
115
|
-
/** The address of the newly instantiated contract */
|
|
116
|
-
readonly contractAddress: string;
|
|
117
|
-
readonly logs: readonly logs.Log[];
|
|
118
|
-
/** Block height in which the transaction is included */
|
|
119
|
-
readonly height: number;
|
|
120
|
-
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
|
|
121
|
-
readonly transactionHash: string;
|
|
122
|
-
readonly events: readonly Event[];
|
|
123
|
-
readonly gasWanted: number;
|
|
124
|
-
readonly gasUsed: number;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Result type of updateAdmin and clearAdmin
|
|
129
|
-
*/
|
|
130
|
-
export interface ChangeAdminResult {
|
|
131
|
-
readonly logs: readonly logs.Log[];
|
|
132
|
-
/** Block height in which the transaction is included */
|
|
133
|
-
readonly height: number;
|
|
134
|
-
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
|
|
135
|
-
readonly transactionHash: string;
|
|
136
|
-
readonly events: readonly Event[];
|
|
137
|
-
readonly gasWanted: number;
|
|
138
|
-
readonly gasUsed: number;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
export interface MigrateResult {
|
|
142
|
-
readonly logs: readonly logs.Log[];
|
|
143
|
-
/** Block height in which the transaction is included */
|
|
144
|
-
readonly height: number;
|
|
145
|
-
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
|
|
146
|
-
readonly transactionHash: string;
|
|
147
|
-
readonly events: readonly Event[];
|
|
148
|
-
readonly gasWanted: number;
|
|
149
|
-
readonly gasUsed: number;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
export interface ExecuteInstruction {
|
|
153
|
-
contractAddress: string;
|
|
154
|
-
msg: JsonObject;
|
|
155
|
-
funds?: readonly Coin[];
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export interface ExecuteResult {
|
|
159
|
-
readonly logs: readonly logs.Log[];
|
|
160
|
-
/** Block height in which the transaction is included */
|
|
161
|
-
readonly height: number;
|
|
162
|
-
/** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex */
|
|
163
|
-
readonly transactionHash: string;
|
|
164
|
-
readonly events: readonly Event[];
|
|
165
|
-
readonly gasWanted: number;
|
|
166
|
-
readonly gasUsed: number;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function createDeliverTxResponseErrorMessage(result: DeliverTxResponse): string {
|
|
170
|
-
return `Error when broadcasting tx ${result.transactionHash} at height ${result.height}. Code: ${result.code}; Raw log: ${result.rawLog}`;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function createDefaultRegistry(): Registry {
|
|
174
|
-
return new Registry([...defaultStargateTypes, ...wasmTypes]);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export interface SigningWasmXClientOptions {
|
|
178
|
-
readonly registry?: Registry;
|
|
179
|
-
readonly aminoTypes?: AminoTypes;
|
|
180
|
-
readonly broadcastTimeoutMs?: number;
|
|
181
|
-
readonly broadcastPollIntervalMs?: number;
|
|
182
|
-
readonly gasPrice?: GasPrice;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
export class SigningWasmXClient extends WasmXClient {
|
|
186
|
-
public readonly registry: Registry;
|
|
187
|
-
public readonly broadcastTimeoutMs: number | undefined;
|
|
188
|
-
public readonly broadcastPollIntervalMs: number | undefined;
|
|
189
|
-
|
|
190
|
-
private readonly signer: OfflineSigner;
|
|
191
|
-
private readonly aminoTypes: AminoTypes;
|
|
192
|
-
private readonly gasPrice: GasPrice | undefined;
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Creates an instance by connecting to the given Tendermint RPC endpoint.
|
|
196
|
-
*
|
|
197
|
-
* For now this uses the Tendermint 0.34 client. If you need Tendermint 0.37
|
|
198
|
-
* support, see `createWithSigner`.
|
|
199
|
-
*/
|
|
200
|
-
public static async connectWithSigner(
|
|
201
|
-
endpoint: string | HttpEndpoint,
|
|
202
|
-
signer: OfflineSigner,
|
|
203
|
-
options: SigningWasmXClientOptions = {},
|
|
204
|
-
): Promise<SigningWasmXClient> {
|
|
205
|
-
const tmClient = await Tendermint34Client.connect(endpoint);
|
|
206
|
-
return SigningWasmXClient.createWithSigner(tmClient, signer, options);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Creates an instance from a manually created Tendermint client.
|
|
211
|
-
* Use this to use `Tendermint37Client` instead of `Tendermint34Client`.
|
|
212
|
-
*/
|
|
213
|
-
public static async createWithSigner(
|
|
214
|
-
tmClient: TendermintClient,
|
|
215
|
-
signer: OfflineSigner,
|
|
216
|
-
options: SigningWasmXClientOptions = {},
|
|
217
|
-
): Promise<SigningWasmXClient> {
|
|
218
|
-
return new SigningWasmXClient(tmClient, signer, options);
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/**
|
|
222
|
-
* Creates a client in offline mode.
|
|
223
|
-
*
|
|
224
|
-
* This should only be used in niche cases where you know exactly what you're doing,
|
|
225
|
-
* e.g. when building an offline signing application.
|
|
226
|
-
*
|
|
227
|
-
* When you try to use online functionality with such a signer, an
|
|
228
|
-
* exception will be raised.
|
|
229
|
-
*/
|
|
230
|
-
public static async offline(
|
|
231
|
-
signer: OfflineSigner,
|
|
232
|
-
options: SigningWasmXClientOptions = {},
|
|
233
|
-
): Promise<SigningWasmXClient> {
|
|
234
|
-
return new SigningWasmXClient(undefined, signer, options);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
protected constructor(
|
|
238
|
-
tmClient: TendermintClient | undefined,
|
|
239
|
-
signer: OfflineSigner,
|
|
240
|
-
options: SigningWasmXClientOptions,
|
|
241
|
-
) {
|
|
242
|
-
super(tmClient);
|
|
243
|
-
const {
|
|
244
|
-
registry = createDefaultRegistry(),
|
|
245
|
-
aminoTypes = new AminoTypes({ ...createWasmAminoConverters(), ...createBankAminoConverters() }),
|
|
246
|
-
} = options;
|
|
247
|
-
this.registry = registry;
|
|
248
|
-
this.aminoTypes = aminoTypes;
|
|
249
|
-
this.signer = signer;
|
|
250
|
-
this.broadcastTimeoutMs = options.broadcastTimeoutMs;
|
|
251
|
-
this.broadcastPollIntervalMs = options.broadcastPollIntervalMs;
|
|
252
|
-
this.gasPrice = options.gasPrice;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
public async simulate(
|
|
256
|
-
signerAddress: string,
|
|
257
|
-
messages: readonly EncodeObject[],
|
|
258
|
-
memo: string | undefined,
|
|
259
|
-
): Promise<number> {
|
|
260
|
-
const anyMsgs = messages.map((m) => this.registry.encodeAsAny(m));
|
|
261
|
-
const accountFromSigner = (await this.signer.getAccounts()).find(
|
|
262
|
-
(account) => account.address === signerAddress,
|
|
263
|
-
);
|
|
264
|
-
if (!accountFromSigner) {
|
|
265
|
-
throw new Error("Failed to retrieve account from signer");
|
|
266
|
-
}
|
|
267
|
-
const pubkey = encodeSecp256k1Pubkey(accountFromSigner.pubkey);
|
|
268
|
-
const { sequence } = await this.getSequence(signerAddress);
|
|
269
|
-
const { gasInfo } = await this.forceGetQueryClient().tx.simulate(anyMsgs, memo, pubkey, sequence);
|
|
270
|
-
assertDefined(gasInfo);
|
|
271
|
-
return Uint53.fromString(gasInfo.gasUsed.toString()).toNumber();
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
/** Uploads code and returns a receipt, including the code ID */
|
|
275
|
-
public async upload(
|
|
276
|
-
senderAddress: string,
|
|
277
|
-
wasmCode: Uint8Array,
|
|
278
|
-
deps: string[],
|
|
279
|
-
metadata: object,
|
|
280
|
-
fee: StdFee | "auto" | number,
|
|
281
|
-
memo = "",
|
|
282
|
-
): Promise<UploadResult> {
|
|
283
|
-
const compressed = pako.gzip(wasmCode, { level: 9 });
|
|
284
|
-
const storeCodeMsg: MsgStoreCodeEncodeObject = {
|
|
285
|
-
typeUrl: "/mythos.wasmx.v1.MsgStoreCode",
|
|
286
|
-
value: MsgStoreCode.fromPartial({
|
|
287
|
-
sender: senderAddress,
|
|
288
|
-
byteCode: compressed,
|
|
289
|
-
deps: deps,
|
|
290
|
-
metadata: CodeMetadata.fromPartial(metadata),
|
|
291
|
-
}),
|
|
292
|
-
};
|
|
293
|
-
|
|
294
|
-
const result = await this.signAndBroadcast(senderAddress, [storeCodeMsg], fee, memo);
|
|
295
|
-
if (isDeliverTxFailure(result)) {
|
|
296
|
-
throw new Error(createDeliverTxResponseErrorMessage(result));
|
|
297
|
-
}
|
|
298
|
-
console.log("result", result);
|
|
299
|
-
const parsedLogs = logs.parseRawLog(result.rawLog || "[]");
|
|
300
|
-
// const codeIdAttr = logs.findAttribute(parsedLogs, "store_code", "code_id");
|
|
301
|
-
const storeCodeEvent = result.events.find((ev: any) => ev.type === "store_code")
|
|
302
|
-
if (!storeCodeEvent) throw new Error("No store_code event found in receipt: " + JSON.stringify(result));
|
|
303
|
-
const codeIdAttr = storeCodeEvent.attributes.find((attr: any) => attr.key === "code_id");
|
|
304
|
-
if (!codeIdAttr) throw new Error("No code_id attribute found in event: " + JSON.stringify(result));
|
|
305
|
-
return {
|
|
306
|
-
originalSize: wasmCode.length,
|
|
307
|
-
originalChecksum: toHex(sha256(wasmCode)),
|
|
308
|
-
compressedSize: compressed.length,
|
|
309
|
-
compressedChecksum: toHex(sha256(compressed)),
|
|
310
|
-
codeId: Number.parseInt(codeIdAttr.value, 10),
|
|
311
|
-
logs: parsedLogs,
|
|
312
|
-
height: result.height,
|
|
313
|
-
transactionHash: result.transactionHash,
|
|
314
|
-
events: result.events,
|
|
315
|
-
gasWanted: result.gasWanted,
|
|
316
|
-
gasUsed: result.gasUsed,
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
/** Uploads code and returns a receipt, including the code ID */
|
|
321
|
-
public async deployCode(
|
|
322
|
-
senderAddress: string,
|
|
323
|
-
code: Uint8Array,
|
|
324
|
-
metadata: object,
|
|
325
|
-
deps: string[],
|
|
326
|
-
msg: JsonObject,
|
|
327
|
-
label: string,
|
|
328
|
-
fee: StdFee | "auto" | number,
|
|
329
|
-
options: InstantiateOptions = {},
|
|
330
|
-
): Promise<DeployResult> {
|
|
331
|
-
const storeCodeEvmMsg: MsgDeployCodeEncodeObject = {
|
|
332
|
-
typeUrl: "/mythos.wasmx.v1.MsgDeployCode",
|
|
333
|
-
value: MsgDeployCode.fromPartial({
|
|
334
|
-
sender: senderAddress,
|
|
335
|
-
byteCode: code,
|
|
336
|
-
metadata: CodeMetadata.fromPartial(metadata),
|
|
337
|
-
deps: deps,
|
|
338
|
-
label: label,
|
|
339
|
-
msg: toUtf8(JSON.stringify(msg)),
|
|
340
|
-
funds: [...(options.funds || [])],
|
|
341
|
-
}),
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
const result = await this.signAndBroadcast(senderAddress, [storeCodeEvmMsg], fee, options.memo);
|
|
345
|
-
if (isDeliverTxFailure(result)) {
|
|
346
|
-
throw new Error(createDeliverTxResponseErrorMessage(result));
|
|
347
|
-
}
|
|
348
|
-
const parsedLogs = logs.parseRawLog(result.rawLog || "[]");
|
|
349
|
-
|
|
350
|
-
// const codeIdAttr = logs.findAttribute(parsedLogs, "store_code", "code_id");
|
|
351
|
-
const storeCodeEvent = result.events.find((ev: any) => ev.type === "store_code")
|
|
352
|
-
if (!storeCodeEvent) throw new Error("No store_code event found in receipt: " + JSON.stringify(result));
|
|
353
|
-
const codeIdAttr = storeCodeEvent.attributes.find((attr: any) => attr.key === "code_id");
|
|
354
|
-
if (!codeIdAttr) throw new Error("No code_id attribute found in store_code event: " + JSON.stringify(result));
|
|
355
|
-
|
|
356
|
-
// const contractAddress = logs.findAttribute(parsedLogs, "instantiate", "contract_address");
|
|
357
|
-
const instantiateEvent = result.events.find((ev: any) => ev.type === "instantiate")
|
|
358
|
-
if (!instantiateEvent) throw new Error("No instantiate event found in receipt: " + JSON.stringify(result));
|
|
359
|
-
const contractAddress = instantiateEvent.attributes.find((attr: any) => attr.key === "contract_address");
|
|
360
|
-
if (!contractAddress) throw new Error("No contract_address attribute found in instantiate event: " + JSON.stringify(result));
|
|
361
|
-
|
|
362
|
-
return {
|
|
363
|
-
checksum: toHex(sha256(code)),
|
|
364
|
-
codeId: Number.parseInt(codeIdAttr.value, 10),
|
|
365
|
-
contractAddress: contractAddress.value,
|
|
366
|
-
logs: parsedLogs,
|
|
367
|
-
height: result.height,
|
|
368
|
-
transactionHash: result.transactionHash,
|
|
369
|
-
events: result.events,
|
|
370
|
-
gasWanted: result.gasWanted,
|
|
371
|
-
gasUsed: result.gasUsed,
|
|
372
|
-
};
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
public async instantiate(
|
|
376
|
-
senderAddress: string,
|
|
377
|
-
codeId: number,
|
|
378
|
-
msg: JsonObject,
|
|
379
|
-
label: string,
|
|
380
|
-
fee: StdFee | "auto" | number,
|
|
381
|
-
options: InstantiateOptions = {},
|
|
382
|
-
): Promise<InstantiateResult> {
|
|
383
|
-
const instantiateContractMsg: MsgInstantiateContractEncodeObject = {
|
|
384
|
-
typeUrl: "/mythos.wasmx.v1.MsgInstantiateContract",
|
|
385
|
-
value: MsgInstantiateContract.fromPartial({
|
|
386
|
-
sender: senderAddress,
|
|
387
|
-
// @ts-ignore
|
|
388
|
-
codeId: Long.fromString(new Uint53(codeId).toString()),
|
|
389
|
-
label: label,
|
|
390
|
-
msg: toUtf8(JSON.stringify(msg)),
|
|
391
|
-
funds: [...(options.funds || [])],
|
|
392
|
-
}),
|
|
393
|
-
};
|
|
394
|
-
const result = await this.signAndBroadcast(senderAddress, [instantiateContractMsg], fee, options.memo);
|
|
395
|
-
if (isDeliverTxFailure(result)) {
|
|
396
|
-
throw new Error(createDeliverTxResponseErrorMessage(result));
|
|
397
|
-
}
|
|
398
|
-
const parsedLogs = logs.parseRawLog(result.rawLog || "[]");
|
|
399
|
-
|
|
400
|
-
// const contractAddressAttr = logs.findAttribute(parsedLogs, "instantiate", "contract_address");
|
|
401
|
-
const instantiateEvent = result.events.find((ev: any) => ev.type === "instantiate")
|
|
402
|
-
if (!instantiateEvent) throw new Error("No instantiate event found in receipt: " + JSON.stringify(result));
|
|
403
|
-
const contractAddressAttr = instantiateEvent.attributes.find((attr: any) => attr.key === "contract_address");
|
|
404
|
-
if (!contractAddressAttr) throw new Error("No contract_address attribute found in instantiate event: " + JSON.stringify(result));
|
|
405
|
-
|
|
406
|
-
return {
|
|
407
|
-
contractAddress: contractAddressAttr.value,
|
|
408
|
-
logs: parsedLogs,
|
|
409
|
-
height: result.height,
|
|
410
|
-
transactionHash: result.transactionHash,
|
|
411
|
-
events: result.events,
|
|
412
|
-
gasWanted: result.gasWanted,
|
|
413
|
-
gasUsed: result.gasUsed,
|
|
414
|
-
};
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
public async execute(
|
|
418
|
-
senderAddress: string,
|
|
419
|
-
contractAddress: string,
|
|
420
|
-
msg: JsonObject,
|
|
421
|
-
fee: StdFee | "auto" | number,
|
|
422
|
-
memo = "",
|
|
423
|
-
funds?: readonly Coin[],
|
|
424
|
-
): Promise<ExecuteResult> {
|
|
425
|
-
const instruction: ExecuteInstruction = {
|
|
426
|
-
contractAddress: contractAddress,
|
|
427
|
-
msg: msg,
|
|
428
|
-
funds: funds,
|
|
429
|
-
};
|
|
430
|
-
return this.executeMultiple(senderAddress, [instruction], fee, memo);
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
/**
|
|
434
|
-
* Like `execute` but allows executing multiple messages in one transaction.
|
|
435
|
-
*/
|
|
436
|
-
public async executeMultiple(
|
|
437
|
-
senderAddress: string,
|
|
438
|
-
instructions: readonly ExecuteInstruction[],
|
|
439
|
-
fee: StdFee | "auto" | number,
|
|
440
|
-
memo = "",
|
|
441
|
-
): Promise<ExecuteResult> {
|
|
442
|
-
const msgs: MsgExecuteContractEncodeObject[] = instructions.map((i) => ({
|
|
443
|
-
typeUrl: "/mythos.wasmx.v1.MsgExecuteContract",
|
|
444
|
-
value: MsgExecuteContract.fromPartial({
|
|
445
|
-
sender: senderAddress,
|
|
446
|
-
contract: i.contractAddress,
|
|
447
|
-
msg: toUtf8(JSON.stringify(i.msg)),
|
|
448
|
-
funds: [...(i.funds || [])],
|
|
449
|
-
}),
|
|
450
|
-
}));
|
|
451
|
-
const result = await this.signAndBroadcast(senderAddress, msgs, fee, memo);
|
|
452
|
-
if (isDeliverTxFailure(result)) {
|
|
453
|
-
throw new Error(createDeliverTxResponseErrorMessage(result));
|
|
454
|
-
}
|
|
455
|
-
return {
|
|
456
|
-
logs: logs.parseRawLog(result.rawLog || "[]"),
|
|
457
|
-
height: result.height,
|
|
458
|
-
transactionHash: result.transactionHash,
|
|
459
|
-
events: result.events,
|
|
460
|
-
gasWanted: result.gasWanted,
|
|
461
|
-
gasUsed: result.gasUsed,
|
|
462
|
-
};
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
public async sendTokens(
|
|
466
|
-
senderAddress: string,
|
|
467
|
-
recipientAddress: string,
|
|
468
|
-
amount: readonly Coin[],
|
|
469
|
-
fee: StdFee | "auto" | number,
|
|
470
|
-
memo = "",
|
|
471
|
-
): Promise<DeliverTxResponse> {
|
|
472
|
-
const sendMsg: MsgSendEncodeObject = {
|
|
473
|
-
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
|
|
474
|
-
value: {
|
|
475
|
-
fromAddress: senderAddress,
|
|
476
|
-
toAddress: recipientAddress,
|
|
477
|
-
amount: [...amount],
|
|
478
|
-
},
|
|
479
|
-
};
|
|
480
|
-
return this.signAndBroadcast(senderAddress, [sendMsg], fee, memo);
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
public async delegateTokens(
|
|
484
|
-
delegatorAddress: string,
|
|
485
|
-
validatorAddress: string,
|
|
486
|
-
amount: Coin,
|
|
487
|
-
fee: StdFee | "auto" | number,
|
|
488
|
-
memo = "",
|
|
489
|
-
): Promise<DeliverTxResponse> {
|
|
490
|
-
const delegateMsg: MsgDelegateEncodeObject = {
|
|
491
|
-
typeUrl: "/cosmos.staking.v1beta1.MsgDelegate",
|
|
492
|
-
value: MsgDelegate.fromPartial({ delegatorAddress: delegatorAddress, validatorAddress, amount }),
|
|
493
|
-
};
|
|
494
|
-
return this.signAndBroadcast(delegatorAddress, [delegateMsg], fee, memo);
|
|
495
|
-
}
|
|
496
|
-
|
|
497
|
-
public async undelegateTokens(
|
|
498
|
-
delegatorAddress: string,
|
|
499
|
-
validatorAddress: string,
|
|
500
|
-
amount: Coin,
|
|
501
|
-
fee: StdFee | "auto" | number,
|
|
502
|
-
memo = "",
|
|
503
|
-
): Promise<DeliverTxResponse> {
|
|
504
|
-
const undelegateMsg: MsgUndelegateEncodeObject = {
|
|
505
|
-
typeUrl: "/cosmos.staking.v1beta1.MsgUndelegate",
|
|
506
|
-
value: MsgUndelegate.fromPartial({ delegatorAddress: delegatorAddress, validatorAddress, amount }),
|
|
507
|
-
};
|
|
508
|
-
return this.signAndBroadcast(delegatorAddress, [undelegateMsg], fee, memo);
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
public async withdrawRewards(
|
|
512
|
-
delegatorAddress: string,
|
|
513
|
-
validatorAddress: string,
|
|
514
|
-
fee: StdFee | "auto" | number,
|
|
515
|
-
memo = "",
|
|
516
|
-
): Promise<DeliverTxResponse> {
|
|
517
|
-
const withdrawDelegatorRewardMsg: MsgWithdrawDelegatorRewardEncodeObject = {
|
|
518
|
-
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawDelegatorReward",
|
|
519
|
-
value: MsgWithdrawDelegatorReward.fromPartial({ delegatorAddress: delegatorAddress, validatorAddress }),
|
|
520
|
-
};
|
|
521
|
-
return this.signAndBroadcast(delegatorAddress, [withdrawDelegatorRewardMsg], fee, memo);
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
/**
|
|
525
|
-
* Creates a transaction with the given messages, fee and memo. Then signs and broadcasts the transaction.
|
|
526
|
-
*
|
|
527
|
-
* @param signerAddress The address that will sign transactions using this instance. The signer must be able to sign with this address.
|
|
528
|
-
* @param messages
|
|
529
|
-
* @param fee
|
|
530
|
-
* @param memo
|
|
531
|
-
*/
|
|
532
|
-
public async signAndBroadcast(
|
|
533
|
-
signerAddress: string,
|
|
534
|
-
messages: readonly EncodeObject[],
|
|
535
|
-
fee: StdFee | "auto" | number,
|
|
536
|
-
memo = "",
|
|
537
|
-
): Promise<DeliverTxResponse> {
|
|
538
|
-
let usedFee: StdFee;
|
|
539
|
-
if (fee == "auto" || typeof fee === "number") {
|
|
540
|
-
assertDefined(this.gasPrice, "Gas price must be set in the client options when auto gas is used.");
|
|
541
|
-
const gasEstimation = await this.simulate(signerAddress, messages, memo);
|
|
542
|
-
const multiplier = typeof fee === "number" ? fee : 1.3;
|
|
543
|
-
usedFee = calculateFee(Math.round(gasEstimation * multiplier), this.gasPrice);
|
|
544
|
-
} else {
|
|
545
|
-
usedFee = fee;
|
|
546
|
-
}
|
|
547
|
-
const txRaw = await this.sign(signerAddress, messages, usedFee, memo);
|
|
548
|
-
const txBytes = TxRaw.encode(txRaw).finish();
|
|
549
|
-
return this.broadcastTx(txBytes, this.broadcastTimeoutMs, this.broadcastPollIntervalMs);
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
public async sign(
|
|
553
|
-
signerAddress: string,
|
|
554
|
-
messages: readonly EncodeObject[],
|
|
555
|
-
fee: StdFee,
|
|
556
|
-
memo: string,
|
|
557
|
-
explicitSignerData?: SignerData,
|
|
558
|
-
): Promise<TxRaw> {
|
|
559
|
-
let signerData: SignerData;
|
|
560
|
-
if (explicitSignerData) {
|
|
561
|
-
signerData = explicitSignerData;
|
|
562
|
-
} else {
|
|
563
|
-
const { accountNumber, sequence } = await this.getSequence(signerAddress);
|
|
564
|
-
const chainId = await this.getChainId();
|
|
565
|
-
signerData = {
|
|
566
|
-
accountNumber: accountNumber,
|
|
567
|
-
sequence: sequence,
|
|
568
|
-
chainId: chainId,
|
|
569
|
-
};
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
return isOfflineDirectSigner(this.signer)
|
|
573
|
-
? this.signDirect(signerAddress, messages, fee, memo, signerData)
|
|
574
|
-
: this.signAmino(signerAddress, messages, fee, memo, signerData);
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
private async signAmino(
|
|
578
|
-
signerAddress: string,
|
|
579
|
-
messages: readonly EncodeObject[],
|
|
580
|
-
fee: StdFee,
|
|
581
|
-
memo: string,
|
|
582
|
-
{ accountNumber, sequence, chainId }: SignerData,
|
|
583
|
-
): Promise<TxRaw> {
|
|
584
|
-
assert(!isOfflineDirectSigner(this.signer));
|
|
585
|
-
const accountFromSigner = (await this.signer.getAccounts()).find(
|
|
586
|
-
(account) => account.address === signerAddress,
|
|
587
|
-
);
|
|
588
|
-
if (!accountFromSigner) {
|
|
589
|
-
throw new Error("Failed to retrieve account from signer");
|
|
590
|
-
}
|
|
591
|
-
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
|
|
592
|
-
const signMode = SignMode.SIGN_MODE_LEGACY_AMINO_JSON;
|
|
593
|
-
const msgs = messages.map((msg) => this.aminoTypes.toAmino(msg));
|
|
594
|
-
const signDoc = makeSignDocAmino(msgs, fee, chainId, memo, accountNumber, sequence);
|
|
595
|
-
const { signature, signed } = await this.signer.signAmino(signerAddress, signDoc);
|
|
596
|
-
const signedTxBody: TxBodyEncodeObject = {
|
|
597
|
-
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
598
|
-
value: {
|
|
599
|
-
messages: signed.msgs.map((msg) => this.aminoTypes.fromAmino(msg)),
|
|
600
|
-
memo: signed.memo,
|
|
601
|
-
},
|
|
602
|
-
};
|
|
603
|
-
const signedTxBodyBytes = this.registry.encode(signedTxBody);
|
|
604
|
-
const signedGasLimit = Int53.fromString(signed.fee.gas).toNumber();
|
|
605
|
-
const signedSequence = Int53.fromString(signed.sequence).toNumber();
|
|
606
|
-
const signedAuthInfoBytes = makeAuthInfoBytes(
|
|
607
|
-
[{ pubkey, sequence: signedSequence }],
|
|
608
|
-
signed.fee.amount,
|
|
609
|
-
signedGasLimit,
|
|
610
|
-
signed.fee.granter,
|
|
611
|
-
signed.fee.payer,
|
|
612
|
-
signMode,
|
|
613
|
-
);
|
|
614
|
-
return TxRaw.fromPartial({
|
|
615
|
-
bodyBytes: signedTxBodyBytes,
|
|
616
|
-
authInfoBytes: signedAuthInfoBytes,
|
|
617
|
-
signatures: [fromBase64(signature.signature)],
|
|
618
|
-
});
|
|
619
|
-
}
|
|
620
|
-
|
|
621
|
-
private async signDirect(
|
|
622
|
-
signerAddress: string,
|
|
623
|
-
messages: readonly EncodeObject[],
|
|
624
|
-
fee: StdFee,
|
|
625
|
-
memo: string,
|
|
626
|
-
{ accountNumber, sequence, chainId }: SignerData,
|
|
627
|
-
): Promise<TxRaw> {
|
|
628
|
-
assert(isOfflineDirectSigner(this.signer));
|
|
629
|
-
const accountFromSigner = (await this.signer.getAccounts()).find(
|
|
630
|
-
(account) => account.address === signerAddress,
|
|
631
|
-
);
|
|
632
|
-
if (!accountFromSigner) {
|
|
633
|
-
throw new Error("Failed to retrieve account from signer");
|
|
634
|
-
}
|
|
635
|
-
const pubkey = encodePubkey(encodeSecp256k1Pubkey(accountFromSigner.pubkey));
|
|
636
|
-
const txBody: TxBodyEncodeObject = {
|
|
637
|
-
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
|
638
|
-
value: {
|
|
639
|
-
messages: messages,
|
|
640
|
-
memo: memo,
|
|
641
|
-
},
|
|
642
|
-
};
|
|
643
|
-
const txBodyBytes = this.registry.encode(txBody);
|
|
644
|
-
const gasLimit = Int53.fromString(fee.gas).toNumber();
|
|
645
|
-
const authInfoBytes = makeAuthInfoBytes(
|
|
646
|
-
[{ pubkey, sequence }],
|
|
647
|
-
fee.amount,
|
|
648
|
-
gasLimit,
|
|
649
|
-
fee.granter,
|
|
650
|
-
fee.payer,
|
|
651
|
-
);
|
|
652
|
-
const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber);
|
|
653
|
-
const { signature, signed } = await this.signer.signDirect(signerAddress, signDoc);
|
|
654
|
-
return TxRaw.fromPartial({
|
|
655
|
-
bodyBytes: signed.bodyBytes,
|
|
656
|
-
authInfoBytes: signed.authInfoBytes,
|
|
657
|
-
signatures: [fromBase64(signature.signature)],
|
|
658
|
-
});
|
|
659
|
-
}
|
|
660
|
-
}
|
package/src/tendermintclient.ts
DELETED