Skip to content

Commit 25236c9

Browse files
author
Matthias Zimmermann
committed
amend arkiv.transfer_eth to allow for NamedAccount as recipient
1 parent e83ba43 commit 25236c9

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

.env.testing

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Copy this to .env and modify as needed
33

44
# External Node Configuration (uncomment to use external node instead of testcontainers)
5-
RPC_URL=https://intproxy.hoodi.arkiv.network/rpc
6-
WS_URL=wss://intproxy.hoodi.arkiv.network/rpc/ws
5+
RPC_URL=https://kaolin.hoodi.arkiv.network/rpc
6+
WS_URL=wss://kaolin.hoodi.arkiv.network/rpc/ws
77

88
# External Wallet Configuration (required when using external node)
99
WALLET_FILE_1=wallet_alice.json

src/arkiv/module.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from web3 import Web3
99
from web3.types import TxParams, TxReceipt
1010

11+
from arkiv.account import NamedAccount
12+
1113
from .contract import EVENTS_ABI, FUNCTIONS_ABI, STORAGE_ADDRESS
1214
from .types import (
1315
ALL,
@@ -174,7 +176,12 @@ def entity_exists(self, entity_key: EntityKey) -> bool:
174176
except Exception:
175177
return False
176178

177-
def transfer_eth(self, to: ChecksumAddress, amount_wei: int) -> TxHash:
179+
def transfer_eth(
180+
self,
181+
to: NamedAccount | ChecksumAddress,
182+
amount_wei: int,
183+
wait_for_confirmation: bool = True,
184+
) -> TxHash:
178185
"""
179186
Transfer ETH to the given address.
180187
@@ -185,14 +192,28 @@ def transfer_eth(self, to: ChecksumAddress, amount_wei: int) -> TxHash:
185192
Returns:
186193
Transaction hash of the transfer
187194
"""
195+
to_address: ChecksumAddress = to.address if isinstance(to, NamedAccount) else to
188196
tx_hash_bytes = self.client.eth.send_transaction(
189197
{
190-
"to": to,
198+
"to": to_address,
191199
"value": Web3.to_wei(amount_wei, "wei"),
192200
"gas": 21000, # Standard gas for ETH transfer
193201
}
194202
)
195203
tx_hash = TxHash(HexStr(tx_hash_bytes.to_0x_hex()))
204+
logger.info(f"TX sent: Transferring {amount_wei} wei to {to}: {tx_hash}")
205+
206+
if wait_for_confirmation:
207+
logger.info("Waiting for TX confirmation ...")
208+
tx_receipt: TxReceipt = self.client.eth.wait_for_transaction_receipt(
209+
tx_hash
210+
)
211+
tx_status: int = tx_receipt["status"]
212+
if tx_status != TX_SUCCESS:
213+
raise RuntimeError(f"Transaction failed with status {tx_status}")
214+
215+
logger.info(f"TX confirmed: {tx_receipt}")
216+
196217
return tx_hash
197218

198219
def get_entity(self, entity_key: EntityKey, fields: int = ALL) -> Entity:

tests/test_arkiv_fixture.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,49 @@ def test_arkiv_accounts_are_funded(
8181
assert account_2_balance > 0, "Fixture account 2 should be funded"
8282

8383
logger.info("Arkiv fixture accounts are funded (balances > 0)")
84+
85+
86+
def test_arkiv_transfer_eth_account(
87+
arkiv_client_http: Arkiv, account_1: NamedAccount, account_2: NamedAccount
88+
) -> None:
89+
"""Test that ETH (GLM) transfer works."""
90+
account_1_balance_before = arkiv_client_http.eth.get_balance(account_1.address)
91+
account_2_balance_before = arkiv_client_http.eth.get_balance(account_2.address)
92+
amount = 42
93+
94+
arkiv_client_http.arkiv.transfer_eth(account_2, amount)
95+
96+
account_1_balance_after = arkiv_client_http.eth.get_balance(account_1.address)
97+
account_2_balance_after = arkiv_client_http.eth.get_balance(account_2.address)
98+
99+
assert account_2_balance_after == account_2_balance_before + amount, (
100+
"Account 2 balance should increase by transfer amount"
101+
)
102+
assert account_1_balance_after <= account_1_balance_before - amount, (
103+
"Account 1 balance should decrease by transfer amount"
104+
)
105+
106+
logger.info("Arkiv ETH transfer between accounts succeeded (to: NamedAccount)")
107+
108+
109+
def test_arkiv_transfer_eth_address(
110+
arkiv_client_http: Arkiv, account_1: NamedAccount, account_2: NamedAccount
111+
) -> None:
112+
"""Test that ETH (GLM) transfer works."""
113+
account_1_balance_before = arkiv_client_http.eth.get_balance(account_1.address)
114+
account_2_balance_before = arkiv_client_http.eth.get_balance(account_2.address)
115+
amount = 42
116+
117+
arkiv_client_http.arkiv.transfer_eth(account_2.address, amount)
118+
119+
account_1_balance_after = arkiv_client_http.eth.get_balance(account_1.address)
120+
account_2_balance_after = arkiv_client_http.eth.get_balance(account_2.address)
121+
122+
assert account_2_balance_after == account_2_balance_before + amount, (
123+
"Account 2 balance should increase by transfer amount"
124+
)
125+
assert account_1_balance_after <= account_1_balance_before - amount, (
126+
"Account 1 balance should decrease by transfer amount"
127+
)
128+
129+
logger.info("Arkiv ETH transfer between accounts succeeded (to: checksum address)")

0 commit comments

Comments
 (0)