Ejemplo n.º 1
0
async def take_offer(args: dict, wallet_client: WalletRpcClient,
                     fingerprint: int) -> None:
    if "." in args["file"]:
        filepath = pathlib.Path(args["file"])
        with open(filepath, "r") as file:
            offer_hex: str = file.read()
            file.close()
    else:
        offer_hex = args["file"]

    examine_only: bool = args["examine_only"]
    fee: int = int(Decimal(args["fee"]) * units["chia"])

    try:
        offer = Offer.from_bech32(offer_hex)
    except ValueError:
        print("Please enter a valid offer file or hex blob")
        return

    offered, requested = offer.summary()
    print("Summary:")
    print("  OFFERED:")
    await print_offer_summary(wallet_client, offered)
    print("  REQUESTED:")
    await print_offer_summary(wallet_client, requested)
    print(f"Fees: {Decimal(offer.bundle.fees()) / units['chia']}")

    if not examine_only:
        confirmation = input("Would you like to take this offer? (y/n): ")
        if confirmation in ["y", "yes"]:
            trade_record = await wallet_client.take_offer(offer, fee=fee)
            print(f"Accepted offer with ID {trade_record.trade_id}")
            print(
                f"Use chia wallet get_offers --id {trade_record.trade_id} -f {fingerprint} to view its status"
            )
Ejemplo n.º 2
0
 async def get_offer(self,
                     trade_id: bytes32,
                     file_contents: bool = False) -> TradeRecord:
     res = await self.fetch("get_offer", {
         "trade_id": trade_id.hex(),
         "file_contents": file_contents
     })
     offer_str = bytes(Offer.from_bech32(
         res["offer"])).hex() if file_contents else ""
     return TradeRecord.from_json_dict_convenience(res["trade_record"],
                                                   offer_str)
Ejemplo n.º 3
0
    async def create_offer_for_ids(
            self,
            offer_dict: Dict[uint32, int],
            fee=uint64(0),
            validate_only: bool = False
    ) -> Tuple[Optional[Offer], TradeRecord]:
        send_dict: Dict[str, int] = {}
        for key in offer_dict:
            send_dict[str(key)] = offer_dict[key]

        res = await self.fetch("create_offer_for_ids", {
            "offer": send_dict,
            "validate_only": validate_only,
            "fee": fee
        })
        offer: Optional[Offer] = None if validate_only else Offer.from_bech32(
            res["offer"])
        offer_str: str = "" if offer is None else bytes(offer).hex()
        return offer, TradeRecord.from_json_dict_convenience(
            res["trade_record"], offer_str)
Ejemplo n.º 4
0
    async def get_all_offers(
        self,
        start: int = 0,
        end: int = 50,
        sort_key: str = None,
        reverse: bool = False,
        file_contents: bool = False,
        exclude_my_offers: bool = False,
        exclude_taken_offers: bool = False,
        include_completed: bool = False,
    ) -> List[TradeRecord]:
        res = await self.fetch(
            "get_all_offers",
            {
                "start": start,
                "end": end,
                "sort_key": sort_key,
                "reverse": reverse,
                "file_contents": file_contents,
                "exclude_my_offers": exclude_my_offers,
                "exclude_taken_offers": exclude_taken_offers,
                "include_completed": include_completed,
            },
        )

        records = []
        if file_contents:
            optional_offers = [
                bytes(Offer.from_bech32(o)).hex() for o in res["offers"]
            ]
        else:
            optional_offers = [""] * len(res["trade_records"])
        for record, offer in zip(res["trade_records"], optional_offers):
            records.append(
                TradeRecord.from_json_dict_convenience(record, offer))

        return records