Skip to content

Commit

Permalink
Merge pull request #6260 from ethereum-optimism/07-11-feat_Add_ts_con…
Browse files Browse the repository at this point in the history
…tract_package_for_TS

feat: Add ts contract package for TS
  • Loading branch information
OptimismBot authored Jul 19, 2023
2 parents 4604825 + 3695b73 commit 836ec28
Show file tree
Hide file tree
Showing 29 changed files with 72,444 additions and 358 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"changelog": ["@changesets/changelog-github", { "repo": "ethereum-optimism/optimism" }],
"commit": false,
"fixed": [],
"linked": [],
"linked": [["contracts-bedrock", "contracts-ts"]],
"access": "public",
"baseBranch": "develop",
"updateInternalDependencies": "patch",
Expand Down
26 changes: 26 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ jobs:
- "packages/fault-detector/node_modules"
- "packages/replica-healthcheck/node_modules"
- "packages/sdk/node_modules"
- "packages/contracts-ts/node_modules"
- run:
name: print forge version
command: forge --version
Expand Down Expand Up @@ -709,6 +710,24 @@ jobs:
name: Upload coverage
command: codecov --verbose --clean --flags <<parameters.coverage_flag>>

contracts-ts-tests:
docker:
- image: us-docker.pkg.dev/oplabs-tools-artifacts/images/ci-builder:latest
resource_class: large
steps:
- checkout
- attach_workspace: { at: "." }
- restore_cache:
name: Restore pnpm Package Cache
keys:
- pnpm-packages-v2-{{ checksum "pnpm.lock.yaml" }}
- check-changed:
patterns: sdk,contracts-bedrock,contracts
- run:
name: Check generated and build
command: pnpm generate:check
working_directory: packages/contracts-ts

sdk-next-tests:
docker:
- image: us-docker.pkg.dev/oplabs-tools-artifacts/images/ci-builder:latest
Expand Down Expand Up @@ -1336,6 +1355,13 @@ workflows:
dependencies: "(common-ts|core-utils|sdk)"
requires:
- pnpm-monorepo
- js-lint-test:
name: contracts-ts-tests
coverage_flag: contracts-ts-tests
package_name: contracts-ts
dependencies: '(contracts-bedrock|contracts-ts)'
requires:
- pnpm-monorepo
- js-lint-test:
name: sdk-next-tests
coverage_flag: sdk-next-tests
Expand Down
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
"inputs": ["default", "testing", "^production"],
"dependsOn": ["^build"]
},
"generate": {
"dependsOn": ["^build"]
},
"build:contracts": {
"inputs": [
"configsProject",
Expand Down
4 changes: 4 additions & 0 deletions op-bindings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ $ make devtools
```

The geth docs for `abigen` can be found [here](https://geth.ethereum.org/docs/dapp/native-bindings).

## See also

TypeScript bindings are also generated in [@eth-optimism/contracts-ts](../packages/contracts-ts/)
3 changes: 3 additions & 0 deletions op-bindings/predeploys/addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package predeploys

import "github.com/ethereum/go-ethereum/common"

// TODO - we should get a single toml yaml or json file source of truth in @eth-optimism/bedrock package
// This needs to be kept in sync with @eth-optimism/contracts-ts/wagmi.config.ts which also specifies this
// To improve robustness and maintainability contracts-bedrock should export all addresses
const (
L2ToL1MessagePasser = "0x4200000000000000000000000000000000000016"
DeployerWhitelist = "0x4200000000000000000000000000000000000002"
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"scripts": {
"clean": "npx nx run-many --target=clean",
"bindings": "nx bindings @eth-optimism/contracts-bedrock",
"build": "npx nx run-many --target=build",
"test": "npx nx run-many --target=test",
"lint": "npx nx run-many --target=lint",
Expand Down
6 changes: 4 additions & 2 deletions packages/contracts-bedrock/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"contracts/**/*.sol"
],
"scripts": {
"bindings": "cd ../../op-bindings && make",
"build": "npx nx build:contracts",
"bindings": "pnpm bindings:ts && pnpm bindings:go",
"bindings:ts": "pnpm generate:addresses && nx generate @eth-optimism/contracts-ts",
"bindings:go": "cd ../../op-bindings && make",
"build": "nx build:contracts",
"prebuild:contracts": "./scripts/verify-foundry-install.sh",
"build:contracts": "pnpm build:forge",
"build:forge": "forge build",
Expand Down
8 changes: 8 additions & 0 deletions packages/contracts-ts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
artifacts
cache
typechain
.deps
.envrc
.env
/dist/
coverage
7 changes: 7 additions & 0 deletions packages/contracts-ts/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
artifacts
cache
typechain
.deps
.envrc
.env
/dist/
3 changes: 3 additions & 0 deletions packages/contracts-ts/.prettierrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
...require('../../.prettierrc.js'),
}
14 changes: 14 additions & 0 deletions packages/contracts-ts/CODE_GEN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Code gen

Summary -

- This package is generated from [contracts-bedrock](../contracts-bedrock/)
- It's version is kept in sync with contracts bedrock via the [changeset config](../../.changeset/config.json) e.g. if contracts-bedrock is `4.2.0` this package will have the same version.

## Code gen instructions

To run the code gen run the `generate` script from [package.json](./package.json). Make sure node modules is installed.

```bash
pnpm i && pnpm generate
```
21 changes: 21 additions & 0 deletions packages/contracts-ts/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Optimism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions packages/contracts-ts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
## Contracts TS

[![codecov](https://codecov.io/gh/ethereum-optimism/optimism/branch/develop/graph/badge.svg?token=0VTG7PG7YR&flag=contracts-bedrock-tests)](https://codecov.io/gh/ethereum-optimism/optimism)

ABI and Address constants + generated code from [@eth-optimism/contracts-bedrock/](../contracts-bedrock/) for use in TypeScript.

Much of this package is generated. See [CODE_GEN.md](./CODE_GEN.md) for instructions on how to generate.

#### @eth-optimism/contracts-ts

The main entrypoint exports constants related to contracts bedrock as const. As const allows it to be [used in TypeScript with stronger typing than importing JSON](https://github.com/microsoft/TypeScript/issues/32063).

- Exports contract abis.
- Exports contract addresses

```typescript
import {
l2OutputOracleProxyABI,
l2OutputOracleAddresses,
} from '@eth-optimism/contracts-ts'

console.log(l2OutputOracleAddresses[10], abi)
```

Addresses are also exported as an object for convenience.

```typescript
import { addresses } from '@eth-optimism/contracts-ts'

console.log(addresses.l2OutputOracle[10])
```

#### @eth-optimism/contracts-ts/react

- All [React hooks](https://wagmi.sh/cli/plugins/react) `@eth-optimism/contracts-ts/react`

```typescript
import { useAddressManagerAddress } from '@eth-optimism/contracts-ts/react'

const component = () => {
const { data, error, loading } = useAddressManagerAddress()
if (loading) {
return <div>Loading</div>
}
if (err) {
return <div>Error</div>
}
return <div>{data}</div>
}
```

#### @eth-optimism/contracts-ts/actions

- All [wagmi actions](https://wagmi.sh/react/actions) for use in Vanilla JS or non react code

```typescript
import { readSystemConfig } from '@eth-optimism/contracts-ts/actions'
console.log(await readSystemConfig())
```

#### See Also

- [Contracts bedrock specs](../../specs/)
- [Wagmi](https://wagmi.sh)
Loading

0 comments on commit 836ec28

Please sign in to comment.