|
| 1 | +# Copyright (c) 2016-2025 The Ruby-Eth Contributors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Provides the {Eth} module. |
| 16 | +module Eth |
| 17 | + |
| 18 | + # Provides classes to access smart contracts |
| 19 | + module Contract |
| 20 | + |
| 21 | + # Provides Ethereum consensus deposit contract helpers |
| 22 | + module Deposit |
| 23 | + extend self |
| 24 | + |
| 25 | + # Default address of the deposit contract on mainnet |
| 26 | + DEFAULT_ADDRESS = Address.new("0x00000000219ab540356cBB839Cbe05303d7705Fa").freeze |
| 27 | + |
| 28 | + # Load deposit contract ABI |
| 29 | + ABI = JSON.parse( |
| 30 | + File.read(File.join(File.dirname(__FILE__), "../../abi/deposit_contract.json")) |
| 31 | + ) |
| 32 | + |
| 33 | + # Returns a contract wrapper for the deposit contract |
| 34 | + # |
| 35 | + # @param address [String] address of the deposit contract |
| 36 | + # @return [Eth::Contract] contract instance |
| 37 | + def contract(address = DEFAULT_ADDRESS) |
| 38 | + Contract.from_abi( |
| 39 | + name: "DepositContract", |
| 40 | + address: address, |
| 41 | + abi: ABI, |
| 42 | + ) |
| 43 | + end |
| 44 | + |
| 45 | + # Submits a deposit to the deposit contract |
| 46 | + # |
| 47 | + # @param client [Eth::Client] client used to submit the deposit |
| 48 | + # @param pubkey [String] BLS12-381 public key |
| 49 | + # @param withdrawal_credentials [String] withdrawal credentials |
| 50 | + # @param signature [String] BLS12-381 signature |
| 51 | + # @param deposit_data_root [String] deposit data root |
| 52 | + # @param value [Integer] amount to deposit in wei |
| 53 | + # @param address [String] deposit contract address |
| 54 | + # @param kwargs [Hash] additional transaction options |
| 55 | + # @return [Object] result of transaction submission |
| 56 | + def deposit(client, pubkey, withdrawal_credentials, signature, deposit_data_root, value:, address: DEFAULT_ADDRESS, **kwargs) |
| 57 | + client.transact( |
| 58 | + contract(address), |
| 59 | + "deposit", |
| 60 | + pubkey, |
| 61 | + withdrawal_credentials, |
| 62 | + signature, |
| 63 | + deposit_data_root, |
| 64 | + **kwargs.merge(tx_value: value), |
| 65 | + ) |
| 66 | + end |
| 67 | + |
| 68 | + # Parses a DepositEvent log entry |
| 69 | + # |
| 70 | + # @param log [Hash] log entry as returned by RPC |
| 71 | + # @return [Hash] decoded event parameters with hex values |
| 72 | + def parse_deposit_event(log) |
| 73 | + evt = contract(log["address"]).events.find { |e| e.name == "DepositEvent" } |
| 74 | + values = evt.decode_params(log["topics"], log["data"]) |
| 75 | + values = values.transform_keys(&:to_sym) |
| 76 | + values.transform_values { |v| Util.bin_to_prefixed_hex(v) } |
| 77 | + end |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments