Skip to content
This repository has been archived by the owner on Aug 6, 2021. It is now read-only.

Commit

Permalink
Merge pull request #51 from atpar/APm-45-rewrite-deploy
Browse files Browse the repository at this point in the history
APm-45-rewrite-deploy
  • Loading branch information
jo-es committed Aug 31, 2020
2 parents eb25291 + 078ae11 commit b74c169
Show file tree
Hide file tree
Showing 101 changed files with 52,600 additions and 4,088 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"coveralls": "^3.0.9",
"ganache-cli": "^6.8.1",
"lcov-result-merger": "^3.1.0",
"lerna": "^3.13.0",
"truffle": "^5.1.8"
"lerna": "^3.13.0"
},
"scripts": {
"bootstrap": "lerna bootstrap",
Expand Down
13 changes: 10 additions & 3 deletions packages/ap-contracts/.solcover.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ module.exports = {
'token/FDT/IFundsDistributionToken.sol',
'token/FDT/SafeMathInt.sol',
'token/FDT/SafeMathUint.sol',
'Migrations.sol'
]
};
],
providerOptions: {
"default_balance_ether": 5000
},
mocha: {
timeout: 90000,
grep: "@skip-on-coverage", // Find everything with this tag
invert: true // Run the grep's inverse set.
}
};
2 changes: 1 addition & 1 deletion packages/ap-contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ yarn test

### Deploy to local ganache chain
```sh
yarn migrate:ap-chain
yarn setup-ap-chain
```
13 changes: 11 additions & 2 deletions packages/ap-contracts/buidler.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { TASK_COMPILE_GET_COMPILER_INPUT } = require('@nomiclabs/buidler/builtin-tasks/task-names');


usePlugin('@nomiclabs/buidler-truffle5');
usePlugin('@nomiclabs/buidler-web3');
usePlugin('solidity-coverage');
usePlugin("buidler-deploy");
// usePlugin('buidler-gas-reporter');

task(TASK_COMPILE_GET_COMPILER_INPUT).setAction(async (_, __, runSuper) => {
Expand All @@ -22,7 +22,8 @@ function getMnemonic() {

module.exports = {
paths: {
artifacts: './test/artifacts'
artifacts: './build/contracts',
deployments: 'deployments',
},
defaultNetwork: 'buidlerevm',
networks: {
Expand Down Expand Up @@ -73,6 +74,14 @@ module.exports = {
}
}
},
namedAccounts: {
deployer: {
default: 0, // the first account
},
admin: {
default: 1,
}
},
solc: {
version: '0.6.11',
optimizer: {
Expand Down
13 changes: 13 additions & 0 deletions packages/ap-contracts/deploy/0-include-tags-to-deploy-ap-chain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Deploy contracts on the ap-chain
// NOTE: (unlike "migrate") it updates neither deployments nor artifacts
module.exports = async () => await Promise.resolve();
module.exports.tags = ["deploy-ap-chain"];
module.exports.dependencies = [
"_env",
"_package",
"_deployment",
"_init",
];

// run on the ap-chain only
module.exports.skip = ({ usrNs: { chainId }}) => Promise.resolve(chainId !== '1994');
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Generate artifacts
module.exports = async () => await Promise.resolve();
module.exports.tags = ["artifacts"];
module.exports.dependencies = [
"_env",
"_package",
"_deployment", // deployments needed just to get metadata
"_artifacts", // generate '../artifacts/*.min.json' files
];

// run on the buidlerEVM network only
module.exports.skip = ({ usrNs: { chainId }}) => Promise.resolve(`${chainId}` !== '31337' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Make node.js terminate on unhandled rejections, and run "migrate"
module.exports = async () => await Promise.resolve();
module.exports.tags = ["migrate-terminate"];
module.exports.dependencies = [
"_terminate-on-rejections",
"migrate"
];
12 changes: 12 additions & 0 deletions packages/ap-contracts/deploy/0-include-tags-to-migrate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Deploy contracts and update deployments.js
module.exports = async () => await Promise.resolve();
module.exports.tags = ["migrate"];
module.exports.dependencies = [
"_env", // extend buidler runtime environment
"_package", // define contracts to deploy
"_balance", // assert deployer balance
"_deployment", // deploy contracts
"_init", // init contracts
"_export", // update '../deployments.js'
"_verification", // do contract verification
];
12 changes: 12 additions & 0 deletions packages/ap-contracts/deploy/0-include-tags-to-setup-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// declares tags (dependencies) only
module.exports = async () => await Promise.resolve();
module.exports.tags = ["u-tests", "e2e-tests"]
module.exports.dependencies = [
"_env",
"_env-tests",
"_package",
"_deployment",
"_init",
"_init-tests",
"_instances-tests",
];
32 changes: 32 additions & 0 deletions packages/ap-contracts/deploy/1-extend-buidler-env-for-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = extendBuidlerEnvForTests;
module.exports.tags = ["_env-tests"];
module.exports.dependencies = ["_env"];

/**
* @typedef {import('./1-extend-buidler-env').ExtendedBRE}
* @typedef Instances {{name: string, instance: any}} - web3 Contract objects, name: contract name + 'Instance'
* @typedef {ExtendedBRE} ExtendedTestBRE
* @property {Instances} instances
*/

/** @param {ExtendedBRE} buidlerRuntime */
async function extendBuidlerEnvForTests(buidlerRuntime) {

const { deployments: { log }, usrNs, web3 } = buidlerRuntime;
const { accounts, roles } = usrNs;

if (!usrNs.instances) usrNs.instances = {};

roles.defaultActor = checkAddress(accounts[2]);
log(`roles: ${JSON.stringify(roles, null, 2)}`);

// shall be async
return Promise.resolve();

function checkAddress(address) {
if (!web3.utils.isAddress(address)) {
throw new Error(`invalid address ${address}`);
}
return address;
}
}
57 changes: 57 additions & 0 deletions packages/ap-contracts/deploy/1-extend-buidler-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module.exports = extendBuidlerEnv;
module.exports.tags = ["_env"];

/**
* @typedef {Object} BuidlerRuntimeEnvironment (https://github.com/wighawag/buidler-deploy#environment-extensions)
*
* @typedef {{name: string: address: string}} Roles
* @typedef UserNamespace
* @property {String} chainId
* @property {String[]} accounts
* @property {Roles} roles
* @property {import('./2-define-package').Package} package
* @property {{name: string, helper:any}} helpers - misc helper functions
*
* @typedef {BuidlerRuntimeEnvironment} ExtendedBRE - extended Builder Runtime Environment
* @property {UserNamespace} usrNs - extension
*/

/** @param buidlerRuntime {ExtendedBRE} */
async function extendBuidlerEnv(buidlerRuntime) {
if (typeof buidlerRuntime.usrNs !== 'undefined') throw new Error("unexpected Buidler Runtime Environment");

const { deployments: { log }, getNamedAccounts, getChainId, web3 } = buidlerRuntime;

const { admin, deployer } = await getNamedAccounts();
const id = `${await getChainId()}`;

// ganache hardcoded to return 1337 on 'eth_chainId' RPC request that buidler uses
const chainId = ( id === '1337' ) ? `${await web3.eth.net.getId()}` : id;

const accounts = await web3.eth.getAccounts();
const roles = {
deployer: deployer || accounts[0],
admin: admin || accounts[1] || accounts[0],
};
Object.keys(roles).forEach(
(key) => {
if (!web3.utils.isAddress(roles[key])) {
throw new Error(`invalid address for role: ${key}`);
}
}
);

if (!web3.eth.Contract.defaultAccount) {
web3.eth.Contract.defaultAccount = deployer;
}

buidlerRuntime.usrNs = {
chainId,
accounts,
roles,
package: {},
helpers: {},
};

log(`Buidler Runtime Environment extended, chainId: ${chainId}`);
}
140 changes: 140 additions & 0 deletions packages/ap-contracts/deploy/2-define-package.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
module.exports = definePackage;
module.exports.tags = ["_package"];
module.exports.dependencies = ["_env"];

/**
* @typedef {Object} DeployOptions - https://github.com/wighawag/buidler-deploy#deploy-function
* @typedef {import('./1-extend-buidler-env').ExtendedBRE}
*
* @typedef {Object} ContractsListItem
* @property {string} name - contract name
* @property {DeployOptions} [options]
* @property {function (ExtendedBRE): DeployOptions} [getOptions] - getter for options
* @property {boolean} [exportable] - `true` to export into `deployments.json` and `artifacts/` (default - `true`)
* @property {boolean} [deployable] - if `false`, neither deploy nor export to `deployments.json` (default - `true`)
*
* @typedef {Object} Package
* @property {ContractsListItem[]} contracts
* @property {DeployOptions} defaultDeployOptions
*/

/** @param {ExtendedBRE} buidlerRuntime */
async function definePackage(buidlerRuntime) {

const { deployments: { log }, usrNs } = buidlerRuntime;
if ( typeof usrNs !== 'object' || typeof usrNs.package !== 'object' ) {
throw new Error("unexpected Buidler Runtime Environment");
}

usrNs.package.contracts = [

// ACTUS-Solidity
{ name: "ANNEngine" },
{ name: "CECEngine" },
{ name: "CEGEngine" },
{ name: "CERTFEngine" },
{ name: "PAMEngine" },

// Asset Registry
{ name: "ANNEncoder", exportable: false },
{ name: "CECEncoder", exportable: false },
{ name: "CEGEncoder", exportable: false },
{ name: "CERTFEncoder", exportable: false },
{ name: "PAMEncoder", exportable: false },
{
name: "ANNRegistry",
options: { libraries: { ANNEncoder: "{{ANNEncoder.address}}" }},
},
{
name: "CECRegistry",
options: { libraries: { CECEncoder: "{{CECEncoder.address}}" }},
},
{
name: "CEGRegistry",
options: { libraries: { CEGEncoder: "{{CEGEncoder.address}}" }},
},
{
name: "CERTFRegistry",
options: { libraries: { CERTFEncoder: "{{CERTFEncoder.address}}" }},
},
{
name: "PAMRegistry",
options: { libraries: { PAMEncoder: "{{PAMEncoder.address}}" }},
},

// Data Registry
{ name: "DataRegistry" },

// Asset Actor
{
name: "ANNActor",
options: { args: [ "{{ANNRegistry.address}}", "{{DataRegistry.address}}" ]},
},
{
name: "CECActor",
options: { args: [ "{{CECRegistry.address}}", "{{DataRegistry.address}}" ]},
},
{
name: "CEGActor",
options: { args: [ "{{CEGRegistry.address}}", "{{DataRegistry.address}}" ]},
},
{
name: "CERTFActor",
options: { args: [ "{{CERTFRegistry.address}}", "{{DataRegistry.address}}" ]},
},
{
name: "PAMActor",
options: { args: [ "{{PAMRegistry.address}}", "{{DataRegistry.address}}" ]},
},

// Custodian
{
name: "Custodian",
options: { args: [ "{{CECActor.address}}", "{{CECRegistry.address}}" ]},
},

// FDT
{ name: "ProxySafeVanillaFDT", exportable: false },
{ name: "ProxySafeSimpleRestrictedFDT", exportable: false },
{
name: "FDTFactory",
options: { libraries: {
VanillaFDTLogic: "{{ProxySafeVanillaFDT.address}}",
SimpleRestrictedFDTLogic: "{{ProxySafeSimpleRestrictedFDT.address}}",
}},
},

// ICT
{ name: "ProxySafeICT", exportable: false },
{
name: "ICTFactory",
exportable: false,
options: { libraries: { ICTLogic: "{{ProxySafeICT.address}}" }},
},

// DvPSettlement
{ name: "DvPSettlement" },

// settlement token (for templates on testnets)
{ name: "SettlementToken", exportable: false },

// export artifacts only (do not deploy)
{ name: "BaseActor", deployable: false },
{ name: "BaseRegistry", deployable: false },
{ name: "ERC20", deployable: false },
{ name: "ERC1404", deployable: false },
{ name: "VanillaFDT", deployable: false }

];

usrNs.package.defaultDeployOptions = {
from: usrNs.roles.deployer,
gas: 4500000,
log: true,
};

log(`${usrNs.package.contracts.length} contracts defined`);

// shall be async
return Promise.resolve();
}
22 changes: 22 additions & 0 deletions packages/ap-contracts/deploy/3-assert-deployer-balance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = assertDeployerBalance;
module.exports.tags = ["_balance"];
module.exports.dependencies = ["_env"];

/**
* @typedef {import('./1-extend-buidler-env').ExtendedBRE}
* @param {ExtendedBRE} buidlerRuntime
*/
async function assertDeployerBalance(buidlerRuntime) {

const { deployments: { log }, usrNs: { roles: { deployer } }, web3 } = buidlerRuntime;

if ( !web3.utils.isAddress(deployer) ) {
throw new Error("unexpected Buidler Runtime Environment");
}
const balance = parseFloat(web3.utils.fromWei(await web3.eth.getBalance(deployer), 'ether'));
if( balance < 0.5 ) {
log(`ATTENTION!!! low deployer balance: ${balance} ether`);
} else {
log(`deployer balance: ${balance} ether`);
}
}

0 comments on commit b74c169

Please sign in to comment.