Skip to content

Commit

Permalink
Merge pull request #11493 from AmineKhaldi/nft0_cli_fees
Browse files Browse the repository at this point in the history
Add the ability to set fees from CLI for minting NFTs, transferring NFTs and adding URIs to NFTs
  • Loading branch information
trepca authored May 11, 2022
2 parents bbcf6fd + 50f8ab9 commit 15dfb0a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
33 changes: 33 additions & 0 deletions chia/cmds/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,13 +490,23 @@ 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,
id: int,
artist_address: str,
hash: str,
uris: str,
fee: str,
) -> None:
import asyncio
from .wallet_funcs import execute_with_wallet, mint_nft
Expand All @@ -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))

Expand All @@ -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
Expand All @@ -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))

Expand All @@ -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
Expand All @@ -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))

Expand Down
9 changes: 6 additions & 3 deletions chia/cmds/wallet_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions chia/rpc/wallet_rpc_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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}")
Expand All @@ -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}")
Expand Down
23 changes: 17 additions & 6 deletions chia/rpc/wallet_rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 15dfb0a

Please sign in to comment.