Skip to content

Commit

Permalink
Upgrade web3.py (#1467)
Browse files Browse the repository at this point in the history
* Upgrade web3.py version and adapt some function names.
* More function name replacements.
* More upgrades from dependabot branches.
  • Loading branch information
calina-c authored Aug 28, 2023
1 parent 303a9a0 commit a28e7fa
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 134 deletions.
4 changes: 2 additions & 2 deletions READMEs/df.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions READMEs/key-value-private.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ model_value = "<insert MLP weights here>"
# 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)
Expand All @@ -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)
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions READMEs/profile-nfts-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ocean_lib/data_provider/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions ocean_lib/data_provider/test/test_data_service_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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=(",", ":"))
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ocean_lib/example_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions ocean_lib/models/data_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
24 changes: 12 additions & 12 deletions ocean_lib/models/data_nft_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions ocean_lib/models/datatoken_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
40 changes: 20 additions & 20 deletions ocean_lib/models/test/test_data_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -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},
)

Expand All @@ -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."
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -277,23 +277,23 @@ 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/",
[],
),
{"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"
Expand Down Expand Up @@ -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]

Expand Down Expand Up @@ -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]

Expand All @@ -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]

Expand Down Expand Up @@ -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]
)
Expand Down Expand Up @@ -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]

Expand Down
Loading

0 comments on commit a28e7fa

Please sign in to comment.