minter 0.0.1 → 0.0.3

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.
Files changed (42) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +925 -6
  3. data/bin/console +6 -5
  4. data/lib/ffi/go/compile.rb +14 -0
  5. data/lib/ffi/transaction_ffi.rb +94 -0
  6. data/lib/ffi/wallet_ffi.rb +16 -0
  7. data/lib/minter.rb +11 -4
  8. data/lib/minter/api/client.rb +53 -0
  9. data/lib/minter/api/connection.rb +50 -0
  10. data/lib/minter/api/errors.rb +18 -0
  11. data/lib/minter/api/resources/address_resource.rb +21 -0
  12. data/lib/minter/api/resources/block_resource.rb +14 -0
  13. data/lib/minter/api/resources/candidate_resource.rb +19 -0
  14. data/lib/minter/api/resources/coin_resource.rb +13 -0
  15. data/lib/minter/api/resources/estimate_resource.rb +31 -0
  16. data/lib/minter/api/resources/events_resource.rb +14 -0
  17. data/lib/minter/api/resources/max_gas_resource.rb +12 -0
  18. data/lib/minter/api/resources/min_gas_price_resource.rb +12 -0
  19. data/lib/minter/api/resources/missed_blocks_resource.rb +13 -0
  20. data/lib/minter/api/resources/nonce_resource.rb +14 -0
  21. data/lib/minter/api/resources/send_transaction_resource.rb +13 -0
  22. data/lib/minter/api/resources/status_resource.rb +12 -0
  23. data/lib/minter/api/resources/transactions_resource.rb +25 -0
  24. data/lib/minter/api/resources/validators_resource.rb +13 -0
  25. data/lib/minter/api/result.rb +54 -0
  26. data/lib/minter/key.rb +29 -0
  27. data/lib/minter/transactions/buy_coin_tx.rb +36 -0
  28. data/lib/minter/transactions/create_coin_tx.rb +40 -0
  29. data/lib/minter/transactions/declare_candidacy_tx.rb +38 -0
  30. data/lib/minter/transactions/delegate_tx.rb +34 -0
  31. data/lib/minter/transactions/edit_candidate_tx.rb +34 -0
  32. data/lib/minter/transactions/redeem_check_tx.rb +32 -0
  33. data/lib/minter/transactions/sell_all_coin_tx.rb +34 -0
  34. data/lib/minter/transactions/sell_coin_tx.rb +36 -0
  35. data/lib/minter/transactions/send_coin_tx.rb +34 -0
  36. data/lib/minter/transactions/set_candidate_off_tx.rb +30 -0
  37. data/lib/minter/transactions/set_candidate_on_tx.rb +30 -0
  38. data/lib/minter/transactions/signed_tx.rb +16 -0
  39. data/lib/minter/transactions/unbond_tx.rb +34 -0
  40. data/lib/minter/version.rb +2 -2
  41. data/lib/minter/wallet.rb +13 -0
  42. metadata +129 -9
data/bin/console CHANGED
@@ -1,14 +1,15 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
- require "minter/ruby/sdk"
5
+ require "pry"
6
+ require "minter"
5
7
 
6
8
  # You can add fixtures and/or initialization code here to make experimenting
7
9
  # with your gem easier. You can also use a different console, if you like.
8
10
 
9
11
  # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
+ Pry.start
12
13
 
13
- require "irb"
14
- IRB.start(__FILE__)
14
+ # require "irb"
15
+ # IRB.start(__FILE__)
@@ -0,0 +1,14 @@
1
+ out_path = './goext'
2
+ arg_string = ""
3
+ {
4
+ out: out_path,
5
+ targets: ['linux/amd64', 'darwin/amd64'],
6
+ buildmode: 'c-shared',
7
+ }.each_pair do |name, args|
8
+ arg_string += "-#{name}"
9
+ arg_string += "=#{[args].flatten.join(',')}" unless args.nil?
10
+ arg_string += " "
11
+ end
12
+
13
+ puts "xgo #{arg_string.strip} ./"
14
+ system("xgo #{arg_string.strip} ./")
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ module Minter
5
+ module TransactionFfi
6
+ extend FFI::Library
7
+ filename = RUBY_PLATFORM.match('darwin') ? "/transaction-darwin-10.6-amd64.dylib" : "/transaction-linux-amd64.so"
8
+ ffi_lib File.join(File.dirname(__FILE__), filename)
9
+ attach_function :SignTransaction, [:string], :string
10
+ attach_function :TransactionHash, [:string], :string
11
+ attach_function :DecodeTransaction, [:string], :string
12
+ attach_function :SignCreateCoinTransaction, [:string], :string
13
+ attach_function :SignSellCoinTransaction, [:string], :string
14
+ attach_function :SignBuyCoinTransaction, [:string], :string
15
+ attach_function :SignSellAllCoinTransaction, [:string], :string
16
+ attach_function :SignDeclareCandidacyTransaction, [:string], :string
17
+ attach_function :SignDelegateTransaction, [:string], :string
18
+ attach_function :SignUnbondTransaction, [:string], :string
19
+ attach_function :SignSetCandidateOffTransaction, [:string], :string
20
+ attach_function :SignSetCandidateOnTransaction, [:string], :string
21
+ attach_function :SignRedeemCheckTransaction, [:string], :string
22
+ attach_function :SignEditCandidateTransaction, [:string], :string
23
+ # attach_function :SignMultisendTransaction, [:string], :string
24
+ end
25
+ end
26
+
27
+ # Examples of USAGE FFI
28
+ # ===SEND MNT===
29
+ # tx_params = {
30
+ # Nonce: 27,
31
+ # ChainId: 2,
32
+ # Type: 1,
33
+ # AddressTo: "Mx7633980c000139dd3bd24a3f54e06474fa941e16",
34
+ # Value: 24_000_000_000_000_000_000,
35
+ # Coin: "MNT",
36
+ # GasCoin: "MNT",
37
+ # GasPrice: 10,
38
+ # PrivateKey: "290cd647206bea71ec06d8dfacce30c872aea3fb1ea333b7f475b70467250ca0"
39
+ # }
40
+ # tx = Minter::TransactionFfi.SignTransaction(tx_params.to_json)
41
+ # Minter::Api::Client.new.send_transaction(transaction: tx)
42
+
43
+ # ==CREATE COIN==
44
+ # tx_params = {
45
+ # Name: "DEVTWO",
46
+ # Symbol: "DEVTWO",
47
+ # InitialAmount: 1000000000000000000,
48
+ # MaxSupply: 10000000000000000000,
49
+ # InitialReserve: 10000,
50
+ # ReserveRation: 10,
51
+ #
52
+ # Nonce: 29,
53
+ # ChainId: 2,
54
+ # GasCoin: "MNT",
55
+ # GasPrice: 1,
56
+ # PrivateKey: "290cd647206bea71ec06d8dfacce30c872aea3fb1ea333b7f475b70467250ca0"
57
+ # }
58
+ #
59
+ # tx = Minter::TransactionFfi.SignCreateCoinTransaction(tx_params.to_json)
60
+ # Minter::Api::Client.new.send_transaction(transaction: tx)
61
+
62
+ # ===SELL_COIN===
63
+ # tx_params = {
64
+ # CoinToSell: "DEVDEV",
65
+ # ValueToSell: 1000,
66
+ # CoinToBuy: "MNT",
67
+ # MinimumValueToBuy: 100000000,
68
+ #
69
+ # Nonce: 31,
70
+ # ChainId: 2,
71
+ # GasCoin: "MNT",
72
+ # GasPrice: 1,
73
+ # PrivateKey: "290cd647206bea71ec06d8dfacce30c872aea3fb1ea333b7f475b70467250ca0"
74
+ # }
75
+ #
76
+ # tx = Minter::TransactionFfi.SignSellCoinTransaction(tx_params.to_json)
77
+ # Minter::Api::Client.new.send_transaction(transaction: tx)
78
+
79
+ # ===BUY_COIN===
80
+ # tx_params = {
81
+ # CoinToBuy: "DEVDEV",
82
+ # ValueToBuy: 1000,
83
+ # CoinToSell: "MNT",
84
+ # MaximumValueToSell: 100000000,
85
+ #
86
+ # Nonce: 31,
87
+ # ChainId: 2,
88
+ # GasCoin: "MNT",
89
+ # GasPrice: 1,
90
+ # PrivateKey: "290cd647206bea71ec06d8dfacce30c872aea3fb1ea333b7f475b70467250ca0"
91
+ # }
92
+ #
93
+ # tx = Minter::TransactionFfi.SignBuyCoinTransaction(tx_params.to_json)
94
+ # Minter::Api::Client.new.send_transaction(transaction: tx)
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ module Minter
5
+ module WalletFfi
6
+ extend FFI::Library
7
+ filename = RUBY_PLATFORM.match('darwin') ? "/wallet-darwin-10.6-amd64.dylib" : "/wallet-linux-amd64.so"
8
+ ffi_lib File.join(File.dirname(__FILE__), filename)
9
+ attach_function :NewMnemonic, [], :string
10
+ attach_function :PrivateKeyFromMnemonic, [:string], :string
11
+ attach_function :PublicKeyFromPrivateKey, [:string], :string
12
+ attach_function :AddressFromMnemonic, [:string], :string
13
+ attach_function :AddressFromPrivateKey, [:string], :string
14
+ attach_function :AddressFromPublicKey, [:string], :string
15
+ end
16
+ end
data/lib/minter.rb CHANGED
@@ -1,7 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dotenv/load"
4
+ require "minter/api/client"
5
+
1
6
  require "minter/version"
7
+ require "minter/wallet.rb"
8
+ require "minter/key.rb"
9
+ require "ffi/wallet_ffi.rb"
10
+ require "ffi/transaction_ffi.rb"
11
+ Dir[File.dirname(__FILE__) + "/minter/transactions/*.rb"].each { |file| require_relative file }
2
12
 
3
13
  module Minter
4
- def self.minter
5
- puts "minter gem"
6
- end
7
- end
14
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ Dir[File.dirname(__FILE__) + "/resources/*.rb"].each do |file|
4
+ require_relative file
5
+ end
6
+ require_relative "connection"
7
+
8
+ module Minter
9
+ module Api
10
+ class Client
11
+ attr_accessor :node_url, :authenticated_api_url, :api_version
12
+ attr_accessor :proxy, :connect_timeout, :read_timeout, :write_timeout
13
+
14
+ include Minter::Api::AddressResource
15
+ include Minter::Api::BlockResource
16
+ include Minter::Api::CandidateResource
17
+ include Minter::Api::CoinResource
18
+ include Minter::Api::Connection
19
+ include Minter::Api::EstimateResource
20
+ include Minter::Api::EventsResource
21
+ include Minter::Api::MinGasPriceResource
22
+ include Minter::Api::MaxGasResource
23
+ include Minter::Api::MissedBlocksResource
24
+ include Minter::Api::NonceResource
25
+ include Minter::Api::SendTransactionResource
26
+ include Minter::Api::StatusResource
27
+ include Minter::Api::TransactionsResource
28
+ include Minter::Api::ValidatorsResource
29
+
30
+ DEFAULT_TIMEOUT = 30
31
+ API_VERSION = 2
32
+
33
+ def initialize(args = {})
34
+ self.node_url = ENV["NODE_URL"]
35
+ self.connect_timeout = args[:connect_timeout] || DEFAULT_TIMEOUT
36
+ self.read_timeout = args[:read_timeout] || DEFAULT_TIMEOUT
37
+ self.write_timeout = args[:write_timeout] || DEFAULT_TIMEOUT
38
+ end
39
+
40
+ def config
41
+ {
42
+ node_url: node_url,
43
+ authenticated_api_url: authenticated_api_url,
44
+ api_version: api_version,
45
+ connect_timeout: connect_timeout,
46
+ read_timeout: read_timeout,
47
+ write_timeout: write_timeout,
48
+ proxy: proxy
49
+ }
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "http"
4
+ require_relative "errors"
5
+ require_relative "result"
6
+
7
+ module Minter
8
+ module Api
9
+ module Connection
10
+ DEFAULT_HEADERS = [
11
+ CONTENT_TYPE_HEADER = "application/json",
12
+ ACCEPT_HEADER = "application/json"
13
+ ].freeze
14
+
15
+ def check_params(params, allowed_params)
16
+ return params if (params.keys - allowed_params).empty?
17
+
18
+ raise ParamsError
19
+ end
20
+
21
+ private
22
+
23
+ def get(path, params = {})
24
+ response = connection.get(build_url(config[:node_url], path), params: params)
25
+
26
+ Minter::Api::Result.new(response)
27
+ end
28
+
29
+ def connection
30
+ @connection ||= new_connection
31
+ end
32
+
33
+ def build_url(node_url, path)
34
+ URI.parse(node_url + path)
35
+ end
36
+
37
+ def new_connection
38
+ HTTP.timeout(timeout_config).headers(accept: ACCEPT_HEADER, content_type: CONTENT_TYPE_HEADER)
39
+ end
40
+
41
+ def timeout_config
42
+ @timeout_config ||= {
43
+ connect: config[:connect_timeout],
44
+ write: config[:write_timeout],
45
+ read: config[:read_timeout]
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ class ClientError < StandardError; end
6
+ class ParamsError < ClientError; end
7
+
8
+ class ServerError < StandardError; end
9
+ class ConnectionClosed < StandardError; end
10
+ class BadRequestError < ServerError; end
11
+ class NotFoundError < ServerError; end
12
+ class ForbiddenError < ServerError; end
13
+ class UnauthorizedError < ServerError; end
14
+ class PreconditionFailedError < ServerError; end
15
+ class InternalServerError < ServerError; end
16
+ class WebsocketError < ServerError; end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module AddressResource
6
+ def address(address:, height: nil)
7
+ params = { address: address, height: height }.compact
8
+ path = "/address"
9
+
10
+ get(path, params)
11
+ end
12
+
13
+ alias balance address
14
+
15
+ def addresses(_params = {})
16
+ path = "/address"
17
+ get(path)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module BlockResource
6
+ def block(height:)
7
+ params = { height: height }
8
+ path = "/block"
9
+
10
+ get(path, params)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module CandidateResource
6
+ def candidate(public_key:, height: nil)
7
+ path = "/candidate"
8
+ params = { pub_key: public_key, height: height }.compact
9
+ get(path, params)
10
+ end
11
+
12
+ def candidates(height: 1, include_stakes: nil)
13
+ path = "/candidates"
14
+ params = { height: height, include_stakes: nil }.compact
15
+ get(path, params)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module CoinResource
6
+ def coin_info(symbol:, height:)
7
+ params = { symbol: symbol, height: height }
8
+ path = "/coin_info"
9
+ get(path, params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module EstimateResource
6
+ def estimate_coin_buy(coinToSell:, valueToBuy:, coinToBuy:, height: 1)
7
+ params = { coinToSell: coinToSell, valueToBuy: valueToBuy, coinToBuy: coinToBuy, height: height }.compact
8
+ path = "/estimate_coin_buy"
9
+ get(path, params)
10
+ end
11
+
12
+ def estimate_coin_sell(coinToSell:, valueToSell:, coinToBuy:, height: nil)
13
+ params = { coinToSell: coinToSell, valueToSell: valueToSell, coinToBuy: coinToBuy, height: height }.compact
14
+ path = "/estimate_coin_sell"
15
+ get(path, params)
16
+ end
17
+
18
+ def estimate_coin_sell_all(coinToSell:, valueToSell:, coinToBuy:, gasPrice:, height: nil)
19
+ path = "/estimate_coin_sell_all"
20
+ params = { coinToSell: coinToSell, valueToSell: valueToBuy, coinToBuy: coinToBuy, gasPrice: gasPrice, height: height }.compact
21
+ get(path, params)
22
+ end
23
+
24
+ def estimate_tx_comission(transaction:)
25
+ path = "/estimate_tx_commission"
26
+ params = { transaction: transaction }
27
+ get(path, params)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module EventsResource
6
+ def events(height: nil)
7
+ height ||= 1
8
+ params = { height: height }
9
+ path = "/events"
10
+ get(path, params)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module MaxGasResource
6
+ def max_gas
7
+ path = "/max_gas"
8
+ get(path)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minter
4
+ module Api
5
+ module MinGasPriceResource
6
+ def min_gas_price
7
+ path = "/min_gas_price"
8
+ get(path)
9
+ end
10
+ end
11
+ end
12
+ end