|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace chia.dotnet |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Wraps an NFT wallet |
| 11 | + /// </summary> |
| 12 | + public sealed class NFTWallet : Wallet |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// ctor |
| 16 | + /// </summary> |
| 17 | + /// <param name="walletId">The wallet_id to wrap</param> |
| 18 | + /// <param name="walletProxy">Wallet RPC proxy to use for communication</param> |
| 19 | + public NFTWallet(uint walletId, WalletProxy walletProxy) |
| 20 | + : base(walletId, walletProxy) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Validates that <see cref="Wallet.WalletId"/> is a <see cref="WalletType.NFT"/> |
| 26 | + /// </summary> |
| 27 | + /// <returns>True if the wallet is an NFT wallet</returns> |
| 28 | + public override async Task Validate(CancellationToken cancellationToken = default) |
| 29 | + { |
| 30 | + await Validate(WalletType.NFT, cancellationToken).ConfigureAwait(false); |
| 31 | + } |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Adds an Uri to an NFT |
| 35 | + /// </summary> |
| 36 | + /// <param name="uri">The uri</param> |
| 37 | + /// <param name="key">The uri key</param> |
| 38 | + /// <param name="nftCoinId">The nft coin id</param> |
| 39 | + /// <param name="fee">Transaction fee</param> |
| 40 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 41 | + /// <returns>An <see cref="SpendBundle"/></returns> |
| 42 | + public async Task<SpendBundle> AddUri(string uri, string key, string nftCoinId, ulong fee = 0, CancellationToken cancellationToken = default) |
| 43 | + { |
| 44 | + dynamic data = CreateWalletDataObject(); |
| 45 | + data.uri = uri; |
| 46 | + data.key = key; |
| 47 | + data.nft_coin_id = nftCoinId; |
| 48 | + data.fee = fee; |
| 49 | + |
| 50 | + return await WalletProxy.SendMessage<SpendBundle>("nft_add_uri", "spend_bundle", data, cancellationToken).ConfigureAwait(false); |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Gets NFTs from a wallet |
| 55 | + /// </summary> |
| 56 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 57 | + /// <returns>The DID id</returns> |
| 58 | + public async Task<IEnumerable<NFTInfo>> GetNFTs(CancellationToken cancellationToken = default) |
| 59 | + { |
| 60 | + var response = await WalletProxy.SendMessage("nft_get_nfts", CreateWalletDataObject(), cancellationToken).ConfigureAwait(false); |
| 61 | + |
| 62 | + return Converters.ToObject<IEnumerable<NFTInfo>>(response, "nft_list"); |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Gets the DID |
| 67 | + /// </summary> |
| 68 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 69 | + /// <returns>The list of NFTs</returns> |
| 70 | + public async Task<string> GetDID(CancellationToken cancellationToken = default) |
| 71 | + { |
| 72 | + var response = await WalletProxy.SendMessage("nft_get_wallet_did", CreateWalletDataObject(), cancellationToken).ConfigureAwait(false); |
| 73 | + |
| 74 | + return response.did_id; |
| 75 | + } |
| 76 | + |
| 77 | + /// <summary> |
| 78 | + /// Mints an NFT |
| 79 | + /// </summary> |
| 80 | + /// <param name="info">Info about the NFT to be minted</param> |
| 81 | + /// <param name="fee">Transaction fee</param> |
| 82 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 83 | + /// <returns>A <see cref="SpendBundle"/></returns> |
| 84 | + public async Task<SpendBundle> MintNFT(NFTMintingInfo info, ulong fee = 0, CancellationToken cancellationToken = default) |
| 85 | + { |
| 86 | + dynamic data = CreateWalletDataObject(); |
| 87 | + data.royalty_address = info.RoyaltyAddress; |
| 88 | + data.target_address = info.TargetAddress; |
| 89 | + data.uris = info.Uris; |
| 90 | + data.meta_uris = info.MetaUris; |
| 91 | + data.license_uris = info.LicenseUris; |
| 92 | + data.hash = info.Hash; |
| 93 | + data.series_number = info.SeriesNumber; |
| 94 | + data.series_total = info.SeriesTotal; |
| 95 | + data.meta_hash = info.MetaHash; |
| 96 | + data.license_hash = info.LicenseHash; |
| 97 | + data.did_id = info.DIDID; |
| 98 | + data.royalty_percentage = info.RoyaltyPercentage; |
| 99 | + data.fee = fee; |
| 100 | + |
| 101 | + return await WalletProxy.SendMessage<SpendBundle>("nft_mint_nft", "spend_bundle", data, cancellationToken).ConfigureAwait(false); |
| 102 | + } |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Sets the DID for an NFT |
| 106 | + /// </summary> |
| 107 | + /// <param name="didId">The DID ID</param> |
| 108 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 109 | + /// <returns>A <see cref="SpendBundle"/></returns> |
| 110 | + public async Task<SpendBundle> SetDID(string didId, CancellationToken cancellationToken = default) |
| 111 | + { |
| 112 | + dynamic data = CreateWalletDataObject(); |
| 113 | + data.did_id = didId; |
| 114 | + |
| 115 | + return await WalletProxy.SendMessage<SpendBundle>("nft_set_nft_did", "spend_bundle", data, cancellationToken).ConfigureAwait(false); |
| 116 | + } |
| 117 | + |
| 118 | + /// <summary> |
| 119 | + /// Sets the status of an NFT |
| 120 | + /// </summary> |
| 121 | + /// <param name="coinId">The coin ID</param> |
| 122 | + /// <param name="inTransaction">In transaction idicator</param> |
| 123 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 124 | + /// <returns>An awaitable <see cref="Task"/></returns> |
| 125 | + public async Task SetStatus(string coinId, bool inTransaction = true, CancellationToken cancellationToken = default) |
| 126 | + { |
| 127 | + dynamic data = CreateWalletDataObject(); |
| 128 | + data.coin_id = coinId; |
| 129 | + data.in_transaction = inTransaction; |
| 130 | + |
| 131 | + await WalletProxy.SendMessage("nft_set_nft_status", data, cancellationToken).ConfigureAwait(false); |
| 132 | + } |
| 133 | + |
| 134 | + /// <summary> |
| 135 | + /// Sets the status of an NFT |
| 136 | + /// </summary> |
| 137 | + /// <param name="targetAddress">The target address</param> |
| 138 | + /// <param name="coinId">The coin ID</param> |
| 139 | + /// <param name="fee">Transaction fee</param> |
| 140 | + /// <param name="cancellationToken">A token to allow the call to be cancelled</param> |
| 141 | + /// <returns>A <see cref="SpendBundle"/></returns> |
| 142 | + public async Task<SpendBundle> Transfer(string targetAddress, string coinId, ulong fee = 0, CancellationToken cancellationToken = default) |
| 143 | + { |
| 144 | + dynamic data = CreateWalletDataObject(); |
| 145 | + data.target_address = targetAddress; |
| 146 | + data.nft_coin_id = coinId; |
| 147 | + data.fee = fee; |
| 148 | + |
| 149 | + return await WalletProxy.SendMessage<SpendBundle>("nft_transfer_nft", "spend_bundle", data, cancellationToken).ConfigureAwait(false); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments