Claim Outputs
You can use wallet.rs to send funds with an expiration. If you don't claim these funds within the expiration period, they will be returned to the sender.
Code Example
The following example will:
- Create an account manager.
- Get Alice's and Bob's account which were created in the first guide.
- Prepare and send an output with an expiration unlock.
- Sync Bob's account to get the output event.
- Claim the output by calling the
account.claimOutputs([outputId])
function from Bob's account.
- Rust
- Nodejs
- Python
- Java
Guide Coming Soon
This how to guide is not available in your language of choice at the moment. Please feel free to browse more examples which may suit your requirements.
/**
* This example sends IOTA tokens with an expiration date and claims them.
*/
const getUnlockedManager = require('./account-manager');
let manager
let bob
async function run() {
try {
manager = await getUnlockedManager();
const alice = await manager.getAccount('Alice');
bob = await manager.getAccount('Bob');
manager.listen(['NewOutput'], handleNewOutputOfBob)
const recipientAddress = bob.meta.publicAddresses[0].address
const amount = '1000000';
const outputData = await alice.prepareOutput(
{
recipientAddress,
amount,
unlocks: {
expirationUnixTime: Math.round(new Date().getTime() / 1000) + 15000
}
},
);
const resp = await alice.sendOutputs([outputData])
console.log('Transaction is sent', resp)
// Sync account to get the output event
setTimeout(async () => {
await bob.sync();
}, 10000)
} catch (error) {
console.log('Error: ', error);
}
}
async function handleNewOutputOfBob(err, data) {
try {
console.log('Output received:', data)
const event = JSON.parse(data)
if (event.accountIndex === bob.meta.index) {
const outputId = event.event.NewOutput.output.outputId
await bob.sync()
const resp = await bob.claimOutputs([outputId])
console.log('Output has been claimed in the following transaction:', resp)
process.exit(0)
}
} catch (error) {
console.log('Error: ', error);
}
}
run();
You can run the example by running the following command from the wallet/bindings/nodejs/examples/
folder:
node 21-claim-outputs.js
from iota_wallet import IotaWallet
# In this example we will claim outputs that have additional unlock conditions as expiration or storage deposit return
wallet = IotaWallet('./alice-database')
account = wallet.get_account('Alice')
wallet.set_stronghold_password("some_hopefully_secure_password")
# Sync account with the node
response = account.sync()
# Only the unspent outputs in the account
output_ids = account.get_outputs_with_additional_unlock_conditions('All')
print(f'Available outputs to claim: {output_ids}')
transaction = account.claim_outputs(output_ids)
print(f'Sent transaction: {transaction}')
You can run the example by running the following command from the binding/python/examples
folder:
python3 10-claim-outputs.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.OutputId;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class ClaimOutputs {
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.
OutputId outputId = new OutputId("0xeb572c09b9cdf4e29c65ecbe10c06d484c04d33da3bea6d9bb1653aa6617e8450000");
// Claim the given outputs
Transaction t = a.claimOutputs(new org.iota.types.account_methods.ClaimOutputs().withOutputIdsToClaim(new OutputId[]{
outputId
}));
// Print the 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
Guide Coming Soon
This how to guide is not available in your language of choice at the moment. Please feel free to browse more examples which may suit your requirements.
Transaction is sent {
payload: {
type: 6,
essence: {
type: 1,
networkId: '1856588631910923207',
inputs: [Array],
inputsCommitment: '0xe564a6ec7749b3c09e06b05c76c95b37a34d0fd026dfb4082b8e4ff900f9dc81',
outputs: [Array]
},
unlocks: [ [Object] ]
},
blockId: '0x8ce286630d376d3f6f2c74c1f758e38a0b88d776887aa6e4c7af0c125b533c7f',
inclusionState: 'Pending',
timestamp: '1663005430349',
transactionId: '0xad3f8e00af9d56d22eff5ec730e01918e52d80c87e94859e00907099091aa95a',
networkId: '1856588631910923207',
incoming: false,
note: null
}
Output received: {"accountIndex":0,"event":{"NewOutput":{"output":{"outputId":"0xad3f8e00af9d56d22eff5ec730e01918e52d80c87e94859e00907099091aa95a0100","metadata":{"blockId":"0x8ce286630d376d3f6f2c74c1f758e38a0b88d776887aa6e4c7af0c125b533c7f","transactionId":"0xad3f8e00af9d56d22eff5ec730e01918e52d80c87e94859e00907099091aa95a","outputIndex":1,"isSpent":false,"milestoneIndexBooked":974130,"milestoneTimestampBooked":1663005433,"ledgerIndex":974130},"output":{"type":3,"amount":"998837200","unlockConditions":[{"type":0,"address":{"type":0,"pubKeyHash":"0xeed1e4e3e9e7555e2f1592594bf916523e7fef3a9a8c0d630a4934440b822647"}}]},"isSpent":false,"address":{"type":0,"pubKeyHash":"0xeed1e4e3e9e7555e2f1592594bf916523e7fef3a9a8c0d630a4934440b822647"},"networkId":"1856588631910923207","remainder":true,"chain":[{"hardened":true,"bs":[128,0,0,44]},{"hardened":true,"bs":[128,0,16,123]},{"hardened":true,"bs":[128,0,0,0]},{"hardened":true,"bs":[128,0,0,0]},{"hardened":true,"bs":[128,0,0,0]}]},"transaction":null,"transactionInputs":null}}}
Output received: {"accountIndex":1,"event":{"NewOutput":{"output":{"outputId":"0xad3f8e00af9d56d22eff5ec730e01918e52d80c87e94859e00907099091aa95a0000","metadata":{"blockId":"0x8ce286630d376d3f6f2c74c1f758e38a0b88d776887aa6e4c7af0c125b533c7f","transactionId":"0xad3f8e00af9d56d22eff5ec730e01918e52d80c87e94859e00907099091aa95a","outputIndex":0,"isSpent":false,"milestoneIndexBooked":974130,"milestoneTimestampBooked":1663005433,"ledgerIndex":974131},"output":{"type":3,"amount":"1000000","unlockConditions":[{"type":0,"address":{"type":0,"pubKeyHash":"0x085748b21812b9fd706d58983a7b16dabe8a068bd8473ee1cd322fc9e02d8703"}}]},"isSpent":false,"address":{"type":0,"pubKeyHash":"0x085748b21812b9fd706d58983a7b16dabe8a068bd8473ee1cd322fc9e02d8703"},"networkId":"1856588631910923207","remainder":false,"chain":[{"hardened":true,"bs":[128,0,0,44]},{"hardened":true,"bs":[128,0,16,123]},{"hardened":true,"bs":[128,0,0,1]},{"hardened":true,"bs":[128,0,0,0]},{"hardened":true,"bs":[128,0,0,0]}]},"transaction":null,"transactionInputs":null}}}
Output has been claimed in the following transaction: [
{
payload: { type: 6, essence: [Object], unlocks: [Array] },
blockId: '0x3881d8a790b70d6cd3d5b7e4ade65f20cc2a1a9c88af76d612b3646fdd471d7a',
inclusionState: 'Pending',
timestamp: '1663005447689',
transactionId: '0x046a1112ba53beeee146dd4b0a28c59565fda3f62628b53655b5719b326e25d0',
networkId: '1856588631910923207',
incoming: false,
note: null
}
]
Available outputs to claim:[
'0x79edb042038431009408d734a337f6fae9dcec97b0d7572665d0c184ea74b82f0000'
]
Sent transaction:{
'payload':{
'type':6,
'essence':{
'type':1,
'networkId':'1856588631910923207',
'inputs':[
{
'type':0,
'transactionId':'0x79edb042038431009408d734a337f6fae9dcec97b0d7572665d0c184ea74b82f',
'transactionOutputIndex':0
},
{
'type':0,
'transactionId':'0xf4676daae96cf629c04d8d62989dec6d9dbbac221959d72c399729f92047ec14',
'transactionOutputIndex':0
}
],
'inputsCommitment':'0x51a6fe8c7670746c2a45f620be0055c1853f84599c68490a7258ee3c41ba2332',
'outputs':[
{
'type':3,
'amount':'50600',
'unlockConditions':[
{
'type':0,
'address':{
'type':0,
'pubKeyHash':'0xf8ba764448d689422aa59a5c5dc97108450a29cb8956208f631ab4a82338468a'
}
}
]
},
{
'type':3,
'amount':'1000000001',
'unlockConditions':[
{
'type':0,
'address':{
'type':0,
'pubKeyHash':'0xf4d94c9f4e63d553c81e644225fa3bbc412f820c4eafd495aeb0bb05a29922b2'
}
}
]
}
]
},
'unlocks':[
{
'type':0,
'signature':{
'type':0,
'publicKey':'0xee4164cca9a455db3164ec2a3c3ebc2db57aec8c4cb3231c3dc6cda0c700e1bd',
'signature':'0xf474de893c5a47d9114d2a95f1bd0e4c83b464e8a17886a07ea8d04046949a17dbb8173183c08d4cadbe530c7f42a65fa3286a5f28644a76cf59f6411a04220f'
}
},
{
'type':1,
'reference':0
}
]
},
'blockId':'0x99d29eceb87b44664361f7faf9d8f3b072c6e3e890953fdf956f6d148d9b04b2',
'inclusionState':'Pending',
'timestamp':'1665992931904',
'transactionId':'0xeed7357bf0c0bbdca809e5e537af5a10872e488b26a157af78c68e104420ad75',
'networkId':'1856588631910923207',
'incoming':False,
'note':None
}
{
"payload":{
"type":6,
"essence":{
"type":1,
"networkId":"1856588631910923207",
"inputs":[
{
"type":0,
"transactionId":"0xeb572c09b9cdf4e29c65ecbe10c06d484c04d33da3bea6d9bb1653aa6617e845",
"transactionOutputIndex":0
}
],
"inputsCommitment":"0x4ef5a27871871d370f7bcf41b783f6b1c44221b228624e74c760923dac9efe99",
"outputs":[
{
"type":3,
"amount":"1000000",
"unlockConditions":[
{
"type":0,
"address":{
"type":0,
"pubKeyHash":"0x4cfde0600797ae07d19d67d78910e70950bfdaf716f0035e9a30b97828aaf6a2"
}
}
]
}
]
},
"unlocks":[
{
"type":0,
"signature":{
"type":0,
"publicKey":"0xde3152ce9d67415b9c5a042ea01caccc3f73ff1c0c77036874cb8badf9798d56",
"signature":"0xa0ec431d1272d27147809b6178b0e69760de49e7d8f78122cc255362e48f1500345968c0500e07f7b01d35b836c22613c56667e59510b02480c5b257ff74ef04"
}
}
]
},
"blockId":"0x4090b35d0a819b291201e09308eba4a39ffc3cffe2127dae4a29e1e78af64aec",
"inclusionState":"Pending",
"timestamp":"1664900613290",
"transactionId":"0x4893b9813145001e67fe89394c2e9464c9a2d07dea1536d36beb83258e5aa1c0",
"networkId":"1856588631910923207",
"incoming":false
}