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

Add custom events #683

Merged
merged 26 commits into from
Oct 19, 2023
Merged
Changes from 7 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c53f277
add customize event
Jason-W123 Oct 16, 2023
9710ef5
add option
Jason-W123 Oct 16, 2023
2a7383f
fix
Jason-W123 Oct 16, 2023
d5c9dc5
reorder
Jason-W123 Oct 16, 2023
598e212
format
Jason-W123 Oct 16, 2023
0afb684
add explain
Jason-W123 Oct 16, 2023
2ae3720
Merge branch 'master' into add-custom-events
symbolpunk Oct 16, 2023
15071da
yarn format
mahsamoosavi Oct 16, 2023
baa23e3
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
222b428
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
a4ac6f1
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
603df22
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
a7120df
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
53fe78a
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
2c2ba2b
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
cc89b30
resolve change requests
Jason-W123 Oct 17, 2023
1e8b395
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
4b5f850
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
f40f07b
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 17, 2023
5ea6460
format
Jason-W123 Oct 17, 2023
3316316
Merge branch 'add-custom-events' of https://github.com/OffchainLabs/a…
Jason-W123 Oct 17, 2023
c6d8562
Merge branch 'master' into add-custom-events
mahsamoosavi Oct 17, 2023
251688b
Merge branch 'master' into add-custom-events
symbolpunk Oct 17, 2023
7e835db
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 18, 2023
e70c618
Update arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Jason-W123 Oct 18, 2023
ee2a6e2
Merge branch 'master' into add-custom-events
symbolpunk Oct 19, 2023
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
106 changes: 105 additions & 1 deletion arbitrum-docs/launch-orbit-chain/how-tos/customize-precompile.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ To support these additional use-cases, follow the instructions described in [How

:::

There are two ways to customize your chain's precompiles:
This tutorial provides three ways to customize your chain's precompiles:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

1. Add new methods to an existing [precompile][precompile_impl_dir_link].
2. Create a new precompile.
3. Define a new event.

### Prerequisites

Expand Down Expand Up @@ -162,6 +163,109 @@ You should see something like this:
hi
```

## Option 3: Define a new event

Here we will still use the precompile `Arbsys` as an example and add a simple `Hi` event to be emitted in `SayHi` method which we added in `ArbSys.sol` contract at Option 1, first go to the [precompiles implementation][precompile_impl_dir_link] directory, and find `ArbSys.go`, edit the `ArbSys` struct:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```go
// ArbSys provides system-level functionality for interacting with L1 and understanding the call stack.
type ArbSys struct {
Address addr // 0x64
L2ToL1Tx func(ctx, mech, addr, addr, huge, huge, huge, huge, huge, huge, []byte) error
L2ToL1TxGasCost func(addr, addr, huge, huge, huge, huge, huge, huge, []byte) (uint64, error)
SendMerkleUpdate func(ctx, mech, huge, bytes32, huge) error
SendMerkleUpdateGasCost func(huge, bytes32, huge) (uint64, error)
InvalidBlockNumberError func(huge, huge) error

// deprecated event
L2ToL1Transaction func(ctx, mech, addr, addr, huge, huge, huge, huge, huge, huge, huge, []byte) error
L2ToL1TransactionGasCost func(addr, addr, huge, huge, huge, huge, huge, huge, huge, []byte) (uint64, error)

// Add your customize event here:
Hi func(ctx, mech, addr) error
// This is needed which will tell you how much gas it will cost, the param is the same as your event but without the first two (ctx, mech), the return param is always (uint64, error)
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved
HiGasCost func(addr) (uint64, error)
}
```

Then add the event to `SayHi` method:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```go
func (con *ArbSys) SayHi(c ctx, evm mech) (string, error) {
err := con.Hi(c, evm, c.caller)
return "hi", err
}
```

Now navigate to the [precompiles interface][precompile_interface_dir_link] directory, open `Arbsys.sol`, and add the required interface. Ensure that the Event name on the interface matches the name of the function you introduced in `ArbSys` struct in the previous step:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```solidity
event Hi(address caller);
```

If you want to make the param in event as `index`, just add index to solidity interface as this:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```solidity
event Hi(address indexed caller);
symbolpunk marked this conversation as resolved.
Show resolved Hide resolved
```

Now as emiting events need to cost gas, so we should remove `view` function behavior from the method:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```solidity
function sayHi() external returns(string memory);
```

Next, build Nitro by following the instructions in [How to build Nitro locally](/node-running/how-tos/build-nitro-locally). Note that if you've already built the Docker image, you still need run the last step to rebuild.
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

Run Nitro with the following command:

```shell
docker run --rm -it -v /some/local/dir/arbitrum:/home/user/.arbitrum -p 0.0.0.0:8547:8547 -p 0.0.0.0:8548:8548 @latestNitroNodeImage@ --parent-chain.connection.url=<YourParentChainUrl> --chain.id=<YourOrbitChainId> --http.api=net,web3,eth,debug --http.corsdomain=* --http.addr=0.0.0.0 --http.vhosts=*
```

:::info

Note that the instructions provided in [How to run a full node](/node-running/how-tos/running-a-full-node) **will not** work with your Orbit node. See [Command-line options (Orbit)](/launch-orbit-chain/reference/command-line-options) for Orbit-specific CLI flags.

:::

### Send the transaction and get the transaction receipt

Send transaction to `ArbSys`, note as the function is not view/pure function, we need to send transaction with gas cost:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```
cast send 0x0000000000000000000000000000000000000064 "sayHi()(string)"
```

Then we will have a transaction hash result, call `eth_getTransactionReceipt` with that transaction hash, you may get:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
Then we will have a transaction hash result, call `eth_getTransactionReceipt` with that transaction hash, you may get:
Then you will have a transaction hash result. Calling`eth_getTransactionReceipt` with the transaction hash will give you the following:

Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```
{"jsonrpc":"2.0","id":1,"result":{"blockHash":"Your_blockHash","blockNumber":"Your_blockNumber","contractAddress":null,"cumulativeGasUsed":"0x680b","effectiveGasPrice":"0x5f5e100","from":"Your_address","gasUsed":"0x680b","gasUsedForL1":"0xe35","l1BlockNumber":"l1_blockNumber","logs":[{"address":"0x0000000000000000000000000000000000000064","topics":["0xa9378d5bd800fae4d5b8d4c6712b2b64e8ecc86fdc831cb51944000fc7c8ecfa","0x000000000000000000000000{Your_address}"],"data":"0x","blockNumber":"Your_blockNumber","transactionHash":"Your_txHash","transactionIndex":"0x1","blockHash":"Your_blockHash","logIndex":"0x0","removed":false}],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000100000000000000040000000000000080004000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000004000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0x1","to":"0x0000000000000000000000000000000000000064","transactionHash":"Your_txHash","transactionIndex":"0x1","type":"0x2"}}
```

You should see `logs` field within the transaction receipt like this:
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

```
"logs":[
{
"address":"0x0000000000000000000000000000000000000064",
"topics":[
"0xa9378d5bd800fae4d5b8d4c6712b2b64e8ecc86fdc831cb51944000fc7c8ecfa",
"0x000000000000000000000000{Your_address}"
],
"data":"0x",
"blockNumber":"0x40",
"transactionHash":"{Your_txHash}",
"transactionIndex":"0x1",
"blockHash":"0x0b367d705002b3575db99354a0964c033f929f26f4442ed347e47ae43a8f28e4",
"logIndex":"0x0",
"removed":false
}
]
```

From the `logs` field, we can see the topics[0] is `0xa937..cfa`, which is the event signature of `Hi(address)`, you can check it using [4 byte directory](https://www.4byte.directory/event-signatures/?bytes_signature=0xa9378d5bd800fae4d5b8d4c6712b2b64e8ecc86fdc831cb51944000fc7c8ecfa), and topics[1] is `Your_address` which is exactly what we defined above!
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved
Jason-W123 marked this conversation as resolved.
Show resolved Hide resolved

<!-- For clarity in the code, we add here all links to github, using "link references" (First the interfaces, and below the implementations) -->
<!--
Note that nitro-contracts repository is hardcoded because atm we use the "nitro" repository for referencing precompiles for the current released nitro version (for Arb chains),
Expand Down