Merge pull request #12004 from Chia-Network/nft_offer_cli

Add CLI support for NFT offers
This commit is contained in:
William Allen 2022-06-22 10:58:37 -05:00 committed by GitHub
commit 6c09d75531
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,7 +14,7 @@ from chia.rpc.wallet_rpc_client import WalletRpcClient
from chia.server.outbound_message import NodeType
from chia.server.start_wallet import SERVICE_NAME
from chia.types.blockchain_format.sized_bytes import bytes32
from chia.util.bech32m import encode_puzzle_hash
from chia.util.bech32m import bech32_decode, decode_puzzle_hash, encode_puzzle_hash
from chia.util.config import load_config
from chia.util.default_root import DEFAULT_ROOT_PATH
from chia.util.ints import uint16, uint32, uint64
@ -284,22 +284,65 @@ async def make_offer(args: dict, wallet_client: WalletRpcClient, fingerprint: in
print("Not creating offer: Must be offering and requesting at least one asset")
else:
offer_dict: Dict[Union[uint32, str], int] = {}
driver_dict: Dict[str, Any] = {}
printable_dict: Dict[str, Tuple[str, int, int]] = {} # Dict[asset_name, Tuple[amount, unit, multiplier]]
nft_warning: bool = False
for item in [*offers, *requests]:
wallet_id, amount = tuple(item.split(":")[0:2])
if int(wallet_id) == 1:
name: str = "XCH"
unit: int = units["chia"]
else:
name = await wallet_client.get_cat_name(wallet_id)
unit = units["cat"]
name, amount = tuple(item.split(":")[0:2])
try:
id: Union[uint32, str] = bytes32.from_hexstr(name).hex()
unit = 1
except ValueError:
try:
hrp, _ = bech32_decode(name)
if hrp == "nft":
coin_id = decode_puzzle_hash(name)
unit = 1
info = NFTInfo.from_json_dict((await wallet_client.get_nft_info(coin_id.hex()))["nft_info"])
nft_warning = True
id = info.launcher_id.hex()
assert isinstance(id, str)
if item in requests:
driver_dict[id] = {
"type": "singleton",
"launcher_id": "0x" + id,
"launcher_ph": "0x" + info.launcher_puzhash.hex(),
"also": {
"type": "metadata",
"metadata": info.chain_info,
"updater_hash": "0x" + info.updater_puzhash.hex(),
},
}
if info.supports_did:
driver_dict[id]["also"]["also"] = {
"type": "ownership",
"owner": "()",
"transfer_program": {
"type": "royalty transfer program",
"launcher_id": "0x" + info.launcher_id.hex(),
"royalty_address": "0x" + info.royalty_puzzle_hash.hex(),
"royalty_percentage": str(info.royalty_percentage),
},
}
else:
id = decode_puzzle_hash(name).hex()
assert hrp is not None
unit = units[hrp]
except ValueError:
id = uint32(int(name))
if id == 1:
name = "XCH"
unit = units["chia"]
else:
name = await wallet_client.get_cat_name(str(id))
unit = units["cat"]
multiplier: int = -1 if item in offers else 1
printable_dict[name] = (amount, unit, multiplier)
if uint32(int(wallet_id)) in offer_dict:
if id in offer_dict:
print("Not creating offer: Cannot offer and request the same asset in a trade")
break
else:
offer_dict[uint32(int(wallet_id))] = int(Decimal(amount) * unit) * multiplier
offer_dict[id] = int(Decimal(amount) * unit) * multiplier
else:
print("Creating Offer")
print("--------------")
@ -315,11 +358,22 @@ async def make_offer(args: dict, wallet_client: WalletRpcClient, fingerprint: in
if multiplier > 0:
print(f" - {amount} {name} ({int(Decimal(amount) * unit)} mojos)")
if nft_warning:
nft_confirmation = input(
"Offers for NFTs will have royalties automatically added. "
+ "Are you sure you would like to continue? (y/n): "
)
if nft_confirmation not in ["y", "yes"]:
print("Not creating offer...")
return
confirmation = input("Confirm (y/n): ")
if confirmation not in ["y", "yes"]:
print("Not creating offer...")
else:
offer, trade_record = await wallet_client.create_offer_for_ids(offer_dict, fee=fee)
offer, trade_record = await wallet_client.create_offer_for_ids(
offer_dict, driver_dict=driver_dict, fee=fee
)
if offer is not None:
with open(pathlib.Path(filepath), "w") as file:
file.write(offer.to_bech32())