From 102280ed4147972bd52b504697ec155b337de7ff Mon Sep 17 00:00:00 2001 From: Amine Khaldi Date: Wed, 11 May 2022 21:03:32 +0100 Subject: [PATCH 1/2] Add the ability to set fees for minting NFTs, transferring NFTs and adding URIs to NFTs. --- chia/cmds/wallet.py | 33 +++++++++++++++++++++++++++++++++ chia/cmds/wallet_funcs.py | 9 ++++++--- chia/rpc/wallet_rpc_client.py | 23 +++++++++++++++++------ 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/chia/cmds/wallet.py b/chia/cmds/wallet.py index 120cd90ebe37..b277f0f5be9e 100644 --- a/chia/cmds/wallet.py +++ b/chia/cmds/wallet.py @@ -490,6 +490,15 @@ def nft_wallet_create_cmd(wallet_rpc_port: Optional[int], fingerprint: int) -> N @click.option("-aa", "--artist-address", help="Artist's backpayment address", type=str, required=True) @click.option("-nh", "--hash", help="NFT content hash", type=str, required=True) @click.option("-u", "--uris", help="Comma separated list of URIs", type=str, required=True) +@click.option( + "-m", + "--fee", + help="Set the fees per transaction, in XCH.", + type=str, + default="0", + show_default=True, + callback=validate_fee, +) def nft_mint_cmd( wallet_rpc_port: Optional[int], fingerprint: int, @@ -497,6 +506,7 @@ def nft_mint_cmd( artist_address: str, hash: str, uris: str, + fee: str, ) -> None: import asyncio from .wallet_funcs import execute_with_wallet, mint_nft @@ -506,6 +516,7 @@ def nft_mint_cmd( "artist_address": artist_address, "hash": hash, "uris": [u.strip() for u in uris.split(",")], + "fee": fee, } asyncio.run(execute_with_wallet(wallet_rpc_port, fingerprint, extra_params, mint_nft)) @@ -522,12 +533,22 @@ def nft_mint_cmd( @click.option("-i", "--id", help="Id of the NFT wallet to use", type=int, required=True) @click.option("-ni", "--nft-coin-id", help="Id of the NFT coin to add the URI to", type=str, required=True) @click.option("-u", "--uri", help="URI to add to the NFT", type=str, required=True) +@click.option( + "-m", + "--fee", + help="Set the fees per transaction, in XCH.", + type=str, + default="0", + show_default=True, + callback=validate_fee, +) def nft_add_uri_cmd( wallet_rpc_port: Optional[int], fingerprint: int, id: int, nft_coin_id: str, uri: str, + fee: str, ) -> None: import asyncio from .wallet_funcs import execute_with_wallet, add_uri_to_nft @@ -536,6 +557,7 @@ def nft_add_uri_cmd( "wallet_id": id, "nft_coin_id": nft_coin_id, "uri": uri, + "fee": fee, } asyncio.run(execute_with_wallet(wallet_rpc_port, fingerprint, extra_params, add_uri_to_nft)) @@ -552,12 +574,22 @@ def nft_add_uri_cmd( @click.option("-i", "--id", help="Id of the NFT wallet to use", type=int, required=True) @click.option("-ni", "--nft-coin-id", help="Id of the NFT coin to transfer", type=str, required=True) @click.option("-aa", "--artist-address", help="Target artist's wallet address", type=str, required=True) +@click.option( + "-m", + "--fee", + help="Set the fees per transaction, in XCH.", + type=str, + default="0", + show_default=True, + callback=validate_fee, +) def nft_transfer_cmd( wallet_rpc_port: Optional[int], fingerprint: int, id: int, nft_coin_id: str, artist_address: str, + fee: str, ) -> None: import asyncio from .wallet_funcs import execute_with_wallet, transfer_nft @@ -566,6 +598,7 @@ def nft_transfer_cmd( "wallet_id": id, "nft_coin_id": nft_coin_id, "artist_address": artist_address, + "fee": fee, } asyncio.run(execute_with_wallet(wallet_rpc_port, fingerprint, extra_params, transfer_nft)) diff --git a/chia/cmds/wallet_funcs.py b/chia/cmds/wallet_funcs.py index a726c38f883f..411cf1748dde 100644 --- a/chia/cmds/wallet_funcs.py +++ b/chia/cmds/wallet_funcs.py @@ -684,7 +684,8 @@ async def mint_nft(args: Dict, wallet_client: WalletRpcClient, fingerprint: int) artist_address = args["artist_address"] hash = args["hash"] uris = args["uris"] - response = await wallet_client.mint_nft(wallet_id, artist_address, hash, uris) + fee = args["fee"] + response = await wallet_client.mint_nft(wallet_id, artist_address, hash, uris, fee) spend_bundle = response["spend_bundle"] print(f"NFT minted Successfully with spend bundle: {spend_bundle}") except Exception as e: @@ -696,7 +697,8 @@ async def add_uri_to_nft(args: Dict, wallet_client: WalletRpcClient, fingerprint wallet_id = args["wallet_id"] nft_coin_id = args["nft_coin_id"] uri = args["uri"] - response = await wallet_client.add_uri_to_nft(wallet_id, nft_coin_id, uri) + fee = args["fee"] + response = await wallet_client.add_uri_to_nft(wallet_id, nft_coin_id, uri, fee) spend_bundle = response["spend_bundle"] print(f"URI added successfully with spend bundle: {spend_bundle}") except Exception as e: @@ -708,7 +710,8 @@ async def transfer_nft(args: Dict, wallet_client: WalletRpcClient, fingerprint: wallet_id = args["wallet_id"] nft_coin_id = args["nft_coin_id"] artist_address = args["artist_address"] - response = await wallet_client.transfer_nft(wallet_id, nft_coin_id, artist_address) + fee = args["fee"] + response = await wallet_client.transfer_nft(wallet_id, nft_coin_id, artist_address, fee) spend_bundle = response["spend_bundle"] print(f"NFT transferred successfully with spend bundle: {spend_bundle}") except Exception as e: diff --git a/chia/rpc/wallet_rpc_client.py b/chia/rpc/wallet_rpc_client.py index 8831b2f97120..abc3661d070e 100644 --- a/chia/rpc/wallet_rpc_client.py +++ b/chia/rpc/wallet_rpc_client.py @@ -570,18 +570,29 @@ async def create_new_nft_wallet(self, did_wallet_id): response = await self.fetch("create_new_wallet", request) return response - async def mint_nft(self, wallet_id, artist_address, hash, uris): - request: Dict[str, Any] = {"wallet_id": wallet_id, "artist_address": artist_address, "hash": hash, "uris": uris} + async def mint_nft(self, wallet_id, artist_address, hash, uris, fee): + request: Dict[str, Any] = { + "wallet_id": wallet_id, + "artist_address": artist_address, + "hash": hash, + "uris": uris, + "fee": fee, + } response = await self.fetch("nft_mint_nft", request) return response - async def add_uri_to_nft(self, wallet_id, nft_coin_id, uri): - request: Dict[str, Any] = {"wallet_id": wallet_id, "nft_coin_id": nft_coin_id, "uri": uri} + async def add_uri_to_nft(self, wallet_id, nft_coin_id, uri, fee): + request: Dict[str, Any] = {"wallet_id": wallet_id, "nft_coin_id": nft_coin_id, "uri": uri, "fee": fee} response = await self.fetch("nft_add_uri", request) return response - async def transfer_nft(self, wallet_id, nft_coin_id, artist_address): - request: Dict[str, Any] = {"wallet_id": wallet_id, "nft_coin_id": nft_coin_id, "target_address": artist_address} + async def transfer_nft(self, wallet_id, nft_coin_id, artist_address, fee): + request: Dict[str, Any] = { + "wallet_id": wallet_id, + "nft_coin_id": nft_coin_id, + "target_address": artist_address, + "fee": fee, + } response = await self.fetch("nft_transfer_nft", request) return response From 50f8ab9d2714ede8ad0f3ddc1bb778c9de99fcf5 Mon Sep 17 00:00:00 2001 From: Amine Khaldi Date: Wed, 11 May 2022 21:48:38 +0100 Subject: [PATCH 2/2] Fix handling fees on the RPC API side. --- chia/rpc/wallet_rpc_api.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/chia/rpc/wallet_rpc_api.py b/chia/rpc/wallet_rpc_api.py index 1b73cc674fa2..445f774f19d1 100644 --- a/chia/rpc/wallet_rpc_api.py +++ b/chia/rpc/wallet_rpc_api.py @@ -1334,7 +1334,8 @@ async def nft_mint_nft(self, request) -> Dict: ("h", hexstr_to_bytes(request["hash"])), ] ) - spend_bundle = await nft_wallet.generate_new_nft(metadata, puzzle_hash, request.get("fee", uint64(0))) + fee = uint64(request.get("fee", 0)) + spend_bundle = await nft_wallet.generate_new_nft(metadata, puzzle_hash, fee=fee) return {"wallet_id": wallet_id, "success": True, "spend_bundle": spend_bundle} async def nft_get_nfts(self, request) -> Dict: @@ -1358,7 +1359,8 @@ async def nft_transfer_nft(self, request): nft_wallet: NFTWallet = self.service.wallet_state_manager.wallets[wallet_id] try: nft_coin_info = nft_wallet.get_nft_coin_by_id(bytes32.from_hexstr(request["nft_coin_id"])) - spend_bundle = await nft_wallet.transfer_nft(nft_coin_info, puzzle_hash, fee=request.get("fee", uint64(0))) + fee = uint64(request.get("fee", 0)) + spend_bundle = await nft_wallet.transfer_nft(nft_coin_info, puzzle_hash, fee=fee) return {"wallet_id": wallet_id, "success": True, "spend_bundle": spend_bundle} except Exception as e: log.exception(f"Failed to transfer NFT: {e}") @@ -1371,7 +1373,8 @@ async def nft_add_uri(self, request) -> Dict: nft_wallet: NFTWallet = self.service.wallet_state_manager.wallets[wallet_id] try: nft_coin_info = nft_wallet.get_nft_coin_by_id(bytes32.from_hexstr(request["nft_coin_id"])) - spend_bundle = await nft_wallet.update_metadata(nft_coin_info, uri, request.get("fee", uint64(0))) + fee = uint64(request.get("fee", 0)) + spend_bundle = await nft_wallet.update_metadata(nft_coin_info, uri, fee=fee) return {"wallet_id": wallet_id, "success": True, "spend_bundle": spend_bundle} except Exception as e: log.exception(f"Failed to update NFT metadata: {e}")