Send NFTs
After you have minted an
NFT,
you can easily send it by calling the Account.send_nft(addresses_nft_ids, options)
function.
As with any output, you can set a storage deposit and output unlock conditions. Keep in mind that if you set unlock conditions, whoever you send the native tokens to may need to claim them.
Code Example
Before you run the code example, make sure to update the token ID with one which is available in your account. If you haven't done so already, you can follow the how to mint a native token guide. If you don't know the token ID you can check your accounts balance to retrieve the available native tokens in your account.
The following example will:
- Create an account manager.
- Get Alice's account which was created in the first guide.
- Define the type of native token and amount to send.
- Send the native tokens calling the
Account.send_nft(addresses_nft_ids, options)
function.
- Rust
- Nodejs
- Python
- Java
This example uses dotenv, which is not safe for use in production environments.
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example send_nft --release
// In this example we will send an nft
// Rename `.env.example` to `.env` first
use std::{env, str::FromStr};
use dotenv::dotenv;
use iota_wallet::{account_manager::AccountManager, iota_client::block::output::NftId, AddressAndNftId, Result};
#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv().ok();
// Create the account manager
let manager = AccountManager::builder().finish().await?;
// Get the account we generated with `01_create_wallet`
let account = manager.get_account("Alice").await?;
// Set the stronghold password
manager
.set_stronghold_password(&env::var("STRONGHOLD_PASSWORD").unwrap())
.await?;
let outputs = vec![AddressAndNftId {
address: "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu".to_string(),
// Replace with an NftId that is available in the account
nft_id: NftId::from_str("0xe192461b30098a5da889ef6abc9e8130bf3b2d980450fa9201e5df404121b932")?,
}];
let transaction = account.send_nft(outputs, None).await?;
println!(
"Transaction: {} Block sent: {}/api/core/v2/blocks/{}",
transaction.transaction_id,
&env::var("NODE_URL").unwrap(),
transaction.block_id.expect("no block created yet")
);
Ok(())
}
Run the example by running the following command:
cargo run --example send_nft --release
/**
* This example will send an NFT
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const { initLogger } = require('@iota/wallet');
initLogger({
name: './wallet.log',
levelFilter: 'debug',
targetExclusions: ["h2", "hyper", "rustls"]
});
const manager = await getUnlockedManager();
const account = await manager.getAccount('0');
await account.sync();
// Send the full NFT output to the specified address
const response = await account.sendNft([{
//TODO: Replace with the address of your choice!
address: 'rms1qrrv7flg6lz5cssvzv2lsdt8c673khad060l4quev6q09tkm9mgtupgf0h0',
//TODO: Replace with an NFT id from your account, you can mint one with `25-mint-nft.js`.
nftId: '0x09aa7871e126cc41f1f3784a479a5dd5f23e4dd8b97e932a001e77a11ad42f0c',
}]);
console.log(response);
console.log(
`Check your block on ${process.env.NODE_URL}/api/core/v2/blocks/${response.blockId}`,
);
// To send an NFT with expiration unlock condition prepareOutput() can be used like this:
// const output = await account.prepareOutput({
// recipientAddress: 'rms1qz6aj69rumk3qu0ra5ag6p6kk8ga3j8rfjlaym3wefugs3mmxgzfwa6kw3l',
// amount: "47000",
// unlocks: {
// expirationUnixTime: 1677065933
// },
// assets: {
// nftId: '0x447b20b81e2311a6c16a32eaeda2f2f2472c4b43ed4ffc80a0c0f850130fc4bb',
// },
// storageDeposit: { returnStrategy: 'Gift' }
// });
// const transaction = await account.sendOutputs([output]);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();
You can run the example by running the following command from the wallet/bindings/nodejs/examples/
folder:
node 26-send-nft.js
from iota_wallet import IotaWallet
# In this example we will send an nft
wallet = IotaWallet('./alice-database')
account = wallet.get_account('Alice')
# Sync account with the node
response = account.sync()
print(f'Synced: {response}')
wallet.set_stronghold_password("some_hopefully_secure_password")
outputs = [{
"address": "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu",
"nftId": "0x17f97185f80fa56eab974de6b7bbb80fa812d4e8e37090d166a0a41da129cebc",
}]
transaction = account.send_nft(outputs)
print(f'Sent transaction: {transaction}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 6-send-nft.py
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import org.iota.Wallet;
import org.iota.types.*;
import org.iota.types.account_methods.SyncAccount;
import org.iota.types.exceptions.InitializeWalletException;
import org.iota.types.exceptions.WalletException;
import org.iota.types.ids.NftId;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class SendNft {
public static void main(String[] args) throws WalletException, InterruptedException, InitializeWalletException {
// This example assumes that a wallet has already been created using the ´SetupWallet.java´ example.
// If you haven't run the ´SetupWallet.java´ example yet, you must run it first to be able to load the wallet as shown below:
Wallet wallet = new Wallet(new WalletConfig()
.withClientOptions(new ClientConfig().withNodes(Env.NODE))
.withSecretManager(new StrongholdSecretManager(Env.STRONGHOLD_PASSWORD, null, Env.STRONGHOLD_VAULT_PATH))
.withCoinType(CoinType.Shimmer)
.withStoragePath(Env.STORAGE_PATH)
);
// Get account and sync it with the registered node to ensure that its balances are up-to-date.
AccountHandle a = wallet.getAccount(new AccountAlias(Env.ACCOUNT_NAME));
a.syncAccount(new SyncAccount().withOptions(new SyncOptions()));
// TODO: replace with your own values.
String receiverAddress = a.getPublicAddresses()[0].getAddress();
NftId nftId = new NftId("0xdbed22679570aecc16da90648836607981e87c1ed3e3a24daf0942aa29a66003");
// Send transaction.
Transaction t = a.sendNft(new org.iota.types.account_methods.SendNft().withAddressesAndNftIds(new AddressAndNftId[] {new AddressAndNftId()
.withAddress(receiverAddress)
.withNftId(nftId)
}));
// Print transaction.
System.out.println(t);
// In case you are done and don't need the wallet instance anymore you can destroy the instance to clean up memory.
// For this, check out the ´DestroyWallet.java´ example.
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
Transaction: 0x4d3e01a0cf6d25e80af5ed774f0f741a476709a5517c21555e2c0351dda81f77
Block sent: http://localhost:14265/api/core/v2/blocks/0x7ad6ce0789ea5b5340b7045a13947af95737b3728410cdde8362021ac57f3731
{
payload: {
type: 6,
essence: {
type: 1,
networkId: '1856588631910923207',
inputs: [Array],
inputsCommitment: '0xd549b56a017e51a4ff053171b55aba62aac59e38a167067769991ccfb663ce1b',
outputs: [Array]
},
unlocks: [ [Object] ]
},
blockId: '0x40c74e3e9bc8913e3d5c75723d8e018695f3be8a78bd05479500eff4a7a10915',
inclusionState: 'Pending',
timestamp: '1663000240615',
transactionId: '0x03d4fe1335d0880db4163e7cb19735e016c803b015c9e65123e07de579ed41d2',
networkId: '1856588631910923207',
incoming: false,
note: null
}
Check your block on http://localhost:14265/api/core/v2/blocks/0x40c74e3e9bc8913e3d5c75723d8e018695f3be8a78bd05479500eff4a7a10915
Synced:{
'baseCoin':{
'total':'3302172702',
'available':'3302172702'
},
'requiredStorageDeposit':'910202100',
'nativeTokens':[
{
'tokenId':'0x08a5526c4a15558b709340822edf00cb348d8606a27e2e59b00432a0afe8afb74d0100000000',
'total':'0x3de',
'available':'0x3de'
},
{
'tokenId':'0x08b83d49922e341d2cb45159707cfafdc9dc8fdb9d119543480dbaa5773eed8c4a0100000000',
'total':'0x64',
'available':'0x64'
}
],
'nfts':[
'0x77133189021f50d8d66e0678e553af9f46a832a24239653d3555edb8dc859e1f',
'0x1e808b7c6e603aaeb5f718881a74fedae72981ac7d5f0294eb561cad0e653566',
'0x1b670afba8d59a445cbaf167f1fda05879362e3ea034f5c4a0979fbeb5a3964b',
'0x3f0e11e9d9f48a57d0fba43d7d1158ee673cb8055f80a5ce45ad174c962c0d8a',
'0xdc8be91d779aac048aa9001ab99ecf12cf62a4701185a95f6206a1a201bfbe7c',
'0xceae643ff7c112a3adce8f55f7953ba0707ade21256a7a09068c0b47f7c62c5b',
'0x17f97185f80fa56eab974de6b7bbb80fa812d4e8e37090d166a0a41da129cebc'
],
'aliases':[
'0x96717e6d19c13b1c5b120d60b23217f541b5b779e51212e01d72e7fa1f7090cf',
'0xa5526c4a15558b709340822edf00cb348d8606a27e2e59b00432a0afe8afb74d',
'0x97eb7a447cd62e1c373ff8188ba422f5c1b0687707d38e10e8366a1c20d33fea',
'0xf9c702ffe50c35d331b2df02295c2cc6d92f883530ff231bd76d1f6a72cb1d95'
],
'foundries':[
'0x08a5526c4a15558b709340822edf00cb348d8606a27e2e59b00432a0afe8afb74d0100000000'
],
'potentiallyLockedOutputs':{
'0x9a5869b61b29f17326e04b6161d9cd169687e79476c556bd0c3cbbc3648d4ff60000':False,
'0x850c1e43dff1a28a42d71edc6d4ad0b9f251c03993f9b0684a34f645514ffe270000':False
}
}
Sent transaction:{
'payload':{
'type':6,
'essence':{
'type':1,
'networkId':'1856588631910923207',
'inputs':[
{
'type':0,
'transactionId':'0x84debb878f8e124f2bd893e04c6672a8ad31788c47a520f77c11bdb03727ef1c',
'transactionOutputIndex':3
}
],
'inputsCommitment':'0xb70cc0104197c9b9b1bc7199b9093e47d710b0e7e14b7bc09a79ebc7e42e16a1',
'outputs':[
{
'type':6,
'amount':'47500',
'nftId':'0x17f97185f80fa56eab974de6b7bbb80fa812d4e8e37090d166a0a41da129cebc',
'unlockConditions':[
{
'type':0,
'address':{
'type':0,
'pubKeyHash':'0x60200bad8137a704216e84f8f9acfe65b972d9f4155becb4815282b03cef99fe'
}
}
],
'features':[
{
'type':2,
'data':'0x68656c6c6f'
}
],
'immutableFeatures':[
{
'type':2,
'data':'0x68656c6c6f'
}
]
}
]
},
'unlocks':[
{
'type':0,
'signature':{
'type':0,
'publicKey':'0xe62838fda7e8b77bf80e49967f0f089ae2a7230547d5231649732952f6336fae',
'signature':'0x079dfe7ab4830be937757d3362ef5dab8d0d70297ab4dce8a3ab79f2a8b28610ff8d84fe037e642afa28c07a3f87ed2072930f57e9b7f8390e9e117e26966809'
}
}
]
},
'blockId':'0x5c49a0c8b9ee920ca575d895842c43ace5bfd2feb2ccc611eb37ec4cde7acabc',
'inclusionState':'Pending',
'timestamp':'1665918220888',
'transactionId':'0x3158626d7f404bded6af815f104e4001009a3f82fab7f9eb267cc691d039eed5',
'networkId':'1856588631910923207',
'incoming':False,
'note':None
}
{
"payload": {
"type": 6,
"essence": {
"type": 1,
"networkId": "1856588631910923207",
"inputs": [
{
"type": 0,
"transactionId": "0xd96c9cdf8c6b095f7ab44105f8298c766a7433664db651bfbd7832566d59f103",
"transactionOutputIndex": 0
}
],
"inputsCommitment": "0xd64acb6fec68315c200484458fb0a2c1e9b7d06106aecdcb9c86403d955ad69a",
"outputs": [
{
"type": 6,
"amount": "52000",
"nftId": "0xdbed22679570aecc16da90648836607981e87c1ed3e3a24daf0942aa29a66003",
"unlockConditions": [
{
"type": 0,
"address": {
"type": 0,
"pubKeyHash": "0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
],
"features": [
{
"type": 2,
"data": "0x5368696d6d65722e20546f6b656e697a652045766572797468696e672e2048656c6c6f2066726f6d20746865204a6176612062696e64696e672e"
}
]
}
]
},
"unlocks": [
{
"type": 0,
"signature": {
"type": 0,
"publicKey": "0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature": "0xc42fd26b652581e6b2f58ef316b9817830093ae959d716f06c97284fadad72e39838031205d89afa3f0177ae2b1780435945be6a29274a1c66ad47d622cf630b"
}
}
]
},
"blockId": "0xa2dc2b45e42ef4c5f847f329faa27ee7b3e50ede0d6b46aeed996f4d74e87b95",
"inclusionState": "Pending",
"timestamp": "1664876443672",
"transactionId": "0xb423ef2155828a4b301795a77357c70bdac76c47f6ee1a20af6a298540fd9b86",
"networkId": "1856588631910923207",
"incoming": false
}