From a28e7fa3a02d36bff2c16bfca6b9eaabed7bdfd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C4=83lina=20Cenan?= Date: Mon, 28 Aug 2023 16:51:05 +0300 Subject: [PATCH] Upgrade web3.py (#1467) * Upgrade web3.py version and adapt some function names. * More function name replacements. * More upgrades from dependabot branches. --- READMEs/df.md | 4 +- READMEs/key-value-private.md | 6 +-- READMEs/profile-nfts-flow.md | 6 +-- ocean_lib/data_provider/base.py | 2 +- .../test/test_data_service_provider.py | 6 +-- ocean_lib/example_config.py | 2 + ocean_lib/models/data_nft.py | 8 ++-- ocean_lib/models/data_nft_factory.py | 24 +++++------ ocean_lib/models/datatoken_base.py | 10 ++--- ocean_lib/models/test/test_data_nft.py | 40 +++++++++---------- .../models/test/test_data_nft_factory.py | 8 ++-- ocean_lib/models/test/test_datatoken.py | 28 ++++++------- ocean_lib/models/test/test_exchange_fees.py | 2 +- ocean_lib/models/test/test_exchange_main.py | 4 +- ocean_lib/models/test/test_factory_router.py | 2 +- ocean_lib/models/ve/test/test_ve_allocate.py | 4 +- ocean_lib/models/ve/test/test_ve_ocean.py | 10 ++--- ocean_lib/ocean/mint_fake_ocean.py | 2 +- ocean_lib/ocean/test/test_crypto.py | 2 +- ocean_lib/ocean/test/test_util.py | 8 ++-- ocean_lib/ocean/util.py | 14 ++++--- ocean_lib/services/service.py | 5 ++- ocean_lib/web3_internal/contract_base.py | 8 ++-- ocean_lib/web3_internal/contract_utils.py | 6 +-- .../web3_internal/test/test_contract_utils.py | 8 ++-- ocean_lib/web3_internal/test/test_wallet.py | 6 ++- ocean_lib/web3_internal/utils.py | 22 +++++----- setup.py | 10 ++--- tests/integration/ganache/test_market_flow.py | 2 +- tests/integration/remote/util.py | 2 +- tests/resources/helper_functions.py | 16 ++++---- 31 files changed, 143 insertions(+), 134 deletions(-) diff --git a/READMEs/df.md b/READMEs/df.md index b0add95ce..3ff7836ed 100644 --- a/READMEs/df.md +++ b/READMEs/df.md @@ -43,7 +43,7 @@ Now, let's lock OCEAN for veOCEAN. In the same Python console: # simulate passage of time, until next Thursday, the start of DF(X) web3 = ocean.config_dict["web3_instance"] provider = web3.provider -latest_block = web3.eth.getBlock("latest") +latest_block = web3.eth.get_block("latest") WEEK = 7 * 86400 # seconds in a week t0 = latest_block.timestamp @@ -139,7 +139,7 @@ In the same Python console: #simulate passage of time, until next Thursday, which is the start of DF(X+1) WEEK = 7 * 86400 # seconds in a week -latest_block = web3.eth.getBlock("latest") +latest_block = web3.eth.get_block("latest") t0 = latest_block.timestamp t1 = t0 // WEEK * WEEK + WEEK t2 = t1 + WEEK diff --git a/READMEs/key-value-private.md b/READMEs/key-value-private.md index 97d39d756..7ca9e1fa2 100644 --- a/READMEs/key-value-private.md +++ b/READMEs/key-value-private.md @@ -59,7 +59,7 @@ model_value = "" # Compute a symmetric key: unique to this (nft, nft field) and (your priv key) # Therefore you can calculate it anytime from ocean_lib.ocean import crypto -symkey = crypto.calc_symkey(data_nft.address + model_label + alice.privateKey.hex()) +symkey = crypto.calc_symkey(data_nft.address + model_label + alice._private_key.hex()) # Symmetrically encrypt AI model model_value_symenc = crypto.sym_encrypt(model_value, symkey) @@ -77,7 +77,7 @@ There are many possible ways for Alice to share the symkey to Bob. Here, Alice s In the Python console: ```python # Get Bob's public key. There are various ways; see appendix. -pubkey = crypto.calc_pubkey(bob.privateKey.hex()) +pubkey = crypto.calc_pubkey(bob._private_key.hex()) # Asymmetrically encrypt symkey, using Bob's public key symkey_asymenc = crypto.asym_encrypt(symkey, pubkey) @@ -97,7 +97,7 @@ In the Python console: symkey_asymenc2 = data_nft.get_data("symkey") # Asymetrically decrypt symkey, with Bob's private key -symkey2 = crypto.asym_decrypt(symkey_asymenc2, bob.privateKey.hex()) +symkey2 = crypto.asym_decrypt(symkey_asymenc2, bob._private_key.hex()) ``` ## 6. Retrieve from chain & decrypt AI model diff --git a/READMEs/profile-nfts-flow.md b/READMEs/profile-nfts-flow.md index c8749a9e6..c467abe2a 100644 --- a/READMEs/profile-nfts-flow.md +++ b/READMEs/profile-nfts-flow.md @@ -56,10 +56,10 @@ profiledata_name_hash = Web3.keccak(text=profiledata_name) preimage = data_nft.address + profiledata_name preimage = sha256(preimage.encode('utf-8')).hexdigest() prefix = "\x19Ethereum Signed Message:\n32" -msg = Web3.solidityKeccak( - ["bytes", "bytes"], [Web3.toBytes(text=prefix), Web3.toBytes(text=preimage)] +msg = Web3.solidity_keccak( + ["bytes", "bytes"], [Web3.to_bytes(text=prefix), Web3.to_bytes(text=preimage)] ) -signed = sign_with_key(msg, alice.privateKey.hex()) +signed = sign_with_key(msg, alice._private_key.hex()) symkey = b64encode(str(signed).encode('ascii'))[:43] + b'=' # bytes # Prep value for setter diff --git a/ocean_lib/data_provider/base.py b/ocean_lib/data_provider/base.py index 1e52cc62e..cda0ffce8 100644 --- a/ocean_lib/data_provider/base.py +++ b/ocean_lib/data_provider/base.py @@ -73,7 +73,7 @@ def sign_message( # if isinstance(wallet, ClefAccount): # return nonce, str(sign_with_clef(f"{msg}{nonce}", wallet)) - return nonce, str(sign_with_key(f"{msg}{nonce}", wallet.privateKey.hex())) + return nonce, str(sign_with_key(f"{msg}{nonce}", wallet._private_key.hex())) @staticmethod @enforce_types diff --git a/ocean_lib/data_provider/test/test_data_service_provider.py b/ocean_lib/data_provider/test/test_data_service_provider.py index 7f22be814..e31567a0d 100644 --- a/ocean_lib/data_provider/test/test_data_service_provider.py +++ b/ocean_lib/data_provider/test/test_data_service_provider.py @@ -194,7 +194,7 @@ def test_delete_job_result(provider_wallet): @pytest.mark.integration def test_encrypt(provider_wallet, file1, file2): """Tests successful encrypt job.""" - key = provider_wallet.privateKey.hex() + key = provider_wallet._private_key.hex() # Encrypt file objects res = {"files": [file1.to_dict(), file2.to_dict()]} result = DataEncryptor.encrypt(res, DEFAULT_PROVIDER_URL, 8996) @@ -204,7 +204,7 @@ def test_encrypt(provider_wallet, file1, file2): assert encrypted_files.startswith("0x") if isinstance(encrypted_files, str): - encrypted_files = Web3.toBytes(hexstr=encrypted_files) + encrypted_files = Web3.to_bytes(hexstr=encrypted_files) decrypted_document = ecies.decrypt(key, encrypted_files) decrypted_document_string = decrypted_document.decode("utf-8") assert decrypted_document_string == json.dumps(res, separators=(",", ":")) @@ -218,7 +218,7 @@ def test_encrypt(provider_wallet, file1, file2): assert result.content.decode("utf-8").startswith("0x") if isinstance(encrypted_document, str): - encrypted_document = Web3.toBytes(hexstr=encrypted_document) + encrypted_document = Web3.to_bytes(hexstr=encrypted_document) decrypted_document = ecies.decrypt(key, encrypted_document) decrypted_document_string = decrypted_document.decode("utf-8") assert decrypted_document_string == test_string diff --git a/ocean_lib/example_config.py b/ocean_lib/example_config.py index 3b93b2373..dd89b4c3f 100644 --- a/ocean_lib/example_config.py +++ b/ocean_lib/example_config.py @@ -126,4 +126,6 @@ def get_web3(network_url: str) -> Web3: web3.middleware_onion.inject(geth_poa_middleware, layer=0) + web3.strict_bytes_type_checking = False + return web3 diff --git a/ocean_lib/models/data_nft.py b/ocean_lib/models/data_nft.py index e6b07be90..5df9ec20f 100644 --- a/ocean_lib/models/data_nft.py +++ b/ocean_lib/models/data_nft.py @@ -376,9 +376,11 @@ def deploy_contract(self, config_dict, tx_dict) -> DataNFT: tx_dict, ) - registered_event = data_nft_factory.contract.events.NFTCreated().processReceipt( - receipt, errors=DISCARD - )[0] + registered_event = ( + data_nft_factory.contract.events.NFTCreated().process_receipt( + receipt, errors=DISCARD + )[0] + ) data_nft_address = registered_event.args.newTokenAddress return DataNFT(config_dict, data_nft_address) diff --git a/ocean_lib/models/data_nft_factory.py b/ocean_lib/models/data_nft_factory.py index 9cf8f330e..ff79e6e61 100644 --- a/ocean_lib/models/data_nft_factory.py +++ b/ocean_lib/models/data_nft_factory.py @@ -171,13 +171,13 @@ def create_with_erc20( tx_dict, ) - registered_nft_event = self.contract.events.NFTCreated().processReceipt( + registered_nft_event = self.contract.events.NFTCreated().process_receipt( receipt, errors=DISCARD )[0] data_nft_address = registered_nft_event.args.newTokenAddress data_nft_token = DataNFT(self.config_dict, data_nft_address) - registered_token_event = self.contract.events.TokenCreated().processReceipt( + registered_token_event = self.contract.events.TokenCreated().process_receipt( receipt, errors=DISCARD )[0] datatoken_address = registered_token_event.args.newTokenAddress @@ -228,22 +228,22 @@ def create_with_erc20_and_fixed_rate( tx_dict, ) - registered_nft_event = self.contract.events.NFTCreated().processReceipt( + registered_nft_event = self.contract.events.NFTCreated().process_receipt( receipt, errors=DISCARD )[0] data_nft_address = registered_nft_event.args.newTokenAddress data_nft_token = DataNFT(self.config_dict, data_nft_address) - registered_token_event = self.contract.events.TokenCreated().processReceipt( + registered_token_event = self.contract.events.TokenCreated().process_receipt( receipt, errors=DISCARD )[0] datatoken_address = registered_token_event.args.newTokenAddress datatoken = DatatokenBase.get_typed(self.config_dict, datatoken_address) registered_fixed_rate_event = ( - self.contract.events.NewFixedRate().processReceipt(receipt, errors=DISCARD)[ - 0 - ] + self.contract.events.NewFixedRate().process_receipt( + receipt, errors=DISCARD + )[0] ) exchange_id = registered_fixed_rate_event.args.exchangeId fixed_rate_exchange = FixedRateExchange( @@ -296,20 +296,20 @@ def create_with_erc20_and_dispenser( tx_dict, ) - registered_nft_event = self.contract.events.NFTCreated().processReceipt( + registered_nft_event = self.contract.events.NFTCreated().process_receipt( receipt, errors=DISCARD )[0] data_nft_address = registered_nft_event.args.newTokenAddress data_nft_token = DataNFT(self.config_dict, data_nft_address) - registered_token_event = self.contract.events.TokenCreated().processReceipt( + registered_token_event = self.contract.events.TokenCreated().process_receipt( receipt, errors=DISCARD )[0] datatoken_address = registered_token_event.args.newTokenAddress datatoken = DatatokenBase.get_typed(self.config_dict, datatoken_address) registered_dispenser_event = ( - self.contract.events.DispenserCreated().processReceipt( + self.contract.events.DispenserCreated().process_receipt( receipt, errors=DISCARD )[0] ) @@ -352,7 +352,7 @@ def create_with_metadata( ), tx_dict, ) - registered_nft_event = self.contract.events.NFTCreated().processReceipt( + registered_nft_event = self.contract.events.NFTCreated().process_receipt( receipt, errors=DISCARD )[0] data_nft_address = registered_nft_event.args.newTokenAddress @@ -382,7 +382,7 @@ def search_exchange_by_datatoken( @enforce_types def get_token_address(self, receipt): - event = self.contract.events.NFTCreated().processReceipt( + event = self.contract.events.NFTCreated().process_receipt( receipt, errors=DISCARD )[0] return event.args.newTokenAddress diff --git a/ocean_lib/models/datatoken_base.py b/ocean_lib/models/datatoken_base.py index 65da9ca14..3f47890ea 100644 --- a/ocean_lib/models/datatoken_base.py +++ b/ocean_lib/models/datatoken_base.py @@ -37,9 +37,9 @@ def __init__( amount: Optional[int] = 0, ): self.address = ( - Web3.toChecksumAddress(address.lower()) if address else ZERO_ADDRESS + Web3.to_checksum_address(address.lower()) if address else ZERO_ADDRESS ) - self.token = Web3.toChecksumAddress(token.lower()) if token else ZERO_ADDRESS + self.token = Web3.to_checksum_address(token.lower()) if token else ZERO_ADDRESS self.amount = amount @@ -255,7 +255,7 @@ def get_start_order_logs( for log in event_filter.get_all_entries(): receipt = web3.eth.wait_for_transaction_receipt(log.transactionHash) - processed_events = self.contract.events.OrderStarted().processReceipt( + processed_events = self.contract.events.OrderStarted().process_receipt( receipt, errors=DISCARD ) for processed_event in processed_events: @@ -302,7 +302,7 @@ def create_exchange( tx = self.createFixedRate(*(args_tup + (tx_dict,))) - event = self.contract.events.NewFixedRate().processReceipt(tx, errors=DISCARD)[ + event = self.contract.events.NewFixedRate().process_receipt(tx, errors=DISCARD)[ 0 ] exchange_id = event.args.exchangeId @@ -444,7 +444,7 @@ def get_from_pricing_schema_and_order(self, *args, **kwargs): wallet = kwargs["tx_dict"]["from"] amt_needed = exchange.BT_needed( - Web3.toWei(1, "ether"), consume_market_fees.amount + Web3.to_wei(1, "ether"), consume_market_fees.amount ) base_token = DatatokenBase.get_typed( exchange._FRE.config_dict, exchange.details.base_token diff --git a/ocean_lib/models/test/test_data_nft.py b/ocean_lib/models/test/test_data_nft.py index 02a145a6c..d839fea13 100644 --- a/ocean_lib/models/test/test_data_nft.py +++ b/ocean_lib/models/test/test_data_nft.py @@ -131,7 +131,7 @@ def test_permissions( 0, consumer_wallet.address, 10, - Web3.toHex(text="SomeData"), + Web3.to_hex(text="SomeData"), {"from": another_consumer_wallet}, ) @@ -141,7 +141,7 @@ def test_permissions( 0, consumer_wallet.address, 10, - Web3.toHex(text="SomeData"), + Web3.to_hex(text="SomeData"), {"from": consumer_wallet}, ) assert tx, "Could not execute call to consumer." @@ -235,14 +235,14 @@ def test_success_update_metadata(publisher_wallet, consumer_wallet, config, data 1, "http://myprovider:8030", b"0x123", - Web3.toBytes(hexstr=BLOB), - Web3.toBytes(hexstr=BLOB), - Web3.toBytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), [], {"from": consumer_wallet}, ) - event = data_nft.contract.events.MetadataCreated().processReceipt( + event = data_nft.contract.events.MetadataCreated().process_receipt( receipt, errors=DISCARD )[0] assert event.args.decryptorUrl == "http://myprovider:8030" @@ -255,14 +255,14 @@ def test_success_update_metadata(publisher_wallet, consumer_wallet, config, data 1, "http://foourl", b"0x123", - Web3.toBytes(hexstr=BLOB), - Web3.toBytes(hexstr=BLOB), - Web3.toBytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), [], {"from": consumer_wallet}, ) - event = data_nft.contract.events.MetadataUpdated().processReceipt( + event = data_nft.contract.events.MetadataUpdated().process_receipt( receipt, errors=DISCARD )[0] assert event.args.decryptorUrl == "http://foourl" @@ -277,9 +277,9 @@ def test_success_update_metadata(publisher_wallet, consumer_wallet, config, data 1, "http://foourl", b"0x123", - Web3.toBytes(hexstr=BLOB), - Web3.toBytes(hexstr=BLOB), - Web3.toBytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), + Web3.to_bytes(hexstr=BLOB), 1, "https://anothernewurl.com/nft/", [], @@ -287,13 +287,13 @@ def test_success_update_metadata(publisher_wallet, consumer_wallet, config, data {"from": publisher_wallet}, ) - event = data_nft.contract.events.TokenURIUpdate().processReceipt( + event = data_nft.contract.events.TokenURIUpdate().process_receipt( receipt, errors=DISCARD )[0] assert event.args.tokenURI == "https://anothernewurl.com/nft/" assert event.args.updatedBy == publisher_wallet.address - event = data_nft.contract.events.MetadataUpdated().processReceipt( + event = data_nft.contract.events.MetadataUpdated().process_receipt( receipt, errors=DISCARD )[0] assert event.args.decryptorUrl == "http://foourl" @@ -482,7 +482,7 @@ def test_erc721_datatoken_functions( "https://newurl.com/nft/", {"from": publisher_wallet}, ) - registered_event = data_nft.contract.events.TokenURIUpdate().processReceipt( + registered_event = data_nft.contract.events.TokenURIUpdate().process_receipt( receipt, errors=DISCARD )[0] @@ -595,7 +595,7 @@ def test_transfer_nft( 1, {"from": publisher_wallet}, ) - transfer_event = data_nft.contract.events.Transfer().processReceipt( + transfer_event = data_nft.contract.events.Transfer().process_receipt( receipt, errors=DISCARD )[0] @@ -615,7 +615,7 @@ def test_transfer_nft( 1, {"from": publisher_wallet}, ) - transfer_event = data_nft.contract.events.Transfer().processReceipt( + transfer_event = data_nft.contract.events.Transfer().process_receipt( receipt, errors=DISCARD )[0] @@ -653,7 +653,7 @@ def test_transfer_nft( ) set_publishing_fee_event = ( - datatoken.contract.events.PublishMarketFeeChanged().processReceipt( + datatoken.contract.events.PublishMarketFeeChanged().process_receipt( receipt, errors=DISCARD )[0] ) @@ -701,7 +701,7 @@ def test_nft_transfer_with_fre( {"from": publisher_wallet}, ) - transfer_event = data_nft.contract.events.Transfer().processReceipt( + transfer_event = data_nft.contract.events.Transfer().process_receipt( receipt, errors=DISCARD )[0] diff --git a/ocean_lib/models/test/test_data_nft_factory.py b/ocean_lib/models/test/test_data_nft_factory.py index c8e713898..c1ffe5eb5 100644 --- a/ocean_lib/models/test/test_data_nft_factory.py +++ b/ocean_lib/models/test/test_data_nft_factory.py @@ -169,7 +169,7 @@ def test_combo_functions( metadata_decryptor_url="http://myprovider:8030", metadata_decryptor_address=b"0x123", metadata_flags=bytes(0), - metadata_data=Web3.toHex(text="my cool metadata."), + metadata_data=Web3.to_hex(text="my cool metadata."), metadata_data_hash=create_checksum("my cool metadata."), metadata_proofs=[], tx_dict={"from": publisher_wallet}, @@ -257,7 +257,7 @@ def test_start_multiple_order( # provider_data = json.dumps({"timeout": 0}, separators=(",", ":")) provider_data = b"\x00" - message = Web3.solidityKeccak( + message = Web3.solidity_keccak( ["bytes", "address", "address", "uint256", "uint256"], [ provider_data, @@ -298,7 +298,7 @@ def test_start_multiple_order( ) registered_erc20_start_order_event = ( - datatoken.contract.events.OrderStarted().processReceipt( + datatoken.contract.events.OrderStarted().process_receipt( receipt, errors=DISCARD )[0] ) @@ -327,7 +327,7 @@ def test_start_multiple_order( [reuse_order], {"from": consumer_wallet} ) - reused_event = datatoken.contract.events.OrderReused().processReceipt( + reused_event = datatoken.contract.events.OrderReused().process_receipt( receipt, errors=DISCARD )[0] assert reused_event diff --git a/ocean_lib/models/test/test_datatoken.py b/ocean_lib/models/test/test_datatoken.py index e423d2120..cbf10d7e5 100644 --- a/ocean_lib/models/test/test_datatoken.py +++ b/ocean_lib/models/test/test_datatoken.py @@ -79,12 +79,12 @@ def test_main( ] # Should succeed to setData if erc20Deployer - value = Web3.toHex(text="SomeData") + value = Web3.to_hex(text="SomeData") key = Web3.keccak(hexstr=datatoken.address) datatoken.setData(value, {"from": publisher_wallet}) - assert Web3.toHex(data_nft.getData(key)) == value + assert Web3.to_hex(data_nft.getData(key)) == value # Should succeed to call cleanPermissions if NFTOwner datatoken.cleanPermissions({"from": publisher_wallet}) @@ -134,7 +134,7 @@ def test_start_order(config, publisher_wallet, consumer_wallet, data_NFT_and_DT) provider_fee_address = publisher_wallet.address provider_data = provider_fees["providerData"] - provider_message = Web3.solidityKeccak( + provider_message = Web3.solidity_keccak( ["bytes32", "bytes"], [receipt.transactionHash, provider_data], ) @@ -142,9 +142,9 @@ def test_start_order(config, publisher_wallet, consumer_wallet, data_NFT_and_DT) provider_fee_address, data=provider_message ) - message = Web3.solidityKeccak( + message = Web3.solidity_keccak( ["bytes"], - [Web3.toHex(Web3.toBytes(text="12345"))], + [Web3.to_hex(Web3.to_bytes(text="12345"))], ) consumer_signed = config["web3_instance"].eth.sign( consumer_wallet.address, data=message @@ -154,12 +154,12 @@ def test_start_order(config, publisher_wallet, consumer_wallet, data_NFT_and_DT) receipt.transactionHash, provider_data, provider_signed, - Web3.toHex(Web3.toBytes(text="12345")), + Web3.to_hex(Web3.to_bytes(text="12345")), consumer_signed, consumer_wallet.address, {"from": publisher_wallet}, ) - executed_event = datatoken.contract.events.OrderExecuted().processReceipt( + executed_event = datatoken.contract.events.OrderExecuted().process_receipt( receipt_interm, errors=DISCARD )[0] assert executed_event.args.orderTxId == receipt.transactionHash @@ -174,15 +174,15 @@ def test_start_order(config, publisher_wallet, consumer_wallet, data_NFT_and_DT) receipt.transactionHash, provider_data, provider_signed, - Web3.toHex(Web3.toBytes(text="12345")), + Web3.to_hex(Web3.to_bytes(text="12345")), consumer_signed, consumer_wallet.address, {"from": publisher_wallet}, ) - message = Web3.solidityKeccak( + message = Web3.solidity_keccak( ["bytes"], - [Web3.toHex(Web3.toBytes(text="12345"))], + [Web3.to_hex(Web3.to_bytes(text="12345"))], ) consumer_signed = config["web3_instance"].eth.sign( consumer_wallet.address, data=message @@ -193,7 +193,7 @@ def test_start_order(config, publisher_wallet, consumer_wallet, data_NFT_and_DT) receipt.transactionHash, provider_data, consumer_signed, - Web3.toHex(Web3.toBytes(text="12345")), + Web3.to_hex(Web3.to_bytes(text="12345")), consumer_signed, consumer_wallet.address, {"from": publisher_wallet}, @@ -205,14 +205,14 @@ def test_start_order(config, publisher_wallet, consumer_wallet, data_NFT_and_DT) provider_fees=provider_fees, tx_dict={"from": publisher_wallet}, ) - reused_event = datatoken.contract.events.OrderReused().processReceipt( + reused_event = datatoken.contract.events.OrderReused().process_receipt( receipt_interm, errors=DISCARD )[0] assert reused_event, "Cannot find OrderReused event" assert reused_event.args.orderTxId == receipt.transactionHash assert reused_event.args.caller == publisher_wallet.address - provider_fee_event = datatoken.contract.events.ProviderFee().processReceipt( + provider_fee_event = datatoken.contract.events.ProviderFee().process_receipt( receipt_interm, errors=DISCARD )[0] assert provider_fee_event, "Cannot find ProviderFee event" @@ -321,7 +321,7 @@ def test_exceptions(consumer_wallet, config, publisher_wallet, DT): # Should fail to setData if NOT erc20Deployer with pytest.raises(Exception, match="NOT DEPLOYER ROLE"): - datatoken.setData(Web3.toHex(text="SomeData"), {"from": consumer_wallet}) + datatoken.setData(Web3.to_hex(text="SomeData"), {"from": consumer_wallet}) # Should fail to call cleanPermissions if NOT NFTOwner with pytest.raises(Exception, match="not NFTOwner"): diff --git a/ocean_lib/models/test/test_exchange_fees.py b/ocean_lib/models/test/test_exchange_fees.py index 17f0a382e..57822e9af 100644 --- a/ocean_lib/models/test/test_exchange_fees.py +++ b/ocean_lib/models/test/test_exchange_fees.py @@ -283,7 +283,7 @@ def buy_or_sell_dt_and_verify_balances_swap_fees( DT_exchange2 = details.dt_balance # Get Swapped event - swapped_event = exchange._FRE.contract.events.Swapped().processReceipt( + swapped_event = exchange._FRE.contract.events.Swapped().process_receipt( tx, errors=DISCARD )[0] BT_publish_market_fee_amt = swapped_event.args.marketFeeAmount diff --git a/ocean_lib/models/test/test_exchange_main.py b/ocean_lib/models/test/test_exchange_main.py index 3c00b5541..3c8694c29 100644 --- a/ocean_lib/models/test/test_exchange_main.py +++ b/ocean_lib/models/test/test_exchange_main.py @@ -218,7 +218,7 @@ def test_with_nondefaults(OCEAN, DT, alice, bob, carlos, dan, FRE): DT_alice1 = DT.balanceOf(alice) receipt = exchange.collect_DT(details.dt_balance, {"from": alice}) - event = exchange._FRE.contract.events.TokenCollected().processReceipt( + event = exchange._FRE.contract.events.TokenCollected().process_receipt( receipt, errors=DISCARD )[0] DT_received = event.args.amount @@ -227,7 +227,7 @@ def test_with_nondefaults(OCEAN, DT, alice, bob, carlos, dan, FRE): OCEAN_alice1 = OCEAN.balanceOf(alice) receipt = exchange.collect_BT(details.bt_balance, {"from": alice}) - event = exchange._FRE.contract.events.TokenCollected().processReceipt( + event = exchange._FRE.contract.events.TokenCollected().process_receipt( receipt, errors=DISCARD )[0] OCEAN_received = event.args.amount diff --git a/ocean_lib/models/test/test_factory_router.py b/ocean_lib/models/test/test_factory_router.py index 310c763f8..a080f830d 100644 --- a/ocean_lib/models/test/test_factory_router.py +++ b/ocean_lib/models/test/test_factory_router.py @@ -19,7 +19,7 @@ # FactoryRouter methods @pytest.mark.unit def test_router_owner(factory_router: FactoryRouter): - assert Web3.isChecksumAddress(factory_router.routerOwner()) + assert Web3.is_checksum_address(factory_router.routerOwner()) @pytest.mark.unit diff --git a/ocean_lib/models/ve/test/test_ve_allocate.py b/ocean_lib/models/ve/test/test_ve_allocate.py index c6bf3d86c..e8b7c2ceb 100644 --- a/ocean_lib/models/ve/test/test_ve_allocate.py +++ b/ocean_lib/models/ve/test/test_ve_allocate.py @@ -44,7 +44,7 @@ def test_single_events(ve_allocate): nftaddr1 = accounts[1].address tx = ve_allocate.setAllocation(100, nftaddr1, 1, {"from": accounts[0]}) - event = ve_allocate.contract.events.AllocationSet().processReceipt( + event = ve_allocate.contract.events.AllocationSet().process_receipt( tx, errors=DISCARD )[0] @@ -83,7 +83,7 @@ def test_batch_events(ve_allocate): tx = ve_allocate.setBatchAllocation( [25, 75], [nftaddr1, nftaddr2], [1, 1], {"from": accounts[0]} ) - event = ve_allocate.contract.events.AllocationSetMultiple().processReceipt( + event = ve_allocate.contract.events.AllocationSetMultiple().process_receipt( tx, errors=DISCARD )[0] diff --git a/ocean_lib/models/ve/test/test_ve_ocean.py b/ocean_lib/models/ve/test/test_ve_ocean.py index d2941453a..78d31464d 100644 --- a/ocean_lib/models/ve/test/test_ve_ocean.py +++ b/ocean_lib/models/ve/test/test_ve_ocean.py @@ -33,7 +33,7 @@ def test_ve_ocean1(ocean, factory_deployer_wallet, ocean_token): TA = to_wei(0.0001) OCEAN.mint(alice_wallet.address, TA, {"from": factory_deployer_wallet}) - latest_block = web3.eth.getBlock("latest") + latest_block = web3.eth.get_block("latest") veOCEAN.checkpoint({"from": factory_deployer_wallet, "gas": latest_block.gasLimit}) OCEAN.approve(veOCEAN.address, TA, {"from": alice_wallet}) @@ -47,7 +47,7 @@ def test_ve_ocean1(ocean, factory_deployer_wallet, ocean_token): assert OCEAN.balanceOf(alice_wallet.address) != 0 - latest_block = web3.eth.getBlock("latest") + latest_block = web3.eth.get_block("latest") veOCEAN.create_lock( TA, t2, @@ -65,7 +65,7 @@ def test_ve_ocean1(ocean, factory_deployer_wallet, ocean_token): assert veOCEAN.get_last_user_slope(alice_wallet) != 0 - latest_block = web3.eth.getBlock("latest") + latest_block = web3.eth.get_block("latest") alice_vote_power = float( from_wei(veOCEAN.balanceOf(alice_wallet, latest_block.timestamp)) ) @@ -75,7 +75,7 @@ def test_ve_ocean1(ocean, factory_deployer_wallet, ocean_token): provider.make_request("evm_increaseTime", [t2]) provider.make_request("evm_mine", []) - latest_block = web3.eth.getBlock("latest") + latest_block = web3.eth.get_block("latest") veOCEAN.withdraw( { "from": alice_wallet, @@ -85,6 +85,6 @@ def test_ve_ocean1(ocean, factory_deployer_wallet, ocean_token): ) assert OCEAN.balanceOf(alice_wallet.address) == TA - latest_block = web3.eth.getBlock("latest") + latest_block = web3.eth.get_block("latest") assert veOCEAN.get_last_user_slope(alice_wallet) == 0 assert veOCEAN.balanceOf(alice_wallet, latest_block.timestamp) == 0 diff --git a/ocean_lib/ocean/mint_fake_ocean.py b/ocean_lib/ocean/mint_fake_ocean.py index 28f4cf868..69c6f9ed8 100644 --- a/ocean_lib/ocean/mint_fake_ocean.py +++ b/ocean_lib/ocean/mint_fake_ocean.py @@ -37,5 +37,5 @@ def mint_fake_OCEAN(config: dict) -> None: if OCEAN_token.balanceOf(w.address) < amt_distribute: OCEAN_token.mint(w.address, amt_distribute, {"from": deployer_wallet}) - if config["web3_instance"].eth.getBalance(w.address) < to_wei(2): + if config["web3_instance"].eth.get_balance(w.address) < to_wei(2): send_ether(config, deployer_wallet, w.address, to_wei(4)) diff --git a/ocean_lib/ocean/test/test_crypto.py b/ocean_lib/ocean/test/test_crypto.py index 7cd1ca220..cfc121041 100644 --- a/ocean_lib/ocean/test/test_crypto.py +++ b/ocean_lib/ocean/test/test_crypto.py @@ -28,7 +28,7 @@ def test_sym_encrypt_decrypt(): @enforce_types def test_asym_encrypt_decrypt(alice): - privkey = alice.privateKey.hex() # str + privkey = alice._private_key.hex() # str pubkey = crypto.calc_pubkey(privkey) # str value = "hello there" diff --git a/ocean_lib/ocean/test/test_util.py b/ocean_lib/ocean/test/test_util.py index f958de404..cbc593a5b 100644 --- a/ocean_lib/ocean/test/test_util.py +++ b/ocean_lib/ocean/test/test_util.py @@ -24,8 +24,8 @@ def test_get_ocean_token_address(config): assert "Ocean" in addresses address = get_ocean_token_address(config) - assert Web3.isChecksumAddress(address), "It is not a checksum token address." - assert address == Web3.toChecksumAddress(addresses["Ocean"]) + assert Web3.is_checksum_address(address), "It is not a checksum token address." + assert address == Web3.to_checksum_address(addresses["Ocean"]) @pytest.mark.unit @@ -33,8 +33,8 @@ def test_get_address_by_type(config): addresses = util.get_contracts_addresses(config) address = get_address_of_type(config, "Ocean") - assert Web3.isChecksumAddress(address), "It is not a checksum token address." - assert address == Web3.toChecksumAddress(addresses["Ocean"]) + assert Web3.is_checksum_address(address), "It is not a checksum token address." + assert address == Web3.to_checksum_address(addresses["Ocean"]) @pytest.mark.unit diff --git a/ocean_lib/ocean/util.py b/ocean_lib/ocean/util.py index 0605ba2bf..fb16d6c79 100644 --- a/ocean_lib/ocean/util.py +++ b/ocean_lib/ocean/util.py @@ -25,7 +25,7 @@ def get_address_of_type( if not isinstance(addresses[address_type], dict) else addresses[address_type].get(key, addresses[address_type]["1"]) ) - return Web3.toChecksumAddress(address.lower()) + return Web3.to_checksum_address(address.lower()) @enforce_types @@ -35,7 +35,9 @@ def get_ocean_token_address(config_dict: dict) -> str: """ addresses = get_contracts_addresses(config_dict) - return Web3.toChecksumAddress(addresses.get("Ocean").lower()) if addresses else None + return ( + Web3.to_checksum_address(addresses.get("Ocean").lower()) if addresses else None + ) @enforce_types @@ -65,7 +67,7 @@ def str_with_wei(amt_wei: int) -> str: def get_from_address(tx_dict: dict) -> str: address = tx_dict["from"].address - return Web3.toChecksumAddress(address.lower()) + return Web3.to_checksum_address(address.lower()) @enforce_types @@ -89,8 +91,8 @@ def get_args_object(args, kwargs, args_class): def send_ether( config, from_wallet, to_address: str, amount: Union[int, float], priority_fee=None ): - if not Web3.isChecksumAddress(to_address): - to_address = Web3.toChecksumAddress(to_address) + if not Web3.is_checksum_address(to_address): + to_address = Web3.to_checksum_address(to_address) web3 = config["web3_instance"] chain_id = web3.eth.chain_id @@ -112,6 +114,6 @@ def send_ether( tx["maxPriorityFeePerGas"] = priority_fee tx["maxFeePerGas"] = base_fee * 2 + priority_fee - signed_tx = web3.eth.account.signTransaction(tx, from_wallet.privateKey) + signed_tx = web3.eth.account.sign_transaction(tx, from_wallet._private_key) tx_hash = web3.eth.send_raw_transaction(signed_tx.rawTransaction) return web3.eth.wait_for_transaction_receipt(tx_hash) diff --git a/ocean_lib/services/service.py b/ocean_lib/services/service.py index 96c07e2f5..a3f72fd06 100644 --- a/ocean_lib/services/service.py +++ b/ocean_lib/services/service.py @@ -137,9 +137,10 @@ def add_publisher_trusted_algorithm(self, algo_ddo) -> list: def add_publisher_trusted_algorithm_publisher(self, publisher_address: str) -> list: trusted_algo_publishers = [ - Web3.toChecksumAddress(tp) for tp in self.get_trusted_algorithm_publishers() + Web3.to_checksum_address(tp) + for tp in self.get_trusted_algorithm_publishers() ] - publisher_address = Web3.toChecksumAddress(publisher_address) + publisher_address = Web3.to_checksum_address(publisher_address) if publisher_address in trusted_algo_publishers: return trusted_algo_publishers diff --git a/ocean_lib/web3_internal/contract_base.py b/ocean_lib/web3_internal/contract_base.py index 93f30de78..21b84900e 100644 --- a/ocean_lib/web3_internal/contract_base.py +++ b/ocean_lib/web3_internal/contract_base.py @@ -63,8 +63,8 @@ def wrap(*args, **kwargs): result = result.build_transaction(tx_dict2) - # sign with wallet privateKey and send transaction - signed_tx = web3.eth.account.sign_transaction(result, wallet.privateKey) + # sign with wallet private key and send transaction + signed_tx = web3.eth.account.sign_transaction(result, wallet._private_key) receipt = web3.eth.send_raw_transaction(signed_tx.rawTransaction) return web3.eth.wait_for_transaction_receipt(receipt) @@ -129,7 +129,7 @@ def to_checksum_address(address: str) -> ChecksumAddress: :param address: Address, hex str :return: address, hex str """ - return Web3.toChecksumAddress(address.lower()) + return Web3.to_checksum_address(address.lower()) @enforce_types def get_event_signature(self, event_name: str) -> str: @@ -169,7 +169,7 @@ def get_logs( for log in event_filter.get_all_entries(): receipt = web3.eth.wait_for_transaction_receipt(log.transactionHash) fn = getattr(self.contract.events, event_name) - processed_events = fn().processReceipt(receipt, errors=DISCARD) + processed_events = fn().process_receipt(receipt, errors=DISCARD) for processed_event in processed_events: events.append(processed_event) diff --git a/ocean_lib/web3_internal/contract_utils.py b/ocean_lib/web3_internal/contract_utils.py index fee4c529c..cd1bd20ae 100644 --- a/ocean_lib/web3_internal/contract_utils.py +++ b/ocean_lib/web3_internal/contract_utils.py @@ -75,7 +75,7 @@ def get_contracts_addresses(config: dict) -> Optional[Dict[str, str]]: @enforce_types -# Check singnet/snet-cli#142 (comment). You need to provide a lowercase address then call web3.toChecksumAddress() +# Check singnet/snet-cli#142 (comment). You need to provide a lowercase address then call web3.to_checksum_address() # for software safety. def _checksum_contract_addresses( network_addresses: Dict[str, Any] @@ -87,8 +87,8 @@ def _checksum_contract_addresses( continue if isinstance(value, dict): for k, v in value.items(): - value.update({k: Web3.toChecksumAddress(v.lower())}) + value.update({k: Web3.to_checksum_address(v.lower())}) else: - network_addresses.update({key: Web3.toChecksumAddress(value.lower())}) + network_addresses.update({key: Web3.to_checksum_address(value.lower())}) return network_addresses diff --git a/ocean_lib/web3_internal/test/test_contract_utils.py b/ocean_lib/web3_internal/test/test_contract_utils.py index f5942b515..75e31cdcc 100644 --- a/ocean_lib/web3_internal/test/test_contract_utils.py +++ b/ocean_lib/web3_internal/test/test_contract_utils.py @@ -13,8 +13,8 @@ def test_checksum_contract_addresses(monkeypatch): "test": "0x20802d1a9581b94e51db358c09e0818d6bd071b4", "dict": {"address": "0xe2dd09d719da89e5a3d0f2549c7e24566e947260"}, } - assert Web3.isChecksumAddress(addresses["test"]) is False - assert Web3.isChecksumAddress(addresses["dict"]["address"]) is False + assert Web3.is_checksum_address(addresses["test"]) is False + assert Web3.is_checksum_address(addresses["dict"]["address"]) is False checksum_addresses = _checksum_contract_addresses(addresses) - assert Web3.isChecksumAddress(checksum_addresses["test"]) is True - assert Web3.isChecksumAddress(addresses["dict"]["address"]) is True + assert Web3.is_checksum_address(checksum_addresses["test"]) is True + assert Web3.is_checksum_address(addresses["dict"]["address"]) is True diff --git a/ocean_lib/web3_internal/test/test_wallet.py b/ocean_lib/web3_internal/test/test_wallet.py index 37a67cb3f..c39e8b54b 100644 --- a/ocean_lib/web3_internal/test/test_wallet.py +++ b/ocean_lib/web3_internal/test/test_wallet.py @@ -15,7 +15,9 @@ def test_generating_wallets(ocean_token, config): generated_wallet = generate_wallet() assert generated_wallet.address, "Wallet has not an address." - assert config["web3_instance"].eth.getBalance(generated_wallet.address) == to_wei(3) + assert config["web3_instance"].eth.get_balance(generated_wallet.address) == to_wei( + 3 + ) assert ocean_token.balanceOf(generated_wallet.address) == to_wei(50) @@ -35,4 +37,4 @@ def test_generating_wallets(ocean_token, config): for key_label in env_key_labels: key = os.environ.get(key_label) env_private_keys.append(key) - assert generated_wallet.privateKey.hex() not in env_private_keys + assert generated_wallet._private_key.hex() not in env_private_keys diff --git a/ocean_lib/web3_internal/utils.py b/ocean_lib/web3_internal/utils.py index 358bdf50e..9a25af08d 100644 --- a/ocean_lib/web3_internal/utils.py +++ b/ocean_lib/web3_internal/utils.py @@ -26,15 +26,15 @@ def to_32byte_hex(val: int) -> str: :param val: :return: """ - return Web3.toHex(Web3.toBytes(val).rjust(32, b"\0")) + return Web3.to_hex(Web3.to_bytes(val).rjust(32, b"\0")) # reinstate as part of #1461 # @enforce_types # def sign_with_clef(message_hash: str, wallet: ClefAccount) -> str: -# message_hash = Web3.solidityKeccak( +# message_hash = Web3.solidity_keccak( # ["bytes"], -# [Web3.toBytes(text=message_hash)], +# [Web3.to_bytes(text=message_hash)], # ) # # orig_sig = wallet._provider.make_request( @@ -46,16 +46,16 @@ def to_32byte_hex(val: int) -> str: @enforce_types def sign_with_key(message_hash: Union[HexBytes, str], key: str) -> str: if isinstance(message_hash, str): - message_hash = Web3.solidityKeccak( + message_hash = Web3.solidity_keccak( ["bytes"], - [Web3.toBytes(text=message_hash)], + [Web3.to_bytes(text=message_hash)], ) - pk = keys.PrivateKey(Web3.toBytes(hexstr=key)) + pk = keys.PrivateKey(Web3.to_bytes(hexstr=key)) prefix = "\x19Ethereum Signed Message:\n32" - signable_hash = Web3.solidityKeccak( - ["bytes", "bytes"], [Web3.toBytes(text=prefix), Web3.toBytes(message_hash)] + signable_hash = Web3.solidity_keccak( + ["bytes", "bytes"], [Web3.to_bytes(text=prefix), Web3.to_bytes(message_hash)] ) return keys.ecdsa_sign(message_hash=signable_hash, private_key=pk) @@ -72,7 +72,7 @@ def split_signature(signature: Any) -> Signature: assert len(signature) == 65, ( f"invalid signature, " f"expecting bytes of length 65, got {len(signature)}" ) - v = Web3.toInt(signature[-1]) + v = Web3.to_int(signature[-1]) r = to_32byte_hex(int.from_bytes(signature[:32], "big")) s = to_32byte_hex(int.from_bytes(signature[32:64], "big")) if v != 27 and v != 28: @@ -87,6 +87,6 @@ def get_gas_fees() -> tuple: gas_resp = requests.get("https://gasstation.polygon.technology/v2") return ( - Web3.toWei(gas_resp.json()["fast"]["maxPriorityFee"], "gwei"), - Web3.toWei(gas_resp.json()["fast"]["maxFee"], "gwei"), + Web3.to_wei(gas_resp.json()["fast"]["maxPriorityFee"], "gwei"), + Web3.to_wei(gas_resp.json()["fast"]["maxFee"], "gwei"), ) diff --git a/setup.py b/setup.py index d37ed385a..80fda8cf9 100644 --- a/setup.py +++ b/setup.py @@ -23,9 +23,9 @@ "requests>=2.21.0", "pytz", # used minimally and unlikely to change, common dependency "enforce-typing==1.0.0.post1", - "eciespy==0.3.11", - "cryptography==41.0.2", - "web3==5.31.3" + "eciespy==0.4.0", + "cryptography==41.0.3", + "web3==6.9.0" # web3.py requires eth-abi, requests, and more, # so those will be installed too. # See https://github.com/ethereum/web3.py/blob/master/setup.py @@ -35,11 +35,11 @@ test_requirements = [ "codacy-coverage==1.3.11", - "coverage==7.2.7", + "coverage==7.3.0", "mccabe==0.7.0", "pytest==6.2.5", "pytest-watch==4.2.0", - "pytest-env==0.6.2", + "pytest-env", # common dependency "matplotlib", # just used in a readme test and unlikely to change, common dependency "mkcodes==0.1.1", "pytest-sugar==0.9.7", diff --git a/tests/integration/ganache/test_market_flow.py b/tests/integration/ganache/test_market_flow.py index 40fcb0ab1..efefc7e4b 100644 --- a/tests/integration/ganache/test_market_flow.py +++ b/tests/integration/ganache/test_market_flow.py @@ -86,7 +86,7 @@ def test_market_flow( orders = consumer_ocean.get_user_orders( consumer_wallet.address, - Web3.toChecksumAddress(datatoken.address), + Web3.to_checksum_address(datatoken.address), ) assert ( orders diff --git a/tests/integration/remote/util.py b/tests/integration/remote/util.py index 6d92020e1..61d27bca3 100644 --- a/tests/integration/remote/util.py +++ b/tests/integration/remote/util.py @@ -114,7 +114,7 @@ def do_ocean_tx_and_handle_gotchas(ocean, alice_wallet): "maxPriorityFeePerGas": priority_fee, "maxFeePerGas": max_fee, "gas": ocean.config_dict["web3_instance"] - .eth.getBlock("latest") + .eth.get_block("latest") .gasLimit, }, symbol, diff --git a/tests/resources/helper_functions.py b/tests/resources/helper_functions.py index e636d5e50..260550263 100644 --- a/tests/resources/helper_functions.py +++ b/tests/resources/helper_functions.py @@ -244,10 +244,10 @@ def get_provider_fees( }, separators=(",", ":"), ) - message_hash = Web3.solidityKeccak( + message_hash = Web3.solidity_keccak( ["bytes", "address", "address", "uint256", "uint256"], [ - Web3.toHex(Web3.toBytes(text=provider_data)), + Web3.to_hex(Web3.to_bytes(text=provider_data)), provider_fee_address, provider_fee_token, provider_fee_amount, @@ -261,11 +261,11 @@ def get_provider_fees( "providerFeeAddress": provider_fee_address, "providerFeeToken": provider_fee_token, "providerFeeAmount": str(provider_fee_amount), - "providerData": Web3.toHex(Web3.toBytes(text=provider_data)), + "providerData": Web3.to_hex(Web3.to_bytes(text=provider_data)), # make it compatible with last openzepellin https://github.com/OpenZeppelin/openzeppelin-contracts/pull/1622 "v": (signed.v + 27) if signed.v <= 1 else signed.v, - "r": Web3.toHex(Web3.toBytes(signed.r).rjust(32, b"\0")), - "s": Web3.toHex(Web3.toBytes(signed.s).rjust(32, b"\0")), + "r": Web3.to_hex(Web3.to_bytes(signed.r).rjust(32, b"\0")), + "s": Web3.to_hex(Web3.to_bytes(signed.s).rjust(32, b"\0")), "validUntil": valid_until, } return provider_fee @@ -339,10 +339,10 @@ def get_mock_provider_fees(mock_type, wallet, valid_until=0): provider_fee_amount = 0 provider_data = json.dumps({"timeout": 0}, separators=(",", ":")) - message = Web3.solidityKeccak( + message = Web3.solidity_keccak( ["bytes", "address", "address", "uint256", "uint256"], [ - Web3.toHex(Web3.toBytes(text=provider_data)), + Web3.to_hex(Web3.to_bytes(text=provider_data)), wallet.address, provider_fee_token, provider_fee_amount, @@ -361,5 +361,5 @@ def get_mock_provider_fees(mock_type, wallet, valid_until=0): "r": signature.r, "s": signature.s, "validUntil": valid_until, - "providerData": Web3.toHex(Web3.toBytes(text=provider_data)), + "providerData": Web3.to_hex(Web3.to_bytes(text=provider_data)), }