Allow setting source and builder in .upload()

This commit is contained in:
Simon Warta 2020-02-28 15:13:11 +01:00
parent f8c71cf471
commit a8e3e87f69
6 changed files with 41 additions and 6 deletions

View File

@ -32,5 +32,6 @@ export {
SigningCallback,
SigningCosmWasmClient,
ExecuteResult,
UploadMeta,
UploadReceipt,
} from "./signingcosmwasmclient";

View File

@ -4,7 +4,7 @@ import { assert } from "@iov/utils";
import { Secp256k1Pen } from "./pen";
import { RestClient } from "./restclient";
import { SigningCosmWasmClient } from "./signingcosmwasmclient";
import { SigningCosmWasmClient, UploadMeta } from "./signingcosmwasmclient";
import { getRandomizedHackatom, makeRandomAddress, pendingWithoutWasmd } from "./testutils.spec";
import { Coin } from "./types";
@ -50,6 +50,23 @@ describe("SigningCosmWasmClient", () => {
expect(compressedSize).toBeLessThan(wasm.length * 0.5);
expect(codeId).toBeGreaterThanOrEqual(1);
});
it("can set builder and source", async () => {
pendingWithoutWasmd();
const pen = await Secp256k1Pen.fromMnemonic(faucet.mnemonic);
const client = new SigningCosmWasmClient(httpUrl, faucet.address, signBytes => pen.sign(signBytes));
const wasm = getRandomizedHackatom();
const meta: UploadMeta = {
source: "https://crates.io/api/v1/crates/cw-nameservice/0.1.0/download",
builder: "confio/cosmwasm-opt:0.6.2",
};
const { codeId } = await client.upload(wasm, meta);
const codeDetails = await client.getCodeDetails(codeId);
expect(codeDetails.source).toEqual(meta.source);
expect(codeDetails.builder).toEqual(meta.builder);
});
});
describe("instantiate", () => {

View File

@ -51,6 +51,13 @@ const defaultFees: FeeTable = {
},
};
export interface UploadMeta {
/** The source URL */
readonly source?: string;
/** The builder tag */
readonly builder?: string;
}
export interface UploadReceipt {
/** Size of the original wasm code in bytes */
readonly originalSize: number;
@ -96,7 +103,10 @@ export class SigningCosmWasmClient extends CosmWasmClient {
}
/** Uploads code and returns a receipt, including the code ID */
public async upload(wasmCode: Uint8Array, memo = ""): Promise<UploadReceipt> {
public async upload(wasmCode: Uint8Array, meta: UploadMeta = {}, memo = ""): Promise<UploadReceipt> {
const source = meta.source || "";
const builder = meta.builder || "";
const compressed = pako.gzip(wasmCode, { level: 9 });
const storeCodeMsg: MsgStoreCode = {
type: "wasm/store-code",
@ -104,8 +114,8 @@ export class SigningCosmWasmClient extends CosmWasmClient {
sender: this.senderAddress,
// eslint-disable-next-line @typescript-eslint/camelcase
wasm_byte_code: Encoding.toBase64(compressed),
source: "",
builder: "",
source: source,
builder: builder,
},
};
const fee = this.fees.upload;

View File

@ -31,5 +31,6 @@ export {
SigningCallback,
SigningCosmWasmClient,
ExecuteResult,
UploadMeta,
UploadReceipt,
} from "./signingcosmwasmclient";

View File

@ -11,6 +11,12 @@ export interface FeeTable {
readonly exec: StdFee;
readonly send: StdFee;
}
export interface UploadMeta {
/** The source URL */
readonly source?: string;
/** The builder tag */
readonly builder?: string;
}
export interface UploadReceipt {
/** Size of the original wasm code in bytes */
readonly originalSize: number;
@ -40,7 +46,7 @@ export declare class SigningCosmWasmClient extends CosmWasmClient {
getNonce(address?: string): Promise<GetNonceResult>;
getAccount(address?: string): Promise<CosmosSdkAccount | undefined>;
/** Uploads code and returns a receipt, including the code ID */
upload(wasmCode: Uint8Array, memo?: string): Promise<UploadReceipt>;
upload(wasmCode: Uint8Array, meta?: UploadMeta, memo?: string): Promise<UploadReceipt>;
instantiate(
codeId: number,
initMsg: object,

View File

@ -72,7 +72,7 @@ async function main() {
const client = new SigningCosmWasmClient(httpUrl, faucet.address, signBytes => pen.sign(signBytes));
const wasm = fs.readFileSync(__dirname + "/contracts/cw-erc20.wasm");
const uploadReceipt = await client.upload(wasm, "Upload ERC20 contract");
const uploadReceipt = await client.upload(wasm, {}, "Upload ERC20 contract");
console.info(`Upload succeeded. Receipt: ${JSON.stringify(uploadReceipt)}`);
for (const initMsg of [initMsgHash, initMsgIsa, initMsgJade]) {