cita-sdk-ruby 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 191e6e673e063a855ab3d9bff925ed99d88cb211d3a6600fbead317c5059ca87
4
+ data.tar.gz: 320fb2ce7b5b1fdec68fa20f1b68d171599bf682caaaa539e4f1d3b18750a57b
5
+ SHA512:
6
+ metadata.gz: cda5eb17355ba2889eeb10faed7cd1755f62a7c19fd0981b03447c2dacbb04ed009488dcacaeff314bf59dce6cee241d8493b76551f2345e6afc793bb3fb33a5
7
+ data.tar.gz: eb19a729cb48b60b23890925955b5839c7f2ac31983e9de6e4779eac22fadd8621d8cd264ef2974c6b833663d1c789b4f1f987ea6a3804f44551fdce6c40e6a3
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gem
data/.gitmodules ADDED
@@ -0,0 +1,6 @@
1
+ [submodule "cita-proto"]
2
+ path = cita-proto
3
+ url = https://github.com/cryptape/cita-proto.git
4
+ [submodule "secp256k1"]
5
+ path = secp256k1
6
+ url = https://github.com/bitcoin-core/secp256k1.git
data/.pryrc ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "awesome_print"
4
+ AwesomePrint.pry!
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,103 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ Exclude:
4
+ - 'spec/**/*'
5
+ - 'bin/*'
6
+ - 'Rakefile'
7
+ - 'Gemfile'
8
+ - 'Gemfile.lock'
9
+ - 'cita.gemspec'
10
+ - 'cita-proto/**/*'
11
+ - 'secp256k1/**/*'
12
+ - 'lib/cita/protos/**/*'
13
+
14
+ Bundler/OrderedGems:
15
+ Enabled: false
16
+
17
+ Layout/AccessModifierIndentation:
18
+ EnforcedStyle: indent
19
+
20
+ Layout/EmptyLineAfterMagicComment:
21
+ Enabled: false
22
+
23
+ Layout/SpaceInsideHashLiteralBraces:
24
+ EnforcedStyle: space
25
+
26
+ Metrics/AbcSize:
27
+ Max: 100
28
+
29
+ Metrics/ClassLength:
30
+ CountComments: false
31
+ Max: 300
32
+
33
+ Metrics/CyclomaticComplexity:
34
+ Max: 25
35
+
36
+ Metrics/LineLength:
37
+ AllowURI: true
38
+ Enabled: false
39
+
40
+ Metrics/MethodLength:
41
+ CountComments: false
42
+ Max: 55
43
+
44
+ Metrics/ModuleLength:
45
+ CountComments: false
46
+ Max: 200
47
+
48
+ Metrics/ParameterLists:
49
+ Max: 5
50
+ CountKeywordArgs: true
51
+
52
+ Metrics/PerceivedComplexity:
53
+ Max: 20
54
+
55
+ Naming/MemoizedInstanceVariableName:
56
+ Enabled: false
57
+
58
+ Style/CollectionMethods:
59
+ Enabled: true
60
+ PreferredMethods:
61
+ find_all: 'select'
62
+
63
+ Style/DoubleNegation:
64
+ Enabled: true
65
+
66
+ Style/FrozenStringLiteralComment:
67
+ Enabled: true
68
+
69
+ Style/GuardClause:
70
+ Enabled: false
71
+
72
+ Style/Lambda:
73
+ Enabled: false
74
+
75
+ Style/Documentation:
76
+ Enabled: false
77
+
78
+ Style/PercentLiteralDelimiters:
79
+ PreferredDelimiters:
80
+ '%i': '()'
81
+ '%w': '()'
82
+
83
+ Style/ClassAndModuleChildren:
84
+ Enabled: false
85
+
86
+ Naming/AccessorMethodName:
87
+ Enabled: false
88
+
89
+ Metrics/BlockLength:
90
+ CountComments: false
91
+ Max: 25
92
+ ExcludedMethods:
93
+ - refine
94
+ - included
95
+
96
+ Style/StringLiterals:
97
+ EnforcedStyle: double_quotes
98
+ SupportedStyles:
99
+ - single_quotes
100
+ - double_quotes
101
+ # If `true`, strings which span multiple lines using `\` for continuation must
102
+ # use the same type of quotes on each line.
103
+ ConsistentQuotesInMultiline: false
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.5
7
+ - 2.5.3
8
+ before_install: gem install bundler -v 1.16.6
9
+ install:
10
+ - cd secp256k1 && ./autogen.sh && ./configure --enable-module-recovery --enable-experimental --enable-module-ecdh && make && sudo make install && cd ..
11
+ - bundle install
12
+ script:
13
+ - bundle exec rake
14
+
15
+ after_success:
16
+ - bash <(curl -s https://codecov.io/bash)
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at classicalliu@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cita.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cita-sdk-ruby (0.20.0)
5
+ activesupport (~> 5.2.1)
6
+ ciri-crypto (= 0.1.1)
7
+ faraday (~> 0.15.3)
8
+ google-protobuf (~> 3.6)
9
+ web3-eth (~> 0.2.16)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activesupport (5.2.1)
15
+ concurrent-ruby (~> 1.0, >= 1.0.2)
16
+ i18n (>= 0.7, < 2)
17
+ minitest (~> 5.1)
18
+ tzinfo (~> 1.1)
19
+ ast (2.4.0)
20
+ awesome_print (1.8.0)
21
+ bitcoin-secp256k1 (0.4.0)
22
+ ffi (>= 1.9.10)
23
+ ciri-crypto (0.1.1)
24
+ bitcoin-secp256k1 (~> 0.4.0)
25
+ ciri-utils (~> 0.2.0)
26
+ ciri-utils (0.2.2)
27
+ digest-sha3 (~> 1.1.0)
28
+ coderay (1.1.2)
29
+ concurrent-ruby (1.0.5)
30
+ diff-lcs (1.3)
31
+ digest-sha3 (1.1.0)
32
+ faraday (0.15.3)
33
+ multipart-post (>= 1.2, < 3)
34
+ ffi (1.9.25)
35
+ google-protobuf (3.6.1-universal-darwin)
36
+ i18n (1.1.1)
37
+ concurrent-ruby (~> 1.0)
38
+ jaro_winkler (1.5.1)
39
+ method_source (0.9.0)
40
+ minitest (5.11.3)
41
+ multipart-post (2.0.0)
42
+ parallel (1.12.1)
43
+ parser (2.5.1.2)
44
+ ast (~> 2.4.0)
45
+ powerpack (0.1.2)
46
+ pry (0.11.3)
47
+ coderay (~> 1.1.0)
48
+ method_source (~> 0.9.0)
49
+ rainbow (3.0.0)
50
+ rake (10.5.0)
51
+ rlp (0.7.3)
52
+ rspec (3.8.0)
53
+ rspec-core (~> 3.8.0)
54
+ rspec-expectations (~> 3.8.0)
55
+ rspec-mocks (~> 3.8.0)
56
+ rspec-core (3.8.0)
57
+ rspec-support (~> 3.8.0)
58
+ rspec-expectations (3.8.1)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.8.0)
61
+ rspec-mocks (3.8.0)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.8.0)
64
+ rspec-support (3.8.0)
65
+ rubocop (0.59.2)
66
+ jaro_winkler (~> 1.5.1)
67
+ parallel (~> 1.10)
68
+ parser (>= 2.5, != 2.5.1.1)
69
+ powerpack (~> 0.1)
70
+ rainbow (>= 2.2.2, < 4.0)
71
+ ruby-progressbar (~> 1.7)
72
+ unicode-display_width (~> 1.0, >= 1.0.1)
73
+ ruby-progressbar (1.10.0)
74
+ thread_safe (0.3.6)
75
+ tzinfo (1.2.5)
76
+ thread_safe (~> 0.1)
77
+ unicode-display_width (1.4.0)
78
+ web3-eth (0.2.16)
79
+ digest-sha3 (~> 1.1.0)
80
+ rlp (~> 0.7.3)
81
+
82
+ PLATFORMS
83
+ ruby
84
+
85
+ DEPENDENCIES
86
+ awesome_print (~> 1.8)
87
+ bundler (~> 1.16)
88
+ cita-sdk-ruby!
89
+ pry (~> 0.11)
90
+ rake (~> 10.0)
91
+ rspec (~> 3.0)
92
+ rubocop (~> 0.59)
93
+
94
+ BUNDLED WITH
95
+ 1.16.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Cryptape
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # cita-sdk-ruby
2
+
3
+ [![Build Status](https://travis-ci.org/cryptape/cita-sdk-ruby.svg?branch=master)](https://travis-ci.org/cryptape/cita-sdk-ruby)
4
+ [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](https://www.rubydoc.info/github/cryptape/cita-sdk-ruby/master)
5
+
6
+ CITA Ruby SDK
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'cita-sdk-ruby'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install cita-sdk-ruby
23
+
24
+ ## Usage
25
+
26
+ See `keccak256`, `to_hex` and other utils methods in `CITA::Utils` module
27
+
28
+ RPC calls [RPC list](https://docs.nervos.org/cita/#/rpc_guide/rpc)
29
+ ```ruby
30
+ cita = CITA::Client.new("your url")
31
+
32
+ cita.rpc.block_number
33
+ cita.rpc.get_block_by_number("0x0", true)
34
+ # or
35
+ cita.rpc.getBlockByNumber("0x0", true)
36
+ ```
37
+
38
+ sign and unsign
39
+ ```ruby
40
+ # make a Transaction object first
41
+ transaction = CITA::Transaction.new(
42
+ to: "8ff0f5b85fba9a6429e2e256880291774f8e224f",
43
+ nonce: "e4f195c409fe47c58a624de37c730679",
44
+ quota: 30000,
45
+ valid_until_block: 1882078,
46
+ data: "",
47
+ value: "0x3e8",
48
+ chain_id: "1",
49
+ version: 1
50
+ )
51
+
52
+ # sign transaction with your private key
53
+ content = CITA::TransactionSigner.encode(transaction, "you private key")
54
+
55
+ # you can unsign content by `decode` method
56
+ CITA::TransactionSigner.decode(content)
57
+ ```
58
+
59
+ send transaction
60
+ ```ruby
61
+ cita.rpc.send_transaction(transaction, private_key)
62
+ ```
63
+
64
+ transfer tokens
65
+ ```ruby
66
+ cita.rpc.transfer(to: "to address", value: 1000, private_key)
67
+ ```
68
+
69
+ contract
70
+ ```ruby
71
+ contract = cita.contract_at(abi, contract_address)
72
+ # for RPC call (constant functions)
73
+ response = contract.call_func(method: :symbol)
74
+ # for RPC sendTransaction
75
+ response = contract.send_func(tx: tx, private_key: private_key, method: :transfer, params: [address, tokens])
76
+ ```
77
+
78
+ ## CHANGELOG
79
+
80
+ ### v0.1
81
+
82
+ * supports CITA 0.19
83
+
84
+ ### v0.2
85
+
86
+ * supports CITA 0.20 and 0.19
87
+ * set default transaction version to 1
88
+ * update TransactionSinger.decode output type to hash with symbol keys
89
+ * parse TransactionSinger.decode hash value to hex string if it's bytes.
90
+
91
+ ### v0.20.0
92
+
93
+ * rename to `cita-sdk-ruby`
94
+ * rename top module name to `CITA`
95
+
96
+ ## Contributing
97
+
98
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cryptape/cita-sdk-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
99
+
100
+ ## License
101
+
102
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
103
+
104
+ ## Code of Conduct
105
+
106
+ Everyone interacting in the cita-sdk-ruby project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/cryptape/cita-sdk-ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cita"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ require "pry"
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/cita.gemspec ADDED
@@ -0,0 +1,47 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "cita/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cita-sdk-ruby"
8
+ spec.version = CITA::VERSION
9
+ spec.authors = ["classicalliu"]
10
+ spec.email = ["classicalliu@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby CITA SDK}
13
+ spec.description = %q{Ruby CITA SDK for signature and rpc call}
14
+ spec.homepage = "https://github.com/cryptape/cita-sdk-ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "pry", "~> 0.11"
39
+ spec.add_development_dependency "awesome_print", "~> 1.8"
40
+ spec.add_development_dependency "rubocop", "~> 0.59"
41
+
42
+ spec.add_dependency "google-protobuf", "~> 3.6"
43
+ spec.add_dependency "ciri-crypto", "0.1.1"
44
+ spec.add_dependency "faraday", "~> 0.15.3"
45
+ spec.add_dependency "activesupport", "~> 5.2.1"
46
+ spec.add_dependency "web3-eth", "~> 0.2.16"
47
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CITA
4
+ class Address
5
+ # @param str [String]
6
+ def initialize(str)
7
+ @addr = Utils.remove_hex_prefix(str)
8
+ end
9
+
10
+ # get address with `0x` prefix
11
+ #
12
+ # @return [String] address hex string with `0x` prefix
13
+ def addr
14
+ Utils.add_hex_prefix(@addr)
15
+ end
16
+
17
+ # compare address is equal
18
+ #
19
+ # @param other [CITA::Address]
20
+ def ==(other)
21
+ addr.casecmp(other.addr)
22
+ end
23
+
24
+ # TODO: valid? method that check address is valid?
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CITA
4
+ class Client
5
+ attr_reader :url, :rpc, :http, :contract
6
+
7
+ def initialize(url)
8
+ @url = url
9
+ @rpc = RPC.new(url)
10
+ @http = rpc.http
11
+ end
12
+
13
+ def contract_at(abi, address)
14
+ @contract = Contract.new(abi, url, address, rpc)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "web3/eth"
4
+
5
+ module CITA
6
+ class Contract
7
+ include Web3::Eth::Abi::AbiCoder
8
+
9
+ attr_reader :url, :abi, :address, :rpc
10
+
11
+ # @param abi [String | Hash] json string or hash
12
+ # @param url [String] chain url
13
+ # @param address [String] contract address
14
+ # @param rpc [CITA::RPC]
15
+ #
16
+ # @return [void]
17
+ def initialize(abi, url, address = nil, rpc = nil)
18
+ @url = url
19
+ @abi = abi
20
+ @address = address
21
+ @rpc = rpc
22
+ parse_url
23
+ end
24
+
25
+ # wrapper Web3::Eth abi encoder for encoded data
26
+ #
27
+ # @param method_name [Symbol | String] method name you call
28
+ # @param *params [Array] method params you call
29
+ #
30
+ # @return [String] hex data
31
+ def function_data(method_name, *params)
32
+ data, _output_types = function_data_with_ot(method_name, *params)
33
+ data
34
+ end
35
+
36
+ # call contract functions by rpc `call` method
37
+ #
38
+ # @param method [Symbol | String] the method name you call
39
+ # @param params [Array] the method params you call
40
+ # @param tx [Hash] see rpc `call` doc for more info
41
+ #
42
+ # @return [any]
43
+ def call_func(method:, params: [], tx: {}) # rubocop:disable Naming/UncommunicativeMethodParamName
44
+ data, output_types = function_data_with_ot(method, *params)
45
+ resp = @rpc.call_rpc(:call, params: [tx.merge(data: data, to: address), "latest"])
46
+ result = resp["result"]
47
+
48
+ data = [Utils.remove_hex_prefix(result)].pack("H*")
49
+ return if data.nil?
50
+
51
+ re = decode_abi output_types, data
52
+ re.length == 1 ? re.first : re
53
+ end
54
+
55
+ # call contract functions by sendRawTransaction
56
+ #
57
+ # @param tx [Hash | CITA::Transaction]
58
+ # @param private_key [String] hex string
59
+ # @param method [Symbol | String] method name you call
60
+ # @param *params [Array] your params
61
+ #
62
+ # @return [nil | Hash] {hash: "", status: ""}, sendRawTransactionResult
63
+ def send_func(tx:, private_key:, method:, params: []) # rubocop:disable Naming/UncommunicativeMethodParamName
64
+ data, _output_types = function_data_with_ot(method, *params)
65
+ transaction = if tx.is_a?(Hash)
66
+ Transaction.from_hash(tx)
67
+ else
68
+ tx
69
+ end
70
+ transaction.data = data
71
+ resp = @rpc.send_transaction(transaction, private_key)
72
+
73
+ resp&.dig("result")
74
+ end
75
+
76
+ private
77
+
78
+ # parse url to host, port and scheme
79
+ def parse_url
80
+ uri = URI.parse(@url)
81
+ @host = uri.host
82
+ @port = uri.port
83
+ @scheme = uri.scheme
84
+ end
85
+
86
+ # is this url in https?
87
+ def https?
88
+ @scheme == "https"
89
+ end
90
+
91
+ # wrapper Web3::Eth abi encoder for encoded data
92
+ def function_data_with_ot(method_name, *params)
93
+ web3 = Web3::Eth::Rpc.new host: @host, port: @port, connect_options: { use_ssl: https? }
94
+ contract = web3.eth.contract(abi).at(address)
95
+ contract.function_data(method_name, *params)
96
+ end
97
+ end
98
+ end