def main(did, pool_address, order_tx_id=None): ocean = Ocean(config=Config(options_dict=get_config_dict())) publisher = Wallet(ocean.web3, private_key='0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58') # 0xe2DD09d719Da89e5a3D0F2549c7E24566e947260 #consumer = Wallet(ocean.web3, private_key='0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16') # 0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0 publisher_wallet = Wallet(ocean.web3, private_key=os.getenv('Publisher_Key')) #addr: 0xc966Ba2a41888B6B4c5273323075B98E27B9F364 consumer = Wallet(ocean.web3, private_key=os.getenv('Consumer_Key')) #addr: 0xEF5dc33A53DD2ED3F670B53F07cEc5ADD4D80504 if not (did and pool_address): metadata_file = './examples/data/metadata.json' with open(metadata_file) as f: metadata = json.load(f) asset, pool = publish_asset(metadata, publisher) #Dataset asset created successfully: did=did:op:784Cc17176533cc962cf659B9f49349ba6F9df3b, datatoken=0x784Cc17176533cc962cf659B9f49349ba6F9df3b #pool_address = 0x3490DDd035B2e1DA30Af09AB6090Bf71fdb94898 else: asset = ocean.assets.resolve(did) pool = BPool(pool_address) if not asset: print(f'publish asset failed, cannot continue with running compute.') return print(f'Requesting compute using asset {asset.did} and pool {pool.address}') algo_file = './examples/data/algorithm.py' order_tx_id= job_id, status = run_compute(asset.did, consumer, algo_file, pool.address, order_tx_id) print(f'Compute started on asset {asset.did}: job_id={job_id}, status={status}')
def test1(): # ocean instance config = ExampleConfig.get_config() ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) ocean = Ocean(config) OCEAN_address_before = ocean.OCEAN_address # deploy, distribute, etc deploy_fake_OCEAN() # test: OCEAN address should have changed OCEAN_address_after = ocean.OCEAN_address assert OCEAN_address_before != OCEAN_address_after # test: TEST_PRIVATE_KEY{1,2} should each hold OCEAN wallet1 = Wallet(ocean.web3, private_key=os.getenv("TEST_PRIVATE_KEY1")) wallet2 = Wallet(ocean.web3, private_key=os.getenv("TEST_PRIVATE_KEY2")) OCEAN_after = BToken(ocean.OCEAN_address) assert OCEAN_after.balanceOf(wallet1.address) > 0 assert OCEAN_after.balanceOf(wallet2.address) > 0
def main(did, pool_address, order_tx_id=None): ocean = Ocean(config=Config(options_dict=get_config_dict())) publisher = Wallet( ocean.web3, private_key="0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58", ) # 0xe2DD09d719Da89e5a3D0F2549c7E24566e947260 consumer = Wallet( ocean.web3, private_key="0x9bf5d7e4978ed5206f760e6daded34d657572bd49fa5b3fe885679329fb16b16", ) # 0x068Ed00cF0441e4829D9784fCBe7b9e26D4BD8d0 if not (did and pool_address): metadata_file = "./examples/data/metadata.json" with open(metadata_file) as f: metadata = json.load(f) asset, pool = publish_asset(metadata, publisher) else: asset = ocean.assets.resolve(did) pool = BPool(pool_address) if not asset: print("publish asset failed, cannot continue with running compute.") return print(f"Requesting compute using asset {asset.did} and pool {pool.address}") algo_file = "./examples/data/algorithm.py" job_id, status = run_compute( asset.did, consumer, algo_file, pool.address, order_tx_id ) print(f"Compute started on asset {asset.did}: job_id={job_id}, status={status}")
def cancel_or_replace_transaction(from_wallet, nonce_value, gas_price=None, gas_limit=None): w3 = Web3Provider.get_web3() tx = { "from": from_wallet.address, "to": from_wallet.address, "value": 0 } gas = gas_limit if gas_limit is not None else w3.eth.estimateGas(tx) tx = { "from": from_wallet.address, "to": from_wallet.address, "value": 0, "gas": gas + 1, } wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address) raw_tx = wallet.sign_tx(tx, fixed_nonce=nonce_value, gas_price=gas_price) tx_hash = w3.eth.sendRawTransaction(raw_tx) receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30) return receipt
def make_info(name, private_key_name): # assume that this account has ETH with gas. class _Info: pass info = _Info() web3 = get_web3() info.web3 = web3 info.private_key = os.environ.get(private_key_name) info.wallet = Wallet(web3, private_key=info.private_key) info.address = info.wallet.address info.account = Account(private_key=info.private_key) wallet = get_ganache_wallet() if wallet: assert (from_wei(get_ether_balance(wallet.address)) > 4), "Ether balance less than 4." if from_wei(get_ether_balance(info.address)) < 2: send_ether(wallet, info.address, 4) from ocean_lib.ocean.ocean import Ocean info.ocean = Ocean() info.T1 = _deployAndMintToken("TOK1", info.address) info.T2 = _deployAndMintToken("TOK2", info.address) return info
def get_wallet(index): name = "PARITY_ADDRESS" if not index else f"PARITY_ADDRESS{index}" pswrd_name = "PARITY_PASSWORD" if not index else f"PARITY_PASSWORD{index}" key_name = "PARITY_KEY" if not index else f"PARITY_KEY{index}" encrypted_key_name = ("PARITY_ENCRYPTED_KEY" if not index else f"PARITY_ENCRYPTED_KEY{index}") keyfile_name = "PARITY_KEYFILE" if not index else f"PARITY_KEYFILE{index}" address = os.getenv(name) if not address: return None pswrd = os.getenv(pswrd_name) key = os.getenv(key_name) encr_key = os.getenv(encrypted_key_name) key_file = os.getenv(keyfile_name) if key_file and not encr_key: with open(key_file) as _file: encr_key = json.loads(_file.read()) from ocean_lib.web3_internal.wallet import Wallet return Wallet( Web3Provider.get_web3(), private_key=key, encrypted_key=encr_key, address=Web3.toChecksumAddress(address), password=pswrd, )
def make_info(name, private_key_name): # assume that this account has ETH with gas. class _Info: pass info = _Info() web3 = get_web3() config = ExampleConfig.get_config() info.web3 = web3 info.private_key = os.environ.get(private_key_name) info.wallet = Wallet( web3, private_key=info.private_key, block_confirmations=config.block_confirmations, transaction_timeout=config.transaction_timeout, ) info.address = info.wallet.address wallet = get_ganache_wallet() if wallet: assert get_ether_balance( web3, wallet.address) >= to_wei(4), "Ether balance less than 4." if get_ether_balance(web3, info.address) < to_wei(2): send_ether(wallet, info.address, to_wei(4)) from ocean_lib.ocean.ocean import Ocean info.ocean = Ocean(config) info.T1 = _deployAndMintToken(web3, "TOK1", info.address) info.T2 = _deployAndMintToken(web3, "TOK2", info.address) return info
def cancel_or_replace_transaction( from_wallet: Wallet, nonce_value: Optional[Union[str, int]] = None, gas_price: Optional[int] = None, gas_limit: Optional[int] = None, ) -> AttributeDict: web3 = from_wallet.web3 chain_id = web3.eth.chain_id tx = { "from": from_wallet.address, "to": from_wallet.address, "value": 0, "chainId": chain_id, "gasPrice": get_gas_price(web3), } gas = gas_limit if gas_limit is not None else web3.eth.estimate_gas(tx) tx["gas"] = gas + 1 raw_tx = from_wallet.sign_tx(tx, fixed_nonce=nonce_value, gas_price=gas_price) tx_hash = web3.eth.send_raw_transaction(raw_tx) block_confirmations = from_wallet.block_confirmations.value block_number_poll_interval = BLOCK_NUMBER_POLL_INTERVAL[chain_id] transaction_timeout = from_wallet.transaction_timeout.value wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations, block_number_poll_interval, transaction_timeout, ) return web3.eth.get_transaction_receipt(tx_hash)
def buy_data( ocean, private_key, token_address, seller_wallet, min_amount, max_amount, ): """Buy a dataset on the market. Define wallet, verify that there is enough ganache ETH and OCEAN. Create an exchange_id for a new exchange. Args: ocean (): private_key (str): token_address (str): seller_wallet (Wallet): min_amount (float): max_amount (float): Returns: """ wallet = Wallet(ocean.web3, private_key, ocean.config.block_confirmations) assert ocean.web3.eth.get_balance(wallet.address) > 0, 'need ganache ETH' OCEAN_token = BToken(ocean.web3, ocean.OCEAN_address) assert OCEAN_token.balanceOf(wallet.address) > 0, 'need ganache OCEAN' exchange_id = ocean.exchange.create(token_address, to_wei(min_amount), seller_wallet) tx_result = ocean.exchange.buy_at_fixed_rate(to_wei(min_amount), wallet, to_wei(max_amount), exchange_id, token_address, seller_wallet.address) assert tx_result, 'failed buying data tokens at fixed rate.'
def sell_data( ocean, private_key, data_token, amount, fixed_price=True, ): """Sell a dataset on the Ocean market. Mint the datatokens. In the create() step below, ganache OCEAN is needed. Finally, Approve the datatoken for sale. Args: ocean (): wallet (): data_token (): amount (): fixed_price (bool): Whether or not to sell the data at a fixed price. Returns: (bool): Returns True if successful. """ wallet = Wallet(ocean.web3, private_key, ocean.config.block_confirmations) data_token.mint(wallet.address, to_wei(amount), wallet) OCEAN_token = BToken(ocean.web3, ocean.OCEAN_address) assert OCEAN_token.balanceOf(wallet.address) > 0, 'need OCEAN' data_token.approve(ocean.exchange._exchange_address, to_wei(amount), wallet) return True
def download_data(ocean, private_key, did): """Download a dataset that is in a user's possession. Points to the service object, send datatoken to the service, and then downloads the dataset files. If the connection breaks, then the request can be made again with the order_tx_id. Args: ocean (): private_key (): did (str): Dataset ID. Returns: """ wallet = Wallet(ocean.web3, private_key, ocean.config.block_confirmations) fee_receiver = ZERO_ADDRESS # could also be market address asset = ocean.assets.resolve(did) service = asset.get_service(ServiceTypes.ASSET_ACCESS) quote = ocean.assets.order(asset.did, wallet.address, service_index=service.index) order_tx_id = ocean.assets.pay_for_service(ocean.web3, quote.amount, quote.data_token_address, asset.did, service.index, fee_receiver, wallet, service.get_c2d_address()) try: file_path = ocean.assets.download(asset.did, service.index, wallet, order_tx_id, destination='./') return file_path except: return order_tx_id
def deploy(cls, web3: Web3, deployer_wallet: Wallet, *args) -> str: """ Deploy the DataTokenTemplate and DTFactory contracts to the current network. :param web3: :param deployer_wallet: Wallet instance :return: smartcontract address of this contract """ _json = get_contract_definition(cls.CONTRACT_NAME) _contract = web3.eth.contract(abi=_json["abi"], bytecode=_json["bytecode"]) built_tx = _contract.constructor(*args).buildTransaction({ "from": deployer_wallet.address, "gasPrice": cls.get_gas_price(web3) }) if "chainId" not in built_tx: built_tx["chainId"] = web3.eth.chain_id if "gas" not in built_tx: built_tx["gas"] = web3.eth.estimate_gas(built_tx) raw_tx = deployer_wallet.sign_tx(built_tx) logging.debug( f"Sending raw tx to deploy contract {cls.CONTRACT_NAME}, signed tx hash: {raw_tx.hex()}" ) tx_hash = web3.eth.send_raw_transaction(raw_tx) return cls.get_tx_receipt(web3, tx_hash, timeout=60).contractAddress
def deploy(cls, web3, deployer_wallet: Wallet, abi_path: str = '', *args): """ Deploy the DataTokenTemplate and DTFactory contracts to the current network. :param web3: :param abi_path: :param deployer_wallet: Wallet instance :return: smartcontract address of this contract """ if not abi_path: abi_path = ContractHandler.artifacts_path assert abi_path, f'abi_path is required, got {abi_path}' w3 = web3 _json = ContractHandler.read_abi_from_file(cls.CONTRACT_NAME, abi_path) _contract = w3.eth.contract(abi=_json['abi'], bytecode=_json['bytecode']) built_tx = _contract.constructor(*args)\ .buildTransaction({'from': deployer_wallet.address}) if 'gas' not in built_tx: built_tx['gas'] = web3.eth.estimateGas(built_tx) raw_tx = deployer_wallet.sign_tx(built_tx) logging.debug( f'Sending raw tx to deploy contract {cls.CONTRACT_NAME}, signed tx hash: {raw_tx.hex()}' ) tx_hash = web3.eth.sendRawTransaction(raw_tx) return cls.get_tx_receipt(tx_hash, timeout=60).contractAddress
def send_ether(from_wallet: Wallet, to_address: str, amount: int) -> AttributeDict: if not Web3.isChecksumAddress(to_address): to_address = Web3.toChecksumAddress(to_address) web3 = from_wallet.web3 chain_id = web3.eth.chain_id tx = { "from": from_wallet.address, "to": to_address, "value": amount, "chainId": chain_id, "gasPrice": get_gas_price(web3), } tx["gas"] = web3.eth.estimate_gas(tx) raw_tx = from_wallet.sign_tx(tx) tx_hash = web3.eth.send_raw_transaction(raw_tx) block_confirmations = from_wallet.block_confirmations.value block_number_poll_interval = BLOCK_NUMBER_POLL_INTERVAL[chain_id] transaction_timeout = from_wallet.transaction_timeout.value wait_for_transaction_receipt_and_block_confirmations( web3, tx_hash, block_confirmations, block_number_poll_interval, transaction_timeout, ) return web3.eth.get_transaction_receipt(tx_hash)
def get_another_consumer_wallet() -> Wallet: config = get_example_config() return Wallet( get_web3(), private_key=os.environ.get("TEST_PRIVATE_KEY3"), block_confirmations=config.block_confirmations, transaction_timeout=config.transaction_timeout, )
def get_wallet(ocean, private_key): """Get a user's wallet given their private key. Args: ocean (): private_key (str): Returns: """ return Wallet(ocean.web3, private_key, ocean.config.block_confirmations)
def get_factory_deployer_wallet(network): if network == "ganache": return get_ganache_wallet() private_key = os.environ.get("FACTORY_DEPLOYER_PRIVATE_KEY") if not private_key: return None return Wallet(get_web3(), private_key=private_key)
def mint_fake_OCEAN(config: Config) -> None: """ Does the following: 1. Mints tokens 2. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2 """ addresses_file = config.address_file with open(addresses_file) as f: network_addresses = json.load(f) web3 = get_web3(config.network_url) deployer_wallet = Wallet( web3, private_key=os.environ.get("FACTORY_DEPLOYER_PRIVATE_KEY"), block_confirmations=config.block_confirmations, transaction_timeout=config.transaction_timeout, ) OCEAN_token = DataToken(web3, address=network_addresses["development"]["Ocean"]) amt_distribute = to_wei(1000) OCEAN_token.mint( deployer_wallet.address, 2 * amt_distribute, from_wallet=deployer_wallet ) for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]: key = os.environ.get(key_label) if not key: continue w = Wallet( web3, private_key=key, block_confirmations=config.block_confirmations, transaction_timeout=config.transaction_timeout, ) if OCEAN_token.balanceOf(w.address) < amt_distribute: OCEAN_token.transfer(w.address, amt_distribute, from_wallet=deployer_wallet) if get_ether_balance(web3, w.address) < to_wei(2): send_ether(deployer_wallet, w.address, to_wei(4))
def sign_hash(msg_hash, wallet: Wallet): """ This method use `personal_sign`for signing a message. This will always prepend the `\x19Ethereum Signed Message:\n32` prefix before signing. :param msg_hash: :param wallet: Wallet instance :return: signature """ s = wallet.sign(msg_hash) return s.signature.hex()
def get_ganache_wallet(): web3 = Web3Provider.get_web3() if (web3.eth.accounts and web3.eth.accounts[0].lower() == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()): return Wallet( web3, private_key= "0xfd5c1ccea015b6d663618850824154a3b3fb2882c46cefb05b9a93fea8c3d215", ) return None
def get_ganache_wallet(): web3 = get_web3() if (web3.eth.accounts and web3.eth.accounts[0].lower() == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()): return Wallet( web3, private_key= "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58", ) return None
def send_ether(from_wallet: Wallet, to_address: str, ether_amount: int): w3 = Web3Provider.get_web3() if not w3.isChecksumAddress(to_address): to_address = w3.toChecksumAddress(to_address) tx = { "from": from_wallet.address, "to": to_address, "value": w3.toWei(ether_amount, "ether"), } _ = w3.eth.estimateGas(tx) tx = { "from": from_wallet.address, "to": to_address, "value": w3.toWei(ether_amount, "ether"), "gas": 500000, } wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address) raw_tx = wallet.sign_tx(tx) tx_hash = w3.eth.sendRawTransaction(raw_tx) receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30) return receipt
def initialize(private_key): load_dotenv(".env") config = Config(os.getenv('config.ini')) config = Config(os.getenv('config.ini')) print(config.network_url) # config.network_url="https://rinkeby.infura.io/v3/31d95be121a545b688a0e07e4de4d256" ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) ocean = Ocean() wallet = Wallet(ocean.web3, private_key=private_key) return ocean, wallet
def transact_with_contract_function( address, web3, function_name=None, transaction=None, contract_abi=None, fn_abi=None, *args, **kwargs, ): """ Helper function for interacting with a contract function by sending a transaction. This is copied from web3 `transact_with_contract_function` so we can use `personal_sendTransaction` when possible. """ transact_transaction = prepare_transaction( address, web3, fn_identifier=function_name, contract_abi=contract_abi, transaction=transaction, fn_abi=fn_abi, fn_args=args, fn_kwargs=kwargs, ) passphrase = None account_key = None if transaction and "passphrase" in transaction: passphrase = transaction["passphrase"] transact_transaction.pop("passphrase") if "account_key" in transaction: account_key = transaction["account_key"] transact_transaction.pop("account_key") if account_key: raw_tx = Wallet(web3, private_key=account_key).sign_tx(transact_transaction) logging.debug( f"sending raw tx: function: {function_name}, tx hash: {raw_tx.hex()}" ) txn_hash = web3.eth.sendRawTransaction(raw_tx) elif passphrase: txn_hash = web3.personal.sendTransaction(transact_transaction, passphrase) else: txn_hash = web3.eth.sendTransaction(transact_transaction) wait_for_tx(txn_hash, web3, 5) return txn_hash
def get_ganache_wallet(): web3 = get_web3() if (web3.eth.accounts and web3.eth.accounts[0].lower() == "0xe2DD09d719Da89e5a3D0F2549c7E24566e947260".lower()): config = get_example_config() return Wallet( web3, private_key= "0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58", block_confirmations=config.block_confirmations, transaction_timeout=config.transaction_timeout, ) return None
def get_factory_deployer_wallet(network): if network == "ganache": return get_ganache_wallet() private_key = os.environ.get("FACTORY_DEPLOYER_PRIVATE_KEY") if not private_key: return None config = get_example_config() return Wallet( get_web3(), private_key=private_key, block_confirmations=config.block_confirmations, transaction_timeout=config.transaction_timeout, )
def send_ether(from_wallet, to_address, ether_amount): w3 = Web3Provider.get_web3() if not w3.isChecksumAddress(to_address): to_address = w3.toChecksumAddress(to_address) tx = { 'from': from_wallet.address, 'to': to_address, 'value': w3.toWei(ether_amount, 'ether') } gas = w3.eth.estimateGas(tx) tx = { 'from': from_wallet.address, 'to': to_address, 'value': w3.toWei(ether_amount, 'ether'), 'gas': 500000 } wallet = Wallet(w3, private_key=from_wallet.key, address=from_wallet.address) raw_tx = wallet.sign_tx(tx) tx_hash = w3.eth.sendRawTransaction(raw_tx) receipt = w3.eth.waitForTransactionReceipt(tx_hash, timeout=30) return receipt
def mint_fake_OCEAN(): """ Does the following: 1. Mints tokens 2. Distributes tokens to TEST_PRIVATE_KEY1 and TEST_PRIVATE_KEY2 """ config = ExampleConfig.get_config() ConfigProvider.set_config(config) Web3Provider.init_web3( provider=get_web3_connection_provider(config.network_url)) ContractHandler.set_artifacts_path(config.artifacts_path) addresses_file = config.address_file ocean = get_publisher_ocean_instance() web3 = ocean.web3 with open(addresses_file) as f: network_addresses = json.load(f) network = "development" deployer_wallet = get_ganache_wallet() OCEAN_token = DataToken(address=network_addresses[network]["Ocean"]) amt_distribute = 1000 amt_distribute_base = to_base_18(float(amt_distribute)) OCEAN_token.mint(deployer_wallet.address, 2 * amt_distribute_base, from_wallet=deployer_wallet) for key_label in ["TEST_PRIVATE_KEY1", "TEST_PRIVATE_KEY2"]: key = os.environ.get(key_label) if not key: continue w = Wallet(web3, private_key=key) if OCEAN_token.token_balance(w.address) < 1000: OCEAN_token.transfer(w.address, amt_distribute_base, from_wallet=deployer_wallet) if from_wei(get_ether_balance(w.address)) < 2: send_ether(deployer_wallet, w.address, 4)
def test_issue185_system(monkeypatch): """A system-level test, to replicate original failure seen in #185""" setup_issue_185(monkeypatch) # actual test. Imports only come now, to avoid setting class-level attributes from ocean_lib.ocean.ocean import Ocean from ocean_lib.web3_internal.wallet import Wallet private_key = os.getenv("TEST_PRIVATE_KEY1") config = {"network": os.getenv("NETWORK_URL")} ocean = Ocean(config) wallet = Wallet(ocean.web3, private_key=private_key) # this failed before the fix datatoken = ocean.create_data_token("Dataset name", "dtsymbol", from_wallet=wallet) assert datatoken is not None
def setup_all(): setup_network() web3 = Web3Provider.get_web3() if web3.eth.accounts and web3.eth.accounts[0].lower( ) == '0xe2DD09d719Da89e5a3D0F2549c7E24566e947260'.lower(): wallet = Wallet( web3, private_key= '0xc594c6e5def4bab63ac29eed19a134c130388f74f019bc74b8f4389df2837a58' ) provider = get_provider_wallet() if web3.fromWei(Web3Helper.get_ether_balance(provider.address), 'ether') < 10: Web3Helper.send_ether(wallet, provider.address, 25) consumer = get_consumer_wallet() if web3.fromWei(Web3Helper.get_ether_balance(consumer.address), 'ether') < 10: Web3Helper.send_ether(wallet, consumer.address, 25)