klaytn 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ed4fcb0b9e08db6917b1894cb830e6e1defe8d60ee36abab18eba1b115867cbf
4
+ data.tar.gz: 696c31b090e503f89fa591371aff141558ed1fab96455602602897a524a5d45d
5
+ SHA512:
6
+ metadata.gz: d15ee526048fc2241de2137ff7cb615975f9f3766de53298a54259f63a5dac24d1c52d7f6f4dbdc500f150ed8144c84adc8d8168a6bcada89f19a21c41f259d4
7
+ data.tar.gz: 51ea4334ed61229920b3881af2ed3ef3dffd024aaadddc51eb8f96bfed3f1cdece6df4bc9e50d5cac07891e953bb4dc049347009fc7e96934d46d8739708eccc
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
+ .env
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,71 @@
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.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to creating a positive environment
12
+ include:
13
+
14
+ * Using welcoming and inclusive language
15
+ * Being respectful of differing viewpoints and experiences
16
+ * Gracefully accepting constructive criticism
17
+ * Focusing on what is best for the community
18
+ * Showing empathy towards other community members
19
+
20
+ Examples of unacceptable behavior by participants include:
21
+
22
+ * The use of sexualized language or imagery and unwelcome sexual attention or
23
+ advances
24
+ * Trolling, insulting/derogatory comments, and personal or political attacks
25
+ * Public or private harassment
26
+ * Publishing others' private information, such as a physical or electronic
27
+ address, without explicit permission
28
+ * Other conduct which could reasonably be considered inappropriate in a
29
+ professional setting
30
+
31
+ ## Our Responsibilities
32
+
33
+ Project maintainers are responsible for clarifying the standards of acceptable
34
+ behavior and are expected to take appropriate and fair corrective action in
35
+ response to any instances of unacceptable behavior.
36
+
37
+ Project maintainers have the right and responsibility to remove, edit, or
38
+ reject comments, commits, code, wiki edits, issues, and other contributions
39
+ that are not aligned to this Code of Conduct, or to ban temporarily or
40
+ permanently any contributor for other behaviors that they deem inappropriate,
41
+ threatening, offensive, or harmful.
42
+
43
+ ## Scope
44
+
45
+ This Code of Conduct applies both within project spaces and in public spaces
46
+ when an individual is representing the project or its community. Examples of
47
+ representing a project or community include using an official project e-mail
48
+ address, posting via an official social media account, or acting as an appointed
49
+ representative at an online or offline event. Representation of a project may be
50
+ further defined and clarified by project maintainers.
51
+
52
+ ## Enforcement
53
+
54
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
55
+ reported by contacting the project team at launch@ooju.xyz. All
56
+ complaints will be reviewed and investigated and will result in a response that
57
+ is deemed necessary and appropriate to the circumstances. The project team is
58
+ obligated to maintain confidentiality with regard to the reporter of an incident.
59
+ Further details of specific enforcement policies may be posted separately.
60
+
61
+ Project maintainers who do not follow or enforce the Code of Conduct in good
62
+ faith may face temporary or permanent repercussions as determined by other
63
+ members of the project's leadership.
64
+
65
+ ## Attribution
66
+
67
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
68
+ available at [https://contributor-covenant.org/version/1/4][version]
69
+
70
+ [homepage]: https://contributor-covenant.org
71
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in klaytn.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ klaytn (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (6.1.4.6)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ zeitwerk (~> 2.3)
15
+ concurrent-ruby (1.1.9)
16
+ diff-lcs (1.5.0)
17
+ digest-sha3 (1.1.0)
18
+ ethereum.rb (2.5)
19
+ activesupport (>= 4.0)
20
+ digest-sha3 (~> 1.1)
21
+ httparty (0.17.3)
22
+ mime-types (~> 3.0)
23
+ multi_xml (>= 0.5.2)
24
+ i18n (1.10.0)
25
+ concurrent-ruby (~> 1.0)
26
+ mime-types (3.4.1)
27
+ mime-types-data (~> 3.2015)
28
+ mime-types-data (3.2022.0105)
29
+ minitest (5.15.0)
30
+ multi_xml (0.6.0)
31
+ rake (12.3.3)
32
+ rspec (3.11.0)
33
+ rspec-core (~> 3.11.0)
34
+ rspec-expectations (~> 3.11.0)
35
+ rspec-mocks (~> 3.11.0)
36
+ rspec-core (3.11.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-expectations (3.11.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-mocks (3.11.0)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.0)
45
+ tzinfo (2.0.4)
46
+ concurrent-ruby (~> 1.0)
47
+ zeitwerk (2.5.4)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ ethereum.rb (~> 2.5)
54
+ httparty (~> 0.17.0)
55
+ klaytn!
56
+ rake (~> 12.0)
57
+ rspec (~> 3.0)
58
+
59
+ BUNDLED WITH
60
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ryan Kulp
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,115 @@
1
+ # Klaytn Ruby Library
2
+ Execute transactions and interact with smart contracts, wallets, and NFT tokens on the Klaytn blockchain (https://www.klaytn.com/). Most functions require KAS Console credentials and KLAY tokens (inside a KAS Account Pool wallet) to pay transaction fees. Created by [ooju.xyz](https://ooju.xyz).
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'klaytn'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install klaytn
19
+
20
+ ## Usage
21
+ This library is modeled off the [KAS Wallet API](https://refs.klaytnapi.com/en/wallet/latest), but is not an exhaustive implementation. Below are the core features.
22
+
23
+ **Client authentication**
24
+ ```
25
+ opts = {
26
+ kas_access_key: 'KASxxx',
27
+ kas_secret_access_key: 'yyy',
28
+ kas_account_wallet_address: '0x0x',
29
+ kas_account_pool_krn: 'krn:ChainID:wallet:xxx',
30
+ chain_id: 1001 # testnet (baobab) - 1001; mainnet (cypress) - 8217
31
+ }
32
+ ```
33
+
34
+ **Transactions**
35
+ ```
36
+ client = Klaytn::Transaction.new(opts)
37
+
38
+ # fetch a transaction
39
+ result = client.get('0x00301b2b1c09ed43d4b59cbcd5610ebfe17a48215e6f6de10693eb368a489baa') # => {"blockHash"=>"0x597fb..."}
40
+
41
+ # create a transaction
42
+ result = client.send('0x00E7b604e9493d53749e7b7b9e39F313d9F9890a', 1, { memo: 'sending 1 peb' }) # => {"from"=>"0x..."}
43
+ ```
44
+
45
+ **Wallets**
46
+ ```
47
+ client = Klaytn::Wallet.new # no authentication required
48
+
49
+ # get a wallet
50
+ result = client.get('0x00e7b604e9493d53749e7b7b9e39f313d9f9890a') # => {"success"=>true, "code"=>0, "result"=> {"..."}
51
+
52
+ # get a wallet's KLAY balance
53
+ result = client.get_balance('0x00e7b604e9493d53749e7b7b9e39f313d9f9890a') # => 301.52 (KLAY)
54
+ ```
55
+
56
+ **Tokens**
57
+ ```
58
+ # instantiation requires additional params
59
+ opts = opts.merge(contract_address: '0xbceaa2fa50fef79bb5c4b2fc887dbdd3b96130b7')
60
+ client = Klaytn::Token.new(opts)
61
+
62
+ # get a token
63
+ result = client.get(1) # => {"tokenId"=>"0x1", "owner"=>"0x58e0cc86..."}
64
+ ```
65
+
66
+ **Contracts**
67
+ ```
68
+ # deploy a contract
69
+ client = Klaytn::Contract.new(opts)
70
+ result = client.deploy(bytecode) # => {"from"=>"0x6e1f42c1e..."}
71
+
72
+ # interact with a contract
73
+ opts = opts.merge(
74
+ abi: [{...}], # deployed contract ABI
75
+ contract_address: '0xbceaa2fa50fef79bb5c4b2fc887dbdd3b96130b7' # deployed contract address
76
+ )
77
+ client = Klaytn::Contract.new(opts)
78
+ result = client.invoke_function('addAddressToWhitelist', ['0x00E7b604e9493d53749e7b7b9e39F313d9F9890a']) # => {"from"=>"0x3f71cde4246cb..."}
79
+ ```
80
+
81
+ ## Development
82
+
83
+ After checking out the repo, run `bin/setup` to install dependencies. Then run `bin/console` for an interactive prompt that will allow you to experiment.
84
+
85
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
86
+
87
+ ## Testing
88
+
89
+ After installing the gem, run `rspec` to run the test suite. **Important**: you must first authenticate with KAS via:
90
+ ```
91
+ export KAS_ACCESS_KEY=KASxxx
92
+ export KAS_SECRET_ACCESS_KEY=yyy
93
+ export KAS_ACCOUNT_WALLET_ADDRESS=0x0x
94
+ export KAS_ACCOUNT_POOL_KRN=krn:ChainID:wallet:xxx
95
+ ```
96
+
97
+ Without these local `ENV` variables, most tests will fail.
98
+
99
+ Sometimes `rspec` doesn't load variables, even if you can `$echo` them. To prevent subtle bugs, run the test suite via:
100
+ ```
101
+ KAS_ACCESS_KEY="KASxxx" KAS_SECRET_ACCESS_KEY="yyy" KAS_ACCOUNT_WALLET_ADDRESS="0x0x" KAS_ACCOUNT_POOL_KRN="krn:ChainID:wallet:xxx" rake spec
102
+ ```
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at https://github.com/OojuTeam/klaytn-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/OojuTeam/klaytn-ruby/blob/master/CODE_OF_CONDUCT.md).
107
+
108
+
109
+ ## License
110
+
111
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
112
+
113
+ ## Code of Conduct
114
+
115
+ Everyone interacting in the Klaytn Ruby project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/OojuTeam/klaytn-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 "klaytn"
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/klaytn.gemspec ADDED
@@ -0,0 +1,33 @@
1
+ require_relative 'lib/klaytn/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "klaytn"
5
+ spec.version = Klaytn::VERSION
6
+ spec.authors = ["Ryan Kulp", "Ooju"]
7
+ spec.email = ["launch@ooju.xyz"]
8
+
9
+ spec.summary = %q{Klaytn is a Ruby wrapper for the Klaytn blockchain.}
10
+ spec.description = %q{Interact with transactions, smart contracts, and NFTs on the Klaytn blockchain in pure Ruby.}
11
+ spec.homepage = "https://github.com/OojuTeam/klaytn-ruby"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/OojuTeam/klaytn-ruby"
19
+ spec.metadata["changelog_uri"] = "https://github.com/usefomo/fomo-ruby-sdk/releases"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "rspec", "~> 3.9.1"
31
+ spec.add_development_dependency "httparty", "~> 0.17.0"
32
+ spec.add_development_dependency "ethereum.rb", "~> 2.5"
33
+ end
@@ -0,0 +1,11 @@
1
+ module Klaytn
2
+ class Authentication
3
+ def auth_params(kas_access_key, kas_secret_access_key)
4
+ { username: kas_access_key, password: kas_secret_access_key }
5
+ end
6
+
7
+ def headers(chain_id)
8
+ { 'x-chain-id' => chain_id.to_s } # chain id must be string!
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ module Klaytn
2
+ class Base
3
+ INVALID_CLIENT = 'No params provided - please provide a contract address, ABI, KAS credentials, etc'.freeze
4
+ MISSING_KAS_CREDS = 'KAS credentials missing'.freeze
5
+ MISSING_CONTRACT = 'Please provide a deployed smart contract address.'.freeze
6
+
7
+ MISSING_ACCOUNT_WALLET = 'Please provide a KAS Account wallet to pay for transactions.'.freeze # created via KAS > Service > Wallet > Account Pool > Create Account Pool > Create Account
8
+ MISSING_ACCOUNT_POOL_KRN = 'Please provide a KAS Account Pool KRN id to finish linking your KAS Wallet (ex: krn:XXXX:wallet:yyyy...).'.freeze # KAS > Service > Wallet > Account Pool > KRN
9
+ MISSING_ABI = 'Please provide the contract ABI, an array-like object returned by the compiler.'.freeze
10
+ FUNCTION_NOT_FOUND = 'Function with definition XXX not found.'.freeze
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ module Klaytn
2
+ class Client < Base
3
+ attr_reader :contract_address, :chain_id, :headers, :basic_auth
4
+
5
+ def initialize(opts = {})
6
+ raise INVALID_CLIENT if opts == {}
7
+
8
+ @contract_address = opts[:contract_address]
9
+ @chain_id = setup_chain_id(opts)
10
+ @headers = Authentication.new.headers(chain_id)
11
+ @basic_auth = setup_basic_auth(opts)
12
+ end
13
+
14
+ def setup_chain_id(opts)
15
+ opts[:chain_id] || 1001 # default to baobab testnet
16
+ end
17
+
18
+ def setup_basic_auth(opts)
19
+ raise MISSING_KAS_CREDS unless opts[:kas_access_key].present? && opts[:kas_secret_access_key].present?
20
+ Authentication.new.auth_params(opts[:kas_access_key], opts[:kas_secret_access_key])
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,62 @@
1
+ module Klaytn
2
+ class Contract < Client
3
+ BASE_URL = 'https://wallet-api.klaytnapi.com/v2/tx/contract'.freeze
4
+
5
+ attr_reader :kas_account_wallet_address, :kas_account_pool_krn, :abi, :encoder
6
+
7
+ def initialize(opts)
8
+ raise MISSING_ACCOUNT_WALLET if opts[:kas_account_wallet_address].blank?
9
+ raise MISSING_ACCOUNT_POOL_KRN if opts[:kas_account_pool_krn].blank?
10
+
11
+ @kas_account_wallet_address = opts[:kas_account_wallet_address]
12
+ @kas_account_pool_krn = opts[:kas_account_pool_krn]
13
+ @abi = opts[:abi]
14
+
15
+ @encoder = Encoder.new
16
+
17
+ super
18
+ end
19
+
20
+ def deploy(bytecode, submit: true)
21
+ body = {
22
+ from: kas_account_wallet_address, # must be created inside KAS console > Account Pool, and MUST be capital version from Klaytn Scope!
23
+ input: bytecode,
24
+ gas: 900000, # needs to be high for deploy to work
25
+ submit: submit
26
+ }
27
+
28
+ resp = HTTParty.post(BASE_URL + '/deploy', body: body.to_json, headers: headers.merge('x-krn' => kas_account_pool_krn), basic_auth: basic_auth)
29
+ JSON.parse(resp.body)
30
+ end
31
+
32
+ ## DEPRECATE: definition ex: 'addAddressToWhitelist(address)' - string literal Solidity function definition
33
+ # definition ex: 'addAddressToWhitelist' - string literal Solidity function definition, without parameters/parens
34
+ # inputs ex: [ OpenStruct.new({ "internalType": "address", "name": "addr", "type": "address" }) ] - array of dot-notation object from ABI
35
+ # params ex: ['0x0xxxx'] - array of arguments accepted by function
36
+
37
+ def invoke_function(definition, params)
38
+ raise MISSING_CONTRACT if contract_address.blank?
39
+ raise MISSING_ABI if abi.blank?
40
+
41
+ found_function = abi.filter {|input| input[:name] == definition }.first
42
+ raise FUNCTION_NOT_FOUND.gsub('XXX', definition) if found_function.nil?
43
+
44
+ built_defintion = "#{definition}(#{found_function[:inputs].map {|i| i[:type]}.join(',')})"
45
+ built_inputs = found_function[:inputs].map { |i| OpenStruct.new(i) }
46
+
47
+ body = function_body_builder(built_defintion, built_inputs, params)
48
+ resp = HTTParty.post(BASE_URL + '/execute', body: body.to_json, headers: headers.merge('x-krn' => kas_account_pool_krn), basic_auth: basic_auth)
49
+ JSON.parse(resp.body)
50
+ end
51
+
52
+ def function_body_builder(definition, inputs, params)
53
+ {
54
+ from: kas_account_wallet_address,
55
+ to: contract_address,
56
+ input: encoder.encode_function(definition, inputs, params),
57
+ # gas: 900000, # better to exclude, otherwise 'insufficient funds' error is common
58
+ submit: true
59
+ }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,7 @@
1
+ module Klaytn
2
+ class Decoder
3
+ def decode_uint(encoded_uint) # ex: 0x4563918244f40000 => 5000000000000000000 (5 KLAY)
4
+ Ethereum::Decoder.new.decode_uint(encoded_uint)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Klaytn
2
+ class Encoder
3
+ def encode_function(definition, inputs, params)
4
+ prefix = '0x'
5
+ str = Ethereum::Function.calc_id(definition) # ex: "getEarnout(uint256)"
6
+ num = Ethereum::Encoder.new.encode_arguments(inputs, params) # ex: [1], or [1,500]
7
+
8
+ "#{prefix}#{str}#{num}"
9
+ end
10
+
11
+ def encode_uint(integer) # ex: 5000000000000000000 (5 KLAY) => 0x4563918244f40000
12
+ return '0x0' if integer.zero?
13
+ long = Ethereum::Encoder.new.encode_uint(integer)
14
+ '0x' + long.sub!(/^[0]+/,'')
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module Klaytn
2
+ class Token < Client
3
+ BASE_URL = 'https://th-api.klaytnapi.com/v2/contract/nft'.freeze
4
+
5
+ attr_reader :encoder
6
+
7
+ def initialize(opts = {})
8
+ raise MISSING_CONTRACT if opts[:contract_address].blank?
9
+
10
+ @encoder = Encoder.new
11
+ super
12
+ end
13
+
14
+ def get(token_id)
15
+ url = BASE_URL + "/#{contract_address}/token/#{encoded_token_id(token_id)}"
16
+ resp = HTTParty.get(url, headers: headers, basic_auth: basic_auth)
17
+ JSON.parse(resp.body)
18
+ end
19
+
20
+ def encoded_token_id(token_id)
21
+ encoder.encode_uint(token_id)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,40 @@
1
+ module Klaytn
2
+ class Transaction < Client
3
+ BASE_URL = 'https://wallet-api.klaytnapi.com/v2/tx'.freeze
4
+
5
+ attr_reader :kas_account_wallet_address, :kas_account_pool_krn, :encoder
6
+
7
+ def initialize(opts)
8
+ @kas_account_wallet_address = opts[:kas_account_wallet_address] # created via KAS > Service > Wallet > Account Pool > Create Account Pool > Create Account
9
+ @kas_account_pool_krn = opts[:kas_account_pool_krn]
10
+ @encoder = Encoder.new
11
+ super
12
+ end
13
+
14
+ def get(hash)
15
+ resp = HTTParty.get(BASE_URL + "/#{hash}", headers: headers, basic_auth: basic_auth)
16
+ JSON.parse(resp.body)
17
+ end
18
+
19
+ def send(address, amount_in_peb, opts = {})
20
+ raise MISSING_ACCOUNT_WALLET if kas_account_wallet_address.blank?
21
+ raise MISSING_ACCOUNT_POOL_KRN if kas_account_pool_krn.blank?
22
+
23
+ body = {
24
+ from: kas_account_wallet_address,
25
+ to: address,
26
+ value: encoder.encode_uint(amount_in_peb), # '0x0' = 0 KLAY, '0x4563918244f40000' = 5 KLAY
27
+ memo: opts[:memo], # can be a text note; viewable on Klaytn Scope!
28
+ gas: opts[:gas] || 200000, # not required, default is 1,000,000 if excluded
29
+ submit: should_submit?(opts) # create real transaction by default
30
+ }
31
+
32
+ resp = HTTParty.post(BASE_URL + '/value', body: body.to_json, headers: headers.merge('x-krn' => kas_account_pool_krn), basic_auth: basic_auth)
33
+ JSON.parse(resp.body)
34
+ end
35
+
36
+ def should_submit?(opts)
37
+ opts[:submit] == false ? false : true
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Klaytn
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,31 @@
1
+ module Klaytn
2
+ class Wallet < Client
3
+ BASE_URL = 'https://api-cypress-v2.scope.klaytn.com/v2/accounts'.freeze
4
+ PEB_DIVISOR = 1000000000000000000.freeze # friendly 'get_balance' result, ex: "5" == 5 KLAY
5
+
6
+ attr_reader :divisor
7
+
8
+ # no params needed, overrides Klaytn::Client
9
+ def initialize(peb_divisor: true)
10
+ @divisor = peb_divisor ? PEB_DIVISOR : 1
11
+ end
12
+
13
+ def get(address)
14
+ resp = HTTParty.get(BASE_URL + "/#{address}", headers: headers)
15
+ data = JSON.parse(resp.body)
16
+ rescue => e
17
+ raise e.message
18
+ end
19
+
20
+ def get_balance(address)
21
+ data = get(address)
22
+ data.dig('result', 'balance').to_f / divisor
23
+ rescue => e
24
+ raise e.message
25
+ end
26
+
27
+ def headers
28
+ { origin: 'https://scope.klaytn.com' }
29
+ end
30
+ end
31
+ end
data/lib/klaytn.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'klaytn/version'
2
+ require 'httparty'
3
+ require 'ethereum.rb'
4
+
5
+ module Klaytn
6
+ require 'klaytn/authentication'
7
+ require 'klaytn/base'
8
+ require 'klaytn/client'
9
+ require 'klaytn/contract'
10
+ require 'klaytn/decoder'
11
+ require 'klaytn/encoder'
12
+ require 'klaytn/token'
13
+ require 'klaytn/transaction'
14
+ require 'klaytn/wallet'
15
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: klaytn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Kulp
8
+ - Ooju
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2022-03-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 3.9.1
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 3.9.1
28
+ - !ruby/object:Gem::Dependency
29
+ name: httparty
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.17.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.17.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: ethereum.rb
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '2.5'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '2.5'
56
+ description: Interact with transactions, smart contracts, and NFTs on the Klaytn blockchain
57
+ in pure Ruby.
58
+ email:
59
+ - launch@ooju.xyz
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Gemfile.lock
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - klaytn.gemspec
75
+ - lib/klaytn.rb
76
+ - lib/klaytn/authentication.rb
77
+ - lib/klaytn/base.rb
78
+ - lib/klaytn/client.rb
79
+ - lib/klaytn/contract.rb
80
+ - lib/klaytn/decoder.rb
81
+ - lib/klaytn/encoder.rb
82
+ - lib/klaytn/token.rb
83
+ - lib/klaytn/transaction.rb
84
+ - lib/klaytn/version.rb
85
+ - lib/klaytn/wallet.rb
86
+ homepage: https://github.com/OojuTeam/klaytn-ruby
87
+ licenses:
88
+ - MIT
89
+ metadata:
90
+ homepage_uri: https://github.com/OojuTeam/klaytn-ruby
91
+ source_code_uri: https://github.com/OojuTeam/klaytn-ruby
92
+ changelog_uri: https://github.com/usefomo/fomo-ruby-sdk/releases
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.3.0
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubygems_version: 3.0.3
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Klaytn is a Ruby wrapper for the Klaytn blockchain.
112
+ test_files: []