Skip to content

Commit

Permalink
feat(contracts): add aliases for latest versions of the contracts (#1…
Browse files Browse the repository at this point in the history
…3484)

* feat(contracts): add aliass for latest versions of the contracts

* untrack index file

* add alias for all contracts

* update hardhat plugin to use that logic

* bump version + edit README

* fix yarn cmd for contracts index page

* eth-node get latest version

* bump contracts version

* fix contracts build

* bump contract again

* fix autogen index build step
  • Loading branch information
clemsos committed Mar 20, 2024
1 parent a8e9799 commit 385a951
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 122 deletions.
5 changes: 2 additions & 3 deletions docker/development/eth-node/package.json
Expand Up @@ -10,9 +10,8 @@
},
"dependencies": {
"@nomiclabs/hardhat-ethers": "2.2.3",
"@unlock-protocol/hardhat-plugin": "0.1.3",
"@unlock-protocol/contracts": "0.0.22",
"@unlock-protocol/contracts": "0.0.22",
"@unlock-protocol/hardhat-plugin": "latest",
"@unlock-protocol/contracts": "latest",
"eslint": "8.54.0",
"ethers": "5.7.2",
"hardhat": "2.20.1",
Expand Down
3 changes: 2 additions & 1 deletion packages/contracts/.gitignore
Expand Up @@ -3,4 +3,5 @@ test/contracts
artifacts
cache
docs-reorg
docs
docs
src/index.ts
6 changes: 6 additions & 0 deletions packages/contracts/CHANGELOG.md
@@ -1,5 +1,11 @@
# CHANGELOG

## 0.0.22

- add `contracts` alias for all versions
- add `PublicLock` and `Unlock` alias for all versions
- add version numbers as constant

## 0.0.21

- release fix post-attack on Fri Apr 21st 2023 #11690
Expand Down
22 changes: 16 additions & 6 deletions packages/contracts/README.md
Expand Up @@ -3,12 +3,22 @@
### Javascript usage

```js
// get latest
import unlock from '@unlock-protocol/contracts/Unlock'

// get previous versions
import unlock from '@unlock-protocol/contracts/UnlockV0'
import { UnlockV0 } from '@unlock-protocol/contracts'
// get latest version
import { Unlock } from '@unlock-protocol/contracts'
import { PublicLock } from '@unlock-protocol/contracts'

// a previous version
import UnlockV12 from '@unlock-protocol/contracts/UnlockV12'
import { UnlockV12 } from '@unlock-protocol/contracts'

// all contracts
import { contracts } from '@unlock-protocol/contracts'

// the number of latest versions available
import {
PUBLICLOCK_LATEST_VERSION,
UNLOCK_LATEST_VERSION,
} from '@unlock-protocol/contracts'
```

### Solidity usage
Expand Down
10 changes: 5 additions & 5 deletions packages/contracts/package.json
@@ -1,14 +1,14 @@
{
"name": "@unlock-protocol/contracts",
"version": "0.0.21",
"version": "0.0.24",
"main": "dist/index.js",
"scripts": {
"clean": "rm -rf dist docs",
"clean": "rm -rf dist docs src/index.ts",
"test": "hardhat test",
"abi:cleanup": "yarn clean && tsc && node dist/utils/cleanup.js",
"build": "yarn build:index && yarn build:dist",
"build:dist": "yarn clean && tsc && yarn contracts:copy && yarn docs && yarn docs:copy",
"build:index": "yarn clean && tsc && node dist/utils/parser.js > src/index.ts",
"build": "yarn clean && yarn build:index && yarn build:dist",
"build:dist": "yarn contracts:copy && yarn docs && yarn docs:copy",
"build:index": "tsc && node dist/parser.js > src/index.ts && tsc",
"ci": "yarn test",
"lint": "eslint --resolve-plugins-relative-to ../eslint-config --ext .ts,.js src/",
"contracts:copy": "copyfiles --verbose -u 2 src/**/*.sol dist",
Expand Down
85 changes: 0 additions & 85 deletions packages/contracts/src/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/contracts/src/utils/files.ts
Expand Up @@ -36,6 +36,7 @@ export const getAbiPaths = async () => {
'abis/Unlock',
'abis/UnlockDiscountToken',
'abis/Governor',
'abis/utils',
]

const paths = await Promise.all(folders.map((f) => parseExports(f)))
Expand Down
67 changes: 49 additions & 18 deletions packages/contracts/src/utils/parser.ts
@@ -1,11 +1,39 @@
import { getAbiPaths } from './files'

type contractVersion = {
contractName: string
versionNumber: string
abiPath: string
}

const findLatest = (exports: contractVersion[], contract: string) => {
const [latest] = exports
.filter(({ contractName }) => contractName === contract)
.sort(
({ versionNumber: a }, { versionNumber: b }) =>
parseInt(b.replace('V', '')) - parseInt(a.replace('V', ''))
)
const { contractName, versionNumber } = latest
const varName = `${contract.toLocaleUpperCase()}_LATEST_VERSION`
const versionInt = versionNumber.replace('V', '')

console.log(`export { ${contractName}${versionNumber} as ${contract} }`)
console.log(`const ${varName} = ${versionInt}`)
console.log(`export { ${varName} }`)
return latest
}

async function main() {
const paths = await getAbiPaths()
const exports = paths.flat().map((abiPath) => {
const s = abiPath.split('/')
const contractName = s[2]
const versionNumber = s[3].replace(contractName, '').replace('.json', '')
const exports: contractVersion[] = paths.flat().map((abiPath) => {
let contractName, fileName, versionNumber
if (abiPath.includes('/utils/')) {
;[, , , fileName] = abiPath.split('/')
contractName = fileName.replace('.json', '')
} else {
;[, , contractName, fileName] = abiPath.split('/')
versionNumber = fileName.replace(contractName, '').replace('.json', '')
}
return {
contractName,
versionNumber,
Expand All @@ -16,28 +44,31 @@ async function main() {
console.log("// This file is generated, please don't edit directly")
console.log("// Refer to 'utils/parser.ts' and 'yarn build:index' for more\n")

exports.forEach(({ contractName, versionNumber, abiPath }) =>
exports.forEach(({ contractName, versionNumber = '', abiPath }) =>
console.log(`import ${contractName}${versionNumber} from '${abiPath}'`)
)

const utils = [
'LockSerializer',
'UnlockSwapPurchaser',
'UnlockSwapBurner',
'UniswapOracleV3',
]

utils.forEach((util) => {
console.log(`import ${util} from './abis/utils/${util}.json'`)
})

console.log('\n// exports')

exports.forEach(({ contractName, versionNumber }) =>
exports.forEach(({ contractName, versionNumber = '' }) =>
console.log(`export { ${contractName}${versionNumber} }`)
)

console.log(`export { ${utils.toString()} }`)
// alias for all contracts
console.log(
`const contracts = { ${exports
.map(
({ contractName, versionNumber = '' }) =>
`${contractName}${versionNumber}`
)
.toString()} }
export { contracts }`
)

// alias for the newest versions
console.log('\n// latest')
findLatest(exports, 'PublicLock')
findLatest(exports, 'Unlock')
}

main()
Expand Down
4 changes: 4 additions & 0 deletions packages/hardhat-plugin/CHANGELOG.md
@@ -1,5 +1,9 @@
# CHANGELOG

### 0.1.5

- Use version number directly fromn contracts packages

### 0.1.2

- Changing the lock deployed version to the latest
Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat-plugin/package.json
@@ -1,6 +1,6 @@
{
"name": "@unlock-protocol/hardhat-plugin",
"version": "0.1.2",
"version": "0.1.5",
"description": "Hardhat Plugin for Unlock Protocol",
"author": "Unlock Protocol",
"license": "MIT",
Expand Down
10 changes: 8 additions & 2 deletions packages/hardhat-plugin/src/constants.ts
@@ -1,5 +1,11 @@
export const UNLOCK_LATEST_VERSION = 11
export const PUBLIC_LOCK_LATEST_VERSION = 13
import {
UNLOCK_LATEST_VERSION,
PUBLICLOCK_LATEST_VERSION,
} from '@unlock-protocol/contracts'
export {
UNLOCK_LATEST_VERSION,
PUBLICLOCK_LATEST_VERSION as PUBLIC_LOCK_LATEST_VERSION,
}

// task names
export const TASK_CREATE_LOCK = 'unlock:create-lock'
2 changes: 1 addition & 1 deletion packages/hardhat-plugin/src/utils.ts
@@ -1,4 +1,4 @@
import * as contracts from '@unlock-protocol/contracts'
import { contracts } from '@unlock-protocol/contracts'
import { HardhatRuntimeEnvironment } from 'hardhat/types'
import type { Signer, Contract, ContractFactory } from 'ethers'

Expand Down

0 comments on commit 385a951

Please sign in to comment.