Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
feat(cli): add create-authority command
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Jun 16, 2023
1 parent 099dfed commit 4612bd7
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
2 changes: 2 additions & 0 deletions onchain/rollups-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import create_dapp from "./commands/create-dapp";
import create_authority from "./commands/create-authority";

yargs(hideBin(process.argv))
.version()
.command(create_dapp)
.command(create_authority)
.strict()
.alias({ h: "help" }).argv;
173 changes: 173 additions & 0 deletions onchain/rollups-cli/src/commands/create-authority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// Copyright Cartesi Pte. Ltd.

// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
// this file except in compliance with the License. You may obtain a copy of the
// License at http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.

import fs from "fs";
import fse from "fs-extra";
import { Argv, CommandModule } from "yargs";
import { Overrides } from "@ethersproject/contracts";
import { parseUnits } from "@ethersproject/units";
import { AuthorityCreatedEvent } from "@cartesi/rollups/dist/src/types/contracts/consensus/authority/IAuthorityFactory";

import { BlockchainArgs, blockchainBuilder } from "../args";
import { authorityFactory, authorityHistoryPairFactory, inputBox } from "../connect";
import { safeHandler } from "../util";

interface Args extends BlockchainArgs {
authorityOwner?: string;
inputBoxAddress?: string;
salt?: string;
outputFile?: string;
gasPrice?: number;
gasLimit?: number;
}

const builder = (yargs: Argv<{}>): Argv<Args> => {
return blockchainBuilder(yargs, true)
.option("authorityOwner", {
describe: "Authority contract owner",
type: "string",
})
.option("inputBoxAddress", {
describe: "Input box used by the authority",
type: "string",
})
.option("salt", {
describe: "Salt used in deterministic deployment",
type: "string",
})
.option("outputFile", {
describe:
"Output file to write application information in JSON format",
type: "string",
})
.option("gasPrice", {
describe: "Gas price to use for deployment, in GWei",
type: "number",
})
.option("gasLimit", {
describe: "Gas limit to use for deployment",
type: "number",
})
.config();
};

const handler = safeHandler<Args>(async (args) => {
const {
deploymentFile,
mnemonic,
accountIndex,
rpc,
outputFile,
gasPrice,
gasLimit,
} = args;

// connect to provider, use deployment address based on returned chain id of provider
console.log(`connecting to ${rpc}`);

// connect to Authority-History pair factory
const factoryContract = await authorityHistoryPairFactory(
rpc,
mnemonic,
accountIndex,
deploymentFile
);

const address = await factoryContract.signer.getAddress();
console.log(`using account "${address}"`);

const overrides: Overrides = {};
if (gasPrice) {
overrides.gasPrice = parseUnits(gasPrice.toString(), "gwei");
}
if (gasLimit) {
overrides.gasLimit = gasLimit;
}

const authorityOwner = args.authorityOwner || address;

let inputBoxAddress;
if (args.inputBoxAddress) {
inputBoxAddress = args.inputBoxAddress;
} else {
const inputBoxContract = await inputBox(
rpc,
mnemonic,
accountIndex,
deploymentFile
);
inputBoxAddress = inputBoxContract.address;
}

let tx;
if (args.salt) {
tx = await factoryContract[
"newAuthorityHistoryPair(address,address,bytes32)"
](authorityOwner, inputBoxAddress, args.salt, overrides);
} else {
tx = await factoryContract["newAuthorityHistoryPair(address,address)"](
authorityOwner,
inputBoxAddress,
overrides
);
}

console.log(`transaction: ${tx.hash}`);
console.log("waiting for confirmation...");
const receipt = await tx.wait(1);

const authorityFactoryContract = await authorityFactory(
rpc,
mnemonic,
accountIndex,
deploymentFile
);

let authority: string | undefined;
for (const log of receipt.logs) {
if (log.address == authorityFactoryContract.address) {
const event = authorityFactoryContract.interface.parseLog(log);
if (event.name == "AuthorityCreated") {
authority = event.args.authority;
break;
}
}
}

if (authority) {
console.log(`authority: ${authority}`);
if (outputFile) {
console.log(`writing authority address to ${outputFile}`);
fse.outputFileSync(
outputFile,
JSON.stringify(
{
address: authority,
blockHash: receipt.blockHash,
blockNumber: receipt.blockNumber,
transactionHash: receipt.transactionHash,
},
null,
4
)
);
}
}
});

const cmd: CommandModule<{}, Args> = {
command: "create-authority",
describe: "Instantiate rollups authority",
builder,
handler,
};

export default cmd;
23 changes: 23 additions & 0 deletions onchain/rollups-cli/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { ethers } from "ethers";
import {
CartesiDAppFactory,
CartesiDAppFactory__factory,
AuthorityFactory,
AuthorityFactory__factory,
AuthorityHistoryPairFactory,
AuthorityHistoryPairFactory__factory,
InputBox,
InputBox__factory,
} from "@cartesi/rollups";
import goerli from "@cartesi/rollups/export/abi/goerli.json";
import sepolia from "@cartesi/rollups/export/abi/sepolia.json";
Expand Down Expand Up @@ -101,3 +107,20 @@ export const dappFactory = getContractConnector<CartesiDAppFactory>(
"CartesiDAppFactory",
CartesiDAppFactory__factory
);

export const authorityFactory =
getContractConnector<AuthorityFactory>(
"AuthorityFactory",
AuthorityFactory__factory
);

export const authorityHistoryPairFactory =
getContractConnector<AuthorityHistoryPairFactory>(
"AuthorityHistoryPairFactory",
AuthorityHistoryPairFactory__factory
);

export const inputBox = getContractConnector<InputBox>(
"InputBox",
InputBox__factory
);

0 comments on commit 4612bd7

Please sign in to comment.