Skip to content

Commit

Permalink
Merge pull request #91 from Raiden1411/network
Browse files Browse the repository at this point in the history
clients: support for adding full chain config
  • Loading branch information
Raiden1411 authored Aug 27, 2024
2 parents f4e7297 + 309f253 commit 10a4033
Show file tree
Hide file tree
Showing 39 changed files with 1,441 additions and 689 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ pub fn main() !void {
const uri = try std.Uri.parse(parsed.url);
var wallet = try Wallet.init(parsed.priv_key, .{ .allocator = gpa.allocator(), .uri = uri });
var wallet = try Wallet.init(parsed.priv_key, .{
.allocator = gpa.allocator(),
.network_config = .{ .endpoint = .{ .uri = uri } },
});
defer wallet.deinit();
const message = try wallet.signEthereumMessage("Hello World");
Expand Down
2 changes: 1 addition & 1 deletion bench/benchmark.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub fn main() !void {

var client = try HttpRpcClient.init(.{
.allocator = allocator,
.uri = uri,
.network_config = .{ .endpoint = .{ .uri = uri } },
});
defer client.deinit();

Expand Down
23 changes: 2 additions & 21 deletions docs/pages/api/clients/Client.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,8 @@ error{
struct {
/// Allocator used to manage the memory arena.
allocator: Allocator
/// The base fee multiplier used to estimate the gas fees in a transaction
base_fee_multiplier: f64 = 1.2
/// The client chainId.
chain_id: ?Chains = null
/// The interval to retry the request. This will get multiplied in ns_per_ms.
pooling_interval: u64 = 2_000
/// Retry count for failed requests.
retries: u8 = 5
/// Uri for the client to connect to
uri: std.Uri
/// The network config for the client to use.
network_config: NetworkConfig
}
```

Expand Down Expand Up @@ -776,17 +768,6 @@ RPC Method: [`eth_uninstallFilter`](https://ethereum.org/en/developers/docs/apis
pub fn uninstallFilter(self: *PubClient, id: usize) !RPCResponse(bool)
```

## SwitchNetwork
Switch the client network and chainId.\
Invalidates all of the client connections and pointers.\
This will also try to automatically connect to the new RPC.

### Signature

```zig
pub fn switchNetwork(self: *PubClient, new_chain_id: Chains, new_url: []const u8) !void
```

## SendRpcRequest
Writes request to RPC server and parses the response according to the provided type.\
Handles 429 errors but not the rest.
Expand Down
13 changes: 3 additions & 10 deletions docs/pages/api/clients/IPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ error{
InvalidBlockNumberOrIndex,
InvalidBlockNumber,
ReachedMaxRetryLimit,
InvalidNetworkConfig,
} || Allocator.Error || std.fmt.ParseIntError || std.Uri.ParseError || EthereumZigErrors
```

Expand All @@ -34,24 +35,16 @@ Set of intial options for the IPC Client.
struct {
/// Allocator to use to create the ChildProcess and other allocations
allocator: Allocator
/// The base fee multiplier used to estimate the gas fees in a transaction
base_fee_multiplier: f64 = 1.2
/// The client chainId.
chain_id: ?Chains = null
/// The reader buffer growth rate
growth_rate: ?usize = null
/// The target chains config.
network_config: NetworkConfig
/// Callback function for when the connection is closed.
onClose: ?*const fn () void = null
/// Callback function for everytime an event is parsed.
onEvent: ?*const fn (args: JsonParsed(Value)) anyerror!void = null
/// Callback function for everytime an error is caught.
onError: ?*const fn (args: []const u8) anyerror!void = null
/// The path for the IPC path
path: []const u8
/// The interval to retry the connection. This will get multiplied in ns_per_ms.
pooling_interval: u64 = 2_000
/// Retry count for failed connections to server.
retries: u8 = 5
}
```

Expand Down
21 changes: 10 additions & 11 deletions docs/pages/api/clients/WebSocket.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ error{
## ConnectionErrors

```zig
error{ UnsupportedSchema, FailedToConnect, MissingUrlPath, OutOfMemory, UnspecifiedHostName }
error{
UnsupportedSchema,
FailedToConnect,
MissingUrlPath,
OutOfMemory,
UnspecifiedHostName,
InvalidNetworkConfig,
}
```

## InitErrors
Expand All @@ -44,22 +51,14 @@ ConnectionErrors || Allocator.Error || std.Thread.SpawnError
struct {
/// Allocator to use to create the ChildProcess and other allocations
allocator: Allocator
/// The uri of the server to connect to.
uri: std.Uri
/// The client chainId.
chain_id: ?Chains = null
/// The chains config
network_config: NetworkConfig
/// Callback function for when the connection is closed.
onClose: ?*const fn () void = null
/// Callback function for everytime an event is parsed.
onEvent: ?*const fn (args: JsonParsed(Value)) anyerror!void = null
/// Callback function for everytime an error is caught.
onError: ?*const fn (args: []const u8) anyerror!void = null
/// Retry count for failed connections to the server.
retries: u8 = 5
/// The interval to retry the connection. This will get multiplied in ns_per_ms.
pooling_interval: u64 = 2_000
/// The base fee multiplier used to estimate the gas fees in a transaction
base_fee_multiplier: f64 = 1.2
}
```

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/api/clients/ens/ens.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If the contracts are null it defaults to mainnet contracts.
### Signature

```zig
pub fn init(opts: InitOpts, ens_contracts: ?EnsContracts) !*ENS
pub fn init(opts: InitOpts) !*ENS
```

## Deinit
Expand Down
117 changes: 117 additions & 0 deletions docs/pages/api/clients/network.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
## OpMainNetContracts

L1 and L2 optimism contracts

### Properties

```zig
struct {
/// L2 specific.
gasPriceOracle: Address = utils.addressToBytes("0x420000000000000000000000000000000000000F") catch unreachable
/// L2 specific.
l1Block: Address = utils.addressToBytes("0x4200000000000000000000000000000000000015") catch unreachable
/// L2 specific.
l2CrossDomainMessenger: Address = utils.addressToBytes("0x4200000000000000000000000000000000000007") catch unreachable
/// L2 specific.
l2Erc721Bridge: Address = utils.addressToBytes("0x4200000000000000000000000000000000000014") catch unreachable
/// L2 specific.
l2StandartBridge: Address = utils.addressToBytes("0x4200000000000000000000000000000000000010") catch unreachable
/// L2 specific.
l2ToL1MessagePasser: Address = utils.addressToBytes("0x4200000000000000000000000000000000000016") catch unreachable
/// L1 specific. L2OutputOracleProxy contract.
l2OutputOracle: Address = utils.addressToBytes("0xdfe97868233d1aa22e815a266982f2cf17685a27") catch unreachable
/// L1 specific. OptimismPortalProxy contract.
portalAddress: Address = utils.addressToBytes("0xbEb5Fc579115071764c7423A4f12eDde41f106Ed") catch unreachable
/// L1 specific. DisputeGameFactoryProxy contract. Make sure that the chain has fault proofs enabled.
disputeGameFactory: Address = utils.addressToBytes("0x05F9613aDB30026FFd634f38e5C4dFd30a197Fa1") catch unreachable
}
```

## EnsContracts

ENS Contracts

### Properties

```zig
struct {
ensUniversalResolver: Address = utils.addressToBytes("0x8cab227b1162f03b8338331adaad7aadc83b895e") catch unreachable
}
```

## Endpoint

Possible endpoint locations.\
For http/s and ws/s use `Uri` and for IPC use `path`.\
If a uri connection is set for the IPC client it will create and error and vice versa.

```zig
union(enum) {
/// If the connections is url based use this
uri: Uri,
/// If the connection is IPC socket based use this.
path: []const u8,
}
```

## NetworkConfig

The possible configuration of a network.\
The only required field is the `endpoint` so that the client's
know where they can connect to.\
All other fields have default values and adjust them as you need.

### Properties

```zig
struct {
/// The base fee multiplier used to estimate the gas fees in a transaction
base_fee_multiplier: f64 = 1.2
/// The client chainId.
chain_id: Chains = .ethereum
/// Ens contract on configured chain.
ens_contracts: ?EnsContracts = null
/// The multicall3 contract address.
multicall_contract: Address = utils.addressToBytes("0xcA11bde05977b3631167028862bE2a173976CA11") catch unreachable
/// L1 and L2 op_stack contracts
op_stack_contracts: ?OpMainNetContracts = null
/// The interval to retry the request. This will get multiplied in ns_per_ms.
pooling_interval: u64 = 2_000
/// Retry count for failed requests.
retries: u8 = 5
/// Uri for the client to connect to
endpoint: Endpoint
}
```

### GetUriSchema
Gets the uri schema. Returns null if the endpoint
is configured to be `path`.

### Signature

```zig
pub fn getUriSchema(self: NetworkConfig) ?[]const u8
```

### GetNetworkUri
Gets the uri struct if possible. Return null if the endpoint
is configured to be `path`.

### Signature

```zig
pub fn getNetworkUri(self: NetworkConfig) ?Uri
```

## ethereum_mainet

The chain configuration for ethereum mainnet.

```zig
.{
.endpoint = .{ .uri = Uri.parse("https://cloudflare-eth.com'") catch unreachable },
.ens_contracts = .{},
}
```

2 changes: 1 addition & 1 deletion docs/pages/api/clients/optimism/clients/L1PubClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If the contracts are null it defaults to OP contracts.
### Signature

```zig
pub fn init(opts: InitOpts, op_contracts: ?OpMainNetContracts) !*L1
pub fn init(opts: InitOpts) !*L1
```

## Deinit
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/api/clients/optimism/clients/L1WalletClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Caller must deinit after use.
### Signature

```zig
pub fn init(priv_key: ?Hash, opts: InitOpts, op_contracts: ?OpMainNetContracts) !*WalletL1
pub fn init(priv_key: ?Hash, opts: InitOpts) !*WalletL1
```

## Deinit
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/api/clients/optimism/clients/L2PubClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ If the contracts are null it defaults to OP contracts.
### Signature

```zig
pub fn init(opts: InitOpts, op_contracts: ?OpMainNetContracts) !*L2
pub fn init(opts: InitOpts) !*L2
```

## Deinit
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/api/clients/optimism/clients/L2WalletClient.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Caller must deinit after use.
### Signature

```zig
pub fn init(priv_key: ?Hash, opts: InitOpts, op_contracts: ?OpMainNetContracts) !*L2Wallet
pub fn init(priv_key: ?Hash, opts: InitOpts) !*L2Wallet
```

## Deinit
Expand Down
5 changes: 4 additions & 1 deletion docs/pages/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ pub fn main() !void {
const uri = try std.Uri.parse(parsed.url);
var wallet = try Wallet.init(parsed.priv_key, .{ .allocator = gpa.allocator(), .uri = uri });
var wallet = try Wallet.init(parsed.priv_key, .{
.allocator = gpa.allocator(),
.network_config = .{ .endpoint = .{ .uri = uri } },
});
defer wallet.deinit();
const message = try wallet.signEthereumMessage("Hello World");
Expand Down
4 changes: 4 additions & 0 deletions docs/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export const sidebar = {
text: "Block",
link: "/api/types/block",
},
{
text: "Chain Config",
link: "/api/clients/network",
},
{
text: "Transaction",
link: "/api/types/transaction",
Expand Down
3 changes: 3 additions & 0 deletions docs_generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,12 @@ pub const DocsGenerator = struct {
.identifier,
.ptr_type_aligned,
.struct_init,
.struct_init_dot,
.struct_init_one,
.struct_init_comma,
.struct_init_one_comma,
.struct_init_dot_two,
.struct_init_dot_two_comma,
.@"catch",
.field_access,
.tagged_union_two_trailing,
Expand Down
4 changes: 3 additions & 1 deletion examples/contract/contract.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ pub fn main() !void {
.abi = abi_parsed.value,
.wallet_opts = .{
.allocator = gpa.allocator(),
.uri = uri,
.network_config = .{
.endpoint = .{ .uri = uri },
},
},
});
defer contract.deinit();
Expand Down
8 changes: 5 additions & 3 deletions examples/transfer/transfer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ pub fn main() !void {

var wallet = try Wallet.init(parsed.priv_key, .{
.allocator = gpa.allocator(),
.uri = uri,
.chain_id = .sepolia,
.base_fee_multiplier = 3.2,
.network_config = .{
.endpoint = .{ .uri = uri },
.chain_id = .sepolia,
.base_fee_multiplier = 3.2,
},
});
defer wallet.deinit();

Expand Down
5 changes: 4 additions & 1 deletion examples/wallet/wallet.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn main() !void {

const uri = try std.Uri.parse(parsed.url);

var wallet = try Wallet.init(parsed.priv_key, .{ .allocator = gpa.allocator(), .uri = uri });
var wallet = try Wallet.init(parsed.priv_key, .{
.allocator = gpa.allocator(),
.network_config = .{ .endpoint = .{ .uri = uri } },
});
defer wallet.deinit();

const message = try wallet.signEthereumMessage("Hello World");
Expand Down
5 changes: 4 additions & 1 deletion examples/watch/logs.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn main() !void {

const uri = try std.Uri.parse(parsed.url);

var socket = try WebSocket.init(.{ .uri = uri, .allocator = gpa.allocator() });
var socket = try WebSocket.init(.{
.network_config = .{ .endpoint = .{ .uri = uri } },
.allocator = gpa.allocator(),
});
defer socket.deinit();

const id = try socket.watchLogs(.{
Expand Down
5 changes: 4 additions & 1 deletion examples/watch/watch.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ pub fn main() !void {

const uri = try std.Uri.parse(parsed.url);

var socket = try WebSocket.init(.{ .uri = uri, .allocator = gpa.allocator() });
var socket = try WebSocket.init(.{
.network_config = .{ .endpoint = .{ .uri = uri } },
.allocator = gpa.allocator(),
});
defer socket.deinit();

const id = try socket.watchTransactions();
Expand Down
Loading

0 comments on commit 10a4033

Please sign in to comment.