Examples

This documentation allows you to help understand how to use this gem. For clarity, I've added respective JavaScript library code which you see by toggling the language in navigation bar.


Creating a client

First of all, you'll need to create a client which will allow you to communicate with full node.

You can create client using Ruby gem in the following manner:

iota = IOTA::Client.new(provider: 'http://localhost:14265')

# or 

iota = IOTA::Client.new(
    host: 'http://localhost',
    port: 14265
)

You can create client using JS library in the following manner:

var iota = new IOTA({
    'provider': 'http://localhost:14265'
});

// or 

var iota = new IOTA({
    'host': 'http://localhost',
    'port': 14265
});

Making API request & reading response

After connecting to full node, let's validate the information by requesting nodeInfo. This will require making an API call to our full node using IOTA::Client.api

This gem is synchronous and you can pass a block or read the response directly in the following manner:

iota.api.getNodeInfo do |status, data|
    # status: true if successful, else false
    # data: if status is true, data contains response. Otherwise data contains error message.
end

# or

status, data = iota.api.getNodeInfo
# output: [true, {"appName"=>"IRI", "appVersion"=>"1.4.1.2", "jreAvailableProcessors"=>2, "jreFreeMemory"=>147885368, "jreVersion"=>"1.8.0_151", "jreMaxMemory"=>1908932608, "jreTotalMemory"=>691011584, "latestMilestone"=>"GLMXFLRQ9NNUTORVEYRCFYNUCKKPXM9BUPESESVCSSMZZQNXDCTVPLTGVNTTI9DDXHJRWBTIAUESA9999", "latestMilestoneIndex"=>282406, "latestSolidSubtangleMilestone"=>"GLMXFLRQ9NNUTORVEYRCFYNUCKKPXM9BUPESESVCSSMZZQNXDCTVPLTGVNTTI9DDXHJRWBTIAUESA9999", "latestSolidSubtangleMilestoneIndex"=>282406, "neighbors"=>9, "packetsQueueSize"=>0, "time"=>1510812060993, "tips"=>5019, "transactionsToRequest"=>9, "duration"=>1}]

JS library is asynchronous works with callbacks only.

iota.api.getNodeInfo(function(error, data) {
    if(error) {
        console.error(error)
    } else {
        console.log(data)
    }
})

Fetching account details

All functionalities that requires seed i.e. reading account details, generating new addressm, making transfer etc. are moved into IOTA::Models::Account class.

You can create account object like this:

options is optional. It is used if you want to pass following params:

  • security - Integer (default: 2)
  • start - Integer (default: 0)
  • end - Integer (default: nil)
account = IOTA::Models::Account.new(client, 'YOUR_SEED_HERE')
account.getAccountDetails(options)

# Optional
# You can pass options in following way
# account.getAccountDetails(security: 1, start: 0)

puts account.balance
puts account.latestAddress
puts account.addresses
puts account.transfers # Array of `IOTA::Models::Transaction` objects
puts account.inputs # Array of `IOTA::Models::Input` objects
iota.api.getAccountDetails('YOUR_SEED_HERE', options)

Making transfer

You can pass the transfers and options hash just like JS library and make transfer. You can also create Transfer and Input class and pass it to make transfer. Both examples are shown here.

Using hash:

transfers = [{
    address: 'JEPQXSBRKBPFUVFOFIVKHXKF9THOOGFHPMWAGFWQHBPYIUDRSFRIWSAXWATZY9EIWKVBTVZDGZGVBEPEXWOLLHOWEZ',
    value: 1000000 # 1Mi
}]

# If you want library to fetch input and make transfer
account.sendTransfer(depth, minWeightMagnitude, transfers)

# If you want to specify inputs or other options
options = {
    inputs: [{
        address: 'ADDRESS',
        balance: 12312323,
        keyIndex: 0,
        security: 2
    }],
    address: 'REMAINDER_ADDRESS', # optional
    security: 2, # optional
    hmacKey: 'HMAC_KEY' # optional
}

account.sendTransfer(depth, minWeightMagnitude, transfers, options)

Using models:

account = IOTA::Models::Account.new('YOUR_SEED_HERE')

transfer = IOTA::Models::Transfer.new(
    address: 'JEPQXSBRKBPFUVFOFIVKHXKF9THOOGFHPMWAGFWQHBPYIUDRSFRIWSAXWATZY9EIWKVBTVZDGZGVBEPEXWOLLHOWEZ',
    value: 1000000 # 1Mi,
    hmacKey: 'HMAC_KEY' # optional
)

input = IOTA::Models::Input.new(
    address: 'ADDRESS',
    balance: 12312323,
    keyIndex: 0,
    security: 2
)

options = {
    inputs: [input],
    address: 'REMAINDER_ADDRESS', # optional
    security: 2, # optional
    hmacKey: 'HMAC_KEY' # optional
}

account.sendTransfer(depth, minWeightMagnitude, transfers, options)

The JS way:

var transfers = [{
    'address': 'JEPQXSBRKBPFUVFOFIVKHXKF9THOOGFHPMWAGFWQHBPYIUDRSFRIWSAXWATZY9EIWKVBTVZDGZGVBEPEXWOLLHOWEZ',
    'value': 1000000
}]

var options = {
    'inputs': [
        {
            address: 'XB9IBINADVMP9K9FEIIR9AYEOFUU9DP9EBCKOTPSDVSNRRNVSJOPTFUHSKSLPDJLEHUBOVEIOJFPDCZS9',
            balance: 1500,
            keyIndex: 0,
            security: 3
        }, {
            address: 'W9AZFNWZZZNTAQIOOGYZHKYJHSVMALVTWJSSZDDRVEIXXWPNWEALONZLPQPTCDZRZLHNIHSUKZRSZAZ9W',
            balance: 8500,
            keyIndex: 7,
            security: 2
        }
    ]
}

iota.api.sendTransfer('YOUR_SEED_HERE', depth, minWeightMagnitude, transfers , options, callback)

results matching ""

    No results matching ""