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

feat(contracts): add aliases for latest versions of the contracts #13484

Merged
merged 11 commits into from Mar 20, 2024
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,
Comment on lines +19 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

} from '@unlock-protocol/contracts'
```

### Solidity usage
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
@@ -1,6 +1,6 @@
{
"name": "@unlock-protocol/contracts",
"version": "0.0.21",
"version": "0.0.24",
"main": "dist/index.js",
"scripts": {
"clean": "rm -rf dist docs",
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Use version number directly fromn contracts packages
- Use version number directly from 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