Skip to content

Commit

Permalink
Fix: replace deprecated pkg_resources.resource_string
Browse files Browse the repository at this point in the history
Replace usage in chains/ethereum.py by `importlib.resources.load_text`.
  • Loading branch information
odesenfans committed Nov 5, 2023
1 parent 62a1f7e commit e717fe4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/aleph/chains/ethereum.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import asyncio
import functools
import importlib.resources
import json
import logging
from typing import AsyncIterator, Dict, Tuple

import pkg_resources
from aleph_message.models import Chain
from configmanager import Config
from eth_account import Account
Expand Down Expand Up @@ -49,15 +49,15 @@ def get_web3(config) -> Web3:

async def get_contract_abi():
return json.loads(
pkg_resources.resource_string(
"aleph.chains", "assets/ethereum_sc_abi.json"
).decode("utf-8")
importlib.resources.read_text(
"aleph.chains.assets", "ethereum_sc_abi.json", encoding="utf-8"
)
)


async def get_contract(config, web3: Web3):
return web3.eth.contract(
config.ethereum.sync_contract.value, abi=await get_contract_abi()
address=config.ethereum.sync_contract.value, abi=await get_contract_abi()
)


Expand Down
18 changes: 18 additions & 0 deletions tests/chains/test_ethereum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest
from configmanager import Config
from web3 import Web3

from aleph.chains.ethereum import get_contract


@pytest.fixture
def web3():
return Web3()


@pytest.mark.asyncio
async def test_get_contract(mock_config: Config, web3: Web3):
contract = await get_contract(config=mock_config, web3=web3)
# The type hint provided by the web3 library is clearly wrong. This is a simple check
# to ensure that we get a proper web3 object. Improve as needed.
assert contract.w3 == web3

0 comments on commit e717fe4

Please sign in to comment.