Skip to content

Commit

Permalink
updated docs
Browse files Browse the repository at this point in the history
  • Loading branch information
krofax committed Oct 16, 2024
1 parent 4328376 commit dcdbcae
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 47 deletions.
26 changes: 13 additions & 13 deletions pages/builders/app-developers/tutorials/send-tx-from-eth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ You're going to use the `viem` package for this tutorial. Since Viem is a [Node.
{<h3>Make a Project Folder</h3>}

```bash
mkdir op-sample-project
cd op-sample-project
mkdir trigger-transaction
cd trigger-transaction
```

{<h3>Initialize the Project</h3>}
Expand Down Expand Up @@ -89,7 +89,7 @@ You need to import some dependencies into your Node REPL session.

{<h3>Import Viem</h3>}

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L3-L5 hash=7a906fc04fa87f91fc33049240e8e0f8
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L3-L6 hash=1e06dede41cb7ba0bd9414a8962521c6
```

</Steps>
Expand All @@ -102,12 +102,12 @@ You'll need a few variables throughout this tutorial. Let's set those up now.

{<h3>Load your private key</h3>}

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L7-L8 hash=46ba01375a5d8844b2315f0e579dfac3
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L8-L9 hash=46ba01375a5d8844b2315f0e579dfac3
```

{<h3>Create the RPC providers and wallets</h3>}

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L10-L13 hash=a364c906d788d0614588c8db059cfcca
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L11-L13 hash=d5a5a1252f4b6ff026cd58de8e6ae7f1
```

</Steps>
Expand All @@ -116,7 +116,7 @@ You'll need a few variables throughout this tutorial. Let's set those up now.

You'll be sending a small amount of ETH as part of this tutorial. Quickly check your balance on OP Sepolia so that you know how much you had at the start of the tutorial.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L15-L17 hash=5b95f18440ad72b6200ed7b5df00ca8f
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L17-L18 hash=2bbd74b9de0c0fa0daca043ab9030ff0
```

## Trigger the Transaction
Expand All @@ -127,35 +127,35 @@ Now you'll use the `OptimismPortal` contract to trigger a transaction on OP Sepo

{<h3>Create the OptimismPortal object</h3>}

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L19-L30 hash=bb10ed96a0eed859d491afc1929001ce
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L20-L31 hash=b062257111aacc2f3a985542e451269c
```

{<h3>Estimate the required gas</h3>}

When sending transactions via the `OptimismPortal` contract it's important to always include a gas buffer. This is because the `OptimismPortal` charges a variable amount of gas depending on the current demand for L2 transactions triggered via L1. If you do not include a gas buffer, your transactions may fail.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L32-L43 hash=ae495cba1a72841059a32edaf20f854d
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L33-L45 hash=f5d0d92f161514a3359997143804af0b
```

{<h3>Send the transaction</h3>}

Now you'll send the transaction. Note that you are including a buffer of 20% on top of the gas estimate.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L45-L54 hash=6fb0ba06fb29be7ee2c945fca75a50f4
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L50-L61 hash=59e3ee527809087e9e615f28caa49083
```

{<h3>Wait for the L1 transaction</h3>}

First you'll need to wait for the L1 transaction to be mined.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L56 hash=3c0301f8a9ca78bd8bfbff6aef807c77
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L60 hash=0efd9bd3369de7f5f36ea5540a5d8078
```

{<h3>Wait for the L2 transaction</h3>}

Now you'll need to wait for the corresponding L2 transaction to be included in a block. This transaction is automatically created as a result of your L1 transaction. Here you'll determine the hash of the L2 transaction and then wait for that transaction to be included in the L2 blockchain.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L58-L60 hash=cdd456e54b0f8507f96365dae05b8856
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L67-L73 hash=bf903509fb370c2b4e85dbfbf05650ee
```

</Steps>
Expand All @@ -164,12 +164,12 @@ Now you'll need to wait for the corresponding L2 transaction to be included in a

You should have a little less ETH on OP Sepolia now. Check your balance to confirm.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L62-L63 hash=7cac2670afeb0fcc88346bc59b90466c
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L75-L76 hash=dd456528a8bae103b73c5bcd19216916
```

Make sure that the difference is equal to the amount you were expecting to send.

```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L65-L66 hash=e2ef73867c6d755ca89b9d3381522208
```js file=<rootDir>/public/tutorials/send-tx-from-eth.js#L78-L79 hash=3885097e127ff18b3c2c2fc1d3d5a7c0
```

## Next Steps
Expand Down
81 changes: 47 additions & 34 deletions public/tutorials/send-tx-from-eth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const { createPublicClient, createWalletClient, http, parseEther, formatEther } = require('viem');
const { optimismSepolia, sepolia } = require('viem/chains');
const { privateKeyToAccount } = require('viem/accounts');
const { publicActionsL2, publicActionsL1, walletActionsL2, walletActionsL1, getL2TransactionHashes } = require ('viem/op-stack')

const privateKey = process.env.TUTORIAL_PRIVATE_KEY;
const account = privateKeyToAccount(privateKey);
Expand All @@ -15,54 +16,66 @@
const address = account.address;
const initialBalance = await l2PublicClient.getBalance({ address });
console.log(`Initial balance: ${formatEther(initialBalance)} ETH`);

const optimismPortalAbi = [
{
{
inputs: [
{ internalType: 'uint256', name: '_gasLimit', type: 'uint256' },
{ internalType: 'bytes', name: '_data', type: 'bytes' },
{ internalType: 'uint256', name: '_gasLimit', type: 'uint256' },
{ internalType: 'bytes', name: '_data', type: 'bytes' },
],
name: 'depositTransaction',
outputs: [],
stateMutability: 'payable',
type: 'function',
},
},
];
const optimismPortalAddress = '0x1000000000000000000000000000000000000000';

const optimismPortalAddress = '0x5b47E1A08Ea6d985D6649300584e6722Ec4B1383';
const gasLimit = 100000n;
const data = '0x';
const value = parseEther('0.000001');
const value = parseEther('0.000069420');

const gasEstimate = await l1PublicClient.estimateContractGas({
address: optimismPortalAddress,
abi: optimismPortalAbi,
functionName: 'depositTransaction',
args: [gasLimit, data],
value,
address: optimismPortalAddress,
abi: optimismPortalAbi,
functionName: 'depositTransaction',
args: [gasLimit, data],
value,
account: account.address,
});

const { hash: l1TxHash } = await l1WalletClient.writeContract({
address: optimismPortalAddress,
abi: optimismPortalAbi,
functionName: 'depositTransaction',
args: [gasLimit, data],
value,
gas: gasEstimate * 120n / 100n, // 20% buffer
});

console.log(`L1 transaction hash: ${l1TxHash}`);

const l1Receipt = await l1PublicClient.waitForTransactionReceipt({ hash: l1TxHash });

const l2TxHash = l1Receipt.logs[0].topics[1];
const l2Receipt = await l2PublicClient.waitForTransactionReceipt({ hash: l2TxHash });
console.log(`L2 transaction hash: ${l2TxHash}`);

const finalBalance = await l2PublicClient.getBalance({ address });
console.log(`Gas estimate: ${gasEstimate}`);

// Step 3: Send the transaction
const { request } = await l1PublicClient.simulateContract({
account,
address: optimismPortalAddress,
abi: optimismPortalAbi,
functionName: 'depositTransaction',
args: [gasLimit, data],
value,
gas: gasEstimate * 120n / 100n, // 20% buffer
})

const l1TxHash = await l1WalletClient.writeContract(request)
console.log(`L1 transaction hash: ${l1TxHash}`)

// Step 4: Wait for the L1 transaction
const l1Receipt = await l1PublicClient.waitForTransactionReceipt({hash: l1TxHash})
console.log('L1 transaction confirmed:', l1Receipt)

const [l2Hash] = getL2TransactionHashes(l1TxHash)
console.log(`Corresponding L2 transaction hash: ${l2Hash}`);

const l2Receipt = await l2PublicClient.waitForTransactionReceipt({
hash: l2Hash,
});
console.log('L2 transaction confirmed:', l2Receipt);

const finalBalance = await l2Wallet.getBalance()
console.log(`Final balance: ${formatEther(finalBalance)} ETH`);
const difference = initialBalance - finalBalance;
console.log(`Difference: ${formatEther(difference)} ETH`);

const difference = initialBalance - finalBalance
console.log(`Difference in balance: ${formatEther(difference)} ETH`);

})()

0 comments on commit dcdbcae

Please sign in to comment.