Test virus contract

This commit is contained in:
Simon Warta 2023-01-30 16:20:16 +01:00
parent 33271bc51c
commit 6481106ed7
7 changed files with 129 additions and 8 deletions

116
packages/cli/examples/virus.ts Executable file
View File

@ -0,0 +1,116 @@
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { calculateFee, GasPrice } from "@cosmjs/stargate";
import * as fs from "fs";
const rpcEndpoint = "http://localhost:26659";
// Example user from scripts/wasmd/README.md
const alice = {
mnemonic: "enlist hip relief stomach skate base shallow young switch frequent cry park",
address0: "wasm14qemq0vw6y3gc3u3e0aty2e764u4gs5lndxgyk",
address1: "wasm1hhg2rlu9jscacku2wwckws7932qqqu8xm5ca8y",
address2: "wasm1xv9tklw7d82sezh9haa573wufgy59vmwnxhnsl",
address3: "wasm17yg9mssjenmc3jkqth6ulcwj9cxujrxxg9nmzk",
address4: "wasm1f7j7ryulwjfe9ljplvhtcaxa6wqgula3nh873j",
};
function traverseMap(map: Map<string, string>): string {
const keys = Array.from(map.keys()).sort();
let out = `Map {\n`;
for (const key of keys) {
const value = map.get(key)!;
out += ` ${key} => ${value},\n`;
}
out += `}`;
return out;
}
export function ellideMiddle(str: string, maxOutLen: number): string {
if (str.length <= maxOutLen) {
return str;
}
const ellide = "...";
const frontLen = Math.ceil((maxOutLen - ellide.length) / 2);
const tailLen = Math.floor((maxOutLen - ellide.length) / 2);
return str.slice(0, frontLen) + ellide + str.slice(str.length - tailLen, str.length);
}
function shortAddr(address: string): string {
return ellideMiddle(address, 25)
}
function mermaid(map: Map<string, string>): string {
let out = "```mermaid\n";
out += `graph LR;\n`;
for (const [child, parent] of map.entries()) {
out += ` ${shortAddr(parent)} --> ${shortAddr(child)};\n`;
}
out += "```";
return out;
}
async function main(wasmPath: string) {
const gasPrice = GasPrice.fromString("0.025ucosm");
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: "wasm" });
const client = await SigningCosmWasmClient.connectWithSigner(rpcEndpoint, wallet, { gasPrice });
// Upload contract
const wasm = fs.readFileSync(wasmPath);
const uploadReceipt = await client.upload(alice.address0, wasm, "auto", "Upload virus contract");
console.info("Upload succeeded. Receipt:", uploadReceipt);
const codeId = uploadReceipt.codeId;
console.info("Code ID:", codeId);
// Instantiate
const { contractAddress } = await client.instantiate(
alice.address0,
uploadReceipt.codeId,
{},
"My instance",
"auto",
{ memo: `Create a virus instance` },
);
console.info(`Contract instantiated at: ${contractAddress}`);
// Execute contract
const spreadMsg = {
spread: {
parent_path: "",
levels: 3,
},
};
const result = await client.execute(alice.address0, contractAddress, spreadMsg, "auto");
// console.info(`All events:`, result.events);
const predictedAddresses = new Map<string, string>();
const parents = new Map<string, string>();
const wasmEvents = result.events.filter((event) => event.type === "wasm");
for (const attrs of wasmEvents.map((event) => event.attributes)) {
const parent = attrs.find((attr) => attr.key == "_contract_address")!.value;
const path0 = attrs.find((attr) => attr.key == "path0")!.value;
const path1 = attrs.find((attr) => attr.key == "path1")!.value;
const predicted0 = attrs.find((attr) => attr.key == "predicted_address0")!.value;
const predicted1 = attrs.find((attr) => attr.key == "predicted_address1")!.value;
predictedAddresses.set(path0, predicted0);
predictedAddresses.set(path1, predicted1);
parents.set(predicted0, parent);
parents.set(predicted1, parent);
}
console.info(`Predicted addresses:`, traverseMap(predictedAddresses));
console.info(`Graph for GitHub:\n`, mermaid(parents));
const actualAddresses = result.events
.filter((event) => event.type === "instantiate")
.flatMap((event) => event.attributes)
.filter((attr) => attr.key === "_contract_address")
.map((attr) => attr.value);
console.info(`Actual addresses:`, actualAddresses);
}
const repoRoot = process.cwd() + "/../.."; // This assumes you are in `packages/cli`
const wasm = `${repoRoot}/scripts/wasmd/contracts/virus.wasm`;
await main(wasm);
console.info("The show is over.");

View File

@ -1,2 +1,3 @@
13a1fc994cc6d1c81b746ee0c0ff6f90043875e0bf1d9be6b7d779fc978dc2a5 hackatom.wasm
1da6c16de2cbaf7ad8cbb66f0925ba33f5c278cb2491762d04658c1480ea229b ibc_reflect.wasm
dd19142b0a113728b80ed9f67503b2ebb3a1dc676961cb1abc096d746e9309a3 hackatom.wasm
02e71ef6579f631436c08d6df70cb794fee0662aa72e9371a0be631748dbd106 ibc_reflect.wasm
b658ecac74ea925c6185bf414a4df264ef4a7c12d677a751d3961faa6b43e9cf virus.wasm

View File

@ -2,9 +2,10 @@
# This must get from 2-3 different repos, fix the versions here:
COSMWASM_VERSION="v1.0.0-beta"
COSMWASM_VERSION="v1.2.1"
curl -sS -L -O "https://github.com/CosmWasm/cosmwasm/releases/download/${COSMWASM_VERSION}/hackatom.wasm"
curl -sS -L -O "https://github.com/CosmWasm/cosmwasm/releases/download/${COSMWASM_VERSION}/ibc_reflect.wasm"
curl -sS -L -O "https://github.com/CosmWasm/cosmwasm/releases/download/${COSMWASM_VERSION}/virus.wasm"
sha256sum *.wasm >checksums.sha256

BIN
scripts/wasmd/contracts/hackatom.wasm (Stored with Git LFS)

Binary file not shown.

BIN
scripts/wasmd/contracts/ibc_reflect.wasm (Stored with Git LFS)

Binary file not shown.

BIN
scripts/wasmd/contracts/virus.wasm (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -1,5 +1,5 @@
# Choose from https://hub.docker.com/r/cosmwasm/wasmd/tags
REPOSITORY="cosmwasm/wasmd"
VERSION="v0.27.0"
VERSION="v0.31.0-rc0"
CONTAINER_NAME="wasmd"