Skip to content

Commit

Permalink
feat: deploy package
Browse files Browse the repository at this point in the history
  • Loading branch information
adjisb committed Jun 14, 2023
1 parent 406114d commit 789ef58
Show file tree
Hide file tree
Showing 26 changed files with 2,357 additions and 31 deletions.
16 changes: 16 additions & 0 deletions packages/deploy/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# generated docs
generated-markups

# editors
.idea
41 changes: 41 additions & 0 deletions packages/deploy/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const path = require('path');
const tsconfigPath = path.join(__dirname, 'tsconfig.json');
module.exports = {
root: true,
extends: [
'eslint:recommended',
'plugin:mocha/recommended',
'plugin:prettier/recommended',
],
parserOptions: {
ecmaVersion: 2020,
},
plugins: ['mocha'],
env: {
commonjs: true,
node: true,
mocha: true,
},
overrides: [
{
files: ['*.ts'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: [tsconfigPath],
ecmaVersion: 2020,
sourceType: 'module',
},
plugins: ['mocha', '@typescript-eslint'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:mocha/recommended',
'plugin:prettier/recommended',
],
rules: {
'@typescript-eslint/no-misused-promises': 'error',
'@typescript-eslint/no-floating-promises': 'error',
},
},
],
};
16 changes: 16 additions & 0 deletions packages/deploy/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# generated docs
generated-markups

# editors
.idea
16 changes: 16 additions & 0 deletions packages/deploy/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
coverage
coverage.json
typechain
typechain-types

# Hardhat files
cache
artifacts

# generated docs
generated-markups

# editors
.idea
4 changes: 4 additions & 0 deletions packages/deploy/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
singleQuote: true,
bracketSpacing: false,
};
70 changes: 70 additions & 0 deletions packages/deploy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# TSB contracts deploy

This package is used to compile, deploy, test and keep track of the contracts we use in our environments.

## Compilation

The source code of the contracts is imported via `npm` dependencies (except for some mocks used for testing). For
example the instant giveaway contract `SignedMultiGiveaway.sol` is imported from the
package `@sandbox-smart-contracts/giveaway` that is in this same monorepo by adding the dependency to the `package.json`
file.

We use `hardhat` to compile the project and to compile code from other packages we have a specific
task `importedPackages` that adapts the compilation workflow to our needs. The task is configured via the
key `importedPackages` in the `hardhat.config.ts` file. `importedPackages` is a javascript object. The keys are the
package name and the value is the paths to the source code inside the package, for example:

``` solidity
const importedPackages = {
'@sandbox-smart-contracts/giveaway': 'contracts/SignedMultiGiveaway.sol',
};
```

## Deployment

To deploy the contract we use [hardhat-deploy](https://github.com/wighawag/hardhat-deploy) you can check the manual for
the specifics, the only think you must take into account is to use the Fully Qualified Name of the contract when
deploying it (
see: [reading-artifacts](https://hardhat.org/hardhat-runner/docs/advanced/artifacts#reading-artifacts)), for
example: `'@sandbox-smart-contracts/giveaway/contracts/SignedMultiGiveaway.sol:SignedMultiGiveaway'` for the
`SignedMultiGiveaway` contract.

To execute a deployment run: `yarn deploy --network NETWORK --tags SOMETAGS`
where:

- `NETWORK` is the network name like: `mumbai`, `goerli`, `polygon`, `mainet`, etc
- `SOMETAGS` are the tags used to limit which deployment scripts will be executed (
see:[hardhat-deploy](https://github.com/wighawag/hardhat-deploy) configuration)

## Testing

We assume that the imported contracts are well tested in their own package by having enough unit tests and more that 80%
coverage. This repo contains integrations tests and tests that verify the integrity of the system. For example in the
case of the `SignedMultiGiveaway` contract we check the roles and the users assigned to them are correctly configured.

The tests can be run in different contexts:

- on hardhat:
- run all the deployment scripts just to test them: `yarn deploy`
- run the integration tests using `hardhat-deploy` fixtures: `yarn test`
- on a real network `testnet` or `mainnet`, run the integration tests over contracts already deployed and fail if
something is wrong, for example: `yarn test --network mumbai`
- to run on a fork of some network the following environment variables must be set:
- HARDHAT_DEPLOY_FIXTURE=true
- HARDHAT_FORK=mumbai
- HARDHAT_DEPLOY_ACCOUNTS_NETWORK=mumbai

and then `yarn test` can be executed for integration tests or `yarn deploy`
with or without tags to test just the deployment scripts.

To simplify the execution of the integration tests over forks the following targets are added to the package.json
file: `fork:mainnet`, `fork:polygon`, `fork:goerli` and `fork:mumbai`.

# Adding contract from a new package

- add the dependency to the `package.json` file as usual (`yarn add`).
- add an entry to the `importedPackages` in the `hardhat.config.ts` file so the code is compiled.
- add the deployment scripts to the `deploy` directory. Check
the [hardhat-deploy](https://github.com/wighawag/hardhat-deploy) docs on how to write deployments scripts.
- In the deployment scripts use the Fully Qualified Name of the contract,
see: [reading-artifacts](https://hardhat.org/hardhat-runner/docs/advanced/artifacts#reading-artifacts)
26 changes: 26 additions & 0 deletions packages/deploy/contracts/mocks/TrustedForwarderMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//SPDX-License-Identifier: MIT
// solhint-disable-next-line compiler-version
pragma solidity 0.8.2;

contract TrustedForwarderMock {

struct ForwardRequest {
address from;
address to;
uint256 value;
uint256 gas;
uint256 nonce;
bytes data;
}

function execute(ForwardRequest calldata req, bytes calldata)
public
payable
returns (bool, bytes memory)
{
(bool success, bytes memory returndata) =
req.to.call{gas : req.gas, value : req.value}(abi.encodePacked(req.data, req.from));
assert(gasleft() > req.gas / 63);
return (success, returndata);
}
}
30 changes: 30 additions & 0 deletions packages/deploy/deploy/100_claim/50_multi_claim.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {DeployFunction} from 'hardhat-deploy/types';
import {HardhatRuntimeEnvironment} from 'hardhat/types';

const func: DeployFunction = async function (
hre: HardhatRuntimeEnvironment
): Promise<void> {
const {deployments, getNamedAccounts} = hre;
const {deployer, upgradeAdmin, sandAdmin} = await getNamedAccounts();
const TRUSTED_FORWARDER = await deployments.get('TRUSTED_FORWARDER_V2');
await deployments.deploy('MultiGiveawayV1', {
from: deployer,
contract:
'@sandbox-smart-contracts/giveaway/contracts/SignedMultiGiveaway.sol:SignedMultiGiveaway',
log: true,
skipIfAlreadyDeployed: true,
proxy: {
owner: upgradeAdmin,
proxyContract: 'OptimizedTransparentProxy',
execute: {
methodName: 'initialize',
args: [TRUSTED_FORWARDER.address, sandAdmin],
},
upgradeIndex: 0,
},
});
};

export default func;
func.tags = ['MultiGiveawayV1', 'MultiGiveawayV1_deploy', 'L2'];
func.dependencies = ['TRUSTED_FORWARDER_V2'];
33 changes: 33 additions & 0 deletions packages/deploy/deploy/100_claim/60_multi_claim_role_setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {DeployFunction} from 'hardhat-deploy/types';
import {HardhatRuntimeEnvironment} from 'hardhat/types';

const func: DeployFunction = async function (
hre: HardhatRuntimeEnvironment
): Promise<void> {
const {deployments, getNamedAccounts} = hre;
const {execute, read, catchUnknownSigner} = deployments;
const {sandAdmin, backendCashbackWallet} = await getNamedAccounts();
const signerRole = await read('MultiGiveawayV1', 'SIGNER_ROLE');
if (
!(await read(
'MultiGiveawayV1',
'hasRole',
signerRole,
backendCashbackWallet
))
) {
await catchUnknownSigner(
execute(
'MultiGiveawayV1',
{from: sandAdmin, log: true},
'grantRole',
signerRole,
backendCashbackWallet
)
);
}
};

export default func;
func.tags = ['MultiGiveawayV1', 'MultiGiveawayV1_role_setup', 'L2'];
func.dependencies = ['MultiGiveawayV1_deploy'];
16 changes: 16 additions & 0 deletions packages/deploy/deploy_mocks/100_trusted_forwarder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {HardhatRuntimeEnvironment} from 'hardhat/types';
import {DeployFunction} from 'hardhat-deploy/types';

const func: DeployFunction = async function (hre: HardhatRuntimeEnvironment) {
const {deployments, getNamedAccounts} = hre;
const {deploy} = deployments;

const {deployer} = await getNamedAccounts();
await deploy('TRUSTED_FORWARDER_V2', {
from: deployer,
contract: 'TrustedForwarderMock',
log: true,
});
};
export default func;
func.tags = ['TRUSTED_FORWARDER', 'TRUSTED_FORWARDER_V2'];
1 change: 1 addition & 0 deletions packages/deploy/deployments/goerli/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5
1 change: 1 addition & 0 deletions packages/deploy/deployments/mainnet/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions packages/deploy/deployments/mumbai/.chainId
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
80001
Loading

1 comment on commit 789ef58

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage for this commit

100.00%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
packages/giveaway/contracts
   ERC2771Handler.sol100%100%100%100%
   SignedMultiGiveaway.sol100%100%100%100%
   SignedMultiGiveawayBase.sol100%100%100%100%
packages/giveaway/contracts/test
   FakeMintableERC1155.sol100%100%100%100%
   FakeMintableERC20.sol100%100%100%100%
   FakeMintableERC721.sol100%100%100%100%

Please sign in to comment.