coinbase 0.0.1 → 4.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +7 -0
- data/CONTRIBUTING.md +53 -0
- data/Gemfile +4 -0
- data/LICENSE +201 -0
- data/README.md +621 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/coinbase.gemspec +26 -0
- data/lib/coinbase/util.rb +16 -0
- data/lib/coinbase/wallet/adapters/em_http.rb +78 -0
- data/lib/coinbase/wallet/adapters/net_http.rb +68 -0
- data/lib/coinbase/wallet/api_client.rb +755 -0
- data/lib/coinbase/wallet/api_errors.rb +120 -0
- data/lib/coinbase/wallet/api_response.rb +41 -0
- data/lib/coinbase/wallet/ca-coinbase.crt +101 -0
- data/lib/coinbase/wallet/client.rb +101 -0
- data/lib/coinbase/wallet/coinbase-callback.pub +14 -0
- data/lib/coinbase/wallet/models/account.rb +193 -0
- data/lib/coinbase/wallet/models/address.rb +12 -0
- data/lib/coinbase/wallet/models/api_object.rb +46 -0
- data/lib/coinbase/wallet/models/checkout.rb +19 -0
- data/lib/coinbase/wallet/models/order.rb +12 -0
- data/lib/coinbase/wallet/models/transaction.rb +21 -0
- data/lib/coinbase/wallet/models/transfer.rb +13 -0
- data/lib/coinbase/wallet/models/user.rb +15 -0
- data/lib/coinbase/wallet/version.rb +5 -0
- data/lib/coinbase/wallet.rb +24 -156
- data/spec/account_spec.rb +199 -0
- data/spec/callback_signature_verification_spec.rb +16 -0
- data/spec/clients/client_spec.rb +34 -0
- data/spec/clients/oauth_client_spec.rb +56 -0
- data/spec/endpoints_spec.rb +352 -0
- data/spec/error_spec.rb +137 -0
- data/spec/models/address_spec.rb +26 -0
- data/spec/models/api_object_spec.rb +70 -0
- data/spec/models/checkout_spec.rb +52 -0
- data/spec/models/current_user_spec.rb +29 -0
- data/spec/models/order_spec.rb +52 -0
- data/spec/models/request_spec.rb +47 -0
- data/spec/models/transfer_spec.rb +64 -0
- data/spec/models/user_spec.rb +24 -0
- data/spec/spec_helper.rb +13 -0
- metadata +67 -112
- data/lib/coinbase/address.rb +0 -127
- data/lib/coinbase/asset.rb +0 -20
- data/lib/coinbase/balance_map.rb +0 -48
- data/lib/coinbase/constants.rb +0 -38
- data/lib/coinbase/network.rb +0 -55
- data/lib/coinbase/transfer.rb +0 -153
- data/lib/coinbase.rb +0 -18
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Coinbase::Wallet::Order do
|
4
|
+
before :all do
|
5
|
+
@object_data = {
|
6
|
+
'id' => '0fdfb26e-bd26-5e1c-b055-7b935e57fa33',
|
7
|
+
'code' => '66BEOV2A',
|
8
|
+
'status' => 'paid',
|
9
|
+
'type' => 'order',
|
10
|
+
'name' => 'Order #123',
|
11
|
+
'description' => 'Sample order',
|
12
|
+
'amount' => {
|
13
|
+
'amount' => '10.00',
|
14
|
+
'currency' => 'USD'
|
15
|
+
},
|
16
|
+
'payout_amount' => nil,
|
17
|
+
'bitcoin_address' => 'mymZkiXhQNd6VWWG7VGSVdDX9bKmviti3U',
|
18
|
+
'bitcoin_amount' => {
|
19
|
+
'amount' => '1.00000000',
|
20
|
+
'currency' => 'BTC'
|
21
|
+
},
|
22
|
+
'bitcoin_uri' => 'bitcoin:mrNo5ntJfWP8BGjR2MkAxEgoE8NDu4CM3g?amount=1.00&r=https://www.coinbase.com/r/555b9570a54d75860e00041d',
|
23
|
+
'receipt_url' => 'https://www.coinbase.com/orders/d5d3e516dae19ca5b444fe56405ee917/receipt',
|
24
|
+
'expires_at' => '2015-01-31T20:49:02Z',
|
25
|
+
'mispaid_at' => nil,
|
26
|
+
'paid_at' => '2015-01-31T20:49:02Z',
|
27
|
+
'refund_address' => 'n3z9tkPHcMcUwGBbyjipT1RxJ3qXK4CKNQ',
|
28
|
+
'transaction' => {
|
29
|
+
'id' => 'aee1de26-9d08-56bf-8c51-7f8e6a23e046',
|
30
|
+
'resource' => 'transaction'
|
31
|
+
},
|
32
|
+
'refunds' => [],
|
33
|
+
'mispayments' => [],
|
34
|
+
'metadata' => {},
|
35
|
+
'created_at' => '2015-01-31T20:49:02Z',
|
36
|
+
'updated_at' => '2015-01-31T20:49:02Z',
|
37
|
+
'resource' => 'order',
|
38
|
+
'resource_path' => '/v2/orders/0fdfb26e-bd26-5e1c-b055-7b935e57fa33'
|
39
|
+
}
|
40
|
+
|
41
|
+
@client = Coinbase::Wallet::Client.new(api_key: 'api_key', api_secret: 'api_secret')
|
42
|
+
@object = Coinbase::Wallet::Order.new(@client, @object_data)
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#refund!' do
|
46
|
+
it 'should refund an order' do
|
47
|
+
stub_request(:post, 'https://api.coinbase.com' + @object_data['resource_path'] + '/refund')
|
48
|
+
.to_return(body: { data: mock_item }.to_json)
|
49
|
+
expect(@object.refund!).to eq mock_item
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Coinbase::Wallet::Request do
|
4
|
+
before :all do
|
5
|
+
@object_data = {
|
6
|
+
"id" => "2e9f48cd-0b05-5f7c-9056-17a8acb408ad",
|
7
|
+
"type" => "request",
|
8
|
+
"status" => "pending",
|
9
|
+
"amount" => {
|
10
|
+
"amount" => "1.00000000",
|
11
|
+
"currency" => "BTC"
|
12
|
+
},
|
13
|
+
"native_amount" => {
|
14
|
+
"amount" => "10.00",
|
15
|
+
"currency" => "USD"
|
16
|
+
},
|
17
|
+
"description" => nil,
|
18
|
+
"created_at" => "2015-04-01T10:37:11-07:00",
|
19
|
+
"updated_at" => "2015-04-01T10:37:11-07:00",
|
20
|
+
"resource" => "transaction",
|
21
|
+
"resource_path" => "/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/transactions/2e9f48cd-0b05-5f7c-9056-17a8acb408ad",
|
22
|
+
"to" => {
|
23
|
+
"resource" => "email",
|
24
|
+
"email" => "email@example.com"
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
@client = Coinbase::Wallet::Client.new(api_key: 'api_key', api_secret: 'api_secret')
|
29
|
+
@object = Coinbase::Wallet::Request.new(@client, @object_data)
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#resend!' do
|
33
|
+
it 'should resend an order' do
|
34
|
+
stub_request(:post, 'https://api.coinbase.com' + @object_data['resource_path'] + '/resend')
|
35
|
+
.to_return(body: { data: mock_item }.to_json)
|
36
|
+
expect(@object.resend!).to eq mock_item
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#cancel!' do
|
41
|
+
it 'should cancel an order' do
|
42
|
+
stub_request(:delete, 'https://api.coinbase.com' + @object_data['resource_path'])
|
43
|
+
.to_return(body: { data: mock_item }.to_json)
|
44
|
+
expect(@object.cancel!).to eq mock_item
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Coinbase::Wallet::Transfer do
|
4
|
+
before :all do
|
5
|
+
@object_data = {
|
6
|
+
'id' => '67e0eaec-07d7-54c4-a72c-2e92826897df',
|
7
|
+
'status' => 'pending',
|
8
|
+
'payment_method' => {
|
9
|
+
'id' => '83562370-3e5c-51db-87da-752af5ab9559',
|
10
|
+
'resource' => 'payment_method'
|
11
|
+
},
|
12
|
+
'transaction' => {
|
13
|
+
'id' => '441b9494-b3f0-5b98-b9b0-4d82c21c252a',
|
14
|
+
'resource' => 'transaction'
|
15
|
+
},
|
16
|
+
'amount' => {
|
17
|
+
'amount' => '1.00000000',
|
18
|
+
'currency' => 'BTC'
|
19
|
+
},
|
20
|
+
'total' => {
|
21
|
+
'amount' => '10.25',
|
22
|
+
'currency' => 'USD'
|
23
|
+
},
|
24
|
+
'subtotal' => {
|
25
|
+
'amount' => '10.10',
|
26
|
+
'currency' => 'USD'
|
27
|
+
},
|
28
|
+
'created_at' => '2015-01-31T20:49:02Z',
|
29
|
+
'updated_at' => '2015-01-31T20:49:02Z',
|
30
|
+
'resource' => 'buy',
|
31
|
+
'resource_path' => '/v2/accounts/2bbf394c-193b-5b2a-9155-3b4732659ede/buys/67e0eaec-07d7-54c4-a72c-2e92826897df',
|
32
|
+
'committed' => false,
|
33
|
+
'instant' => false,
|
34
|
+
'fees' => [
|
35
|
+
{
|
36
|
+
'type' => 'coinbase',
|
37
|
+
'amount' => {
|
38
|
+
'amount' => '0.00',
|
39
|
+
'currency' => 'USD'
|
40
|
+
}
|
41
|
+
},
|
42
|
+
{
|
43
|
+
'type' => 'bank',
|
44
|
+
'amount' => {
|
45
|
+
'amount' => '0.15',
|
46
|
+
'currency' => 'USD'
|
47
|
+
}
|
48
|
+
}
|
49
|
+
],
|
50
|
+
'payout_at' => '2015-02-18T16 =>54 =>00-08 =>00'
|
51
|
+
}
|
52
|
+
|
53
|
+
@client = Coinbase::Wallet::Client.new(api_key: 'api_key', api_secret: 'api_secret')
|
54
|
+
@object = Coinbase::Wallet::Transfer.new(@client, @object_data)
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '#commit!' do
|
58
|
+
it 'should commit an transfer (buy/sell/deposit/withdrawal)' do
|
59
|
+
stub_request(:post, 'https://api.coinbase.com' + @object_data['resource_path'] + '/commit')
|
60
|
+
.to_return(body: { data: mock_item }.to_json)
|
61
|
+
expect(@object.commit!).to eq mock_item
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Coinbase::Wallet::User do
|
4
|
+
before :all do
|
5
|
+
@user_data = {
|
6
|
+
'id' => '9da7a204-544e-5fd1-9a12-61176c5d4cd8',
|
7
|
+
'name' => 'User One',
|
8
|
+
'username' => 'user1',
|
9
|
+
'profile_location' => nil,
|
10
|
+
'profile_bio' => nil,
|
11
|
+
'profile_url' => 'https://coinbase.com/user1',
|
12
|
+
'avatar_url' => 'https://images.coinbase.com/avatar?h=vR%2FY8igBoPwuwGren5JMwvDNGpURAY%2F0nRIOgH%2FY2Qh%2BQ6nomR3qusA%2Bh6o2%0Af9rH&s=128',
|
13
|
+
'resource' => 'user',
|
14
|
+
'resource_path' => '/v2/user/9da7a204-544e-5fd1-9a12-61176c5d4cd8'
|
15
|
+
}
|
16
|
+
|
17
|
+
@client = Coinbase::Wallet::Client.new(api_key: 'api_key', api_secret: 'api_secret')
|
18
|
+
@user = Coinbase::Wallet::User.new(@client, @user_data)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should access attributes' do
|
22
|
+
expect(@user.id).to eq @user_data['id']
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,79 +1,24 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: coinbase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
8
|
-
|
7
|
+
- John Duhamel
|
8
|
+
- Jori Lallo
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
12
|
date: 2024-04-19 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: dotenv
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - '='
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: 2.8.1
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - '='
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: 2.8.1
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: eth
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - ">="
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '0'
|
48
|
-
type: :runtime
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - ">="
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: jimson
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: money-tree
|
15
|
+
name: rake
|
71
16
|
requirement: !ruby/object:Gem::Requirement
|
72
17
|
requirements:
|
73
18
|
- - ">="
|
74
19
|
- !ruby/object:Gem::Version
|
75
20
|
version: '0'
|
76
|
-
type: :
|
21
|
+
type: :development
|
77
22
|
prerelease: false
|
78
23
|
version_requirements: !ruby/object:Gem::Requirement
|
79
24
|
requirements:
|
@@ -81,13 +26,13 @@ dependencies:
|
|
81
26
|
- !ruby/object:Gem::Version
|
82
27
|
version: '0'
|
83
28
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
29
|
+
name: rspec
|
85
30
|
requirement: !ruby/object:Gem::Requirement
|
86
31
|
requirements:
|
87
32
|
- - ">="
|
88
33
|
- !ruby/object:Gem::Version
|
89
34
|
version: '0'
|
90
|
-
type: :
|
35
|
+
type: :development
|
91
36
|
prerelease: false
|
92
37
|
version_requirements: !ruby/object:Gem::Requirement
|
93
38
|
requirements:
|
@@ -95,7 +40,7 @@ dependencies:
|
|
95
40
|
- !ruby/object:Gem::Version
|
96
41
|
version: '0'
|
97
42
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
43
|
+
name: webmock
|
99
44
|
requirement: !ruby/object:Gem::Requirement
|
100
45
|
requirements:
|
101
46
|
- - ">="
|
@@ -109,7 +54,7 @@ dependencies:
|
|
109
54
|
- !ruby/object:Gem::Version
|
110
55
|
version: '0'
|
111
56
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
57
|
+
name: timecop
|
113
58
|
requirement: !ruby/object:Gem::Requirement
|
114
59
|
requirements:
|
115
60
|
- - ">="
|
@@ -123,7 +68,7 @@ dependencies:
|
|
123
68
|
- !ruby/object:Gem::Version
|
124
69
|
version: '0'
|
125
70
|
- !ruby/object:Gem::Dependency
|
126
|
-
name:
|
71
|
+
name: pry-byebug
|
127
72
|
requirement: !ruby/object:Gem::Requirement
|
128
73
|
requirements:
|
129
74
|
- - ">="
|
@@ -136,54 +81,64 @@ dependencies:
|
|
136
81
|
- - ">="
|
137
82
|
- !ruby/object:Gem::Version
|
138
83
|
version: '0'
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
version: 1.63.1
|
146
|
-
type: :development
|
147
|
-
prerelease: false
|
148
|
-
version_requirements: !ruby/object:Gem::Requirement
|
149
|
-
requirements:
|
150
|
-
- - '='
|
151
|
-
- !ruby/object:Gem::Version
|
152
|
-
version: 1.63.1
|
153
|
-
- !ruby/object:Gem::Dependency
|
154
|
-
name: yard
|
155
|
-
requirement: !ruby/object:Gem::Requirement
|
156
|
-
requirements:
|
157
|
-
- - '='
|
158
|
-
- !ruby/object:Gem::Version
|
159
|
-
version: 0.9.36
|
160
|
-
type: :development
|
161
|
-
prerelease: false
|
162
|
-
version_requirements: !ruby/object:Gem::Requirement
|
163
|
-
requirements:
|
164
|
-
- - '='
|
165
|
-
- !ruby/object:Gem::Version
|
166
|
-
version: 0.9.36
|
167
|
-
description: Coinbase Ruby SDK for accessing Coinbase Platform APIs
|
168
|
-
email: yuga.cohler@coinbase.com
|
169
|
-
executables: []
|
84
|
+
description: Client library for Coinbase Wallet API v2
|
85
|
+
email:
|
86
|
+
- api@coinbase.com
|
87
|
+
executables:
|
88
|
+
- console
|
89
|
+
- setup
|
170
90
|
extensions: []
|
171
91
|
extra_rdoc_files: []
|
172
92
|
files:
|
173
|
-
-
|
174
|
-
-
|
175
|
-
-
|
176
|
-
-
|
177
|
-
-
|
178
|
-
-
|
179
|
-
-
|
93
|
+
- ".gitignore"
|
94
|
+
- ".travis.yml"
|
95
|
+
- CONTRIBUTING.md
|
96
|
+
- Gemfile
|
97
|
+
- LICENSE
|
98
|
+
- README.md
|
99
|
+
- Rakefile
|
100
|
+
- bin/console
|
101
|
+
- bin/setup
|
102
|
+
- coinbase.gemspec
|
103
|
+
- lib/coinbase/util.rb
|
180
104
|
- lib/coinbase/wallet.rb
|
181
|
-
|
105
|
+
- lib/coinbase/wallet/adapters/em_http.rb
|
106
|
+
- lib/coinbase/wallet/adapters/net_http.rb
|
107
|
+
- lib/coinbase/wallet/api_client.rb
|
108
|
+
- lib/coinbase/wallet/api_errors.rb
|
109
|
+
- lib/coinbase/wallet/api_response.rb
|
110
|
+
- lib/coinbase/wallet/ca-coinbase.crt
|
111
|
+
- lib/coinbase/wallet/client.rb
|
112
|
+
- lib/coinbase/wallet/coinbase-callback.pub
|
113
|
+
- lib/coinbase/wallet/models/account.rb
|
114
|
+
- lib/coinbase/wallet/models/address.rb
|
115
|
+
- lib/coinbase/wallet/models/api_object.rb
|
116
|
+
- lib/coinbase/wallet/models/checkout.rb
|
117
|
+
- lib/coinbase/wallet/models/order.rb
|
118
|
+
- lib/coinbase/wallet/models/transaction.rb
|
119
|
+
- lib/coinbase/wallet/models/transfer.rb
|
120
|
+
- lib/coinbase/wallet/models/user.rb
|
121
|
+
- lib/coinbase/wallet/version.rb
|
122
|
+
- spec/account_spec.rb
|
123
|
+
- spec/callback_signature_verification_spec.rb
|
124
|
+
- spec/clients/client_spec.rb
|
125
|
+
- spec/clients/oauth_client_spec.rb
|
126
|
+
- spec/endpoints_spec.rb
|
127
|
+
- spec/error_spec.rb
|
128
|
+
- spec/models/address_spec.rb
|
129
|
+
- spec/models/api_object_spec.rb
|
130
|
+
- spec/models/checkout_spec.rb
|
131
|
+
- spec/models/current_user_spec.rb
|
132
|
+
- spec/models/order_spec.rb
|
133
|
+
- spec/models/request_spec.rb
|
134
|
+
- spec/models/transfer_spec.rb
|
135
|
+
- spec/models/user_spec.rb
|
136
|
+
- spec/spec_helper.rb
|
137
|
+
homepage: https://developers.coinbase.com/api/v2
|
182
138
|
licenses:
|
183
139
|
- Apache-2.0
|
184
|
-
metadata:
|
185
|
-
|
186
|
-
post_install_message:
|
140
|
+
metadata: {}
|
141
|
+
post_install_message:
|
187
142
|
rdoc_options: []
|
188
143
|
require_paths:
|
189
144
|
- lib
|
@@ -191,15 +146,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
191
146
|
requirements:
|
192
147
|
- - ">="
|
193
148
|
- !ruby/object:Gem::Version
|
194
|
-
version:
|
149
|
+
version: '0'
|
195
150
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
196
151
|
requirements:
|
197
152
|
- - ">="
|
198
153
|
- !ruby/object:Gem::Version
|
199
154
|
version: '0'
|
200
155
|
requirements: []
|
201
|
-
rubygems_version: 3.1.
|
202
|
-
signing_key:
|
156
|
+
rubygems_version: 3.1.2
|
157
|
+
signing_key:
|
203
158
|
specification_version: 4
|
204
|
-
summary: Coinbase
|
159
|
+
summary: Client library for Coinbase Wallet API v2
|
205
160
|
test_files: []
|
data/lib/coinbase/address.rb
DELETED
@@ -1,127 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'balance_map'
|
4
|
-
require_relative 'constants'
|
5
|
-
require 'bigdecimal'
|
6
|
-
require 'eth'
|
7
|
-
require 'jimson'
|
8
|
-
|
9
|
-
module Coinbase
|
10
|
-
# A representation of a blockchain Address, which is a user-controlled account on a Network. Addresses are used to
|
11
|
-
# send and receive Assets, and should be created using {link:Wallet#create_address}. Addresses require a
|
12
|
-
# {link:Eth::Key} to sign transaction data.
|
13
|
-
class Address
|
14
|
-
attr_reader :network_id, :address_id, :wallet_id
|
15
|
-
|
16
|
-
# Returns a new Address object.
|
17
|
-
# @param network_id [Symbol] The ID of the Network on which the Address exists
|
18
|
-
# @param address_id [String] The ID of the Address. On EVM Networks, for example, this is a hash of the public key.
|
19
|
-
# @param wallet_id [String] The ID of the Wallet to which the Address belongs
|
20
|
-
# @param key [Eth::Key] The key backing the Address
|
21
|
-
# @param client [Jimson::Client] (Optional) The JSON RPC client to use for interacting with the Network
|
22
|
-
def initialize(network_id, address_id, wallet_id, key,
|
23
|
-
client: Jimson::Client.new(ENV.fetch('BASE_SEPOLIA_RPC_URL', nil)))
|
24
|
-
# TODO: Don't require key.
|
25
|
-
@network_id = network_id
|
26
|
-
@address_id = address_id
|
27
|
-
@wallet_id = wallet_id
|
28
|
-
@key = key
|
29
|
-
@client = client
|
30
|
-
end
|
31
|
-
|
32
|
-
# Returns the balances of the Address. Currently only ETH balances are supported.
|
33
|
-
# @return [BalanceMap] The balances of the Address, keyed by asset ID. Ether balances are denominated
|
34
|
-
# in ETH.
|
35
|
-
def list_balances
|
36
|
-
# TODO: Handle multiple currencies.
|
37
|
-
eth_balance_in_wei = BigDecimal(@client.eth_getBalance(@address_id, 'latest').to_i(16).to_s)
|
38
|
-
eth_balance = BigDecimal(eth_balance_in_wei / BigDecimal(Coinbase::WEI_PER_ETHER.to_s))
|
39
|
-
|
40
|
-
BalanceMap.new({ eth: eth_balance })
|
41
|
-
end
|
42
|
-
|
43
|
-
# Returns the balance of the provided Asset. Currently only ETH is supported.
|
44
|
-
# @param asset_id [Symbol] The Asset to retrieve the balance for
|
45
|
-
# @return [BigDecimal] The balance of the Asset
|
46
|
-
def get_balance(asset_id)
|
47
|
-
normalized_asset_id = if %i[wei gwei].include?(asset_id)
|
48
|
-
:eth
|
49
|
-
else
|
50
|
-
asset_id
|
51
|
-
end
|
52
|
-
|
53
|
-
eth_balance = list_balances[normalized_asset_id] || BigDecimal(0)
|
54
|
-
|
55
|
-
case asset_id
|
56
|
-
when :eth
|
57
|
-
eth_balance
|
58
|
-
when :gwei
|
59
|
-
eth_balance * Coinbase::GWEI_PER_ETHER
|
60
|
-
when :wei
|
61
|
-
eth_balance * Coinbase::WEI_PER_ETHER
|
62
|
-
else
|
63
|
-
BigDecimal(0)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# Transfers the given amount of the given Asset to the given address. Only same-Network Transfers are supported.
|
68
|
-
# @param amount [Integer, Float, BigDecimal] The amount of the Asset to send.
|
69
|
-
# @param asset_id [Symbol] The ID of the Asset to send. For Ether, :eth, :gwei, and :wei are supported.
|
70
|
-
# @param destination [Wallet | Address | String] The destination of the transfer. If a Wallet, sends to the Wallet's
|
71
|
-
# default address. If a String, interprets it as the address ID.
|
72
|
-
# @return [String] The hash of the Transfer transaction.
|
73
|
-
def transfer(amount, asset_id, destination)
|
74
|
-
# TODO: Handle multiple currencies.
|
75
|
-
raise ArgumentError, "Unsupported asset: #{asset_id}" unless Coinbase::SUPPORTED_ASSET_IDS[asset_id]
|
76
|
-
|
77
|
-
if destination.is_a?(Wallet)
|
78
|
-
raise ArgumentError, 'Transfer must be on the same Network' if destination.network_id != @network_id
|
79
|
-
|
80
|
-
destination = destination.default_address.address_id
|
81
|
-
elsif destination.is_a?(Address)
|
82
|
-
raise ArgumentError, 'Transfer must be on the same Network' if destination.network_id != @network_id
|
83
|
-
|
84
|
-
destination = destination.address_id
|
85
|
-
end
|
86
|
-
|
87
|
-
current_balance = get_balance(asset_id)
|
88
|
-
if current_balance < amount
|
89
|
-
raise ArgumentError, "Insufficient funds: #{amount} requested, but only #{current_balance} available"
|
90
|
-
end
|
91
|
-
|
92
|
-
transfer = Coinbase::Transfer.new(@network_id, @wallet_id, @address_id, amount, asset_id, destination,
|
93
|
-
client: @client)
|
94
|
-
|
95
|
-
transaction = transfer.transaction
|
96
|
-
transaction.sign(@key)
|
97
|
-
@client.eth_sendRawTransaction("0x#{transaction.hex}")
|
98
|
-
|
99
|
-
transfer
|
100
|
-
end
|
101
|
-
|
102
|
-
# Returns the address as a string.
|
103
|
-
# @return [String] The address
|
104
|
-
def to_s
|
105
|
-
@address_id
|
106
|
-
end
|
107
|
-
|
108
|
-
private
|
109
|
-
|
110
|
-
# Normalizes the amount of ETH to send based on the asset ID.
|
111
|
-
# @param amount [Integer, Float, BigDecimal] The amount to normalize
|
112
|
-
# @param asset_id [Symbol] The ID of the Asset being transferred
|
113
|
-
# @return [BigDecimal] The normalized amount in units of ETH
|
114
|
-
def normalize_eth_amount(amount, asset_id)
|
115
|
-
case asset_id
|
116
|
-
when :eth
|
117
|
-
amount.is_a?(BigDecimal) ? amount : BigDecimal(amount.to_s)
|
118
|
-
when :gwei
|
119
|
-
BigDecimal(amount / Coinbase::GWEI_PER_ETHER)
|
120
|
-
when :wei
|
121
|
-
BigDecimal(amount / Coinbase::WEI_PER_ETHER)
|
122
|
-
else
|
123
|
-
raise ArgumentError, "Unsupported asset: #{asset_id}"
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
data/lib/coinbase/asset.rb
DELETED
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Coinbase
|
4
|
-
# A representation of an Asset.
|
5
|
-
class Asset
|
6
|
-
attr_reader :network_id, :asset_id, :display_name, :address_id
|
7
|
-
|
8
|
-
# Returns a new Asset object.
|
9
|
-
# @param network_id [Symbol] The ID of the Network to which the Asset belongs
|
10
|
-
# @param asset_id [Symbol] The Asset ID
|
11
|
-
# @param display_name [String] The Asset's display name
|
12
|
-
# @param address_id [String] (Optional) The Asset's address ID, if one exists
|
13
|
-
def initialize(network_id:, asset_id:, display_name:, address_id: nil)
|
14
|
-
@network_id = network_id
|
15
|
-
@asset_id = asset_id
|
16
|
-
@display_name = display_name
|
17
|
-
@address_id = address_id
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/lib/coinbase/balance_map.rb
DELETED
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'bigdecimal'
|
4
|
-
|
5
|
-
module Coinbase
|
6
|
-
# A convenience class for printing out crypto asset balances in a human-readable format.
|
7
|
-
class BalanceMap < Hash
|
8
|
-
# Returns a new BalanceMap object.
|
9
|
-
# @param hash [Map<Symbol, BigDecimal>] The hash to initialize with
|
10
|
-
def initialize(hash = {})
|
11
|
-
super()
|
12
|
-
hash.each do |key, value|
|
13
|
-
self[key] = value
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
# Returns a string representation of the balance map.
|
18
|
-
# @return [String] The string representation of the balance
|
19
|
-
def to_s
|
20
|
-
to_string
|
21
|
-
end
|
22
|
-
|
23
|
-
# Returns a string representation of the balance map.
|
24
|
-
# @return [String] The string representation of the balance
|
25
|
-
def inspect
|
26
|
-
to_string
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
# Returns a string representation of the balance.
|
32
|
-
# @return [String] The string representation of the balance
|
33
|
-
def to_string
|
34
|
-
result = {}
|
35
|
-
|
36
|
-
each do |asset_id, balance|
|
37
|
-
# Convert to floating-point number (not scientific notation)
|
38
|
-
str = balance.to_s('F')
|
39
|
-
|
40
|
-
str = balance.to_i.to_s if balance.frac.zero?
|
41
|
-
|
42
|
-
result[asset_id] = str
|
43
|
-
end
|
44
|
-
|
45
|
-
result
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|