Skip to content

Commit

Permalink
fix: apply review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
byeongsu-hong committed Mar 7, 2024
1 parent 58abf2c commit 603c17d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 62 deletions.
85 changes: 35 additions & 50 deletions script/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function handleDeploy(_: any, cmd: any) {
]);

saveContext(opts.networkId, ctx);
saveAgentConfig(getNetwork(opts.networkId), ctx);
await saveAgentConfig(getNetwork(opts.networkId), ctx);
}

const deployCore = async (
Expand All @@ -65,14 +65,16 @@ const deployCore = async (
const preload = ctx.deployments.core;
const deployment = preload || {};

deployment.mailbox =
preload?.mailbox ||
(await deployContract(ctx, client, "hpl_mailbox", {
if (preload?.mailbox) {
log(`${preload.mailbox.type} already deployed`);
deployment.mailbox = preload.mailbox;
} else {
deployment.mailbox = await deployContract(ctx, client, "hpl_mailbox", {
hrp,
domain,
owner: client.signer,
}));
if (preload?.mailbox) log(`${deployment.mailbox.type} already deployed`);
});
}

deployment.validator_announce =
preload?.validator_announce ||
Expand All @@ -96,43 +98,18 @@ const deployIsms = async (

const log = (v: string) => console.log("[ism]".green, v);
const preload = ctx.deployments.isms;
const deployment =
preload ||
(await deployIsm(
ctx,
client,
config.deploy.ism instanceof Array
? {
// default ism (multisig)
type: "multisig",
owner: client.signer,
validators: config.deploy.ism
.map((domain) => ({
domain,
addrs: [client.signer_addr],
threshold: 1,
}))
.reduce(
(acc, v) => ({
[v.domain]: { addrs: v.addrs, threshold: v.threshold },
...acc,
}),
{} as Record<string, any>
),
}
: config.deploy.ism
));
if (preload) {
log(`ism ${deployment.type} already deployed`);
log(`ism ${preload.type} already deployed`);
return preload;
}

return deployment;
return deployIsm(ctx, client, config.deploy.ism);
};

const deployHooks = async (
{ networkId }: { networkId: string },
ctx: Context,
client: Client
client: Client,
): Promise<ContextDeployments["hooks"]> => {
if (!config.deploy.hooks) {
throw new Error("Hook deployment config not found");
Expand All @@ -142,27 +119,35 @@ const deployHooks = async (
const preload = ctx.deployments?.hooks;
const deployment = preload || {};

deployment.default =
preload?.default ||
(await deployHook(
if (preload?.default) {
log(`default hook ${preload.default.type} already deployed`);
deployment.default = preload.default;
} else {
if (!config.deploy.hooks.default)
throw Error("Default hook deployment config not found");

deployment.default = await deployHook(
networkId,
ctx,
client,
config.deploy.hooks.default || { type: "mock" }
));
if (preload?.default)
log(`default hook ${deployment.default.type} already deployed`);

deployment.required =
preload?.required ||
(await deployHook(
config.deploy.hooks.default,
);
}

if (preload?.required) {
log(`required hook ${preload.required.type} already deployed`);
deployment.required = preload.required;
} else {
if (!config.deploy.hooks.required)
throw Error("Required hook deployment config not found");

deployment.required = await deployHook(
networkId,
ctx,
client,
config.deploy.hooks.required || { type: "mock" }
));
if (preload?.required)
log(`required hook ${deployment.required.type} already deployed`);
config.deploy.hooks.required,
);
}

return deployment;
};
Expand Down
43 changes: 31 additions & 12 deletions script/commands/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
import { Command } from "commander";
import { Command, Option } from "commander";
import { CodeDetails } from "@cosmjs/cosmwasm-stargate";

import { CONTAINER, Dependencies } from "../shared/ioc";
import { ContractNames } from "../shared/contract";
import { ContextHook, ContextIsm } from "../shared/context";
import { askQuestion, waitTx } from "../shared/utils";
import { contractNames } from "../shared/constants";

export const migrateCmd = new Command("migrate")
.description("Migrate contracts")
.configureHelp({ showGlobalOptions: true })
.option("-c --contracts <contracts...>", "specify contracts to migrate")
.addOption(
new Option(
"-c --contracts <contracts...>",
"specify contract types to migrate",
).choices(contractNames),
)
.action(handleMigrate);

async function handleMigrate(_: any, cmd: any) {
const { contracts } = cmd.optsWithGlobals();
async function handleMigrate(_: object, cmd: Command) {
const { contracts } = cmd.optsWithGlobals() as {
networkId: string;
contracts: ContractNames[];
};

const { ctx, client } = CONTAINER.get(Dependencies);

const flatten = [
const flattenedContracts = [
...flattenIsm(ctx.deployments.isms),
...flattenHook(ctx.deployments.hooks?.default),
...flattenHook(ctx.deployments.hooks?.required),
Expand All @@ -33,48 +42,58 @@ async function handleMigrate(_: any, cmd: any) {

const withContractInfo = await Promise.all(
(contracts
? flatten.filter((v) => contracts.includes(v.type))
: flatten
? flattenedContracts.filter((v) => contracts.includes(v.type))
: flattenedContracts
).map(async (v) => {
const contractInfo = await client.wasm.getContract(v.address);
const codeInfo = await client.wasm.getCodeDetails(contractInfo.codeId);
return { ...v, contractInfo, codeInfo };
})
}),
);

const artifacts = Object.fromEntries(
await Promise.all(
Object.entries(ctx.artifacts).map(async ([contractName, codeId]) => {
const codeInfo = await client.wasm.getCodeDetails(codeId);
return [contractName, codeInfo];
})
)
}),
),
) as Record<ContractNames, CodeDetails>;

const toMigrate = withContractInfo.filter(
(v) =>
v.codeInfo.id !== ctx.artifacts[v.type] &&
v.codeInfo.checksum !== artifacts[v.type].checksum
v.codeInfo.checksum !== artifacts[v.type].checksum,
);

if (toMigrate.length === 0) {
console.log("No changes detected.");
return;
}

for (const migrate of toMigrate) {
console.log(
`${migrate.type} needs migration from`,
`${migrate.codeInfo.id} to ${artifacts[migrate.type].id}`,
`(contract: ${migrate.address})`,
);
}

if (!(await askQuestion("Do you want to proceed? (y/n)"))) {
console.log("Aborted.");
return;
}
console.log("Proceeding to migrate...");

for (const migrate of toMigrate) {
console.log(`Migrating ${migrate.type}...`);

const res = await client.wasm.migrate(
client.signer,
migrate.address,
artifacts[migrate.type].id,
{},
"auto"
"auto",
);
await waitTx(res.transactionHash, client.stargate);
console.log(`${migrate.type} migrated successfully`);
Expand Down

0 comments on commit 603c17d

Please sign in to comment.