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

Api updates #155

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 9 additions & 0 deletions .changeset/grumpy-humans-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"op-viem": major
---

Leave alpha.

- writeDepositETH and simulateDepositETH now take an `args.amount` instead of using a value arg.
- getSecondsToNextL2Output throws an error if the passed latestL2BlockNumber is less than the latestBlockNumber reported by the l2OutputOracle.
- writeFinalizeWithdrawalTransaction takes an `args` parameter like most other actions.
10 changes: 4 additions & 6 deletions site/docs/actions/public/L1/simulateDepositETH.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const { request } = await publicClient.simulateDepositETH({
args: {
to: '0xFd4F24676eD4588928213F37B126B53c07186F45',
gasLimit: 100000,
amount: 1n,
},
value: 1n,
portal: baseAddresses.portal,
account: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
})
Expand All @@ -40,11 +40,9 @@ Returns a `request` that can be passed to Viem's `writeContract` and a `result`
- **Type:** `number`
- The minimum gas limit to use for the deposit transaction.

### value

- **Type:** `bigint`

The amount of ETH to deposit.
- #### amount
- **Type:** `bigint`
- The amount of ETH to deposit.

### portal

Expand Down
26 changes: 10 additions & 16 deletions site/docs/actions/wallet/L1/writeDepositETH.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const hash = await walletClient.writeDepositETH({
args: {
to: '0xFd4F24676eD4588928213F37B126B53c07186F45',
gasLimit: 100000,
amount: 1n,
},
...baseAddresses,
value: 1n,
account: '0xd8da6bf26964af9d7eed9e03e53415d37aa96045',
})
```
Expand All @@ -31,23 +31,17 @@ Returns a transaction hash of the deposit transaction.

### args

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

- **Type:** `Address`

The address to deposit the tokens to.

#### gasLimit

- **Type:** `number`

The minimum gas limit to use for the deposit transaction.

### value

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

The amount of ETH to deposit.
- #### amount
- **Type:** `bigint`
- The amount of ETH to deposit.

### portal

Expand Down
19 changes: 5 additions & 14 deletions site/docs/actions/wallet/L1/writeDepositTransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ A [Transaction Hash](https://viem.sh/docs/glossary/terms#hash).
- **Default:** `0x`
- The calldata of the L2 transaction

- ### mint (optional)
- **Type:** `bigint`
- **Default** `0`
- Value in wei sent with this transaction. This value will be credited to the balance of the caller address on L2 _before_ the L2 transaction created by this transaction is made.

```ts
await walletClient.writeDepositTransaction({
args: { // [!code focus:7]
Expand All @@ -133,20 +138,6 @@ await walletClient.writeDepositTransaction({
})
```

### value (optional)

- **Type:** `number`

Value in wei sent with this transaction. This value will be credited to the balance of the caller address on L2 _before_ the L2 transaction created by this transaction is made.

```ts
await walletClient.writeDepositTransaction({
args,
portalAddress: portal,
value: parseEther(1), // [!code focus:1]
})
```

::: tip
`account`, `accessList`, `chain`, `dataSuffix`, `gasPrice`, `maxFeePerGas`, `maxPriorityFeePerGas`, and `nonce` can all also be passed and behave as with any viem writeContract call. See [their documentation](https://viem.sh/docs/contract/writeContract.html#writecontract) for more details.
:::
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const withdrawal: FinalizeWithdrawalTransactionParameters = {

const hash = await opStackL1WalletClient.writeFinalizeWithdrawalTranasction({
portal: baseAddresses.portal,
withdrawal,
args: { withdrawal },
account,
})
```
Expand Down
3 changes: 1 addition & 2 deletions src/_test/live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ test('correctly retrieves L2 hash', async () => {

const args: DepositTransactionParameters = {
to: account.address,
value: 1n,
mint: 1n,
data: '0x',
gasLimit: 0n,
isCreation: false,
Expand All @@ -48,7 +48,6 @@ test('correctly retrieves L2 hash', async () => {
const depositHash = await walletClient.writeDepositTransaction({
...baseGoerliAddresses,
args,
value: 1n,
})

console.log('depositHash', depositHash)
Expand Down
4 changes: 4 additions & 0 deletions src/actions/public/L1/getSecondsToNextL2Output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ export async function getSecondsToNextL2Output<TChain extends Chain | undefined>
address,
} as ReadContractParameters<typeof ABI, 'L2_BLOCK_TIME'>)

if (latestL2BlockNumber < latestBlockNumber) {
throw new Error(`latestBlock ${latestBlockNumber} is great than latestL2BlockNumber ${latestL2BlockNumber}!`)
}

const blocksTillUpdate = interval - (latestL2BlockNumber - latestBlockNumber)
// NOTE(Wilson): incase there is some problem
// e.g. output posting has stalled or the wrong latestL2BlockNumber is passed
Expand Down
2 changes: 1 addition & 1 deletion src/actions/public/L1/simulateDepositETH.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ test('default', async () => {
args: {
to: accounts[0].address,
gasLimit: 100000,
amount: 1n,
},
value: 1n,
account: accounts[0].address,
...baseAddresses,
})
Expand Down
9 changes: 4 additions & 5 deletions src/actions/public/L1/simulateDepositETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type SimulateDepositETHParameters<
_chainId = TChain extends Chain ? TChain['id'] : number,
> =
& { args: DepositETHParameters; portal: RawOrContractAddress<_chainId> }
& L1SimulateActionBaseType<TChain, TChainOverride, typeof ABI, typeof FUNCTION>
& Omit<L1SimulateActionBaseType<TChain, TChainOverride, typeof ABI, typeof FUNCTION>, 'value'>

export type SimulateDepositETHReturnType<
TChain extends Chain | undefined,
Expand All @@ -28,18 +28,17 @@ export async function simulateDepositETH<
>(
client: PublicClient<Transport, TChain>,
{
args: { to, gasLimit, data = '0x' },
args: { to, gasLimit, data = '0x', amount },
portal,
value,
...rest
}: SimulateDepositETHParameters<TChain, TChainOverride>,
): Promise<SimulateDepositETHReturnType<TChain, TChainOverride>> {
return simulateContract(client, {
address: resolveAddress(portal),
abi: ABI,
functionName: FUNCTION,
args: [to, value, gasLimit, false, data],
value,
args: [to, amount, gasLimit, false, data],
value: amount,
...rest,
} as unknown as SimulateContractParameters<typeof ABI, typeof FUNCTION, TChain, TChainOverride>)
}
8 changes: 4 additions & 4 deletions src/actions/wallet/L1/writeDepositETH.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ test('default', async () => {
to: accounts[0].address,
gasLimit: 21000,
data: '0x',
amount: 1n,
},
value: 1n,
...baseAddresses,
account: accounts[0].address,
}),
).toBeDefined()
})

test('correctly deposits ETH', async () => {
const amount = 1n
const args: DepositETHParameters = {
to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb',
gasLimit: 25000,
data: '0x',
amount,
}
const value = 1n
const hash = await writeDepositETH(walletClient, {
args,
value,
...baseAddresses,
account: accounts[0].address,
})
Expand All @@ -52,7 +52,7 @@ test('correctly deposits ETH', async () => {
expect(deposit.args.to.toLowerCase()).toEqual(args.to)
const expectOpaqueData = encodePacked(
['uint', 'uint', 'uint64', 'bool', 'bytes'],
[value, value, BigInt(args.gasLimit), false, '0x'],
[amount, amount, BigInt(args.gasLimit), false, '0x'],
)
expect(deposit.args.opaqueData).toEqual(expectOpaqueData)
})
18 changes: 10 additions & 8 deletions src/actions/wallet/L1/writeDepositETH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ export type WriteDepositETHParameters<
_chainId = TChain extends Chain ? TChain['id'] : number,
> =
& { args: DepositETHParameters; portal: RawOrContractAddress<_chainId> }
& L1WriteActionBaseType<
TChain,
TAccount,
TChainOverride
& Omit<
L1WriteActionBaseType<
TChain,
TAccount,
TChainOverride
>,
'value'
>

/**
Expand All @@ -29,9 +32,8 @@ export async function writeDepositETH<
>(
client: WalletClient<Transport, TChain, TAccount>,
{
args: { to, gasLimit, data },
args: { to, gasLimit, data, amount },
portal,
value,
...rest
}: WriteDepositETHParameters<
TChain,
Expand All @@ -40,9 +42,9 @@ export async function writeDepositETH<
>,
): Promise<WriteContractReturnType> {
return writeDepositTransaction(client, {
args: { to, value, gasLimit: BigInt(gasLimit), data },
args: { to, value: amount, gasLimit: BigInt(gasLimit), data },
portal: resolveAddress(portal),
value,
value: amount,
...rest,
} as unknown as WriteDepositTransactionParameters<
TChain,
Expand Down
14 changes: 7 additions & 7 deletions src/actions/wallet/L1/writeDepositTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ test('default', async () => {
gasLimit: 25000n,
data: '0x',
isCreation: false,
mint: 0n,
},
value: 0n,
...baseAddresses,
account: accounts[0].address,
}),
Expand All @@ -32,6 +32,7 @@ test('sends transaction to correct infered address', async () => {
gasLimit: 0n,
data: '0x',
isCreation: false,
mint: 1n,
}

const gas = await estimateGas(rollupPublicClient, {
Expand All @@ -45,7 +46,6 @@ test('sends transaction to correct infered address', async () => {

const hash = await writeDepositTransaction(walletClient, {
args,
value: 1n,
...baseAddresses,
account: accounts[0].address,
})
Expand All @@ -65,8 +65,8 @@ test('sends transaction to correct explicit address', async () => {
to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb',
value: 1n,
gasLimit: 25000n,
mint: 1n,
},
value: 1n,
portal: portal,
account: accounts[0].address,
})
Expand All @@ -84,10 +84,10 @@ test('creates correct deposit transaction', async () => {
gasLimit: 25000n,
data: '0x',
isCreation: false,
mint: 1n,
}
const hash = await writeDepositTransaction(walletClient, {
args,
value: args.value!,
...baseAddresses,
account: accounts[0].address,
})
Expand Down Expand Up @@ -119,13 +119,13 @@ test('correctly passes arugments', async () => {
gasLimit: 25000n,
data: '0x',
isCreation: false,
mint: 2n,
}

const hash = await writeDepositTransaction(walletClient, {
args,
...baseAddresses,
account: accounts[0].address,
value: 2n,
})

await mine(testClient, { blocks: 1 })
Expand All @@ -145,13 +145,13 @@ test('uses defaults for data, isCreation, and value', async () => {
const args: DepositTransactionParameters = {
to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb',
gasLimit: 25000n,
mint: 0n,
}

const hash = await writeDepositTransaction(walletClient, {
args,
...baseAddresses,
account: accounts[0].address,
value: 0n,
})

await mine(testClient, { blocks: 1 })
Expand All @@ -173,8 +173,8 @@ test('errors if portal not passed', async () => {
args: {
to: '0x0c54fccd2e384b4bb6f2e405bf5cbc15a017aafb',
gasLimit: 25000n,
mint: 0n,
},
value: 0n,
account: accounts[0].address,
})
).rejects.toThrowError('Invalid address')
Expand Down
15 changes: 10 additions & 5 deletions src/actions/wallet/L1/writeDepositTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type DepositTransactionParameters = {
value?: bigint
isCreation?: boolean
data?: Hex
mint?: bigint
}

export type WriteDepositTransactionParameters<
Expand All @@ -31,10 +32,13 @@ export type WriteDepositTransactionParameters<
_chainId = TChain extends Chain ? TChain['id'] : number,
> =
& { args: DepositTransactionParameters; portal: RawOrContractAddress<_chainId> }
& L1WriteActionBaseType<
TChain,
TAccount,
TChainOverride
& Omit<
L1WriteActionBaseType<
TChain,
TAccount,
TChainOverride
>,
'value'
>

/**
Expand All @@ -59,7 +63,7 @@ export async function writeDepositTransaction<
>(
client: WalletClient<Transport, TChain, TAccount>,
{
args: { to, value = 0n, gasLimit, isCreation = false, data = '0x' },
args: { to, value = 0n, gasLimit, isCreation = false, data = '0x', mint = 0n },
portal,
...rest
}: WriteDepositTransactionParameters<
Expand All @@ -73,6 +77,7 @@ export async function writeDepositTransaction<
abi: ABI,
functionName: FUNCTION,
args: [to, value, gasLimit, isCreation, data],
value: mint,
...rest,
} as unknown as WriteContractParameters<
typeof ABI,
Expand Down
Loading
Loading