Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/ OS-380 npm package versions #361

Merged
merged 36 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
1b14d42
prepare package
Rekard0 Apr 25, 2023
9d73698
remember the branch
Rekard0 Apr 25, 2023
fe9fe0f
only artifact src
Rekard0 Apr 25, 2023
c8f10de
correct the paths
Rekard0 Apr 25, 2023
c878bf4
correct root path
Rekard0 Apr 25, 2023
f319e0c
update hashes
Rekard0 Apr 25, 2023
3575258
add logs
Rekard0 Apr 25, 2023
1c6b6f8
only copy typechain
Rekard0 Apr 25, 2023
9f961cc
generate typechain
Rekard0 Apr 25, 2023
e8d6590
setup rollup
Rekard0 Apr 25, 2023
930b436
update npm packages
Rekard0 Apr 25, 2023
5db940f
update rollup index
Rekard0 Apr 26, 2023
4dc131e
add esm cjs
Rekard0 Apr 26, 2023
6005f06
add readme
Rekard0 Apr 26, 2023
748b382
remove gitIgnore
Rekard0 Apr 26, 2023
f9858c7
update typechain
Rekard0 Apr 27, 2023
1653ca1
update typescript
Rekard0 Apr 27, 2023
03ad311
convert script to typescript
Rekard0 Apr 27, 2023
5dd35fe
change commit hash json
Rekard0 Apr 27, 2023
252b165
rename types to typechain
Rekard0 Apr 28, 2023
ab50a5e
add test
Rekard0 Apr 28, 2023
0ad3a4f
add json to rollup and export types
Rekard0 Apr 28, 2023
b9230a7
update usage test
Rekard0 Apr 28, 2023
30c9593
generate index.ts dynamically
Rekard0 Apr 28, 2023
50e094c
update readme
Rekard0 Apr 28, 2023
744f4f7
add empty line
Rekard0 Apr 28, 2023
62f5bdb
fix index.ts
Rekard0 Apr 28, 2023
e49550f
ci/cd for tests
Rekard0 May 2, 2023
e671daa
fix-typo
Rekard0 May 2, 2023
5f3690b
remove check formatting
Rekard0 May 2, 2023
8eee478
add checkout ref
Rekard0 May 2, 2023
2358a73
update contracts flow
Rekard0 May 2, 2023
062edcf
update contracts workflow
Rekard0 May 2, 2023
8de1a63
update contracts prettier config
Rekard0 May 2, 2023
8029364
remove directories
Rekard0 May 2, 2023
4b94376
remove more dir
Rekard0 May 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Rekard0 marked this conversation as resolved.
Show resolved Hide resolved
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"]
}
Loading