Skip to content

Commit

Permalink
F/ OS-419 export contract source (#370)
Browse files Browse the repository at this point in the history
* update contract versions

* remove typechain

* update test

* update readme

* add script to contracts package

* clean up

* addressing comments

* update commit hashes

* fix test
  • Loading branch information
Rekard0 authored and juliettech13 committed May 12, 2023
1 parent 86ecd0c commit 31796c9
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 79 deletions.
4 changes: 3 additions & 1 deletion packages/contracts-versions/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
index.ts
versions
npm
npm/index.ts
36 changes: 28 additions & 8 deletions packages/contracts-versions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,35 @@ yarn add @aragon/osx-versions

## Usage

Import Active contracts:

```javascript
// import specific version
import {v0_7_0_alpha_active_contracts, v0_7_0_alpha_typechain} from '@aragon/osx-versions';

const typechain = v0_7_0_alpha_typechain;
const idao: v0_7_0_alpha_typechain.IDAO = typechain.IDAO__factory.connect(
ethers.constants.AddressZero,
ethers.providers.getDefaultProvider()
);
// import active contracts from a specific version
import {v1_2_0_active_contracts} from '@aragon/osx-versions';

const mumbaiActiveContracts = v1_2_0_active_contracts.mumbai;
```

Import a specific contract source code from a specific version:

```solidity
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity 0.8.17;
// import legacy contracts from a specific version
import {DAO} from '@aragon/osx-versions/versions/v1_0_1/contracts/core/dao/DAO.sol';
// .....
```

### Generate typechain

To generate TypeChain if needed:

```console
find <path-to>/artifacts/@aragon/osx-versions/versions/ -name '*.json' -type f | grep -v '.dbg.json' | xargs typechain --target=ethers-v5 --out-dir <path-to>/typechain/osx-versions/versions/"
```

## Adding new contract versions
Expand Down
7 changes: 4 additions & 3 deletions packages/contracts-versions/commit_hashes.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"versions": {
"v0_7_0_alpha": "9b912728c315f983af8c4bfd1f8d07021996563f",
"v1_0_0_mainnet_goerli": "c2b9d23a96654e81f22fbf91e6f334ef26a370af",
"v1_0_0_mumbai": "9485d97301611cfc78faa4bd00eb54abb6dd2d5e"
"v1_0_0": "c2b9d23a96654e81f22fbf91e6f334ef26a370af",
"v1_2_0": "9485d97301611cfc78faa4bd00eb54abb6dd2d5e",
"v1_2_1": "a432f25817675038e5891bdd7ec01a3096d8f0db",
"v1_2_2": "1bf17984d1a1b569c3c4b1d86026d87f74f81402"
}
}
73 changes: 37 additions & 36 deletions packages/contracts-versions/create-contract-versions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
const fs = require('fs-extra');
const path = require('path');
const glob = require('glob');
const util = require('util');
const exec = util.promisify(require('child_process').exec);

const monorepoRoot = path.join(__dirname, '../..');
const contractsDir = path.join(monorepoRoot, 'packages/contracts');
const contractVersionsDir = path.join(__dirname, 'build');
const contractVersionsDir = path.join(__dirname, 'versions');
const commitHashes = require('./commit_hashes.json');

async function checkForUncommittedChanges() {
const {stdout} = await exec('git status --porcelain', {cwd: contractsDir});
if (stdout.trim()) {
throw new Error(
'There are uncommitted changes. Please commit or stash them before running this script.'
);
}
}

async function getCurrentBranch() {
const {stdout} = await exec('git branch --show-current', {cwd: contractsDir});
return stdout.trim();
Expand All @@ -22,41 +32,40 @@ async function buildContracts(commit: string) {
}
}

async function copyActiveContracts(commit: string, versionName: string) {
async function copyContracts(versionName: string) {
try {
console.log(`Copying active_contracts.json`);
const srcContracts = path.join(contractsDir, 'src');
const destContracts = path.join(
contractVersionsDir,
versionName,
'contracts'
);

console.log(`Copying contracts from ${srcContracts} to ${destContracts}`);

await fs.copy(srcContracts, destContracts);

const srcActiveContracts = path.join(monorepoRoot, 'active_contracts.json');
const destActiveContracts = path.join(
contractVersionsDir,
versionName,
'active_contracts.json'
);

console.log(`Copying active_contracts.json to ${destActiveContracts}`);

await fs.copy(srcActiveContracts, destActiveContracts);
} catch (error) {
console.error('Error copying active contracts:', error);
}
}

async function generateTypechain(src: string, dest: string) {
try {
// Find all the .json files, excluding the .dbg.json files, in all subdirectories
const {stdout} = await exec(
`find "${src}" -name '*.json' -type f -not -path '*.dbg.json'`
console.error(
'Error copying contracts source code and active contracts:',
error
);
const jsonFiles = stdout
.trim()
.split('\n')
.map((file: string) => `"${file}"`) // Added type annotation here
.join(' ');

// Run typechain for all .json files at once
await exec(`typechain --target ethers-v5 --out-dir "${dest}" ${jsonFiles}`);
} catch (error) {
console.error('Error generating TypeChain output:', error);
}
}

async function createVersions() {
await checkForUncommittedChanges();

const currentBranch = await getCurrentBranch();

for (const version in commitHashes.versions) {
Expand All @@ -67,15 +76,7 @@ async function createVersions() {
`Building contracts for version: ${versionName}, with commit: ${versionCommit}`
);
await buildContracts(versionCommit);
await copyActiveContracts(versionCommit, versionName);

const srcArtifacts = path.join(contractsDir, 'artifacts/src');
const destTypechain = path.join(
contractVersionsDir,
versionName,
'typechain'
);
await generateTypechain(srcArtifacts, destTypechain);
await copyContracts(versionName);
}

// Return to the original branch
Expand All @@ -86,18 +87,18 @@ async function createVersions() {
for (const version in commitHashes.versions) {
const versionName = version;
exports.push(
`export * as ${versionName}_typechain from 'build/${versionName}/typechain';`
);
exports.push(
`import * as ${versionName}_active_contracts from 'build/${versionName}/active_contracts.json';`
`import * as ${versionName}_active_contracts from '../versions/${versionName}/active_contracts.json';`
);
}
exports.push(
`export { ${Object.keys(commitHashes.versions)
.map(versionName => `${versionName}_active_contracts`)
.join(', ')} };`
);
await fs.writeFile(path.join(__dirname, 'index.ts'), exports.join('\n'));

const npmDir = path.join(__dirname, 'npm');
await fs.ensureDir(npmDir);
await fs.writeFile(path.join(npmDir, 'index.ts'), exports.join('\n'));
}

createVersions();
1 change: 1 addition & 0 deletions packages/contracts-versions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@types/node": "^18.16.1",
"jest": "^29.5.0",
"rollup": "^2.70.1",
"rollup-plugin-copy": "^3.4.0",
"rollup-plugin-dts": "^4.2.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
Expand Down
35 changes: 28 additions & 7 deletions packages/contracts-versions/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';
import json from '@rollup/plugin-json';
import copy from 'rollup-plugin-copy';

export default [
{
input: 'index.ts',
plugins: [typescript({project: './tsconfig.json'}), json()],
input: 'npm/index.ts',
plugins: [
typescript({project: './tsconfig.json'}),
json(),
copy({
targets: [
{
src: 'versions/**/active_contracts.json',
dest: 'dist/versions',
rename: (name, extension, fullPath) => {
if (fullPath) {
const matchResult = fullPath.match(/versions\/(.+?)\//);
if (matchResult) {
const versionName = matchResult[1];
return `${versionName}/active_contracts.json`;
}
}
return name;
},
},
],
}),
],
output: [
{
dir: 'dist',
entryFileNames: 'index-esm.js',
format: 'esm',
exports: 'named',
chunkFileNames: 'chunks/[name]-[hash].js',
sourcemap: true,
},
{
dir: 'dist',
entryFileNames: 'index-cjs.js',
format: 'cjs',
exports: 'named',
chunkFileNames: 'chunks/[name]-[hash].js',
sourcemap: true,
},
],
},
{
input: 'index.ts',
input: 'npm/index.ts',
plugins: [dts(), json()],
output: {
dir: 'dist',
entryFileNames: 'bundle.d.ts',
file: 'dist/bundle.d.ts',
format: 'es',
},
},
Expand Down
31 changes: 14 additions & 17 deletions packages/contracts-versions/test/usage.test.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
import {
v1_0_0_mainnet_goerli_active_contracts,
v1_0_0_mainnet_goerli_typechain,
v1_0_0_active_contracts,
v1_2_0_active_contracts,
} from '@aragon/osx-versions';
import {ethers} from 'ethers';

describe('contract-versions', () => {
it('should get typechain for a specific version', async () => {
const typechain = v1_0_0_mainnet_goerli_typechain;
expect(typechain).toBeDefined();
it('should get active contracts for a specific version', async () => {
const activeContracts101 = v1_0_0_active_contracts;
expect(activeContracts101).toBeDefined();

const activeContracts120 = v1_2_0_active_contracts;
expect(activeContracts120).toBeDefined();
});

it('should get active contracts for a specific version', async () => {
const activeContracts = v1_0_0_mainnet_goerli_active_contracts;
expect(activeContracts).toBeDefined();
it('should not have mumbai deployment', async () => {
const activeContracts101: Record<string, any> = v1_0_0_active_contracts;
expect(activeContracts101['mumbai']).toBeUndefined();
});

it('should exported the types properly for a specific version', async () => {
const typechain = v1_0_0_mainnet_goerli_typechain;
const idao: v1_0_0_mainnet_goerli_typechain.IDAO =
typechain.IDAO__factory.connect(
ethers.constants.AddressZero,
ethers.providers.getDefaultProvider()
);
expect(idao).toBeDefined();
it('should have mumbai deployment', async () => {
const activeContracts120 = v1_2_0_active_contracts;
expect(activeContracts120.mumbai).toBeTruthy();
});
});
11 changes: 4 additions & 7 deletions packages/contracts-versions/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
"declaration": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"rootDir": "../../",
"baseUrl": "./",
"paths": {
"@aragon/osx-versions": ["node_modules/@aragon/osx-versions"]
}
"rootDir": "./",
"baseUrl": "./"
},
"include": ["/build"],
"files": ["./index.ts"]
"include": ["./versions", "./npm"],
"files": ["./npm/index.ts"]
}
2 changes: 2 additions & 0 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"test": "hardhat test",
"build": "hardhat compile",
"build:npm": "rollup --config rollup.config.ts",
"build:contracts-versions": "cd ../contracts-versions && yarn build",
"postinstall": "yarn build:contracts-versions",
"coverage": "hardhat coverage --solcoverjs ./.solcover.js",
"flatten": "hardhat flatten",
"analyze": "mythx analyze",
Expand Down
Loading

0 comments on commit 31796c9

Please sign in to comment.