Skip to content

Commit

Permalink
F/ OS-380 npm package versions (#361)
Browse files Browse the repository at this point in the history
* prepare package

* remember the branch

* only artifact src

* correct the paths

* correct root path

* update hashes

* add logs

* only copy typechain

* generate typechain

* setup rollup

* update npm packages

* update rollup index

* add esm cjs

* add readme

* remove gitIgnore

* update typechain

* update typescript

* convert script to typescript

* change commit hash json

* rename types to typechain

* add test

* add json to rollup and export types

* update usage test

* generate index.ts dynamically

* update readme

* add empty line

* fix index.ts

* ci/cd for tests

* fix-typo

* remove check formatting

* add checkout ref

* update contracts flow

* update contracts workflow

* update contracts prettier config

* remove directories

* remove more dir
  • Loading branch information
Rekard0 authored May 2, 2023
1 parent ba250d1 commit 5e7a19e
Show file tree
Hide file tree
Showing 13 changed files with 2,082 additions and 23 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/contract-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,22 @@ jobs:
run:
working-directory: ${{env.working-directory}}
steps:
- uses: actions/checkout@v2
- name: Checkout repository
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
fetch-depth: 0
- name: Install node
uses: actions/setup-node@v3
with:
cache: 'yarn'
node-version: 16
- name: Install dependencies
run: cd ../../ && yarn install --pure-lockfile
- name: Compile contracts-versions
run: cd ../contracts-versions && yarn build
- name: Remove directories
run: rm -rf artifact cache deployments typechain
- name: Check formatting
run: yarn run formatting:check
- name: Compile contracts
Expand Down
34 changes: 34 additions & 0 deletions .github/workflows/contracts-versions-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Contracts-versions Tests
on:
push:
paths:
- 'packages/contracts-versions/**'
- '.github/workflows/contract-*.yml'

env:
working-directory: packages/contracts-versions

jobs:
test-hardhat:
if: github.actor != 'arabot-1'
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{env.working-directory}}
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
ref: ${{ github.ref }}
fetch-depth: 0
- name: Install node
uses: actions/setup-node@v3
with:
cache: 'yarn'
node-version: 16
- name: Install dependencies
run: cd ../../ && yarn install --pure-lockfile
- name: Compile contracts-versions
run: yarn run build
- name: Run Tests
run: yarn run test
1 change: 1 addition & 0 deletions packages/contracts-versions/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
index.ts
51 changes: 51 additions & 0 deletions packages/contracts-versions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Aragon OSx Contracts Versions

A package to manage different contract versions and provide easy access to their ABI, types, and active contracts.

## Installation

```bash
npm install @aragon/osx-versions
```

or

```bash
yarn add @aragon/osx-versions
```

## Usage

```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()
);
```

## Adding new contract versions

1. Update `commit_hashes.json` with the new version name and the associated commit hash.
2. Run the `create-contract-versions.ts` script to build and generate the new version:

```bash
yarn build:contracts
```

3. Run the Rollup build process:

```bash
yarn build:npm
```

## Contributing

Contributions are welcome! Feel free to open a pull request or create an issue to report bugs or request features.

## License

This project is licensed under the AGPL-3.0-or-later License.
7 changes: 7 additions & 0 deletions packages/contracts-versions/commit_hashes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"versions": {
"v0_7_0_alpha": "9b912728c315f983af8c4bfd1f8d07021996563f",
"v1_0_0_mainnet_goerli": "c2b9d23a96654e81f22fbf91e6f334ef26a370af",
"v1_0_0_mumbai": "9485d97301611cfc78faa4bd00eb54abb6dd2d5e"
}
}
103 changes: 103 additions & 0 deletions packages/contracts-versions/create-contract-versions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const fs = require('fs-extra');
const path = require('path');
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 commitHashes = require('./commit_hashes.json');

async function getCurrentBranch() {
const {stdout} = await exec('git branch --show-current', {cwd: contractsDir});
return stdout.trim();
}

async function buildContracts(commit: string) {
try {
await exec(`git checkout ${commit}`, {cwd: contractsDir});
await exec('yarn build', {cwd: contractsDir});
} catch (error) {
console.error('Error building contracts:', error);
}
}

async function copyActiveContracts(commit: string, versionName: string) {
try {
console.log(`Copying active_contracts.json`);
const srcActiveContracts = path.join(monorepoRoot, 'active_contracts.json');
const destActiveContracts = path.join(
contractVersionsDir,
versionName,
'active_contracts.json'
);
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'`
);
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() {
const currentBranch = await getCurrentBranch();

for (const version in commitHashes.versions) {
const versionCommit = commitHashes.versions[version] as string;
const versionName = version;

console.log(
`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);
}

// Return to the original branch
await exec(`git checkout ${currentBranch}`, {cwd: contractsDir});

// Generate npm/index.ts file
const exports: string[] = [];
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';`
);
}
exports.push(
`export { ${Object.keys(commitHashes.versions)
.map(versionName => `${versionName}_active_contracts`)
.join(', ')} };`
);
await fs.writeFile(path.join(__dirname, 'index.ts'), exports.join('\n'));
}

createVersions();
4 changes: 4 additions & 0 deletions packages/contracts-versions/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
44 changes: 44 additions & 0 deletions packages/contracts-versions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "@aragon/osx-versions",
"version": "1.0.0",
"description": "The Aragon OSx contract versions",
"main": "dist/index-cjs.js",
"module": "dist/index-esm.js",
"types": "dist/bundle.d.ts",
"publishConfig": {
"access": "public"
},
"scripts": {
"build:contracts": "ts-node create-contract-versions.ts",
"build:npm": "rollup --config rollup.config.ts",
"build": "yarn build:contracts && yarn build:npm",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/aragon/osx.git"
},
"author": "",
"license": "AGPL-3.0-or-later",
"bugs": {
"url": "https://github.com/aragon/osx/issues"
},
"homepage": "https://github.com/aragon/osx#readme",
"devDependencies": {
"@rollup/plugin-typescript": "^8.3.1",
"@typechain/ethers-v5": "^10.2.0",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.5.1",
"@types/node": "^18.16.1",
"jest": "^29.5.0",
"rollup": "^2.70.1",
"rollup-plugin-dts": "^4.2.0",
"ts-jest": "^29.1.0",
"ts-node": "^10.9.1",
"typechain": "^8.1.1",
"typescript": "^5.0.4"
},
"dependencies": {
"ethers": "^5.6.2"
}
}
35 changes: 35 additions & 0 deletions packages/contracts-versions/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import typescript from '@rollup/plugin-typescript';
import dts from 'rollup-plugin-dts';
import json from '@rollup/plugin-json';

export default [
{
input: 'index.ts',
plugins: [typescript({project: './tsconfig.json'}), json()],
output: [
{
dir: 'dist',
entryFileNames: 'index-esm.js',
format: 'esm',
exports: 'named',
chunkFileNames: 'chunks/[name]-[hash].js',
},
{
dir: 'dist',
entryFileNames: 'index-cjs.js',
format: 'cjs',
exports: 'named',
chunkFileNames: 'chunks/[name]-[hash].js',
},
],
},
{
input: 'index.ts',
plugins: [dts(), json()],
output: {
dir: 'dist',
entryFileNames: 'bundle.d.ts',
format: 'es',
},
},
];
27 changes: 27 additions & 0 deletions packages/contracts-versions/test/usage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {
v1_0_0_mainnet_goerli_active_contracts,
v1_0_0_mainnet_goerli_typechain,
} 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 activeContracts = v1_0_0_mainnet_goerli_active_contracts;
expect(activeContracts).toBeDefined();
});

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();
});
});
19 changes: 19 additions & 0 deletions packages/contracts-versions/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es2018",
"module": "esnext",
"strict": true,
"esModuleInterop": true,
"outDir": "dist",
"declaration": true,
"moduleResolution": "node",
"resolveJsonModule": true,
"rootDir": "../../",
"baseUrl": "./",
"paths": {
"@aragon/osx-versions": ["node_modules/@aragon/osx-versions"]
}
},
"include": ["/build"],
"files": ["./index.ts"]
}
6 changes: 5 additions & 1 deletion packages/contracts/.prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ artifacts
cache
coverage*
gasReporterOutput.json
Releases.md
Releases.md
typechain
deployments
.openzeppelin
deployed_contracts.json
Loading

0 comments on commit 5e7a19e

Please sign in to comment.