LogoLogo
WebsiteDiscordTelegramTwitter
  • 🏠Witnet Oracle Docs
  • Introduction
    • ❓What is Witnet?
      • ⚙️Oracle Architecture
      • 🪙The Witnet Coin (Wit)
      • 👛Wallets
      • 🤔Frequently Asked Questions
      • 😎Awesome Witnet
    • 🚀Quick Tutorials
      • 📈Price Feeds
      • 🎲Randomness
      • 🌐APIs and HTTP GET/POST
      • ⛏️Mining Wit
  • Smart contracts developers
    • ⛓️Supported chains
    • 🔮Wit/Oracle
      • HTTP GET Requests in Solidity
      • HTTP POST Requests in Solidity
      • Query GraphQL APIs in Solidity
      • Dynamic Requests in Solidity
      • UsingWitnet Inheritance
      • API Reference
        • Solidity API
          • Solidity Contracts
            • WitnetRequestBoard
        • Javascript API
        • RADON API
          • RADON Type System
          • RADON Errors
      • Multi-chain Addresses
    • 💹Wit/Price Feeds
      • How To Use Witnet Price Feeds
      • Update Conditions
      • Code Examples
      • API Reference
      • Multi-chain Addresses
        • Arbitrum Price Feeds
        • Avalanche Price Feeds
        • Base Price Feeds
        • Boba Price Feeds
        • Celo Price Feeds
        • Conflux Price Feeds
        • Cronos Price Feeds
        • Dogechain Price Feeds
        • Elastos Price Feeds
        • Ethereum Price Feeds
        • Gnosis Chain Price Feeds
        • Kaia Price Feeds
        • Kava Price Feeds
        • KCC Price Feeds
        • Mantle Price Feeds
        • Meter Price Feeds
        • Metis Price Feeds
        • Moonbeam Price Feeds
        • OKX Price Feeds
        • Optimism Price Feeds
        • Polygon Price Feeds
        • Reef Price Feeds
        • Scroll Price Feeds
        • Syscoin Price Feeds
        • Ultron Price Feeds
        • Request new price feed or chain support
    • 🎲Wit/Randomness
      • Generating Randomness
      • WitnetRandomness Contract
      • Low-level Requests
      • Code Examples
      • API Reference
      • Multi-chain Addresses
    • 🏗️Guides
      • 📖Solidity Contracts
        • Appliances
          • 📃WitnetPriceFeeds
          • 📃WitnetRandomness
        • Core
          • 📃WitnetOracle
          • 📃WitnetRadonRegistry
          • 📃WitnetRequest
          • 📃WitnetRequestFactory
          • 📃WitnetRequestTemplate
        • Mockups
          • 📃UsingWitnet
          • 📃UsingWitnetRandomness
          • 📃UsingWitnetRequest
          • 📃UsingWitnetRequestTemplate
          • 📃WitnetRandomnessRequestConsumer
          • 📃WitnetRequestConsumer
          • 📃WitnetRequestTemplateConsumer
      • 🧙Solidity Wizard
    • 🎓Tutorials
      • Building a Satoshi/Wei custom price feed
  • Witnet SDK
    • 👋Introduction
    • 🚀Getting Started
    • 🏗️How-to Guides
      • Connect to a Wit/RPC provider
      • Query stake entries in Witnet
      • Manage Witnet wallets
      • Manage Witnet UTXOs
      • Manage Witnet transactions
    • 🎓Tutorials
    • 📚Reference Material
      • 🔌APIs
        • Wit/Node JSON-RPC API
  • Witnet Validators
    • 💻Node Requirements
    • 🚀Getting Started (Docker)
    • 🔎Next steps
    • 🤓Advanced Setups
      • Run node as a systemd service
      • Run node with docker compose
      • Paranoid mode (Witnet over proxies and Tor)
      • Configuration file
    • ⌨️CLI Reference
  • 🗜️Compile from Source Code
Powered by GitBook
On this page
  • List locked and unlocked UTXOs of some random address
  • Get total balance of some random address
  • Select all spendable UTXOs in a wallet
  • Select spendable UTXOs in a wallet as to cover some target value
  • Joining wallet's spendable UTXOs into a specific wallet account
  • Splitting UTXOs

Was this helpful?

Edit on GitHub
  1. Witnet SDK
  2. How-to Guides

Manage Witnet UTXOs

Efficiently manage UTXOs in Witnet with the Witnet SDK.

PreviousManage Witnet walletsNextManage Witnet transactions

Last updated 1 month ago

Was this helpful?

The code examples below assume that the environment is properly set up as described in . You can easily adapt the examples to pass the URL of the Wit/RPC provider to use, and/or the wallet master key (and the password, if encrypted).

Transactions in Witnet get paid by signing Unspent Transaction Outputs (or UTXOs), pretty much the same as transactions in Bitcoin. And just like Bitcoin, UTXOs in Witnet can be time-locked. Time-locked UTXOs cannot be spent until the time-lock timestamp is reached.

Aside from UTXOs, Witnet addresses may also hold rights to withdraw delegated stake from network validators. Delegated stake can be withdrawn into UTXOs by means of Unstake Transactions (aka. Stake Withdrawal Transactions). Withdrawn UTXOs get time-locked for a minimum of two weeks.

The balance of any Witnet address is therefore divided in three different fields:

locked

Sum of all time-locked UTXOs, in nanowits. Cannot be spent at this moment.

staked

Funds currently staked into one or more validators, in nanowits. Delegated stake can be eventually withdrawn. Once withdrawn, deposits will remain time-locked for at least two weeks.

unlocked

Sum of currently unlocked UTXOs, in nanowits.

List locked and unlocked UTXOs of some random address

const { Witnet } = require("@witnet/sdk")
const provider = await Witnet.Provider.fromEnv()
const utxos = await provider.getUtxos("wit165ec489lcrt27w5wa7q9je7rgr4dtvyff9vwsc")
import { Witnet } from "@witnet/sdk"
const provider = await Witnet.Provider.fromEnv()
const utxos = await provider.getUtxos("wit165ec489lcrt27w5wa7q9je7rgr4dtvyff9vwsc")
$ npx witnet inspect utxos wit165ec489lcrt27w5wa7q9je7rgr4dtvyff9vwsc --verbose

Get total balance of some random address

const { utils, Witnet } = require("@witnet/sdk")
const provider = await Witnet.Provider.fromEnv()
const balance = await provider.getBalance("wit165ec489lcrt27w5wa7q9je7rgr4dtvyff9vwsc")
import { Witnet } from "@witnet/sdk"
const provider = await Witnet.Provider.fromEnv()
const balance = await provider.getBalance("wit165ec489lcrt27w5wa7q9je7rgr4dtvyff9vwsc")
$ npx witnet inspect balance wit165ec489lcrt27w5wa7q9je7rgr4dtvyff9vwsc

Select all spendable UTXOs in a wallet

const { Witnet } = require("@witnet/sdk")
const wallet = await Witnet.Wallet.fromEnv()
let utxos = await wallet.selectUtxos()
import Witnet from "@witnet/sdk"
const wallet = await Witnet.Wallet.fromEnv()
let utxos = await wallet.selectUtxos()
$ npx witnet wallet utxos

Select spendable UTXOs in a wallet as to cover some target value

Different strategies are possible when selecting input UTXOs as for covering some target value:

UTXO Selection Strategy
Description

"big-first"

Higher value UTXOs are selected first, until target value is covered.

"random"

UTXOs are selected randomly, until target value is covered.

"slim-fit"

The smallest of all UTXOs that individually covers target value is selected. If none fulfills this condition, UTXOs get then selected by following the BigFirst strategy.

"small-first"

Smaller value UTXOs are selected first, until target value is covered.

const { Witnet } = require("@witnet/sdk")

const wallet = await Witnet.Wallet.fromEnv()
const utxos = await wallet.selectUtxos({
    strategy: Witnet.UtxoSelectionStrategy.SmallFirst,
    value: Witnet.Coins.fromWits(10000.0) // 10,000 Wits
});
import { Witnet } from "@witnet/sdk"

const wallet = await Witnet.Wallet.fromEnv()
const utxos = await wallet.selectUtxos({
    strategy: Witnet.UtxoSelectionStrategy.SlimFit,
    value: 10 ** 14,
})
$ npx witnet wallet utxos --value 1000.0 --strategy slim-fit

Joining wallet's spendable UTXOs into a specific wallet account

Joining multiple spendable UTXOs of a wallet into a single UTXO of some specific value is possible by creating a Value Transfer Transaction (or VTT) where all inputs correspond to the spendable UTXOs that are to be joined. The sum of all input UTXOs will then be available as a single UTXO to the recipient of such VTT.

const { Witnet } = require("@witnet/sdk")

const wallet = await Witnet.Wallet.fromEnv({
    strategy: Witnet.UtxoSelectionStrategy.SlimFit,
})
const vtt = await Witnet.ValueTransfers.from(wallet)
const receipt = await vtt.sendTransaction({
    recipients: [[ 
        wallet.accounts[0].pkh, 
        Witnet.Coins.fromWits(10000.0),  // 10,000.00 $WIT
    ]], 
})
import { Witnet } from "@witnet/sdk"

const wallet = await Witnet.Wallet.fromEnv({
    strategy: Witnet.UtxoSelectionStrategy.SlimFit,
})
const vtt = await Witnet.ValueTransfers.from(wallet)
const receipt: Witnet.TransactionReceipt = await vtt.sendTransaction({
    recipients: [[ 
        wallet.accounts[0].pkh, 
        Witnet.Coins.fromWits(10000.0),  // 10,000.00 $WIT
    ]], 
})
$ npx witnet utxos --value 10000.0 --strategy slim-fit --join --into wit1...

The more input UTXOs a VTT has, the greater the transaction weight will be. Because no more than 20,000 VTT weight units can be included within a Witnet block, you can get an exception if trying to build a transaction with too many inputs. In such cases, try changing the UTXO selection strategy to either SlimFit or BigFirst , or creating multiple VTTs with lower target values.

Splitting UTXOs

Splitting spendable funds into multiple smaller UTXOs enables automation scripts and bots to eventually handle concurrent data request transactions signed from one specific address.

const { Witnet } = require("@witnet/sdk")

const wallet = await Witnet.Wallet.fromEnv({
    strategy: Witnet.UtxoSelectionStrategy.BigFirst
})
const vtt = await Witnet.ValueTransfers.from(wallet)
let splits = 50
let coins = Witnet.Coins.fromWits(10000.0 / splits) // 200.0 $WIT per split
const receipt = await vtt.sendTransaction({
    recipients: Array(splits).fill([ "wit1...", coins ]) 
})
import { Witnet } from "@witnet/sdk"

const wallet = await Witnet.Wallet.fromEnv({
    strategy: Witnet.UtxoSelectionStrategy.BigFirst
})
const vtt = await Witnet.ValueTransfers.from(wallet)
let splits = 50
let coins = Witnet.Coins.fromWits(10000.0 / splits) // 200.0 $WIT per split
const receipt: Witnet.TransactionReceipt = await vtt.sendTransaction({
    recipients: Array(splits).fill([ "wit1...", coins ]) 
})
$ npx witnet wallet utxos --value 10000.0 --strategy big-first --split 50 --into wit1...

Due to the VTT weight limitation, the maximum number of output UTXOs in a VTT is restricted to 50.

🏗️
Getting Started