Project

near_api

0.0
The project is in a healthy, maintained state
NEAR blockchain ruby API
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 3.10

Runtime

 Project Readme

tests

NearApi

Near Protocol API ruby library

Installation

Add this line to your application's Gemfile:

gem 'near_api'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install near_api

Configuration

Via ENV variables:

  • NEAR_NODE_URL RPC node url (for ex. https://rpc.mainnet.near.org)
  • NEAR_SIGNER_ID account name for transactions and view_access_key requests (account_id in .near-credentials)
  • NEAR_KEYPAIR base58 encoded private + public keys (ed25519:4556s... in .near-credentials named as private_key ) if no private key is available public key can be specified as NEAR_PUBLIC_KEY for view requests

Via explicit params: example:

key = NearApi::Key.new('account_id.testnet', key_pair: 'ed25519:4556s...')
config = NearApi::Config.new(node_url: 'https://rpc.testnet.near.org') 
NearApi::Transaction.new(..., key: key, config: config)

Usage

Operations:

  • View
  • Transaction

View

NearApi::Api instance view methods:

example:

NearApi::Api.new.view_access_key('account_id.testnet')

Response: hash as described in NEAR Protocol JS API

Other methods can be called as:

NearApi::Api.new.json_rpc('EXPERIMENTAL_changes', {
  "changes_type": "single_access_key_changes",
  "keys": [
    {
      "account_id": "example-acct.testnet",
      "public_key": "ed25519:25KEc7t7MQohAJ4EDThd2vkksKkwangnuJFzcoiXj9oM"
    }
  ],
  "finality": "final"
}
)

Smart contract view function

query smart contract

NearApi::Query.new.call(
  'account_id.testnet',
  'get_num',
  {}
)

Transaction

NearApi::Transaction.new(
  'account_id.testnet', # Transaction call receiver
  [NearApi::Actions::FunctionCall.new('increment', {})], # Array of Actions 
  key: key, # Optional key 
  config: config # Optional config
).call

The example above is waiting for transaction complete

To send transaction async:

NearApi::Transaction.new(...).async

Transaction has one or many actions Actions:

  • Create Account
  • Deploy Contract
  • Function Call
  • Transfer
  • Stake
  • Add Key
  • Delete Key
  • Delete Account

Specs

Transaction status

Transaction_status

NearApi::Status.new.transaction_status(transaction_hash)

Transaction Status with Receipts

NearApi::Status.new.final_transaction_status(transaction_hash)

External Signing

To sign transaction with external signing service (dedicated secure service or hardware ledger)

transaction = NearApi::Transaction.new(...)
digest = transaction.message.digest
# sign with external service 
signature = Ed25519::SigningKey.from_keypair(bytestring).sign(digest)
transaction.call_api('broadcast_tx_commit', transaction.message.message, signature)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/near-rails-guide/near_api

License

The gem is available as open source under the terms of the MIT License.