Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#214] improve sample project #279

Merged
merged 15 commits into from
Mar 5, 2024
Merged
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
14 changes: 13 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ dist
*cache
*artifacts

# TypeChain files
*typechain
*typechain-types

# Coverage files
*coverage
coverage.json

# log/debug files
*log

# env file to run the deployment
env

# OpenZeppelin proxy files
.openzeppelin
.openzeppelin

# Config files
deployment-config.*.js
!deployment-config.template.js
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.10.0
1 change: 1 addition & 0 deletions sample/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.10.0
6 changes: 4 additions & 2 deletions sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ cd ..
yarn install && yarn build
```

After that, go to the `sample` folder, then run `yarn install` to install dependencies.
After that, go to the `sample` folder, then run `npm install` to install dependencies.

You can run either `yarn test` to test the project using tests defined in `tests` folder or `yarn deploy --contract [contract_name]` to deploy a smart contract defined in the [contract folder](./contracts) to MultiBaas. At least two accounts are required to successfully complete the tests.
The reason we use `npm` for the sample project is it allows us to use the peer dependencies of `@nomicfoundation/hardhat-toolbox` without having to explicitly add them to the `package.json`.

You can run either `npm run test` to test the project using tests defined in `tests` folder or `npm run deploy:` to execute the target script to deploy a smart contract defined in the [contract folder](./contracts) to MultiBaas. At least two accounts are required to successfully complete the tests.
110 changes: 0 additions & 110 deletions sample/deploy.ts

This file was deleted.

25 changes: 25 additions & 0 deletions sample/deployment-config.template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const deploymentConfig = {
// Private key of the deployer account, beginning with 0x
deployerPrivateKey: '0x0000000000000000000000000000000000000000000000000000000000000000',

// Full URL such as https://abc123.multibaas.com
deploymentEndpoint: 'http://127.0.0.1:8080',

// The chain ID of the blockchain network
// For example: Curvegrid test network = 2017072401, Ethereum Mainnet = 1, BSC Testnet = 97, BSC Mainnet = 56
ethChainID: 1337,

// API key to access MultiBaas web3 endpoint
// Note that the API key MUST be part of the "Web3" group
// Create one on MultiBaas via navigation bar > Admin > API Keys
web3Key: '<API KEY IN WEB3 GROUP>',

// API key to access MultiBaas from deployer
// Note that the API key MUST be part of the "Administrators" group
// Create one on MultiBaas via navigation bar > Admin > API Keys
adminApiKey: '<API KEY IN ADMINISTRATOR GROUP>',
};

module.exports = {
deploymentConfig,
};
111 changes: 24 additions & 87 deletions sample/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,33 @@
// Copyright (c) 2021 Curvegrid Inc.

import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { HardhatUserConfig, task, types } from "hardhat/config";
import "hardhat-multibaas-plugin";
import {
deployGreeterContract,
deployThenLinkGreeterContract,
deployProxiedGreeterContract,
deployMetaCoinContract,
} from "./deploy";

const APIKey = "MB_PLUGIN_API_KEY";
const Mnemonic = "MB_PLUGIN_MNEMONIC";

const apiKey = process.env["MB_API_KEY"] || APIKey;
const mnemonic = process.env["MNEMONIC"] || Mnemonic;

// create a task to deploy smart contracts defined in `./contracts`
task("deploy", "Deploy sample contracts")
.addParam("contract", "The deploy contract's name")
.addOptionalParam(
"signerId",
"The index of the signer in the account list used to deploy contract",
0,
types.int,
)
.setAction(async (args, hre) => {
const contractName = args.contract as string;
let id = args.signer_id;
const signers = await hre.ethers.getSigners();
if (id >= signers.length) {
throw new Error(
`signerId is ${id} but there are only ${signers.length} signers in total`,
);
}
const signer = signers[id] as SignerWithAddress;

if (contractName.toLowerCase() === "metacoin") {
return deployMetaCoinContract(signer, hre);
}
if (contractName.toLowerCase() === "greeter") {
return deployGreeterContract(signer, hre);
}
if (contractName.toLowerCase() === "linked_greeter") {
return deployThenLinkGreeterContract(signer, hre);
}
throw new Error(`unknown contract: ${contractName}`);
});

// create a task to deploy proxied smart contracts defined in `./contracts`
task("deployProxy", "Deploy sample proxied contracts")
.addParam("contract", "The deploy contract's name")
.addOptionalParam(
"signerId",
"The index of the signer in the account list used to deploy contract",
0,
types.int,
)
.setAction(async (args, hre) => {
const contractName = args.contract as string;
let id = args.signer_id;
const signers = await hre.ethers.getSigners();
if (id >= signers.length) {
throw new Error(
`signerId is ${id} but there are only ${signers.length} signers in total`,
);
}
const signer = signers[id] as SignerWithAddress;

if (contractName.toLowerCase() === "proxied_greeter") {
return deployProxiedGreeterContract(signer, hre);
}
throw new Error(`unknown contract: ${contractName}`);
});
import { HardhatUserConfig } from 'hardhat/config';
import '@nomicfoundation/hardhat-toolbox';
import 'hardhat-multibaas-plugin';
import '@openzeppelin/hardhat-upgrades';
import path from 'path';

let deployerPrivateKey = '0x0000000000000000000000000000000000000000000000000000000000000000';
let deploymentEndpoint, ethChainID, web3Key, adminApiKey;

if (process.env.HARDHAT_NETWORK) {
const CONFIG_FILE = path.join(__dirname, `./deployment-config.${process.env.HARDHAT_NETWORK}`);
({
deploymentConfig: { deploymentEndpoint, ethChainID, deployerPrivateKey, web3Key, adminApiKey },
} = require(CONFIG_FILE));
}

const config: HardhatUserConfig = {
defaultNetwork: "development",
networks: {
development: {
url: `http://localhost:9090/web3/${apiKey}`,
chainId: 25846,
accounts: {
mnemonic,
},
url: `${deploymentEndpoint}/web3/${web3Key}`,
chainId: ethChainID,
accounts: [deployerPrivateKey],
},
},
mbConfig: {
apiKey: adminApiKey,
host: deploymentEndpoint,
allowUpdateAddress: ['development'],
allowUpdateContract: ['development'],
},
paths: {
sources: "./contracts",
tests: "./test",
Expand All @@ -94,12 +37,6 @@ const config: HardhatUserConfig = {
mocha: {
timeout: 20000,
},
mbConfig: {
apiKey,
host: "http://localhost:9090",
allowUpdateAddress: ["development"],
allowUpdateContract: ["development"],
},
solidity: "0.8.13",
};

Expand Down
Loading