stellar_spectrum 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +10 -0
- data/CHANGELOG.md +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +134 -0
- data/LICENSE.txt +21 -0
- data/README.md +80 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/docker-compose.yml +10 -0
- data/lib/stellar_spectrum.rb +29 -0
- data/lib/stellar_spectrum/client.rb +163 -0
- data/lib/stellar_spectrum/services/get_available_channels.rb +13 -0
- data/lib/stellar_spectrum/services/get_key_for_address.rb +11 -0
- data/lib/stellar_spectrum/services/get_locked_accounts.rb +29 -0
- data/lib/stellar_spectrum/services/init_redis.rb +9 -0
- data/lib/stellar_spectrum/version.rb +3 -0
- data/stellar_spectrum.gemspec +48 -0
- metadata +197 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 572c2a17c34bf9b430da4906b24ffc6d734876a1fd59b557a6b7cf081ea0f177
|
4
|
+
data.tar.gz: 2828ed570fafb2f6e7c708b8aaf22f8ef7f50d8d5d1dc4460d9b5ea19e55b8b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 83b828802a2c63dfe94bb8f76fbd7be2fc5050bf4ec25f96e8fce6da67e4e4255fc73de0efa8718f4bf03e9a810cb5cd542758bdc448bd399b0ace584697fa5d
|
7
|
+
data.tar.gz: '0658c887c5257915a22282c38a41dba16e5d4fa93c20e8dd5f9a27ff1d2aa32b10b1fadd04803d21592f52d977c5a50eed3513c28dc3c8436d3022198c066e73'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [0.1.0] - 2018-10-19
|
8
|
+
### Added
|
9
|
+
- Initial release
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -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 ramon.tayag@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,14 @@
|
|
1
|
+
source "https://rubygems.org"
|
2
|
+
|
3
|
+
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
4
|
+
|
5
|
+
|
6
|
+
gem("stellar-sdk", {
|
7
|
+
github: "bloom-solutions/ruby-stellar-sdk",
|
8
|
+
branch: "clarify-payment-channel",
|
9
|
+
})
|
10
|
+
|
11
|
+
# Specify your gem's dependencies in stellar_spectrum.gemspec
|
12
|
+
gemspec
|
13
|
+
|
14
|
+
gem "pry-byebug"
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
GIT
|
2
|
+
remote: https://github.com/bloom-solutions/ruby-stellar-sdk
|
3
|
+
revision: df0113532301625f962956da3d9818787d0910ee
|
4
|
+
branch: clarify-payment-channel
|
5
|
+
specs:
|
6
|
+
stellar-sdk (0.5.0)
|
7
|
+
activesupport (>= 5.2.0)
|
8
|
+
contracts (~> 0.16)
|
9
|
+
excon (~> 0.44, >= 0.44.4)
|
10
|
+
hyperclient (~> 0.7)
|
11
|
+
stellar-base (>= 0.16.0)
|
12
|
+
toml-rb (~> 1.1, >= 1.1.1)
|
13
|
+
|
14
|
+
PATH
|
15
|
+
remote: .
|
16
|
+
specs:
|
17
|
+
stellar_spectrum (0.1.0)
|
18
|
+
activesupport
|
19
|
+
gem_config
|
20
|
+
redis
|
21
|
+
stellar-sdk
|
22
|
+
|
23
|
+
GEM
|
24
|
+
remote: https://rubygems.org/
|
25
|
+
specs:
|
26
|
+
activemodel (5.2.1)
|
27
|
+
activesupport (= 5.2.1)
|
28
|
+
activesupport (5.2.1)
|
29
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
30
|
+
i18n (>= 0.7, < 2)
|
31
|
+
minitest (~> 5.1)
|
32
|
+
tzinfo (~> 1.1)
|
33
|
+
addressable (2.5.2)
|
34
|
+
public_suffix (>= 2.0.2, < 4.0)
|
35
|
+
base32 (0.3.2)
|
36
|
+
byebug (10.0.2)
|
37
|
+
citrus (3.0.2)
|
38
|
+
coderay (1.1.2)
|
39
|
+
concurrent-ruby (1.0.5)
|
40
|
+
contracts (0.16.0)
|
41
|
+
crack (0.4.3)
|
42
|
+
safe_yaml (~> 1.0.0)
|
43
|
+
diff-lcs (1.3)
|
44
|
+
digest-crc (0.4.1)
|
45
|
+
excon (0.62.0)
|
46
|
+
faraday (0.15.3)
|
47
|
+
multipart-post (>= 1.2, < 3)
|
48
|
+
faraday-digestauth (0.3.0)
|
49
|
+
faraday (~> 0.7)
|
50
|
+
net-http-digest_auth (~> 1.4)
|
51
|
+
faraday_hal_middleware (0.1.0)
|
52
|
+
faraday_middleware (~> 0.9)
|
53
|
+
faraday_middleware (0.12.2)
|
54
|
+
faraday (>= 0.7.4, < 1.0)
|
55
|
+
ffi (1.9.25)
|
56
|
+
gem_config (0.3.1)
|
57
|
+
hashdiff (0.3.7)
|
58
|
+
hyperclient (0.9.0)
|
59
|
+
faraday (>= 0.9.0)
|
60
|
+
faraday-digestauth
|
61
|
+
faraday_hal_middleware
|
62
|
+
faraday_middleware
|
63
|
+
net-http-digest_auth
|
64
|
+
uri_template
|
65
|
+
i18n (1.1.0)
|
66
|
+
concurrent-ruby (~> 1.0)
|
67
|
+
method_source (0.9.0)
|
68
|
+
minitest (5.11.3)
|
69
|
+
multipart-post (2.0.0)
|
70
|
+
net-http-digest_auth (1.4.1)
|
71
|
+
pry (0.11.3)
|
72
|
+
coderay (~> 1.1.0)
|
73
|
+
method_source (~> 0.9.0)
|
74
|
+
pry-byebug (3.6.0)
|
75
|
+
byebug (~> 10.0)
|
76
|
+
pry (~> 0.10)
|
77
|
+
public_suffix (3.0.3)
|
78
|
+
rake (10.5.0)
|
79
|
+
rbnacl (5.0.0)
|
80
|
+
ffi
|
81
|
+
rbnacl-libsodium (1.0.16)
|
82
|
+
rbnacl (>= 3.0.1)
|
83
|
+
redis (4.0.2)
|
84
|
+
rspec (3.8.0)
|
85
|
+
rspec-core (~> 3.8.0)
|
86
|
+
rspec-expectations (~> 3.8.0)
|
87
|
+
rspec-mocks (~> 3.8.0)
|
88
|
+
rspec-core (3.8.0)
|
89
|
+
rspec-support (~> 3.8.0)
|
90
|
+
rspec-expectations (3.8.2)
|
91
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
92
|
+
rspec-support (~> 3.8.0)
|
93
|
+
rspec-mocks (3.8.0)
|
94
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
95
|
+
rspec-support (~> 3.8.0)
|
96
|
+
rspec-support (3.8.0)
|
97
|
+
safe_yaml (1.0.4)
|
98
|
+
stellar-base (0.17.0)
|
99
|
+
activesupport (>= 5.2.0)
|
100
|
+
base32
|
101
|
+
digest-crc
|
102
|
+
rbnacl
|
103
|
+
rbnacl-libsodium (~> 1.0.16)
|
104
|
+
xdr (~> 3.0.0)
|
105
|
+
thread_safe (0.3.6)
|
106
|
+
toml-rb (1.1.2)
|
107
|
+
citrus (~> 3.0, > 3.0)
|
108
|
+
tzinfo (1.2.5)
|
109
|
+
thread_safe (~> 0.1)
|
110
|
+
uri_template (0.7.0)
|
111
|
+
vcr (4.0.0)
|
112
|
+
webmock (3.4.2)
|
113
|
+
addressable (>= 2.3.6)
|
114
|
+
crack (>= 0.3.2)
|
115
|
+
hashdiff
|
116
|
+
xdr (3.0.0)
|
117
|
+
activemodel (>= 5.2.0)
|
118
|
+
activesupport (>= 5.2.0)
|
119
|
+
|
120
|
+
PLATFORMS
|
121
|
+
ruby
|
122
|
+
|
123
|
+
DEPENDENCIES
|
124
|
+
bundler (~> 1.16)
|
125
|
+
pry-byebug
|
126
|
+
rake (~> 10.0)
|
127
|
+
rspec (~> 3.0)
|
128
|
+
stellar-sdk!
|
129
|
+
stellar_spectrum!
|
130
|
+
vcr
|
131
|
+
webmock
|
132
|
+
|
133
|
+
BUNDLED WITH
|
134
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Ramon Tayag
|
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,80 @@
|
|
1
|
+
# StellarSpectrum
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.com/bloom-solutions/stellar_spectrum-ruby.svg?branch=master)](https://travis-ci.com/bloom-solutions/stellar_spectrum-ruby)
|
4
|
+
|
5
|
+
Use Stellar payment channels in Ruby with ease.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'stellar_spectrum'
|
13
|
+
```
|
14
|
+
|
15
|
+
## Usage
|
16
|
+
|
17
|
+
In an initializer:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
StellarSpectrum.configure do |c|
|
21
|
+
c.redis_url = "redis://redis"
|
22
|
+
|
23
|
+
# Add as many seeds as you want. They must be funded, and are the source of transaction fees. The product of the time in seconds it takes to make a new ledger in Stellar and the number of transactions you want to be able to do per second is the number of Stellar seeds you should place here. For example:
|
24
|
+
|
25
|
+
# 10 second ledger time * 5 transactions per second = 50 channels
|
26
|
+
c.seeds = [
|
27
|
+
"STELLAR-PAYMENT-CHANNEL-SOURCE-1",
|
28
|
+
"STELLAR-PAYMENT-CHANNEL-SOURCE-2",
|
29
|
+
"STELLAR-PAYMENT-CHANNEL-SOURCE-3",
|
30
|
+
"STELLAR-PAYMENT-CHANNEL-SOURCE-4",
|
31
|
+
]
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
### Initializing a Client
|
36
|
+
|
37
|
+
This is the instance that you will be interacting with to craft transactions.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
spectrum = StellarSpectrum.new(
|
41
|
+
redis_url: "...", # only to override the default set in the config
|
42
|
+
seeds: %w(SEED1 SEED2), # only to override the default set in the config
|
43
|
+
)
|
44
|
+
```
|
45
|
+
|
46
|
+
### Sending a Payment
|
47
|
+
|
48
|
+
The method below picks an available channel in the config's seeds.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
spectrum.send_payment(
|
52
|
+
from: Stellar::Account.from_seed("seed of account that will send 30 XLM"),
|
53
|
+
to: Stellar::Account.from_address("destination address"),
|
54
|
+
amount: Stellar::Amount.new(30),
|
55
|
+
)
|
56
|
+
```
|
57
|
+
|
58
|
+
At this point, one of the payment channels will be marked as "locked", and will not be used until the next Stellar ledger is created.
|
59
|
+
|
60
|
+
When another transaction is created, an available (unlocked) payment channel will be chosen.
|
61
|
+
|
62
|
+
If all payment channels are unavailable (locked), then the call will block the Ruby process/thread until a channel is available. Therefore, unless you have way more channels than you will ever consume, we strongly recommend you send transactions in a background worker like Sidekiq.
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
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).
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bloom-solutions/stellar_spectrum-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.
|
73
|
+
|
74
|
+
## License
|
75
|
+
|
76
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
77
|
+
|
78
|
+
## Code of Conduct
|
79
|
+
|
80
|
+
Everyone interacting in the StellarSpectrum project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/bloom-solutions/stellar_spectrum-ruby/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "stellar_spectrum"
|
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
data/docker-compose.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "gem_config"
|
2
|
+
require "stellar_spectrum/version"
|
3
|
+
require "stellar_spectrum/client"
|
4
|
+
require "stellar-sdk"
|
5
|
+
require "redis"
|
6
|
+
require "active_support/core_ext/hash/except"
|
7
|
+
require "active_support/core_ext/object/blank"
|
8
|
+
require 'active_support/core_ext/integer/inflections'
|
9
|
+
require "stellar_spectrum/services/get_available_channels"
|
10
|
+
require "stellar_spectrum/services/get_key_for_address"
|
11
|
+
require "stellar_spectrum/services/get_locked_accounts"
|
12
|
+
require "stellar_spectrum/services/init_redis"
|
13
|
+
|
14
|
+
module StellarSpectrum
|
15
|
+
|
16
|
+
include GemConfig::Base
|
17
|
+
|
18
|
+
with_configuration do
|
19
|
+
has :redis_url, classes: String
|
20
|
+
has :seeds, classes: Array
|
21
|
+
has :horizon_url, classes: String
|
22
|
+
has :logger
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.new(*args)
|
26
|
+
Client.new(*args)
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
module StellarSpectrum
|
2
|
+
class Client
|
3
|
+
|
4
|
+
attr_accessor :redis_url
|
5
|
+
attr_accessor :seeds
|
6
|
+
attr_accessor :horizon_url
|
7
|
+
attr_accessor :logger
|
8
|
+
|
9
|
+
LOG_TAG = "[StellarSpectrum]"
|
10
|
+
MAX_LOCK_TIME_IN_SECONDS = 30
|
11
|
+
|
12
|
+
def initialize(
|
13
|
+
redis_url: StellarSpectrum.configuration.redis_url,
|
14
|
+
seeds: StellarSpectrum.configuration.seeds,
|
15
|
+
horizon_url: StellarSpectrum.configuration.horizon_url,
|
16
|
+
logger: StellarSpectrum.configuration.logger
|
17
|
+
)
|
18
|
+
self.redis_url = redis_url
|
19
|
+
self.seeds = seeds
|
20
|
+
self.horizon_url = horizon_url
|
21
|
+
self.logger = logger
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_payment(from:, destination:, amount:, memo: nil, tries: 0)
|
25
|
+
tries += 1
|
26
|
+
available_channels = GetAvailableChannels.execute(
|
27
|
+
redis: redis,
|
28
|
+
locked_accounts: locked_accounts,
|
29
|
+
channel_accounts: channel_accounts,
|
30
|
+
)
|
31
|
+
|
32
|
+
if available_channels.empty?
|
33
|
+
log "No available channels, retry for the #{tries.ordinalize} time..."
|
34
|
+
attempt_release_lock!
|
35
|
+
|
36
|
+
return send_payment(
|
37
|
+
from: from,
|
38
|
+
destination: destination,
|
39
|
+
amount: amount,
|
40
|
+
memo: memo,
|
41
|
+
tries: tries,
|
42
|
+
)
|
43
|
+
end
|
44
|
+
|
45
|
+
log "Sending payment. #{available_channels.count} available channels: " \
|
46
|
+
"#{available_channels.map(&:address).inspect}"
|
47
|
+
|
48
|
+
channel_account = available_channels.first
|
49
|
+
|
50
|
+
next_sequence_number = next_sequence_number_for(channel_account)
|
51
|
+
|
52
|
+
successfully_locked = lock!(channel_account.address, {
|
53
|
+
sequence_number: next_sequence_number,
|
54
|
+
})
|
55
|
+
|
56
|
+
if !successfully_locked
|
57
|
+
log "Unable to lock #{channel_account.address}, " \
|
58
|
+
"retry for the #{tries.ordinalize} time..."
|
59
|
+
|
60
|
+
attempt_release_lock!
|
61
|
+
|
62
|
+
return send_payment(
|
63
|
+
from: from,
|
64
|
+
destination: destination,
|
65
|
+
amount: amount,
|
66
|
+
memo: memo,
|
67
|
+
tries: tries,
|
68
|
+
)
|
69
|
+
end
|
70
|
+
|
71
|
+
stellar_client.send_payment(
|
72
|
+
from: from,
|
73
|
+
to: destination,
|
74
|
+
amount: amount,
|
75
|
+
memo: memo,
|
76
|
+
channel_account: channel_account,
|
77
|
+
sequence: next_sequence_number,
|
78
|
+
)
|
79
|
+
end
|
80
|
+
|
81
|
+
def attempt_release_lock!(except_address: nil)
|
82
|
+
if logger
|
83
|
+
log_message = "Attempting to release a lock"
|
84
|
+
if except_address.present?
|
85
|
+
log_message << "(except for address #{except_address})"
|
86
|
+
end
|
87
|
+
log log_message
|
88
|
+
end
|
89
|
+
|
90
|
+
address, address_info = locked_accounts.except(except_address).
|
91
|
+
sort_by {|address, info| info[:pttl]}.last
|
92
|
+
|
93
|
+
return false if address.nil?
|
94
|
+
|
95
|
+
current_sequence_number = current_sequence_number_for(address)
|
96
|
+
address_info = locked_accounts[address]
|
97
|
+
|
98
|
+
# Someone else may have unlocked it while we were taking our sweet
|
99
|
+
# time fetching the sequence number
|
100
|
+
return false if address_info.nil?
|
101
|
+
|
102
|
+
address_sequence_number = address_info[:sequence_number].to_i
|
103
|
+
if current_sequence_number < address_sequence_number
|
104
|
+
log "Not releasing #{address}: sequence number locked at " \
|
105
|
+
"#{address_sequence_number} is > current sequence number " \
|
106
|
+
"#{current_sequence_number}"
|
107
|
+
return false
|
108
|
+
end
|
109
|
+
|
110
|
+
log "Releasing #{address}: sequence number locked at " \
|
111
|
+
"#{address_sequence_number} is <= current sequence number " \
|
112
|
+
"#{current_sequence_number}"
|
113
|
+
unlock!(address)
|
114
|
+
end
|
115
|
+
|
116
|
+
def channel_accounts
|
117
|
+
@channel_accounts ||= seeds.map { |s| Stellar::Account.from_seed(s) }
|
118
|
+
end
|
119
|
+
|
120
|
+
def locked_accounts
|
121
|
+
GetLockedAccounts.execute(redis: redis, channel_accounts: channel_accounts)
|
122
|
+
end
|
123
|
+
|
124
|
+
def stellar_client
|
125
|
+
@stellar_client ||= Stellar::Client.new(horizon: self.horizon_url)
|
126
|
+
end
|
127
|
+
|
128
|
+
def redis
|
129
|
+
@redis ||= InitRedis.execute(redis_url: self.redis_url)
|
130
|
+
end
|
131
|
+
|
132
|
+
def lock!(address, sequence_number:)
|
133
|
+
redis.set(GetKeyForAddress.execute(address), sequence_number, {
|
134
|
+
nx: true,
|
135
|
+
ex: MAX_LOCK_TIME_IN_SECONDS,
|
136
|
+
})
|
137
|
+
end
|
138
|
+
|
139
|
+
def unlock!(address)
|
140
|
+
log "Unlocking address #{address}"
|
141
|
+
redis.del(GetKeyForAddress.execute(address))
|
142
|
+
end
|
143
|
+
|
144
|
+
def next_sequence_number_for(address_or_account)
|
145
|
+
current_sequence_number_for(address_or_account) + 1
|
146
|
+
end
|
147
|
+
|
148
|
+
def current_sequence_number_for(address_or_account)
|
149
|
+
account = address_or_account
|
150
|
+
if address_or_account.is_a?(String)
|
151
|
+
account = Stellar::Account.from_address(address_or_account)
|
152
|
+
end
|
153
|
+
|
154
|
+
stellar_client.account_info(account).sequence.to_i
|
155
|
+
end
|
156
|
+
|
157
|
+
def log(message)
|
158
|
+
return if logger.nil?
|
159
|
+
logger.info [LOG_TAG, message].join(" ")
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module StellarSpectrum
|
2
|
+
class GetAvailableChannels
|
3
|
+
|
4
|
+
def self.execute(redis:, locked_accounts:, channel_accounts:)
|
5
|
+
locked_addresses = locked_accounts.keys
|
6
|
+
channel_accounts.each_with_object([]) do |account, available_channels|
|
7
|
+
next if locked_addresses.include?(account.address)
|
8
|
+
available_channels << account
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module StellarSpectrum
|
2
|
+
class GetLockedAccounts
|
3
|
+
|
4
|
+
REDIS_PTTL_KEY_NON_PRESENT = -2
|
5
|
+
|
6
|
+
def self.execute(redis:, channel_accounts:)
|
7
|
+
addresses = channel_accounts.map(&:address)
|
8
|
+
address_keys = addresses.map {|a| GetKeyForAddress.execute(a) }
|
9
|
+
redis_response = redis.multi do
|
10
|
+
redis.mget(*address_keys)
|
11
|
+
address_keys.each do |key|
|
12
|
+
redis.pttl key
|
13
|
+
end
|
14
|
+
end
|
15
|
+
address_sequence_numbers = redis_response[0]
|
16
|
+
address_pttls = redis_response[1..-1]
|
17
|
+
|
18
|
+
addresses.each_with_object({}).with_index do |(address, hash), i|
|
19
|
+
pttl = address_pttls[i]
|
20
|
+
|
21
|
+
next if pttl == REDIS_PTTL_KEY_NON_PRESENT
|
22
|
+
|
23
|
+
sequence_number = address_sequence_numbers[i]
|
24
|
+
hash[address] = {sequence_number: sequence_number, pttl: pttl}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "stellar_spectrum/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "stellar_spectrum"
|
8
|
+
spec.version = StellarSpectrum::VERSION
|
9
|
+
spec.authors = ["Ramon Tayag", "Jasper Martin"]
|
10
|
+
spec.email = ["ramon.tayag@gmail.com", "jasper@bloom.solutions"]
|
11
|
+
|
12
|
+
spec.summary = %q{Use Stellar payment channels in Ruby with ease}
|
13
|
+
spec.homepage = "https://github.com/bloom-solutions/stellar_spectrum-ruby"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
20
|
+
|
21
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
22
|
+
spec.metadata["source_code_uri"] = "https://github.com/bloom-solutions/stellar_spectrum-ruby"
|
23
|
+
spec.metadata["changelog_uri"] = "https://github.com/bloom-solutions/stellar_spectrum-ruby/blob/master/CHANGELOG.md"
|
24
|
+
else
|
25
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
26
|
+
"public gem pushes."
|
27
|
+
end
|
28
|
+
|
29
|
+
# Specify which files should be added to the gem when it is released.
|
30
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
31
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
32
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
33
|
+
end
|
34
|
+
spec.bindir = "exe"
|
35
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
36
|
+
spec.require_paths = ["lib"]
|
37
|
+
|
38
|
+
spec.add_dependency "activesupport"
|
39
|
+
spec.add_dependency "gem_config"
|
40
|
+
spec.add_dependency "redis"
|
41
|
+
spec.add_dependency "stellar-sdk"
|
42
|
+
|
43
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
44
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
45
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
46
|
+
spec.add_development_dependency "vcr"
|
47
|
+
spec.add_development_dependency "webmock"
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stellar_spectrum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ramon Tayag
|
8
|
+
- Jasper Martin
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-10-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '0'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '0'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: gem_config
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: redis
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: stellar-sdk
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: bundler
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '1.16'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '1.16'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rake
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - "~>"
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '10.0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - "~>"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '10.0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rspec
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - "~>"
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '3.0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - "~>"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '3.0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: vcr
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: webmock
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
description:
|
141
|
+
email:
|
142
|
+
- ramon.tayag@gmail.com
|
143
|
+
- jasper@bloom.solutions
|
144
|
+
executables: []
|
145
|
+
extensions: []
|
146
|
+
extra_rdoc_files: []
|
147
|
+
files:
|
148
|
+
- ".gitignore"
|
149
|
+
- ".rspec"
|
150
|
+
- ".travis.yml"
|
151
|
+
- CHANGELOG.md
|
152
|
+
- CODE_OF_CONDUCT.md
|
153
|
+
- Gemfile
|
154
|
+
- Gemfile.lock
|
155
|
+
- LICENSE.txt
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- bin/console
|
159
|
+
- bin/setup
|
160
|
+
- docker-compose.yml
|
161
|
+
- lib/stellar_spectrum.rb
|
162
|
+
- lib/stellar_spectrum/client.rb
|
163
|
+
- lib/stellar_spectrum/services/get_available_channels.rb
|
164
|
+
- lib/stellar_spectrum/services/get_key_for_address.rb
|
165
|
+
- lib/stellar_spectrum/services/get_locked_accounts.rb
|
166
|
+
- lib/stellar_spectrum/services/init_redis.rb
|
167
|
+
- lib/stellar_spectrum/version.rb
|
168
|
+
- stellar_spectrum.gemspec
|
169
|
+
homepage: https://github.com/bloom-solutions/stellar_spectrum-ruby
|
170
|
+
licenses:
|
171
|
+
- MIT
|
172
|
+
metadata:
|
173
|
+
allowed_push_host: https://rubygems.org
|
174
|
+
homepage_uri: https://github.com/bloom-solutions/stellar_spectrum-ruby
|
175
|
+
source_code_uri: https://github.com/bloom-solutions/stellar_spectrum-ruby
|
176
|
+
changelog_uri: https://github.com/bloom-solutions/stellar_spectrum-ruby/blob/master/CHANGELOG.md
|
177
|
+
post_install_message:
|
178
|
+
rdoc_options: []
|
179
|
+
require_paths:
|
180
|
+
- lib
|
181
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
182
|
+
requirements:
|
183
|
+
- - ">="
|
184
|
+
- !ruby/object:Gem::Version
|
185
|
+
version: '0'
|
186
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
187
|
+
requirements:
|
188
|
+
- - ">="
|
189
|
+
- !ruby/object:Gem::Version
|
190
|
+
version: '0'
|
191
|
+
requirements: []
|
192
|
+
rubyforge_project:
|
193
|
+
rubygems_version: 2.7.7
|
194
|
+
signing_key:
|
195
|
+
specification_version: 4
|
196
|
+
summary: Use Stellar payment channels in Ruby with ease
|
197
|
+
test_files: []
|