Skip to content

Commit

Permalink
Merge pull request #138 from base-org/refactor-chain-config-api
Browse files Browse the repository at this point in the history
Refactor chain config api
  • Loading branch information
zencephalon authored Oct 19, 2023
2 parents 2f5a5ac + 92f1b66 commit 64be79a
Show file tree
Hide file tree
Showing 84 changed files with 787 additions and 1,135 deletions.
49 changes: 49 additions & 0 deletions .changeset/olive-ants-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
"op-viem": patch
---

Actions now receive contract addresses instead of L2 config objects for simplicty and Viem upstream compatibility. op-viem/chains now eexports addresses objects that be spread into actions to pass the required address.

Previously

```ts
import { publicL1Actions } from 'op-viem'
import { base } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const publicClient = createPublicClient({
account,
chain: mainnet,
transport: http(),
}).extend(publicL1Actions)

await getOutputForL2Block(publicClient, {
blockNumber: 2725977n,
l2Chain: base,
})
```

Now

```ts
import { publicL1Actions } from 'op-viem'
import { baseAddresses } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const publicClient = createPublicClient({
account,
chain: mainnet,
transport: http(),
}).extend(publicL1Actions)

await getOutputForL2Block(publicClient, {
blockNumber: 2725977n,
l2OutputOracle: baseAddresses.l2OutputOracle,
})

// more simply
await getOutputForL2Block(publicClient, {
blockNumber: 2725977n,
...baseAddresses,
})
```
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"typescript.enablePromptUseWorkspaceTsdk": true,
"editor.codeActionsOnSave": {
"source.organizeImports.biome": true
},
"[typescript]": {
"editor.defaultFormatter": "dprint.dprint"
}
}
41 changes: 39 additions & 2 deletions site/docs/actions/public/L1/getOutputForL2Block.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Calls to the L2OutputOracle contract on L1 to get the output for a given L2 bloc

```ts [example.ts]
import { publicL1Actions } from 'op-viem'
import { base } from 'op-viem/chains'
import { baseAddresses } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const publicClient = createPublicClient({
Expand All @@ -15,6 +15,43 @@ const publicClient = createPublicClient({

await getOutputForL2Block(publicClient, {
blockNumber: 2725977n,
l2Chain: base,
l2OutputOracle: baseAddresses.l2OutputOracle,
})

// more simply
await getOutputForL2Block(publicClient, {
blockNumber: 2725977n,
...baseAddresses,
})
```

## Return Value

Returns `GetOutputForL2BlockReturnType`.

```ts [example.ts]
export type Proposal = {
outputRoot: Hex
timestamp: bigint
l2BlockNumber: bigint
}

export type GetOutputForL2BlockReturnType = {
proposal: Proposal
outputIndex: bigint
}
```
## Parameters
### blockNumber
- **Type:** `bigint`
The block number of the L2 block for which to get the output.
### l2OutputOracle
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)
The address of the L2OutputOracle contract where the `getOutputForL2Block` call will be made.
29 changes: 16 additions & 13 deletions site/docs/actions/public/L1/getSecondsToFinalizable.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Returns the number of seconds until a withdrawal is finalized for a given withdr

```ts [example.ts]
import { publicL1Actions } from 'op-viem'
import { baseAddresses } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const publicClient = createPublicClient({
Expand All @@ -13,7 +14,15 @@ const publicClient = createPublicClient({
}).extend(publicL1Actions)

const seconds = await getSecondsToFinalizable(publicClient, {
l2Chain: base,
portal: baseAddresses.portal,
l2OutputOracle: baseAddresses.l2OutputOracle,
withdrawalHash:
'0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709',
})

// or more simply
const seconds = await getSecondsToFinalizable(publicClient, {
...baseAddresses,
withdrawalHash:
'0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709',
})
Expand All @@ -25,23 +34,17 @@ Returns a `number` representative of the seconds until withdrawal finalization.

## Parameters

### l2chain (optional)

- **Type:** `OpStackChain`

The L2 chain to deposit to.

### optimismPortalAddress (optional)
### portal

- **Type:** [`Address`](https://viem.sh/docs/glossary/types#address)
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)

The address of the `OptimismPortal` contract where the `readProvenWithdrawals` call will be made. MUST be specified if [l2Chain](#l2chain-optional) not passed.
The address of the `OptimismPortal` contract where the `readProvenWithdrawals` call will be made.

### l2OutputOracleAddress (optional)
### l2OutputOracle

- **Type:** [`Address`](https://viem.sh/docs/glossary/types#address)
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)

The address of the L2OutputOracle contract where the `FINALIZATION_PERIOD_SECONDS` call will be made. MUST be provied if [l2Chain](l2chain-optional) is not.
The address of the L2OutputOracle contract where the `FINALIZATION_PERIOD_SECONDS` call will be made.

### withdrawalHash

Expand Down
19 changes: 12 additions & 7 deletions site/docs/actions/public/L1/getSecondsToNextL2Output.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ const l1Client = createPublicClient({

const time = await l1Client.getSecondsToNextL2Output(, {
latestL2BlockNumber,
l2Chain: base,
l2OutputOracle: baseAddresses.l2OutputOracle,
})
// Or
const time = await l1Client.getSecondsToNextL2Output(, {
latestL2BlockNumber,
...baseAddresses,
})
```

Expand All @@ -28,14 +33,14 @@ Seconds until the next L2 output should be posted.

## Parameters

### l2Chain (optional)
### latestL2BlockNumber

- **Type:** `OpStackChain`
- **Type:** `bigint`

The L2 chain that we are waiting on the output of.
The latest L2 block number.

### l2OutputOracleAddress (optional)
### l2OutputOracle

- **Type:** [`Address`](https://viem.sh/docs/glossary/types#address)
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)

The address of the L2OutputOracle contract. MUST be provied if [l2Chain](l2chain-optional) is not.
The address of the L2OutputOracle contract.
20 changes: 10 additions & 10 deletions site/docs/actions/public/L1/readFinalizedWithdrawals.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ const publicClient = createPublicClient({
}).extend(publicL1Actions)

const finalizedWithdrawal = await readFinalizedWithdrawals(publicClient, {
l2Chain: base,
portal: baseAddresses.portal,
withdrawalHash:
'0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709',
})
// Or
const finalizedWithdrawal = await readFinalizedWithdrawals(publicClient, {
...baseAddresses,
withdrawalHash:
'0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709',
})
Expand All @@ -25,17 +31,11 @@ Returns a `boolean` for whether the withdrawal has been finalized.

## Parameters

### l2chain (optional)

- **Type:** `OpStackChain`

The L2 chain to deposit to.

### optimismPortalAddress (optional)
### portalAddress

- **Type:** [`Address`](https://viem.sh/docs/glossary/types#address)
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)

The `OptimismPortal` contract where the sendMessage call should be made. MUST be specified if [l2Chain](#l2chain-optional) not passed.
The `OptimismPortal` contract where the sendMessage call should be made.

### withdrawalHash

Expand Down
29 changes: 19 additions & 10 deletions site/docs/actions/public/L1/readProvenWithdrawals.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Returns a `ProvenWithdrawal` struct containing the `outputRoot`, `timestamp`, an

```ts [example.ts]
import { publicL1Actions } from 'op-viem'
import { baseAddresses } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const publicClient = createPublicClient({
Expand All @@ -13,7 +14,13 @@ const publicClient = createPublicClient({
}).extend(publicL1Actions)

const provenWithdrawal = await readProvenWithdrawals(publicClient, {
l2Chain: base,
portal: baseAddresses.portal,
withdrawalHash:
'0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709',
})
// or
const provenWithdrawal = await readProvenWithdrawals(publicClient, {
...baseAddresses,
withdrawalHash:
'0xEC0AD491512F4EDC603C2DD7B9371A0B18D4889A23E74692101BA4C6DC9B5709',
})
Expand All @@ -23,19 +30,21 @@ const provenWithdrawal = await readProvenWithdrawals(publicClient, {

Returns an object that represents a `ProvenWithdrawl` struct that contains the `outputRoot`, `timestamp`, and `l2OutputIndex`

## Parameters

### l2chain (optional)

- **Type:** `OpStackChain`
```ts
type ProvenWithdrawal = {
outputRoot: Hex
timestamp: bigint
l2OutputIndex: bigint
}
```
### optimismPortalAddress (optional)
## Parameters
- **Type:** [`Address`](https://viem.sh/docs/glossary/types#address)
### portal
The `OptimismPortal` contract where the sendMessage call should be made. MUST be specified if [l2Chain](#l2chain-optional) not passed.
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)
The L2 chain to deposit to.
The `OptimismPortal` contract where the sendMessage call should be made.
### withdrawalHash
Expand Down
9 changes: 5 additions & 4 deletions site/docs/actions/public/L1/simulateDepositERC20.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Simulates a deposit of ERC20 tokens to L2.

```ts [example.ts]
import { base, publicL1Actions } from 'op-viem'
import { baseAddresses } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const USDCL1 = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48'
Expand All @@ -23,7 +24,7 @@ const { request } = await publicClient.simulateDepositERC20({
amount: 1n,
minGasLimit: 100000,
},
l2chain: base,
l1StandardBridge: baseAddresses.l1StandardBridge,
account: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
})
```
Expand Down Expand Up @@ -60,11 +61,11 @@ Returns a `request` that can be passed to Viem's `writeContract` and a `result`
- **Type:** `Hex`
- Extra data to include in the transaction.

### l2chain
### l1StandardBridge

- **Type:** `OpStackChain`
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)

The L2 chain to deposit to.
The `L1StandardBridge` contract.

### account

Expand Down
27 changes: 13 additions & 14 deletions site/docs/actions/public/L1/simulateDepositETH.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Simulates a deposit of ETH from L1 to L2.

```ts [example.ts]
import { base, publicL1Actions } from 'op-viem'
import { publicL1Actions } from 'op-viem'
import { baseAddresses } from 'op-viem/chains'
import { createPublicClient } from 'viem'

const publicClient = createPublicClient({
Expand All @@ -18,7 +19,7 @@ const { request } = await publicClient.simulateDepositETH({
gasLimit: 100000,
},
value: 1n,
l2chain: base,
portal: baseAddresses.portal,
account: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
})
```
Expand All @@ -29,29 +30,27 @@ Returns a `request` that can be passed to Viem's `writeContract` and a `result`

## Parameters

### to
### args

- **Type:** `Address`

The address to deposit the tokens to.

### gasLimit

- **Type:** `number`
- #### to
- **Type:** `Address`
- The address to deposit the tokens to.

The minimum gas limit to use for the deposit transaction.
- #### gasLimit
- **Type:** `number`
- The minimum gas limit to use for the deposit transaction.

### value

- **Type:** `bigint`

The amount of ETH to deposit.

### l2chain
### portal

- **Type:** `OpStackChain`
- **Type:** [`RawOrContractAddress`](https://viem.sh/docs/glossary/types#raworcontractaddress)

The L2 chain to deposit to.
The `OptimismPortal` contract.

### account

Expand Down
Loading

0 comments on commit 64be79a

Please sign in to comment.