示例#1
0
class Allocation:
    """Payment allocation managment.

    """

    def __init__(self, appkey: Optional[str] = None):
        self._cli = Configuration(app_key=appkey)

    @async_run
    async def new(self, amount: str):
        console = Console()
        async with self._cli.payment() as p:
            p = Payment(p)
            free_allocation = await p.new_allocation(amount=Decimal(amount)).detach()
            console.print(free_allocation)

    @async_run
    async def list(self, details: bool = False):
        "Lists current active payment allocation"

        console = Console()
        console.print(f"[bold]details[/bold]: {details}")
        table = Table(show_header=True, header_style="bold yellow")
        table.add_column("Id")
        table.add_column("Amount")
        table.add_column("Expires")

        async with self._cli.payment() as p:
            p = Payment(p)
            async for allocation in p.allocations():
                table.add_row(
                    allocation.id,
                    str(allocation.amount),
                    str(allocation.expires) if allocation.expires else None,
                )

            if table.row_count:
                console.print(table)
            else:
                console.print("[bold]No allocations[/bold]")

    @async_run
    async def clear(self):
        "Removes all active payment allocations"

        console = Console()
        async with self._cli.payment() as client:
            p = Payment(client)
            allocations = [a async for a in p.allocations()]
            for allocation in track(allocations, description="cleaning allocations"):
                await allocation.delete()
                await sleep(0.2)
        console.print(f"{len(allocations)} removed")
示例#2
0
文件: market.py 项目: prekucki/yapapi
class Demand:
    """Offer subscription managment.

    """
    def __init__(self, appkey: Optional[str] = None):
        self._cli = Configuration(app_key=appkey)
        self.create = Create

    @async_run
    async def list(self, only_expired: bool = False):
        """Lists all active demands"""
        console = Console()
        async with self._cli.market() as client:
            market_api = Market(client)
            it: int = 0
            async for demand in market_api.subscriptions():
                it += 1
                console.print(demand.details.to_dict().items())
                console.print()
            console.print(f"{it} demands running")

    @async_run
    async def clear(self, only_expired: bool = False):
        """Removes demands.

        By default removes all demands.

        :param only_expired: removes only expired demands.

        """
        console = Console()
        it = 0
        with Progress(console=console) as progress:
            tid = progress.add_task("dropping demands", start=False)
            async with self._cli.market() as client:
                market_api = Market(client)
                async for demand in market_api.subscriptions():
                    await demand.delete()
                    it += 1
                    progress.advance(tid)
                    await sleep(0.2)
            progress.update(tid, completed=True)
        console.print(f"{it} demands removed")
async def list_offers(conf: Configuration):
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(yp.Identification(name="some scannig node", subnet_tag="testnet"))
        dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc)))

        async with market_api.subscribe(dbuild.props, dbuild.cons) as subscription:
            async for event in subscription.events():
                print(f"Offer: {event.id}")
                print(f"from {event.issuer}")
                print(f"props {json.dumps(event.props, indent=4)}")
                print("\n\n")
        print("done")
示例#4
0
def main():
    subnet = "goth"

    enable_default_logger()
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                renegotiate_offers(
                    Configuration(),
                    subnet_tag=subnet,
                ),
                timeout=140,
            ))
    except TimeoutError:
        print("Main timeout triggered :(")
示例#5
0
def main():
    parser = utils.build_parser("List offers")
    args = parser.parse_args()

    subnet = args.subnet_tag
    sys.stderr.write(f"Using subnet: {utils.TEXT_COLOR_YELLOW}{subnet}{utils.TEXT_COLOR_DEFAULT}\n")

    enable_default_logger()
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag=subnet,
                ),
                timeout=4,
            )
        )
    except TimeoutError:
        pass
示例#6
0
async def list_offers(conf: Configuration, subnet_tag: str):
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(yp.NodeInfo(
            name="Golem Stats Indexer", subnet_tag=subnet_tag))
        dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc)))
        async with market_api.subscribe(dbuild.properties, dbuild.constraints) as subscription:
            async for event in subscription.events():
                if event.props['golem.runtime.name'] != "wasmtime":

                    if event.issuer in str(test):
                        continue
                    else:
                        data = event.props
                        if event.props['golem.runtime.name'] == "gminer":
                            try:
                                data["wallet"] = event.props['golem.com.payment.platform.polygon-polygon-glm.address']
                            except:
                                data['wallet'] = event.props["golem.com.payment.platform.erc20-polygon-glm.address"]

                            data['golem.node.debug.subnet'] = "Thorg"
                            data['id'] = event.issuer
                            test.append(json.dumps(data))
                        if event.props['golem.runtime.name'] == "hminer":
                            data['wallet'] = event.props['golem.com.payment.platform.polygon-polygon-glm.address']
                            data['golem.node.debug.subnet'] = "Thorg"
                            data['id'] = event.issuer
                            test.append(json.dumps(data))
                        if "golem.com.payment.platform.zksync-mainnet-glm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.zksync-mainnet-glm.address']
                        elif "golem.com.payment.platform.zksync-rinkeby-tglm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.zksync-rinkeby-tglm.address']
                        elif "golem.com.payment.platform.erc20-mainnet-glm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.erc20-mainnet-glm.address']
                        elif "golem.com.payment.platform.erc20-polygon-glm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.erc20-polygon-glm.address']
                        elif "golem.com.payment.platform.erc20-rinkeby-tglm.address" in str(event.props):
                            data['wallet'] = event.props['golem.com.payment.platform.erc20-rinkeby-tglm.address']
                        data['id'] = event.issuer
                        test.append(json.dumps(data))
示例#7
0
def main():
    import pathlib
    import sys

    parent_directory = pathlib.Path(__file__).resolve().parent.parent
    sys.stderr.write(f"Adding {parent_directory} to sys.path.\n")
    sys.path.append(str(parent_directory))
    import utils

    parser = utils.build_parser("List offers")
    args = parser.parse_args()

    subnet = args.subnet_tag
    sys.stderr.write(f"Using subnet: {utils.TEXT_COLOR_YELLOW}{subnet}{utils.TEXT_COLOR_DEFAULT}\n")

    enable_default_logger()
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(list_offers(Configuration(), subnet_tag=subnet,), timeout=4,)
        )
    except TimeoutError:
        pass
示例#8
0
 def __init__(self, appkey: Optional[str] = None):
     self._cli = Configuration(app_key=appkey)
     self.create = Create
示例#9
0
async def yapapi_payment(request):
    conf = Configuration(app_key=request.config.getvalue("yaApiKey"))
    async with conf.payment() as p:
        yield Payment(p)
示例#10
0
class Invoices:
    """Invoice managment.

    """

    def __init__(self, appkey: Optional[str] = None):
        self._cli = Configuration(app_key=appkey)

    @async_run
    async def incoming(self):
        console = Console()
        async with self._cli.payment() as client:
            p = Payment(client)
            async for invoice in p.incoming_invoices():
                console.print(invoice)
        console.print("done")

    @async_run
    async def accept(self, allocation_id: str, invoice_id: str):
        """
        Accepts given `invoice_id` with `allocation_id`

        :param allocation_id: Allocation from which invoice will be paid. see
            `allocation list`.
        :param invoice_id: Invoice identifier.
        """
        console = Console()
        async with self._cli.payment() as client:
            p = Payment(client)
            invoice = await p.invoice(invoice_id)
            allocation = await p.allocation(allocation_id)
            await invoice.accept(amount=invoice.amount, allocation=allocation)
        console.print("done")

    @async_run
    async def list(self, by_status: Optional[str] = None):
        """
        Lists all invoices.
        """
        from rich.columns import Columns
        from rich.text import Text

        def status_to_color(status: payment.InvoiceStatus) -> Optional[str]:
            return INVOICE_STATUS_COLOR.get(str(status), None)

        def filter_invoice(invoice: payment.Invoice) -> bool:
            if by_status:
                return str(invoice.status) == by_status
            return True

        def format_invoice(invoice: payment.Invoice):
            status_style = status_to_color(invoice.status) or ""
            table = Table(
                "[yellow]Attribute[/yellow]",
                "[yellow]Value[/yellow]",
                header_style="bold yellow",
                title=invoice.invoice_id,
                style=status_style,
                title_style=status_style,
            )
            table.add_row(Text.assemble("issuer", style="bold"), invoice.issuer_id)
            date_txt = str(invoice.timestamp.date())
            time_txt = str(invoice.timestamp.time())
            table.add_row("ts", f"{date_txt} [dim]{time_txt}[/dim]")

            table.add_row("amount", invoice.amount)
            table.add_row("status", Text.assemble(str(invoice.status), style=status_style))
            return table

        console = Console()
        async with self._cli.payment() as client:
            p = Payment(client)
            console.print(
                Columns(
                    [
                        format_invoice(invoice)
                        async for invoice in p.invoices()
                        if filter_invoice(invoice)
                    ],
                    width=60,
                    padding=(2, 1),
                )
            )
示例#11
0
from yapapi import props as yp
from yapapi.props.builder import DemandBuilder
from datetime import datetime, timezone
import json
import asyncio
from asyncio import TimeoutError


async def list_offers(conf: Configuration):
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(yp.Identification(name="some scannig node", subnet_tag="testnet"))
        dbuild.add(yp.Activity(expiration=datetime.now(timezone.utc)))

        async with market_api.subscribe(dbuild.props, dbuild.cons) as subscription:
            async for event in subscription.events():
                print(f"Offer: {event.id}")
                print(f"from {event.issuer}")
                print(f"props {json.dumps(event.props, indent=4)}")
                print("\n\n")
        print("done")


try:
    asyncio.get_event_loop().run_until_complete(
        asyncio.wait_for(list_offers(Configuration()), timeout=4)
    )
except TimeoutError:
    pass
示例#12
0
def main():
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag="public-beta",
                ),
                timeout=15,
            )
        )
    except TimeoutError:
        pass

    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag="devnet-beta.1",
                ),
                timeout=15,
            )
        )
    except TimeoutError:
        pass
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag="aarch64-network",
                ),
                timeout=15,
            )
        )
    except TimeoutError:
        pass
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag="LazySubnet",
                ),
                timeout=15,
            )
        )
    except TimeoutError:
        pass
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag="devnet-beta.2",
                ),
                timeout=15,
            )
        )
    except TimeoutError:
        pass
    try:
        asyncio.get_event_loop().run_until_complete(
            asyncio.wait_for(
                list_offers(
                    Configuration(),
                    subnet_tag="devnet-beta",
                ),
                timeout=15,
            )
        )
    except TimeoutError:
        pass
    serialized = json.dumps(test)
    if len(test) > 580:
        r = redis.Redis(host='redis', port=6379, db=0)
        content = r.set("offers", serialized)
示例#13
0
async def renegotiate_offers(conf: Configuration, subnet_tag: str):
    """Rejects every proposal & then renegotiates it"""
    async with conf.market() as client:
        market_api = Market(client)
        dbuild = DemandBuilder()
        dbuild.add(
            yp.NodeInfo(name="some renegotiating node", subnet_tag=subnet_tag))
        dbuild.add(
            yp.Activity(
                expiration=datetime.datetime.now(datetime.timezone.utc) +
                datetime.timedelta(minutes=30)))

        async with market_api.subscribe(dbuild.properties,
                                        dbuild.constraints) as subscription:
            issuers = set()
            proposals = 0
            rejected_proposals = set()  # Already rejected, don't reject again
            async for event in subscription.events():
                node_name = event.props.get("golem.node.id.name")
                proposal_id = event._proposal.proposal.proposal_id
                print(f"\n[{node_name}] {'*'*15} {proposal_id}")
                prev_proposal_id = event._proposal.proposal.prev_proposal_id
                print(f"[{node_name}] prev_proposal_id: {prev_proposal_id}")
                if not event.is_draft:
                    if proposals > PROPOSALS_LIMIT:
                        print(f"[node_name] Skipping additional proposal")
                        break
                    await _respond(event, dbuild)
                    proposals += 1
                    issuers.add(event.issuer)
                    print(
                        f"[{node_name}] Responded. proposals={proposals}, issuers={len(issuers)}"
                    )
                    continue

                print(
                    f"[{node_name}] Offer: {proposal_id} from {event.issuer} is_draft: {event.is_draft}"
                )
                if prev_proposal_id not in rejected_proposals:
                    await event.reject()
                    print(
                        f"[{node_name}] Rejected {len(rejected_proposals)}. id: {proposal_id}"
                    )
                    await asyncio.sleep(1)
                    print(f"[{node_name}] Renegotiating. id: {proposal_id}")
                    new_offer_id = await _respond(event, dbuild)
                    print(f"[{node_name}] new_offer_id: {new_offer_id}")
                    rejected_proposals.add(new_offer_id)
                    continue
                print(".create_agreement()")
                agreement = await event.create_agreement()
                print(".confirm()")
                confirm_result = await agreement.confirm()
                print(f"[{node_name}] agreement.confirm(): {confirm_result}")
                if confirm_result:
                    terminate_reason = {
                        "message": "Work cancelled",
                        "golem.requestor.code": "Cancelled",
                    }
                    terminate_result = await agreement.terminate(
                        terminate_reason)
                    print(f"agreement.terminate(): {terminate_result}")
        print("All done")