Skip to content

Commit

Permalink
Add web3 service
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Balk committed Apr 13, 2019
1 parent 7b424ac commit 90fcd1e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion rails-api/.ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.4
2.4.4
3 changes: 3 additions & 0 deletions rails-api/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ gem 'bootsnap', '>= 1.1.0', require: false
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'

gem 'eth'
gem 'ethereum.rb'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
22 changes: 21 additions & 1 deletion rails-api/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ GEM
devise-jwt (0.5.8)
devise (~> 4.0)
warden-jwt_auth (~> 0.3.5)
digest-sha3 (1.1.0)
dotenv (2.5.0)
dry-auto_inject (0.6.0)
dry-container (>= 0.3.4)
Expand All @@ -68,7 +69,19 @@ GEM
concurrent-ruby (~> 1.0)
dry-configurable (~> 0.1, >= 0.1.3)
erubi (1.7.1)
eth (0.4.10)
digest-sha3 (~> 1.1)
ffi (~> 1.0)
money-tree (~> 0.10.0)
rlp (~> 0.7.3)
scrypt (~> 3.0.6)
ethereum.rb (2.2)
activesupport (>= 4.0)
digest-sha3 (~> 1.1)
ffi (1.9.25)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
rake
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.1.1)
Expand All @@ -93,6 +106,8 @@ GEM
mini_mime (1.0.1)
mini_portile2 (2.3.0)
minitest (5.11.3)
money-tree (0.10.0)
ffi
msgpack (1.2.4)
multi_json (1.13.1)
nio4r (2.3.1)
Expand Down Expand Up @@ -136,7 +151,10 @@ GEM
responders (2.4.0)
actionpack (>= 4.2.0, < 5.3)
railties (>= 4.2.0, < 5.3)
rlp (0.7.3)
ruby_dep (1.5.0)
scrypt (3.0.6)
ffi-compiler (>= 1.0, < 2.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand Down Expand Up @@ -168,6 +186,8 @@ DEPENDENCIES
devise
devise-jwt
dotenv
eth
ethereum.rb
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
Expand All @@ -180,4 +200,4 @@ RUBY VERSION
ruby 2.4.4p296

BUNDLED WITH
1.16.6
2.0.1
31 changes: 31 additions & 0 deletions rails-api/app/services/web3_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class Web3Service
def initialize
contract_address = "0x539bc5Ef3907DC37eFE798926A7D52d75C8FA512"
dummy_private_key = "8695fe6ca35eeb46a6e920093e97ed374aaab45b92e7103cd6b65d1975c018d7"

client = Ethereum::HttpClient.new('http://localhost:8545')

@contract = Ethereum::Contract.create(
client: client,
name: "Certificates",
truffle: { paths: [ "#{Rails.root}/../energy" ]},
address: contract_address
)

# key = Eth::Key.new
# p key.private_hex
# p key.public_hex
# p key.address
# @contract.key = key

@contract.key = Eth::Key.new(priv: dummy_private_key)
end

def push_certificate(id, amount, timestamp, certificate_type)
@contract.transact.push_certificate(id, amount, timestamp, certificate_type)
end

def certificates(id)
@contract.call.certificates(id)
end
end

0 comments on commit 90fcd1e

Please sign in to comment.