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")
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))
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")