Skip to content

Commit 70047fe

Browse files
Create contracts deployment scripts
This commit introduces a deployment script based on the [hardhat-deploy](https://github.com/wighawag/hardhat-deploy) plugin. To deploy all contracts on the given network, the following command can be used: ``` yarn deploy --network <network> ``` If contracts haven't been built yet or changes occurred, this task will build the contracts before running the deployment script. Once the deployment terminates, a new `deployments` directory containing all deployment info will be created. It can be directly used by dApps or other client code as it contains deployment details like chain ID, transaction hash, ABI or address for each contract. There is also a possibility to run the deployment with export mode enabled: ``` yarn deploy --network <network> --export ./export.json ``` This command produces a handy summary file containing deployment info for all contracts in one place. However, it doesn't contain the deployment transaction hash making it inappropriate for some use cases relying on this field. The deployment script is divided into multiple sub-scripts placed in the `deploy` directory. It uses the [tags and dependencies](https://github.com/wighawag/hardhat-deploy#deploy-scripts-tags-and-dependencies) feature provided by the `hardhat-deploy` plugin. Such a structure allows running arbitrary parts of the entire deployment by using the tag mechanism. For example, to deploy only the `VendingMachine` contract (with their dependencies), the following command can be used: ``` yarn deploy --network localhost --tags VendingMachine ``` Multiple deployment sub-scripts also improves the readability and allows specifying dependencies between components in an explicit way.
1 parent cf065bd commit 70047fe

14 files changed

+2095
-47
lines changed

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
node_modules/
2+
23
artifacts/
4+
build/
35
cache/
6+
deployments/
7+
export.json
8+
49
typechain/
10+
11+
yarn-error.log
12+
513
.vscode/
14+
15+
.hardhat/*
16+
!.hardhat/networks_TEMPLATE.ts

.hardhat/networks_TEMPLATE.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// This is a template file. The file should be renamed to `networks.ts` and updated
2+
// with valid data. The properties will overwrite the ones defined in hardhat
3+
// project config file if hardhat.config.ts file contains `localNetworksConfig` property
4+
// pointing to this file.
5+
6+
import { LocalNetworksConfig } from "@keep-network/hardhat-local-networks-config"
7+
8+
const config: LocalNetworksConfig = {
9+
networks: {
10+
ropsten: {
11+
url: "url not set",
12+
from: "address not set",
13+
accounts: ["private key not set"],
14+
tags: ["tenderly"],
15+
},
16+
mainnet: {
17+
url: "url not set",
18+
from: "address not set",
19+
accounts: ["private key not set"],
20+
tags: ["tenderly"],
21+
},
22+
},
23+
}
24+
25+
module.exports = config

README.adoc

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
:toc: macro
2+
3+
= Threshold network contracts
4+
5+
This package contains Threshold network contracts.
6+
7+
toc::[]
8+
9+
== Build, test and deploy
10+
11+
Threshold contracts use https://hardhat.org/[*Hardhat*] development environment.
12+
To build and deploy these contracts, please follow the instructions presented
13+
below.
14+
15+
=== Prerequisites
16+
17+
Please make sure you have the following prerequisites installed on your machine:
18+
19+
- https://nodejs.org[Node.js] >14.17.4
20+
- https://yarnpkg.com[Yarn] >1.22.10
21+
22+
=== Build contracts
23+
24+
To build the smart contracts, install node packages first:
25+
```
26+
yarn install
27+
```
28+
Once packages are installed, you can build the smart contracts using:
29+
```
30+
yarn build
31+
```
32+
Compiled contracts will land in the `build` directory.
33+
34+
=== Test contracts
35+
36+
There are multiple test scenarios living in the `test` directory.
37+
You can run them by doing:
38+
```
39+
yarn test
40+
```
41+
42+
=== Deploy contracts
43+
44+
To deploy all contracts on the given network, please run:
45+
```
46+
yarn deploy --network <network>
47+
```
48+
49+
If contracts haven't been built yet or changes occurred, this task will build
50+
the contracts before running the deployment script. This command produces
51+
an `export.json` file containing contract deployment info.

README.md

-1
This file was deleted.

contracts/token/T.sol

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ contract T is ERC20WithPermit, MisfundRecovery, Checkpoints {
5858
abi.encode(
5959
DELEGATION_TYPEHASH,
6060
delegatee,
61-
nonces[signatory]++,
61+
nonce[signatory]++,
6262
deadline
6363
)
6464
)

deploy/00_resolve_keep_token.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { HardhatRuntimeEnvironment, HardhatNetworkConfig } from "hardhat/types"
2+
import { DeployFunction } from "hardhat-deploy/types"
3+
import { BigNumber } from "ethers"
4+
5+
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
6+
const { getNamedAccounts, deployments, helpers } = hre
7+
const { log } = deployments
8+
const { deployer } = await getNamedAccounts()
9+
const { execute } = deployments
10+
11+
const KeepToken = await deployments.getOrNull("KeepToken")
12+
13+
if (KeepToken && helpers.address.isValid(KeepToken.address)) {
14+
log(`using external KeepToken at ${KeepToken.address}`)
15+
} else if (
16+
hre.network.name !== "hardhat" ||
17+
(hre.network.config as HardhatNetworkConfig).forking.enabled
18+
) {
19+
throw new Error("deployed KeepToken contract not found")
20+
} else {
21+
log(`deploying KeepToken stub`)
22+
23+
await deployments.deploy("KeepToken", {
24+
contract: "TestToken",
25+
from: deployer,
26+
log: true,
27+
})
28+
29+
await execute(
30+
"KeepToken",
31+
{ from: deployer },
32+
"mint",
33+
deployer,
34+
BigNumber.from(10).pow(27)
35+
)
36+
}
37+
}
38+
39+
export default func
40+
41+
func.tags = ["KeepToken"]

deploy/01_deploy_t.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { HardhatRuntimeEnvironment } from "hardhat/types"
2+
import { DeployFunction } from "hardhat-deploy/types"
3+
import { BigNumber } from "ethers"
4+
5+
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
6+
const { getNamedAccounts, deployments } = hre
7+
const { deployer } = await getNamedAccounts()
8+
const { execute } = deployments
9+
10+
const T = await deployments.deploy("T", {
11+
from: deployer,
12+
log: true,
13+
})
14+
15+
await execute(
16+
"T",
17+
{ from: deployer },
18+
"mint",
19+
deployer,
20+
BigNumber.from("10000000000000000000000000000")
21+
)
22+
23+
if (hre.network.tags.tenderly) {
24+
await hre.tenderly.verify({
25+
name: "T",
26+
address: T.address,
27+
})
28+
}
29+
}
30+
31+
export default func
32+
33+
func.tags = ["T"]

deploy/02_deploy_vending_machine.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { HardhatRuntimeEnvironment } from "hardhat/types"
2+
import { DeployFunction } from "hardhat-deploy/types"
3+
4+
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
5+
const { getNamedAccounts, deployments } = hre
6+
const { deployer } = await getNamedAccounts()
7+
const { execute } = deployments
8+
9+
const KeepToken = await deployments.get("KeepToken")
10+
const T = await deployments.get("T")
11+
12+
const vendingMachine = await deployments.deploy("VendingMachine", {
13+
from: deployer,
14+
args: [KeepToken.address, T.address, 1000000000, 10000000000],
15+
log: true,
16+
})
17+
18+
if (hre.network.tags.tenderly) {
19+
await hre.tenderly.verify({
20+
name: "VendingMachine",
21+
address: vendingMachine.address,
22+
})
23+
}
24+
}
25+
26+
export default func
27+
28+
func.tags = ["VendingMachine"]
29+
func.dependencies = [
30+
"T",
31+
"KeepToken",
32+
]

deploy/03_transfer_t.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { HardhatRuntimeEnvironment } from "hardhat/types"
2+
import { DeployFunction } from "hardhat-deploy/types"
3+
import { BigNumber } from "ethers"
4+
5+
const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
6+
const { getNamedAccounts, deployments } = hre
7+
const { deployer } = await getNamedAccounts()
8+
const { execute } = deployments
9+
10+
const VendingMachine = await deployments.get("VendingMachine")
11+
12+
await execute(
13+
"T",
14+
{ from: deployer },
15+
"transfer",
16+
VendingMachine.address,
17+
BigNumber.from("4500000000000000000000000000")
18+
)
19+
}
20+
21+
export default func
22+
23+
func.tags = ["TransferT"]
24+
func.dependencies = [
25+
"VendingMachine",
26+
]

0 commit comments

Comments
 (0)