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

Remove deployment of Authority and History #72

Closed
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
6 changes: 4 additions & 2 deletions onchain/rollups-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@

import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import create from "./commands/create";
import create_dapp from "./commands/create-dapp";
import create_authority from "./commands/create-authority";

yargs(hideBin(process.argv))
.version()
.command(create)
.command(create_dapp)
.command(create_authority)
.strict()
.alias({ h: "help" }).argv;
156 changes: 156 additions & 0 deletions onchain/rollups-cli/src/commands/create-authority.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// 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 } from "../connect";
import { safeHandler } from "../util";

interface Args extends BlockchainArgs {
authorityOwner?: 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("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 tx;
if (args.salt) {
tx = await factoryContract["newAuthorityHistoryPair(address,bytes32)"](
authorityOwner,
args.salt,
overrides
);
} else {
tx = await factoryContract["newAuthorityHistoryPair(address)"](
authorityOwner,
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;
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import { parseUnits } from "@ethersproject/units";
import { ApplicationCreatedEvent } from "@cartesi/rollups/dist/src/types/contracts/dapp/ICartesiDAppFactory";

import { BlockchainArgs, blockchainBuilder } from "../args";
import { authority, factory } from "../connect";
import { dappFactory } from "../connect";
import { safeHandler } from "../util";

interface Args extends BlockchainArgs {
dappOwner?: string;
consensusAddress?: string;
consensusAddress: string;
templateHash?: string;
templateHashFile?: string;
salt?: string;
Expand Down Expand Up @@ -52,6 +52,7 @@ const builder = (yargs: Argv<{}>): Argv<Args> => {
.option("consensusAddress", {
describe: "Consensus contract address",
type: "string",
demandOption: true,
})
.option("templateHash", {
describe: "Cartesi Machine template hash",
Expand Down Expand Up @@ -83,6 +84,7 @@ const builder = (yargs: Argv<{}>): Argv<Args> => {

const handler = safeHandler<Args>(async (args) => {
const {
consensusAddress,
deploymentFile,
mnemonic,
accountIndex,
Expand All @@ -95,16 +97,8 @@ const handler = safeHandler<Args>(async (args) => {
// connect to provider, use deployment address based on returned chain id of provider
console.log(`connecting to ${rpc}`);

// connect to authority
const authorityContract = await authority(
rpc,
mnemonic,
accountIndex,
deploymentFile
);

// connect to factory
const factoryContract = await factory(
// connect to the DApp factory
const factoryContract = await dappFactory(
rpc,
mnemonic,
accountIndex,
Expand All @@ -130,7 +124,6 @@ const handler = safeHandler<Args>(async (args) => {
overrides.gasLimit = gasLimit;
}

const consensusAddress = args.consensusAddress || authorityContract.address;
const dappOwner = args.dappOwner || address;

let tx;
Expand All @@ -151,7 +144,7 @@ const handler = safeHandler<Args>(async (args) => {
console.log("waiting for confirmation...");
const receipt = await tx.wait(1);

// find new application event in receipt
// find ApplicationCreated event in receipt
const event = receipt.events?.find(
(e) => e.event === "ApplicationCreated"
) as ApplicationCreatedEvent | undefined;
Expand Down Expand Up @@ -179,7 +172,7 @@ const handler = safeHandler<Args>(async (args) => {
});

const cmd: CommandModule<{}, Args> = {
command: "create",
command: "create-dapp",
describe: "Instantiate rollups application",
builder,
handler,
Expand Down
24 changes: 16 additions & 8 deletions onchain/rollups-cli/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import fs from "fs";
import { JsonRpcProvider } from "@ethersproject/providers";
import { ethers } from "ethers";
import {
Authority,
Authority__factory,
CartesiDAppFactory,
CartesiDAppFactory__factory,
AuthorityFactory,
AuthorityFactory__factory,
AuthorityHistoryPairFactory,
AuthorityHistoryPairFactory__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 @@ -99,12 +101,18 @@ function getContractConnector<T>(contractName: string, contractFactory: any) {
};
}

export const authority = getContractConnector<Authority>(
"Authority",
Authority__factory
);

export const factory = getContractConnector<CartesiDAppFactory>(
export const dappFactory = getContractConnector<CartesiDAppFactory>(
"CartesiDAppFactory",
CartesiDAppFactory__factory
);

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

export const authorityHistoryPairFactory =
getContractConnector<AuthorityHistoryPairFactory>(
"AuthorityHistoryPairFactory",
AuthorityHistoryPairFactory__factory
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "address",
"name": "authorityOwner",
"type": "address"
},
{
"indexed": false,
"internalType": "contract Authority",
"name": "authority",
"type": "address"
}
],
"name": "AuthorityCreated",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "_authorityOwner",
"type": "address"
},
{
"internalType": "bytes32",
"name": "_salt",
"type": "bytes32"
}
],
"name": "calculateAuthorityAddress",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_authorityOwner",
"type": "address"
},
{
"internalType": "bytes32",
"name": "_salt",
"type": "bytes32"
}
],
"name": "newAuthority",
"outputs": [
{
"internalType": "contract Authority",
"name": "",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_authorityOwner",
"type": "address"
}
],
"name": "newAuthority",
"outputs": [
{
"internalType": "contract Authority",
"name": "",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "function"
}
]
Loading