Esempio n. 1
0
 def factory(cls, account: StatelessAccount, expire_at: pendulum.DateTime):
     return cls(
         iss=TokenConfig.ISSUER,
         sub=TokenConfig.SUBJECT,
         exp=int(expire_at.timestamp()),
         iat=int(now().timestamp()),
         account=account,
     )
Esempio n. 2
0
def deploy(
    keystore: str,
    jsonrpc: str,
    gas: int,
    gas_price: int,
    nonce: int,
    auto_nonce: bool,
    token_address: str,
    airdrop_file_name: str,
    decay_start_time: int,
    decay_start_date: pendulum.DateTime,
    decay_duration: int,
) -> None:

    if decay_start_date is not None and decay_start_time is not None:
        raise click.BadParameter(
            "Both --decay-start-date and --decay-start-time have been specified"
        )
    if decay_start_date is None and decay_start_time is None:
        raise click.BadParameter(
            "Please specify a decay start date with --decay-start-date or --decay-start-time"
        )

    if decay_start_date is not None:
        decay_start_time = int(decay_start_date.timestamp())

    web3 = connect_to_json_rpc(jsonrpc)
    private_key = retrieve_private_key(keystore)

    nonce = get_nonce(web3=web3,
                      nonce=nonce,
                      auto_nonce=auto_nonce,
                      private_key=private_key)
    transaction_options = build_transaction_options(gas=gas,
                                                    gas_price=gas_price,
                                                    nonce=nonce)

    airdrop_data = load_airdrop_file(airdrop_file_name)
    airdrop_items = to_items(airdrop_data)
    merkle_root = compute_merkle_root(airdrop_items)

    constructor_args = (
        token_address,
        sum_of_airdropped_tokens(airdrop_items),
        merkle_root,
        decay_start_time,
        decay_duration,
    )

    merkle_drop = deploy_merkle_drop(
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
        constructor_args=constructor_args,
    )

    click.echo(f"MerkleDrop address: {merkle_drop.address}")
    click.echo(f"Merkle root: {encode_hex(merkle_root)}")
Esempio n. 3
0
    def save_date(self, feed, date: pendulum.DateTime):
        """
        Save the date for the current event.
        """
        # just in case someone didn't follow the type hints
        if isinstance(date, datetime.datetime):
            date = pendulum.from_timestamp(date.timestamp())

        datafile = os.path.join(self.basepath, f'last.{feed}.dat')
        with open(datafile, 'w') as f:
            f.write(str(date.in_tz('UTC')))
Esempio n. 4
0
    async def _query_data_points_between(
            self, start_datetime: pendulum.DateTime,
            stop_datetime: pendulum.DateTime) -> List[Tuple]:
        log.info(
            f'Querying database for datapoints between {start_datetime} and {stop_datetime}.'
        )

        start_timestamp = start_datetime.timestamp()
        stop_timestamp = stop_datetime.timestamp()

        with sqlite3.connect(self.database.name) as connection:
            cursor = connection.cursor()

            args = (start_timestamp, stop_timestamp)

            cursor.execute(
                'SELECT * FROM sensor_data WHERE timestamp_value BETWEEN ? AND ?',
                args)

            rows = cursor.fetchall()

            return rows
Esempio n. 5
0
def chunk_date_range(start_date: DateTime, interval=1) -> Iterable[Mapping[str, any]]:
    """
    Returns a list of the beginning and ending timetsamps of each day between the start date and now.
    The return value is a list of dicts {'oldest': float, 'latest': float} which can be used directly with the Slack API
    """
    intervals = []
    now = pendulum.now()
    # Each stream_slice contains the beginning and ending timestamp for a 24 hour period
    while start_date <= now:
        end = start_date.add(days=interval)
        intervals.append({"oldest": start_date.timestamp(), "latest": end.timestamp()})
        start_date = start_date.add(days=1)

    return intervals
Esempio n. 6
0
def get_offsets(
    subreddit: str,
    after: pendulum.DateTime,
    before: pendulum.DateTime,
    sample_size: int,
    PUSHSHIFT_LIMIT: int,
) -> list[pendulum.DateTime]:
    """For sampling, return a set of hourly offsets, beginning near
    after, that should not overlap"""

    duration = before - after
    info(f"{duration.in_days()=}")
    info(f"{duration.in_hours()=}")
    info(f"{duration.in_weeks()=}")
    results_total = get_pushshift_total(subreddit, after, before)
    results_per_hour = math.ceil(results_total / duration.in_hours())
    info(f"{results_per_hour=} on average")

    info(f"{PUSHSHIFT_LIMIT=}")
    info(f"{sample_size=}")
    queries_total = math.ceil(sample_size / PUSHSHIFT_LIMIT)
    info(f"{queries_total=}")
    info(f"{range(duration.in_hours())=}")

    SEEDS_TO_TRY = 300
    seed = int(after.timestamp())
    for seed_counter in range(SEEDS_TO_TRY):
        seed += seed_counter  # increment seed
        warning(f"attempt {seed_counter} to find non-overlapping offsets")
        offsets = get_cacheable_randos(duration.in_hours(), queries_total, seed)
        if is_overlapping(offsets, PUSHSHIFT_LIMIT, results_per_hour):
            critical(f"  seed attempt {seed_counter} failed")
            continue
        else:
            break
    else:
        print(
            f"I exhausted random sets of offsets at {SEEDS_TO_TRY=}"
            f"Quitting because I'm too likely to pull overlapping results"
        )
        raise RuntimeError

    offsets_as_datetime = []
    for offset_as_hour in offsets:
        offset_as_datetime = after.add(hours=offset_as_hour)
        offsets_as_datetime.append(offset_as_datetime)
    info(f"{len(offsets)=}")
    return offsets_as_datetime
Esempio n. 7
0
    def get_offsets_closest_to_timestamp(
            self, topic_name: str,
            timestamp: pendulum.DateTime) -> Dict[int, OffsetWithTimestamp]:
        """
        Gets the offsets of the message(s) in `topic_name` whose timestamps are at, or right after, the given `timestamp`.
        The timestamps given in the result are the actual timestamp of the offset that was found.
        If there is no message at or after the given `timestamp`, the resulting offset will be `-1` i.e. end of topic
        partition _not including_ the last message.

        :param topic_name: The topic to get the offsets for.
        :param timestamp: The timestamp to find offsets for.
        :return: Dict: partition id -> offset with timestamp.
        """

        config = self.config.create_kafka_python_config()
        with closing(kafka.KafkaConsumer(**config)) as consumer:
            topic_partitions = [
                kafka.TopicPartition(topic=topic_name, partition=partition)
                for partition in consumer.partitions_for_topic(topic_name)
            ]
            timestamp_ms = int(timestamp.timestamp() * 1000)
            offsets: Dict[
                kafka.TopicPartition,
                kafka.structs.OffsetAndTimestamp] = consumer.offsets_for_times(
                    {tp: timestamp_ms
                     for tp in topic_partitions})

        data: Dict[int, OffsetWithTimestamp] = {}
        for tp, offset_data in offsets.items():
            if offset_data is None:
                data[tp.partition] = OffsetWithTimestamp(
                    topic=tp.topic,
                    partition=tp.partition,
                    offset=OFFSET_END,
                    timestamp_ms=None)
            else:
                data[tp.partition] = OffsetWithTimestamp(
                    topic=tp.topic,
                    partition=tp.partition,
                    offset=offset_data.offset,
                    timestamp_ms=offset_data.timestamp,
                )
        return data
Esempio n. 8
0
def deploy(
    start_price: int,
    auction_duration: int,
    minimal_number_of_participants: int,
    maximal_number_of_participants: int,
    release_timestamp: int,
    release_date: pendulum.DateTime,
    keystore: str,
    jsonrpc: str,
    gas: int,
    gas_price: int,
    nonce: int,
    auto_nonce: bool,
) -> None:

    if release_date is not None and release_timestamp is not None:
        raise click.BadParameter(
            f"Both --release-date and --release-timestamp have been specified")
    if release_date is None and release_timestamp is None:
        raise click.BadParameter(
            f"Please specify a release date with --release-date or --release-timestamp"
        )

    if release_date is not None:
        release_timestamp = int(release_date.timestamp())

    web3 = connect_to_json_rpc(jsonrpc)
    private_key = retrieve_private_key(keystore)

    auction_options = AuctionOptions(
        start_price * ETH_IN_WEI,
        auction_duration,
        minimal_number_of_participants,
        maximal_number_of_participants,
        release_timestamp,
    )

    nonce = get_nonce(web3=web3,
                      nonce=nonce,
                      auto_nonce=auto_nonce,
                      private_key=private_key)
    transaction_options = build_transaction_options(gas=gas,
                                                    gas_price=gas_price,
                                                    nonce=nonce)

    contracts = deploy_auction_contracts(
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
        auction_options=auction_options,
    )

    initialize_auction_contracts(
        web3=web3,
        transaction_options=transaction_options,
        contracts=contracts,
        release_timestamp=release_timestamp,
        private_key=private_key,
    )

    click.echo("Auction address: " + contracts.auction.address)
    click.echo("Deposit locker address: " + contracts.locker.address)
    click.echo("Validator slasher address: " + contracts.slasher.address)
Esempio n. 9
0
def deploy(
    start_price: int,
    auction_duration: int,
    minimal_number_of_participants: int,
    maximal_number_of_participants: int,
    use_token: bool,
    token_address: Optional[str],
    release_timestamp: int,
    release_date: pendulum.DateTime,
    keystore: str,
    jsonrpc: str,
    gas: int,
    gas_price: int,
    nonce: int,
    auto_nonce: bool,
    already_deployed_auction,
    already_deployed_locker,
    already_deployed_slasher,
) -> None:

    if use_token and token_address is None:
        raise click.BadParameter(
            "The flag `use-token` was provided, the token address must also be provided via `token-address`"
        )
    if token_address is not None and not use_token:
        raise click.BadParameter(
            "A token address has been provided, "
            "please use the flag --use-token to confirm you want to deploy a token auction"
        )

    if release_date is not None and release_timestamp is not None:
        raise click.BadParameter(
            "Both --release-date and --release-timestamp have been specified")
    if release_date is None and release_timestamp is None:
        raise click.BadParameter(
            "Please specify a release date with --release-date or --release-timestamp"
        )

    if already_deployed_auction is not None and already_deployed_locker is None:
        raise click.BadOptionUsage(
            "--auction",
            "Cannot resume deployment from already deployed auction without already deployed locker. "
            "Locker address is part of auction's constructor argument.",
        )

    if release_date is not None:
        release_timestamp = int(release_date.timestamp())

    web3 = connect_to_json_rpc(jsonrpc)
    private_key = retrieve_private_key(keystore)

    auction_options = AuctionOptions(
        start_price * ETH_IN_WEI,
        auction_duration,
        minimal_number_of_participants,
        maximal_number_of_participants,
        release_timestamp,
        token_address,
    )

    nonce = get_nonce(web3=web3,
                      nonce=nonce,
                      auto_nonce=auto_nonce,
                      private_key=private_key)
    transaction_options = build_transaction_options(gas=gas,
                                                    gas_price=gas_price,
                                                    nonce=nonce)

    contracts = deploy_auction_contracts(
        web3=web3,
        transaction_options=transaction_options,
        private_key=private_key,
        auction_options=auction_options,
        already_deployed_contracts=DeployedContractsAddresses(
            already_deployed_locker, already_deployed_slasher,
            already_deployed_auction),
    )

    initialize_auction_contracts(
        web3=web3,
        transaction_options=transaction_options,
        contracts=contracts,
        release_timestamp=release_timestamp,
        token_address=token_address,
        private_key=private_key,
    )
    slasher: Contract = contracts.slasher

    click.echo("Auction address: " + contracts.auction.address)
    click.echo("Deposit locker address: " + contracts.locker.address)
    click.echo("Validator slasher address: " + slasher.address)
    warning_messages = get_errors_messages_on_contracts_links(contracts)
    if warning_messages:
        warning_messages.append(
            "Verify what is wrong with `auction-deploy status`.")
        click.secho(linesep.join(warning_messages), fg="red")
Esempio n. 10
0
 def __init__(self, default_start_date: DateTime, **kwargs):
     self._start_ts = default_start_date.timestamp()
     super().__init__(**kwargs)
Esempio n. 11
0
def currencynetwork(
    name: str,
    symbol: str,
    decimals: int,
    jsonrpc: str,
    fee_rate: float,
    default_interest_rate: float,
    custom_interests: bool,
    prevent_mediator_interests: bool,
    exchange_contract: str,
    currency_network_contract_name: str,
    expiration_time: int,
    expiration_date: pendulum.DateTime,
    gas: int,
    gas_price: int,
    nonce: int,
    auto_nonce: bool,
    keystore: str,
):
    """Deploy a currency network contract with custom settings and optionally connect it to an exchange contract"""
    if exchange_contract is not None and not is_checksum_address(
            exchange_contract):
        raise click.BadParameter(
            "{} is not a valid address.".format(exchange_contract))

    if custom_interests and default_interest_rate != 0.0:
        raise click.BadParameter("Custom interests can only be set without a"
                                 " default interest rate, but was {}%.".format(
                                     default_interest_rate))

    if prevent_mediator_interests and not custom_interests:
        raise click.BadParameter(
            "Prevent mediator interests is not necessary if custom interests are disabled."
        )

    if expiration_date is not None and expiration_time is not None:
        raise click.BadParameter(
            f"Both --expiration-date and --expiration-times have been specified."
        )

    if expiration_date is None and expiration_time is None:
        expiration_time = 0

    if expiration_date is not None:
        expiration_time = int(expiration_date.timestamp())

    fee_divisor = 1 / fee_rate * 100 if fee_rate != 0 else 0
    if int(fee_divisor) != fee_divisor:
        raise click.BadParameter("This fee rate is not usable")
    fee_divisor = int(fee_divisor)

    default_interest_rate = default_interest_rate * 100
    if int(default_interest_rate) != default_interest_rate:
        raise click.BadParameter("This default interest rate is not usable")
    default_interest_rate = int(default_interest_rate)

    web3 = connect_to_json_rpc(jsonrpc)
    private_key = retrieve_private_key(keystore)
    nonce = get_nonce(web3=web3,
                      nonce=nonce,
                      auto_nonce=auto_nonce,
                      private_key=private_key)
    transaction_options = build_transaction_options(gas=gas,
                                                    gas_price=gas_price,
                                                    nonce=nonce)

    contract = deploy_network(
        web3,
        name,
        symbol,
        decimals,
        fee_divisor=fee_divisor,
        default_interest_rate=default_interest_rate,
        custom_interests=custom_interests,
        prevent_mediator_interests=prevent_mediator_interests,
        exchange_address=exchange_contract,
        currency_network_contract_name=currency_network_contract_name,
        expiration_time=expiration_time,
        transaction_options=transaction_options,
        private_key=private_key,
    )

    click.echo("CurrencyNetwork(name={name}, symbol={symbol}, "
               "decimals={decimals}, fee_divisor={fee_divisor}, "
               "default_interest_rate={default_interest_rate}, "
               "custom_interests={custom_interests}, "
               "prevent_mediator_interests={prevent_mediator_interests}, "
               "exchange_address={exchange_address}): {address}".format(
                   name=name,
                   symbol=symbol,
                   decimals=decimals,
                   fee_divisor=fee_divisor,
                   default_interest_rate=default_interest_rate,
                   custom_interests=custom_interests,
                   prevent_mediator_interests=prevent_mediator_interests,
                   exchange_address=exchange_contract,
                   address=to_checksum_address(contract.address),
               ))
Esempio n. 12
0
def currency_network_proxy(
    name: str,
    symbol: str,
    decimals: int,
    jsonrpc: str,
    fee_rate: float,
    default_interest_rate: float,
    custom_interests: bool,
    prevent_mediator_interests: bool,
    expiration_time: int,
    expiration_date: pendulum.DateTime,
    beacon_address: str,
    owner_address: str,
    gas: int,
    gas_price: int,
    nonce: int,
    keystore: str,
):
    """Deploy an AdministrativeProxy contract eventually pointing towards a currency network contract
    with custom network settings and proxy owner.
    If the currency network contract is of type AdministrativeProxy,
    one may need to unfreeze it and remove the owner to use it."""
    if custom_interests and default_interest_rate != 0.0:
        raise click.BadParameter(
            "Custom interests can only be set without a"
            " default interest rate, but was {}%.".format(default_interest_rate)
        )

    if prevent_mediator_interests and not custom_interests:
        raise click.BadParameter(
            "Prevent mediator interests is not necessary if custom interests are disabled."
        )

    if expiration_date is not None and expiration_time is not None:
        raise click.BadParameter(
            "Both --expiration-date and --expiration-times have been specified."
        )

    if expiration_date is None and expiration_time is None:
        expiration_time = 0

    if expiration_date is not None:
        expiration_time = int(expiration_date.timestamp())

    fee_divisor = 1 / fee_rate * 100 if fee_rate != 0 else 0
    if int(fee_divisor) != fee_divisor:
        raise click.BadParameter("This fee rate is not usable")
    fee_divisor = int(fee_divisor)

    default_interest_rate = default_interest_rate * 100
    if int(default_interest_rate) != default_interest_rate:
        raise click.BadParameter("This default interest rate is not usable")
    default_interest_rate = int(default_interest_rate)

    web3 = connect_to_json_rpc(jsonrpc)
    private_key = retrieve_private_key(keystore)
    nonce = get_nonce(web3=web3, nonce=nonce, private_key=private_key)
    transaction_options = build_transaction_options(
        gas=gas, gas_price=gas_price, nonce=nonce
    )

    network_settings = NetworkSettings(
        name=name,
        symbol=symbol,
        decimals=decimals,
        fee_divisor=fee_divisor,
        default_interest_rate=default_interest_rate,
        custom_interests=custom_interests,
        prevent_mediator_interests=prevent_mediator_interests,
        expiration_time=expiration_time,
    )

    contract = deploy_currency_network_proxy(
        web3=web3,
        network_settings=network_settings,
        beacon_address=beacon_address,
        owner_address=owner_address,
        private_key=private_key,
        transaction_options=transaction_options,
    )

    click.echo(
        "CurrencyNetwork(name={name}, symbol={symbol}, "
        "decimals={decimals}, fee_divisor={fee_divisor}, "
        "default_interest_rate={default_interest_rate}, "
        "custom_interests={custom_interests}, "
        "prevent_mediator_interests={prevent_mediator_interests}): {address}".format(
            name=name,
            symbol=symbol,
            decimals=decimals,
            fee_divisor=fee_divisor,
            default_interest_rate=default_interest_rate,
            custom_interests=custom_interests,
            prevent_mediator_interests=prevent_mediator_interests,
            address=to_checksum_address(contract.address),
        )
    )
Esempio n. 13
0
 def __init__(self, default_start_date: DateTime, **kwargs):
     self._start_ts = default_start_date.timestamp()
     self.set_sub_primary_key()
     super().__init__(**kwargs)