near 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ab3d2b8796c8b9d6db6d842a843882a02b1497070454e67dda41a2105dbe7aa
4
- data.tar.gz: 66de38abb9b8bd7162116f110ae2a3f81412f7e580bb2e479db5133d2504f725
3
+ metadata.gz: cf5d4ecbf47a20dcb7316560abfdf3317505824ed2a501dc769e53186db46571
4
+ data.tar.gz: 5745b4bd4742054e9c5e710b4e267795636fd4d82c0866a3209ed804c306b25a
5
5
  SHA512:
6
- metadata.gz: 8cead3d3f26398a34925d6ba326c981f19be8e4324348f7dd8879a1c291ad11bd80080c11023477e3f4ee5bbfcf5bddfc421e85bea05f8e42acfd672e28bea0f
7
- data.tar.gz: f11b1a7a3c795865375feb3c873e9158009b6508a06150d91b3dff126b074920b2c91e658c6bc121e91098f64c198e47aaff0a7ee1348adb1e39364212eb2db5
6
+ metadata.gz: b877435cf142fab1a9d2f4b6651f1993dbf05b7c8eeea28cc76ea9ac6930a855a6570576ae5fb8012e869949efd1b56ca6cce5474bd318735996c9b8713d74f9
7
+ data.tar.gz: a0e57d4a298b6e52d320a9c2be9ccc8aef0f6abe7ed24e18535c6dbc0fffdbe153f3b8fa3c676cc33ac5823824d1a878175778684c88d1e272354d841ba9a388
data/CHANGES.md CHANGED
@@ -5,6 +5,22 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## 0.2.0 - 2025-01-29
9
+
10
+ ### Added
11
+ - `Network#fetch(block)`.
12
+ - `NEAR.testnet`, `NEAR.mainnet`.
13
+
14
+ ### Changed
15
+ - Use the NEAR_CLI environmnet variable, if set.
16
+ - Use a temporary file for `call_function`.
17
+ - Enhance `call_function()`.
18
+ - Enhance `create_account_with_funding()`.
19
+ - Enhance `delete_account()`.
20
+
21
+ ### Fixed
22
+ - Fix the formatting of balances.
23
+
8
24
  ## 0.1.0 - 2025-01-19
9
25
 
10
26
  ## 0.0.0 - 2025-01-11
data/README.md CHANGED
@@ -17,13 +17,14 @@ It wraps the [NEAR command-line interface] (CLI) into a Ruby interface.
17
17
  - Handles transaction construction, signing, and monitoring.
18
18
  - Integrates with hardware wallets and secure key storage.
19
19
  - Implements type-safe balance operations and input validation.
20
+ - Fetches block data from the [neardata.xyz] API.
20
21
  - Supports both the [mainnet] and [testnet] environments.
21
22
  - Offers cross-platform support with zero library dependencies.
22
23
  - 100% free and unencumbered public domain software.
23
24
 
24
25
  ## 🛠️ Prerequisites
25
26
 
26
- - [NEAR CLI] 0.17+
27
+ - [NEAR CLI] 0.19+
27
28
  - [Ruby] 3.0+
28
29
 
29
30
  ## ⬇️ Installation
@@ -40,12 +41,26 @@ gem install near
40
41
 
41
42
  ```ruby
42
43
  require 'near'
44
+ ```
45
+
46
+ ### Fetching block information
47
+
48
+ Block data is fetched from the high-performance [neardata.xyz] API
49
+ that caches blocks using Cloudflare's network with more than 330
50
+ global edge locations:
51
+
52
+ ```ruby
53
+ block = NEAR.testnet.fetch(186_132_854)
54
+ ```
43
55
 
56
+ ### Instantiating the CLI wrapper
57
+
58
+ ```ruby
44
59
  # Use the NEAR testnet (the default):
45
- testnet = NEAR::CLI.new(network: 'testnet')
60
+ testnet = NEAR::CLI.new(network: NEAR.testnet)
46
61
 
47
62
  # Use the NEAR mainnet (to test in prod):
48
- # mainnet = NEAR::CLI.new(network: 'mainnet')
63
+ # mainnet = NEAR::CLI.new(network: NEAR.mainnet)
49
64
  ```
50
65
 
51
66
  ### Viewing account details
@@ -230,3 +245,4 @@ git clone https://github.com/dryruby/near.rb.git
230
245
  [Ruby]: https://ruby-lang.org
231
246
  [mainnet]: https://docs.near.org/concepts/basics/networks#mainnet
232
247
  [testnet]: https://docs.near.org/concepts/basics/networks#testnet
248
+ [neardata.xyz]: https://neardata.xyz
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/near/account.rb CHANGED
@@ -5,12 +5,23 @@
5
5
  #
6
6
  # @see https://nomicon.io/DataStructures/Account
7
7
  class NEAR::Account
8
+ def self.parse(id)
9
+ self.new(id.to_s)
10
+ end
11
+
8
12
  ##
9
13
  # @param [String, #to_s] id
10
14
  def initialize(id)
11
15
  @id = id.to_s
12
16
  end
13
17
 
18
+ ##
19
+ # @return [NEAR::Account]
20
+ def parent
21
+ return nil unless @id.include?('.')
22
+ self.class.new(@id.split('.').drop(1).join('.'))
23
+ end
24
+
14
25
  ##
15
26
  # The account name.
16
27
  #
data/lib/near/balance.rb CHANGED
@@ -23,6 +23,10 @@ class NEAR::Balance
23
23
  end
24
24
  end
25
25
 
26
+ ##
27
+ # The canonical zero balance.
28
+ ZERO = self.new(0).freeze
29
+
26
30
  ##
27
31
  # The balance as a Ⓝ-prefixed string.
28
32
  #
@@ -35,7 +39,12 @@ class NEAR::Balance
35
39
  # The balance as a string.
36
40
  #
37
41
  # @return [String]
38
- def to_s; @quantity.to_s; end
42
+ def to_s
43
+ case @quantity
44
+ when BigDecimal then @quantity.to_s('F')
45
+ else @quantity.to_s
46
+ end
47
+ end
39
48
 
40
49
  ##
41
50
  # The balance as a rational number.
data/lib/near/block.rb CHANGED
@@ -51,6 +51,12 @@ class NEAR::Block
51
51
  ['now']
52
52
  end
53
53
 
54
+ ##
55
+ # @return [Integer]
56
+ def to_i
57
+ @height
58
+ end
59
+
54
60
  ##
55
61
  # @return [String]
56
62
  def to_s
@@ -88,18 +88,17 @@ module NEAR::CLI::Account
88
88
  ##
89
89
  # Creates a new account funded by another account.
90
90
  #
91
- # @param [String] new_account_id
92
- # @param [String] funding_account_id
93
- # @param [String] public_key
94
- # @param [String] deposit
91
+ # @param [NEAR::Account] new_account
92
+ # @param [NEAR::Account] signer Account that signs & funds the transaction
93
+ # @param [NEAR::Balance] deposit Amount of NEAR to attach
95
94
  # @return [String]
96
- def create_account_with_funding(new_account_id, funding_account_id, public_key, deposit)
95
+ def create_account_with_funding(new_account, signer:, deposit: nil)
97
96
  stdout, stderr = execute(
98
97
  'account',
99
98
  'create-account',
100
- 'fund-myself', new_account_id, deposit,
101
- 'use-manually-provided-public-key', public_key,
102
- 'sign-as', funding_account_id,
99
+ 'fund-myself', new_account.to_s, (deposit ? deposit.to_s : '0') + ' NEAR',
100
+ 'autogenerate-new-keypair', 'save-to-keychain',
101
+ 'sign-as', signer.to_s,
103
102
  'network-config', @network,
104
103
  'sign-with-keychain',
105
104
  'send'
@@ -126,14 +125,15 @@ module NEAR::CLI::Account
126
125
  ##
127
126
  # Deletes an account and transfers remaining balance to beneficiary.
128
127
  #
129
- # @param [String] account_id
130
- # @param [String] beneficiary_id
128
+ # @param [NEAR::Account] account
129
+ # @param [NEAR::Account] beneficiary
131
130
  # @return [String]
132
- def delete_account(account_id, beneficiary_id)
131
+ def delete_account(account, beneficiary: nil)
132
+ account = NEAR::Account.parse(account)
133
133
  stdout, stderr = execute(
134
134
  'account',
135
- 'delete-account', account_id,
136
- 'beneficiary', beneficiary_id,
135
+ 'delete-account', account.to_s,
136
+ 'beneficiary', (beneficiary || account.parent).to_s,
137
137
  'network-config', @network,
138
138
  'sign-with-keychain',
139
139
  'send'
@@ -1,21 +1,24 @@
1
1
  # This is free and unencumbered software released into the public domain.
2
2
 
3
+ require "base64"
4
+ require "pathname"
5
+
3
6
  ##
4
7
  # @see https://github.com/near/near-cli-rs/blob/main/docs/GUIDE.en.md#contract---Manage-smart-contracts-deploy-code-call-functions
5
8
  module NEAR::CLI::Contract
6
9
  ##
7
10
  # Calls a view method on a contract (read-only).
8
11
  #
9
- # @param [String] contract_id
12
+ # @param [NEAR::Account] contract
10
13
  # @param [String] method_name
11
14
  # @param [Hash] args JSON arguments for the method
12
15
  # @param [Block, Integer, String, Symbol] block
13
16
  # @return [String] The method call result
14
- def view_call(contract_id, method_name, args = {}, block: :now)
17
+ def view_call(contract, method_name, args = {}, block: :now)
15
18
  stdout, _ = execute(
16
19
  'contract',
17
20
  'call-function',
18
- 'as-read-only', contract_id, method_name,
21
+ 'as-read-only', contract.to_s, method_name,
19
22
  'json-args', args.to_json,
20
23
  'network-config', @network,
21
24
  *block_args(block)
@@ -26,22 +29,31 @@ module NEAR::CLI::Contract
26
29
  ##
27
30
  # Calls a state-changing method on a contract.
28
31
  #
29
- # @param [String] contract_id
32
+ # @param [NEAR::Account] contract
30
33
  # @param [String] method_name
31
- # @param [Hash] args JSON arguments for the method
32
- # @param [String] signer_id Account that signs the transaction
33
- # @param [String] deposit Amount of NEAR to attach
34
- # @param [String] gas Amount of gas to attach
34
+ # @param [Hash, Array, String, Pathname] args Arguments for the method
35
+ # @param [NEAR::Account] signer Account that signs the transaction
36
+ # @param [NEAR::Balance] deposit Amount of NEAR to attach
37
+ # @param [NEAR::Gas, #to_s] gas Amount of gas to attach
35
38
  # @return [String] Transaction result
36
- def call_function(contract_id, method_name, args = {}, signer_id:, deposit: '0 NEAR', gas: '30 TGas')
39
+ def call_function(contract, method_name, args = {}, signer:, deposit: nil, gas: '100.0 Tgas')
40
+ args = case args
41
+ when Hash, Array then ['json-args', args.to_json]
42
+ when String then case
43
+ when args.ascii_only? then ['text-args', args]
44
+ else ['base64-args', Base64.strict_encode64(args)]
45
+ end
46
+ when Pathname then ['file-args', args.to_s]
47
+ else raise ArgumentError, "Invalid argument type: #{args.inspect}"
48
+ end
37
49
  stdout, stderr = execute(
38
50
  'contract',
39
51
  'call-function',
40
- 'as-transaction', contract_id, method_name,
41
- 'json-args', args.to_json,
42
- 'prepaid-gas', gas,
43
- 'attached-deposit', deposit,
44
- 'sign-as', signer_id,
52
+ 'as-transaction', contract.to_s, method_name.to_s,
53
+ *args,
54
+ 'prepaid-gas', gas.to_s,
55
+ 'attached-deposit', (deposit ? deposit.to_s : '0') + ' NEAR',
56
+ 'sign-as', signer.to_s,
45
57
  'network-config', @network,
46
58
  'sign-with-keychain',
47
59
  'send'
data/lib/near/cli.rb CHANGED
@@ -22,6 +22,8 @@ class NEAR::CLI
22
22
  class ExecutionError < Error; end
23
23
 
24
24
  def self.find_program
25
+ return ENV['NEAR_CLI'] if ENV['NEAR_CLI']
26
+
25
27
  # First, check `$HOME/.cargo/bin/near`:
26
28
  path = File.expand_path('~/.cargo/bin/near')
27
29
  return path if File.executable?(path)
@@ -37,7 +39,7 @@ class NEAR::CLI
37
39
 
38
40
  ##
39
41
  # @param [String, #to_s] path
40
- # @param [String, #to_s] network
42
+ # @param [Network, #to_s] network
41
43
  def initialize(path: self.class.find_program, network: NEAR_ENV)
42
44
  @path, @network = path.to_s, network.to_s
43
45
  end
@@ -61,6 +63,7 @@ class NEAR::CLI
61
63
  # @param [Array<String>] args
62
64
  # @return [Array<String>]
63
65
  def execute(*args)
66
+ #command = [@path, '--quiet', *args.map(&:to_s)] # TODO: near-cli-rs 0.19+
64
67
  command = [@path, *args.map(&:to_s)]
65
68
  puts command.join(' ') if false
66
69
  stdout, stderr, status = Open3.capture3(*command)
@@ -0,0 +1,17 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'network'
4
+
5
+ ##
6
+ # Represents the NEAR Mainnet.
7
+ #
8
+ # @see https://docs.near.org/concepts/basics/networks#mainnet
9
+ class NEAR::Mainnet < NEAR::Network
10
+ NEARDATA_URL = 'https://mainnet.neardata.xyz'.freeze
11
+
12
+ ##
13
+ # @return [void]
14
+ def initialize
15
+ super(:mainnet)
16
+ end
17
+ end # NEAR::Mainnet
@@ -0,0 +1,43 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require 'net/http'
4
+
5
+ ##
6
+ # Represents a NEAR Protocol network.
7
+ #
8
+ # @see https://docs.near.org/concepts/basics/networks
9
+ class NEAR::Network
10
+ ##
11
+ # @param [Symbol, #to_sym] id
12
+ # @return [void]
13
+ def initialize(id)
14
+ @id = id.to_sym
15
+ end
16
+
17
+ ##
18
+ # @return [String]
19
+ def to_s; @id.to_s; end
20
+
21
+ ##
22
+ # @return [String]
23
+ def neardata_url
24
+ self.class.const_get(:NEARDATA_URL)
25
+ end
26
+
27
+ ##
28
+ # Fetches the block at the given height.
29
+ #
30
+ # The block data is fetched from the neardata.xyz API.
31
+ # If the block does not exist, `nil` is returned.
32
+ #
33
+ # @param [NEAR::Block, #to_i] block
34
+ # @return [Object]
35
+ def fetch(block)
36
+ response = Net::HTTP.get_response(URI("#{neardata_url}/v0/block/#{block.to_i}"))
37
+ case response
38
+ when Net::HTTPSuccess then JSON.parse(response.body)
39
+ when Net::HTTPNotFound then nil
40
+ else raise response.to_s
41
+ end
42
+ end
43
+ end # NEAR::Testnet
@@ -0,0 +1,17 @@
1
+ # This is free and unencumbered software released into the public domain.
2
+
3
+ require_relative 'network'
4
+
5
+ ##
6
+ # Represents the NEAR Testnet.
7
+ #
8
+ # @see https://docs.near.org/concepts/basics/networks#testnet
9
+ class NEAR::Testnet < NEAR::Network
10
+ NEARDATA_URL = 'https://testnet.neardata.xyz'.freeze
11
+
12
+ ##
13
+ # @return [void]
14
+ def initialize
15
+ super(:testnet)
16
+ end
17
+ end # NEAR::Testnet
data/lib/near.rb CHANGED
@@ -6,5 +6,22 @@ require_relative 'near/account'
6
6
  require_relative 'near/balance'
7
7
  require_relative 'near/block'
8
8
  require_relative 'near/cli'
9
+ require_relative 'near/mainnet'
10
+ require_relative 'near/network'
11
+ require_relative 'near/testnet'
9
12
  require_relative 'near/transaction'
10
13
  require_relative 'near/version'
14
+
15
+ module NEAR
16
+ ##
17
+ # @return [NEAR::Testnet]
18
+ def self.testnet
19
+ @testnet ||= Testnet.new
20
+ end
21
+
22
+ ##
23
+ # @return [NEAR::Mainnet]
24
+ def self.mainnet
25
+ @mainnet ||= Mainnet.new
26
+ end
27
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: near
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arto Bendiken
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-01-19 00:00:00.000000000 Z
10
+ date: 2025-01-29 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rspec
@@ -59,6 +59,9 @@ files:
59
59
  - lib/near/cli/staking.rb
60
60
  - lib/near/cli/tokens.rb
61
61
  - lib/near/cli/transaction.rb
62
+ - lib/near/mainnet.rb
63
+ - lib/near/network.rb
64
+ - lib/near/testnet.rb
62
65
  - lib/near/transaction.rb
63
66
  - lib/near/version.rb
64
67
  homepage: https://github.com/dryruby/near.rb