Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add instantiate2 broadcast in SigningCosmwasmClient #1407

Merged
merged 3 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cosmwasm-stargate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ export {
createWasmAminoConverters,
isMsgClearAdminEncodeObject,
isMsgExecuteEncodeObject,
isMsgInstantiateContract2EncodeObject,
isMsgInstantiateContractEncodeObject,
isMsgMigrateEncodeObject,
isMsgStoreCodeEncodeObject,
isMsgUpdateAdminEncodeObject,
JsonObject,
MsgClearAdminEncodeObject,
MsgExecuteContractEncodeObject,
MsgInstantiateContract2EncodeObject,
MsgInstantiateContractEncodeObject,
MsgMigrateContractEncodeObject,
MsgStoreCodeEncodeObject,
Expand Down
2 changes: 2 additions & 0 deletions packages/cosmwasm-stargate/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ export {
export {
isMsgClearAdminEncodeObject,
isMsgExecuteEncodeObject,
isMsgInstantiateContract2EncodeObject,
isMsgInstantiateContractEncodeObject,
isMsgMigrateEncodeObject,
isMsgStoreCodeEncodeObject,
isMsgUpdateAdminEncodeObject,
MsgClearAdminEncodeObject,
MsgExecuteContractEncodeObject,
MsgInstantiateContract2EncodeObject,
MsgInstantiateContractEncodeObject,
MsgMigrateContractEncodeObject,
MsgStoreCodeEncodeObject,
Expand Down
15 changes: 15 additions & 0 deletions packages/cosmwasm-stargate/src/modules/wasm/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
MsgClearAdmin,
MsgExecuteContract,
MsgInstantiateContract,
MsgInstantiateContract2,
MsgMigrateContract,
MsgStoreCode,
MsgUpdateAdmin,
Expand All @@ -14,6 +15,7 @@ export const wasmTypes: ReadonlyArray<[string, GeneratedType]> = [
["/cosmwasm.wasm.v1.MsgMigrateContract", MsgMigrateContract],
["/cosmwasm.wasm.v1.MsgStoreCode", MsgStoreCode],
["/cosmwasm.wasm.v1.MsgInstantiateContract", MsgInstantiateContract],
["/cosmwasm.wasm.v1.MsgInstantiateContract2", MsgInstantiateContract2],
["/cosmwasm.wasm.v1.MsgUpdateAdmin", MsgUpdateAdmin],
];

Expand All @@ -39,6 +41,19 @@ export function isMsgInstantiateContractEncodeObject(
);
}

export interface MsgInstantiateContract2EncodeObject extends EncodeObject {
readonly typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract2";
readonly value: Partial<MsgInstantiateContract2>;
}

export function isMsgInstantiateContract2EncodeObject(
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
object: EncodeObject,
): object is MsgInstantiateContract2EncodeObject {
return (
(object as MsgInstantiateContract2EncodeObject).typeUrl === "/cosmwasm.wasm.v1.MsgInstantiateContract2"
);
}

export interface MsgUpdateAdminEncodeObject extends EncodeObject {
readonly typeUrl: "/cosmwasm.wasm.v1.MsgUpdateAdmin";
readonly value: Partial<MsgUpdateAdmin>;
Expand Down
45 changes: 45 additions & 0 deletions packages/cosmwasm-stargate/src/signingcosmwasmclient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Long from "long";
import pako from "pako";
import protobuf from "protobufjs/minimal";

import { instantiate2Address } from "./instantiate2";
import { MsgExecuteContractEncodeObject, MsgStoreCodeEncodeObject } from "./modules";
import { SigningCosmWasmClient } from "./signingcosmwasmclient";
import {
Expand Down Expand Up @@ -250,6 +251,50 @@ describe("SigningCosmWasmClient", () => {
});
});

describe("instantiate2", () => {
it("can instantiate with predictable address", async () => {
pendingWithoutWasmd();
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, {
prefix: wasmd.prefix,
});
const options = { ...defaultSigningClientOptions, prefix: wasmd.prefix };

const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data, defaultUploadFee);
const funds = [coin(1234, "ucosm"), coin(321, "ustake")];
const beneficiaryAddress = makeRandomAddress();
const salt = Uint8Array.from([0x01]);
const wasm = getHackatom().data;
const msg = {
verifier: alice.address0,
beneficiary: beneficiaryAddress,
};
const expectedAddress = instantiate2Address(sha256(wasm), alice.address0, salt, wasmd.prefix);

const { contractAddress } = await client.instantiate2(
alice.address0,
codeId,
salt,
msg,
"My cool label--",
defaultInstantiateFee,
{
memo: "Let's see if the memo is used",
funds: funds,
},
);

const wasmClient = await makeWasmClient(wasmd.endpoint);
const ucosmBalance = await wasmClient.bank.balance(contractAddress, "ucosm");
const ustakeBalance = await wasmClient.bank.balance(contractAddress, "ustake");
expect(ucosmBalance).toEqual(funds[0]);
expect(ustakeBalance).toEqual(funds[1]);

expect(contractAddress).toEqual(expectedAddress);
client.disconnect();
});
});

describe("updateAdmin", () => {
it("can update an admin", async () => {
pendingWithoutWasmd();
Expand Down
43 changes: 42 additions & 1 deletion packages/cosmwasm-stargate/src/signingcosmwasmclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
MsgClearAdmin,
MsgExecuteContract,
MsgInstantiateContract,
MsgInstantiateContract2,
MsgMigrateContract,
MsgStoreCode,
MsgUpdateAdmin,
Expand All @@ -54,6 +55,7 @@ import {
JsonObject,
MsgClearAdminEncodeObject,
MsgExecuteContractEncodeObject,
MsgInstantiateContract2EncodeObject,
MsgInstantiateContractEncodeObject,
MsgMigrateContractEncodeObject,
MsgStoreCodeEncodeObject,
Expand Down Expand Up @@ -83,7 +85,7 @@ export interface UploadResult {
}

/**
* The options of an .instantiate() call.
* The options of .instantiate() and .instantiate2() call.
* All properties are optional.
*/
export interface InstantiateOptions {
Expand Down Expand Up @@ -335,6 +337,45 @@ export class SigningCosmWasmClient extends CosmWasmClient {
};
}

public async instantiate2(
senderAddress: string,
codeId: number,
salt: Uint8Array,
msg: JsonObject,
label: string,
fee: StdFee | "auto" | number,
options: InstantiateOptions = {},
): Promise<InstantiateResult> {
const instantiateContract2Msg: MsgInstantiateContract2EncodeObject = {
typeUrl: "/cosmwasm.wasm.v1.MsgInstantiateContract2",
value: MsgInstantiateContract2.fromPartial({
sender: senderAddress,
codeId: Long.fromString(new Uint53(codeId).toString()),
label: label,
msg: toUtf8(JSON.stringify(msg)),
funds: [...(options.funds || [])],
admin: options.admin,
salt: salt,
fixMsg: false,
}),
};
const result = await this.signAndBroadcast(senderAddress, [instantiateContract2Msg], fee, options.memo);
if (isDeliverTxFailure(result)) {
throw new Error(createDeliverTxResponseErrorMessage(result));
}
const parsedLogs = logs.parseRawLog(result.rawLog);
const contractAddressAttr = logs.findAttribute(parsedLogs, "instantiate", "_contract_address");
return {
contractAddress: contractAddressAttr.value,
logs: parsedLogs,
height: result.height,
transactionHash: result.transactionHash,
events: result.events,
gasWanted: result.gasWanted,
gasUsed: result.gasUsed,
};
}

public async updateAdmin(
senderAddress: string,
contractAddress: string,
Expand Down