コード例 #1
0
def contract_update(contract_hash, start=None, end=None):
    global contract_dict
    global lock

    response = ""

    # If notification request already present, send it to contractor and set request to "sent"
    # TODO: a contract atm can only have one requests, we can change that by using the RPC ids instead
    if contract_hash in contract_dict:
        with lock:
            response = json.dumps(contract_dict[contract_hash])
            if contract_dict[contract_hash]["status"] == "finished":
                contract_dict[contract_hash]["status"] = "sent"

        return response
    else:
        bound_search = 1
        head_level = pytezos.using(shell=network).shell.head.header()["level"]
        if end is None:
            bound_search = 0
            end = head_level
            if run_head_level == -1:
                Thread(target=read_from_head).start()

        try:
            # Check contract exists
            storage = pytezos.using(
                shell=network).contract(contract_hash).storage(head_level)
            with lock:
                # Make new notification request
                contract_dict[contract_hash] = {
                    "contract_hash": contract_hash,
                    "status": "in progress",
                    "data": {},
                    "bound_search": bound_search
                }
                if not bound_search:
                    notification = make_notification(
                        head_level,
                        contract_hash,
                        entrypoint=storage["status"],
                        storage=storage)
                    contract_dict[contract_hash]["data"][
                        head_level] = notification
                response = json.dumps(contract_dict[contract_hash])

            Thread(target=contract_all_update_search,
                   args=(contract_hash, ),
                   kwargs={
                       "start": start,
                       "end": end
                   }).start()
            return response

        except Exception as e:
            print("Error", e)
            return {
                "error":
                "Contract " + contract_hash + " not found. \n" + str(e)
            }
コード例 #2
0
def contract_origin_search(contract_hash):
    start = 0
    end = pytezos.using(shell=network).shell.head.header()["level"]
    contract = pytezos.using(shell=network).contract(contract_hash)
    found = -1
    storage = None
    while found == -1:
        anchor = int((end + start + 1) / 2)
        try:
            storage = contract.storage(anchor)
            try:
                storage = contract.storage(anchor - 1)
                end = anchor
            except Exception:
                found = anchor
        except Exception:
            start = anchor
    if verbose:
        print_notification(found,
                           contract_hash,
                           entrypoint="contract creation",
                           storage=storage)
    return make_notification(found,
                             contract_hash,
                             entrypoint="contract creation",
                             storage=storage)
コード例 #3
0
ファイル: helpers.py プロジェクト: tbinetruy/pytezos
def do_include(ctx: Context, prim, args, annots):
    path = get_string(args[0])

    if isfile(path):
        code = Contract.from_file(path).code
    else:
        parts = path.split(':')
        network = parts[0] if len(parts) > 1 else ctx.get('NETWORK', 'mainnet')
        address = parts[1] if len(parts) > 1 else parts[0]
        assert is_kt(address), f'expected filename or KT address (with network), got {path}'
        code = pytezos.using(network).contract(address).contract.code
        storage = pytezos.using(network).shell.contracts[address].storage()
        ctx.set('STORAGE', storage)

    do_interpret(ctx, code)
コード例 #4
0
def get_updates(registry_address,
                indexer,
                since=None) -> List[Tuple[int, dict]]:
    update_levels = get_update_levels(registry_address,
                                      indexer=indexer,
                                      since=since)

    with yaspin(
            text=f"Retrieving big map diffs since {since or 'origination'}..."
    ):
        baker_registry = pytezos.using('mainnet').contract(registry_address)

        def parse_updates(level):
            big_map_diff = dict()
            opg_list = baker_registry.shell.blocks[level].operations.managers()
            for opg in opg_list:
                try:
                    results = baker_registry.operation_result(opg)
                    for result in results:
                        print(result.storage)
                        big_map_diff.update(**result.storage['big_map_0'])
                except RpcError:
                    pass
            return level, big_map_diff

        with ThreadPoolExecutor(max_workers=10) as executor:
            updates = list(executor.map(parse_updates, update_levels))

    return updates
コード例 #5
0
def upsert_baker(registry_address,
                 baker_address,
                 data,
                 dry_run=False,
                 network='mainnet'):
    data = encode_info(data)
    current_state = get_baker(registry_address=registry_address,
                              baker_address=baker_address,
                              network=network,
                              raw=True)

    if current_state:
        changes = diff(current_state, data, syntax='symmetric')
        log = list(map(format_entry, list(iter_diff(changes))))
        fee = UPDATE_FEE
    else:
        log = [dict(kind='create', address=baker_address)]
        fee = CREATE_FEE

    with yaspin(text='Generating command line...'):
        registry = pytezos.using(shell=network,
                                 key=baker_address).contract(registry_address)
        try:
            call = registry.set_data(delegate=baker_address,
                                     **data).with_amount(fee)
            cmdline = call.cmdline()
        except Exception:
            exit(-1)

    if dry_run:
        with yaspin(text='Simulating operation...'):
            call.result()

    return cmdline, log
コード例 #6
0
def get_snapshot(registry_address,
                 bakers_addresses: list,
                 raw=False,
                 level=None,
                 network='mainnet') -> dict:
    with yaspin(text=f'Retrieving big map snapshot at {level or "head"}...'):
        registry = pytezos.using(network).contract(registry_address)

        def big_map_get(address):
            try:
                data = registry.using(
                    block_id=level or 'head').storage['big_map_0'][address]()
            except AssertionError:
                data = None
            else:
                if raw:
                    for key in ['last_update', 'reporterAccount']:
                        if key in data:
                            del data[key]
                else:
                    data = decode_info(data)

            return address, data

        with ThreadPoolExecutor(max_workers=10) as executor:
            snapshot = dict(executor.map(big_map_get, bakers_addresses))

    return {k: v for k, v in snapshot.items() if v is not None}
コード例 #7
0
ファイル: cli.py プロジェクト: chorusmobility/robonomics-tz
    def activate(self, path, network='babylonnet'):
        """
        Activates and reveals key from the faucet file
        :param path: Path to the .json file downloaded from https://faucet.tzalpha.net/
        :param network: Default is Babylonnet
        """
        ptz = pytezos.using(key=path, shell=network)
        print(f'Activating {ptz.key.public_key_hash()} in the {network}')

        if ptz.balance() == 0:
            try:
                opg = ptz.activate_account().autofill().sign()
                print(f'Injecting activation operation:')
                pprint(opg.json_payload())
                opg.inject(_async=False)
            except RpcError as e:
                pprint(e)
                exit(-1)
            else:
                print(f'Activation succeeded! Claimed balance: {ptz.balance()} ꜩ')
        else:
            print('Already activated')

        try:
            opg = ptz.reveal().autofill().sign()
            print(f'Injecting reveal operation:')
            pprint(opg.json_payload())
            opg.inject(_async=False)
        except RpcError as e:
            pprint(e)
            exit(-1)
        else:
            print(f'Your key {ptz.key.public_key_hash()} is now active and revealed')
コード例 #8
0
ファイル: cli.py プロジェクト: tbinetruy/pytezos
    def deploy(self,
               path,
               storage=None,
               network='carthagenet',
               key=None,
               github_repo_slug=None,
               github_oauth_token=None,
               dry_run=False):
        """
        Deploy contract to the specified network
        :param path: Path to the .tz file
        :param storage: Storage in JSON format (not Micheline)
        :param network:
        :param key:
        :param github_repo_slug:
        :param github_oauth_token:
        :param dry_run: Set this flag if you just want to see what would happen
        """
        ptz = pytezos.using(shell=network, key=key)
        print(
            f'Deploying contract using {ptz.key.public_key_hash()} in the {network}'
        )

        contract = get_contract(path)
        if storage is not None:
            storage = contract.storage.encode(storage)

        try:
            opg = ptz.origination(script=contract.script(
                storage=storage)).autofill().sign()
            print(f'Injecting origination operation:')
            pprint(opg.json_payload())

            if dry_run:
                pprint(opg.preapply())
                exit(0)
            else:
                opg = opg.inject(_async=False)
        except RpcError as e:
            pprint(e)
            exit(-1)
        else:
            originated_contracts = OperationResult.originated_contracts(opg)
            assert len(originated_contracts) == 1
            bcd_link = make_bcd_link(network, originated_contracts[0])
            print(f'Contract was successfully deployed: {bcd_link}')

            if github_repo_slug:
                deployment = create_deployment(github_repo_slug,
                                               github_oauth_token,
                                               environment=network)
                pprint(deployment)
                status = create_deployment_status(
                    github_repo_slug,
                    github_oauth_token,
                    deployment_id=deployment['id'],
                    state='success',
                    environment=network,
                    environment_url=bcd_link)
                pprint(status)
コード例 #9
0
    def read_secret(self, sess):
        k = Key.from_encoded_key(sess['secret'], passphrase=sess['password'])
        p = pytezos.using(key=k, shell=sess['network'])

        print(k.public_key_hash())

        return p
コード例 #10
0
ファイル: tezos.py プロジェクト: yourlabs/djblockchain
    def watch(self, transaction):
        client = pytezos.using(shell=self.blockchain.endpoint)
        opg = None
        i = 300
        while True:
            try:
                # level of the chain latest block
                level_position = client.shell.head.metadata(
                )['level']['level_position']
                opg = self.find_in_past_blocks(client, transaction)
                # level_operation = opg['contents'][0]['level']  (not always present)
                operation_block_id = opg['branch']
                level_operation = client.shell.blocks[
                    operation_block_id].level()
                if level_position - level_operation >= self.blockchain.confirmation_blocks:
                    logger.info(
                        f'block was found at a depth of : {level_position - level_operation}'
                    )
                    break
            except:
                if i:
                    # client.shell.wait_next_block() (might be a better alternative to wait for blocks to append)
                    time.sleep(2)
                    i -= 1
                else:
                    raise

        func = transaction.function or 'deploy'
        sign = (f'{transaction.contract_name}.{func}(*{transaction.args})')
        logger.debug(f'{sign}: watch')
        transaction.gas = opg['contents'][0]['fee']
        result = opg['contents'][0]['metadata']['operation_result']
        if 'originated_contracts' in result:
            transaction.contract_address = result['originated_contracts'][0]
コード例 #11
0
ファイル: cli.py プロジェクト: yourlabsopensource/pytezos
def activate(_ctx, path: str, network: str) -> None:
    ptz = pytezos.using(key=path, shell=network)
    logger.info(
        'Activating %s in the %s',
        ptz.key.public_key_hash(),
        network,
    )

    if ptz.balance() == 0:
        try:
            opg = ptz.reveal().autofill().sign()
            logger.info('Injecting reveal operation:')
            logger.info(pformat(opg.json_payload()))
            opg.inject(_async=False)
        except RpcError as e:
            logger.critical(pformat(e))
            sys.exit(-1)
        else:
            logger.info('Activation succeeded! Claimed balance: %s ꜩ',
                        ptz.balance())
    else:
        logger.info('Already activated')

    try:
        opg = ptz.reveal().autofill().sign()
        logger.info('Injecting reveal operation:')
        logger.info(pformat(opg.json_payload()))
        opg.inject(_async=False)
    except RpcError as e:
        logger.critical(pformat(e))
        sys.exit(-1)
    else:
        logger.info('Your key %s is now active and revealed',
                    ptz.key.public_key_hash())
コード例 #12
0
ファイル: app.py プロジェクト: userevolution/Tezos-Signing
def _validate(doc_hash, doc_name, pt=False):
    global data
    if pt:
        from pytezos import pytezos
        pytezos = pytezos.using(shell='testnet')
        ci = pytezos.contract('KT1DMYxvZCqSVcpea8t6xdVdoDRgPHY3x9sD')
    return data.get(doc_hash, {})
コード例 #13
0
    def read_mnemonic(self, sess):
        k = Key.from_mnemonic(sess['mnemonic'],
                              passphrase=sess['password'],
                              email=sess['email'])
        p = pytezos.using(key=k, shell=sess['network'])

        return p
コード例 #14
0
def check_sync_state():
    from apps.wallet.models import Wallet
    pytezos_client = pytezos.using(
        key=settings.TEZOS_ADMIN_ACCOUNT_PRIVATE_KEY,
        shell=settings.TEZOS_NODE)
    token_contract = pytezos_client.contract(
        settings.TEZOS_TOKEN_CONTRACT_ADDRESS)
    fail_message = ""
    transfer_transaction_payloads = []
    for wallet in Wallet.objects.all():
        try:
            on_chain_balance = token_contract.big_map_get(
                "ledger/{}::{}".format(wallet.address,
                                       wallet.currency.token_id))
            if wallet.balance != on_chain_balance:

                fail_message += "{} has {} onchain balance but on system {}\n".format(
                    wallet.wallet_id, on_chain_balance, wallet.balance)
        except:
            if wallet.balance > 0:
                fail_message += "{} has 0 onchain balance but on system {}\n".format(
                    wallet.wallet_id, wallet.balance)
    if len(fail_message) > 0:
        raise Exception(fail_message)
        return False
    else:
        return True
コード例 #15
0
ファイル: cli.py プロジェクト: gdsoumya/fischcake-box
def cli(rpc, fishcake_box, fishcake_token):
    """
    CLI group
    """
    global pytezos, fishcake_box_addr, fishcake_token_addr
    pytezos = pytezos.using(shell=rpc)
    fishcake_box_addr = fishcake_box
    fishcake_token_addr = fishcake_token
コード例 #16
0
 def test_sign_verify_message(self, alias, message, block, expected):
     client = pytezos.using(key=alias)
     sig = client.sign_message(message, block=block)
     self.assertEqual(expected, sig)
     pytezos.check_message(message,
                           public_key=client.key.public_key(),
                           signature=expected,
                           block=block)
コード例 #17
0
ファイル: helpers.py プロジェクト: tbinetruy/pytezos
def do_reset(ctx: Context, prim, args, annots):
    network = get_string(args[0])
    assert network in networks, f'expected on of {", ".join(networks)}, got {network}'
    ctx.set('NETWORK', network)
    chain_id = ChainID(pytezos.using(network).shell.chains.main.chain_id())
    ctx.set('CHAIN_ID', chain_id)
    ctx.big_maps.reset()
    ctx.drop_all()
コード例 #18
0
def get_contract(path):
    path = get_local_contract_path(path)
    if path:
        contract = ContractInterface.from_file(path)
    else:
        network, address = path.split(':')
        contract = pytezos.using(shell=network).contract(address)
    return contract
コード例 #19
0
ファイル: client.py プロジェクト: Gradius93/wrap-tz-contracts
 def __init__(self,
              shell="http://localhost:8732",
              key="edsk3QoqBuvdamxouPhin7swCvkQNgq4jP5KZPbwWNnwdZpSpJiEbq"):
     client: PyTezosClient = pytezos.using(key=key, shell=shell)
     self.minter = Minter(client)
     self.token = Token(client)
     self.quorum = Quorum(client)
     self.deploy = Deploy(client)
     self.governance = Governance(client)
コード例 #20
0
def get_storage(contract_hash):
    # Send contract's storage
    storage = None
    try:
        if block_id is None:
            storage = pytezos.using(
                shell=network).contract(contract_hash).storage()
        else:
            storage = pytezos.using(
                shell=network).contract(contract_hash).storage(block_id)
    except Exception as e:
        print("Error", e)
        return {"error": str(e)}

    if verbose:
        print("Get contract storage:", block_id, storage, "\n")
    notification = make_notification(block_id, contract_hash, storage=storage)
    return json.dumps(notification)
コード例 #21
0
 def _get_big_map_val(self, big_map: BigMap, key: StackItem):
     key_hash = get_key_hash(key.val_expr, key.type_expr)
     network = big_map.val_expr['_network']
     try:
         res = pytezos.using(network).shell.head.context.big_maps[int(
             big_map)][key_hash]()
     except JSONDecodeError:
         res = None
     return res
コード例 #22
0
ファイル: utils.py プロジェクト: Anshit01/econet
def createUser(username):
    try:
        pyt = pytezos.using(key=config["TEZOS_PRIVATE_KEY"],
                            shell="https://edonet-tezos.giganode.io")
        contract = pyt.contract(config["CONTRACT"])
        contract.createUser(username).operation_group.sign().inject()
    except:
        print(
            "error in creating user in blockchain. probably it already exists."
        )
コード例 #23
0
def do_now(ctx: Context, prim, args, annots):
    res = ctx.get('NOW')
    if not res:
        network = ctx.get('NETWORK')
        if network:
            now = pytezos.using(network).now()
        else:
            now = int(datetime.utcnow().timestamp())
        res = Timestamp(now)
    ctx.push(res, annots=['@now'])
コード例 #24
0
ファイル: utils.py プロジェクト: Anshit01/econet
def updateUserPoints(username, points):
    try:
        pyt = pytezos.using(key=config["TEZOS_PRIVATE_KEY"],
                            shell="https://edonet-tezos.giganode.io")
        contract = pyt.contract(config["CONTRACT"])

        contract.updatePoints(username=username,
                              points=points).operation_group.sign().inject()
    except:
        print("Tezos error")
コード例 #25
0
def get_contract(path):
    if path is None:
        files = glob('*.tz')
        assert len(files) == 1
        contract = ContractInterface.from_file(abspath(files[0]))
    elif exists(path):
        contract = ContractInterface.from_file(path)
    else:
        network, address = path.split(':')
        contract = pytezos.using(shell=network).contract(address)
    return contract
コード例 #26
0
ファイル: __init__.py プロジェクト: yourlabs/pytest-tezos
 def __init__(self):
     self.addresses = []
     self.clients = []
     for i in sandbox_ids:
         key = pytezos.key.from_encoded_key(i)
         self.addresses.append(key.public_key_hash())
         host = 'tz' if os.getenv('CI') else 'localhost'
         self.clients.append(pytezos.using(
             key=key,
             shell=f'http://{host}:8732',
         ))
     self.client = self.clients[0]
コード例 #27
0
ファイル: cli.py プロジェクト: yourlabsopensource/pytezos
def deploy(
    _ctx,
    path: str,
    storage: Optional[str],  # pylint: disable=redefined-outer-name
    network: str,
    key: Optional[str],
    github_repo_slug: Optional[str],
    github_oauth_token: Optional[str],
    dry_run: bool,
):
    ptz = pytezos.using(shell=network, key=key)
    logger.info('Deploying contract using %s in the %s',
                ptz.key.public_key_hash(), network)

    contract = get_contract(path)
    try:
        opg = ptz.origination(script=contract.script(
            initial_storage=storage)).autofill().sign()
        logger.info('Injecting origination operation:')
        logger.info(pformat(opg.json_payload()))

        if dry_run:
            logger.info(pformat(opg.preapply()))
            sys.exit(0)
        else:
            opg = opg.inject(_async=False)
    except RpcError as e:
        logger.critical(pformat(e))
        sys.exit(-1)
    else:
        originated_contracts = OperationResult.originated_contracts(opg)
        if len(originated_contracts) != 1:
            raise Exception(
                'Operation group must has exactly one originated contract')
        bcd_link = make_bcd_link(network, originated_contracts[0])
        logger.info('Contract was successfully deployed: %s', bcd_link)

        if github_repo_slug:
            deployment = create_deployment(
                github_repo_slug,
                github_oauth_token,
                environment=network,
            )
            logger.info(pformat(deployment))
            status = create_deployment_status(
                github_repo_slug,
                github_oauth_token,
                deployment_id=deployment['id'],
                state='success',
                environment=network,
                environment_url=bcd_link,
            )
            logger.info(status)
コード例 #28
0
def get_contract(path):
    if path is None:
        files = glob('*.tz')
        if len(files) != 1:
            raise Exception('No contracts found in working directory, specify --path implicitly')
        contract = ContractInterface.from_file(abspath(files[0]))
    elif exists(path):
        contract = ContractInterface.from_file(path)
    else:
        network, address = path.split(':')
        contract = pytezos.using(shell=network).contract(address)
    return contract
コード例 #29
0
    def read_faucet(self, sess):

        faucet = sess['faucet']
        #print(faucet)
        path = './faucets/{}.json'.format(faucet['pkh'])

        with open(path, 'w') as outfile:
            json.dump(faucet, outfile)

        k = Key.from_faucet(path)
        p = pytezos.using(key=k, shell=sess['network'])

        return p
コード例 #30
0
ファイル: source_route.py プロジェクト: lkazmer/ungrund
    def post(self):
        try:
            payload = v.read_requests(request)
            print(payload)
            res = requests.get(
                "https://api.better-call.dev/v1/contract/mainnet/{}".format(
                    payload['kt']))

            e = res.json()
            print(e)
            p = pytezos.using(shell='mainnet')
            c = p.contract(payload['kt'])

            storage = c.storage()
            print(storage)
            r = requests.post(
                'https://37kpt5uorg.execute-api.us-east-1.amazonaws.com/dev/get_ipfs',
                {"hash": storage['meta']})
            print(r.json())
            meta = json.loads(r.json())
            print(meta)
            storage['goal'] = int(storage['goal'])
            storage['achieved'] = int(storage['achieved'])
            print(meta['title'])

            balance = requests.get(
                'https://api.better-call.dev/v1/account/{}/{}'.format(
                    'mainnet', e['address']))
            balance = balance.json()['balance']

            return {
                'address':
                payload['kt'],
                'timestamp':
                e['timestamp'],
                'balance':
                int(balance),
                'storage':
                storage,
                'title':
                meta['title'],
                'description':
                meta['description'],
                'links':
                meta['links'],
                'percentage':
                round(((int(balance) / 1000000) * 100 / storage['goal']), 2)
            }

        except:
            return 500