Peatio Plugin API v2 gives ability to extend Peatio with any coin which fits into basic Blockchain and Wallet interfaces described inside peatio-core gem.
You need to be familiar with Blockchain and Wallet interfaces.
Note: you can skip optional methods if they are not supported by your coin.
First of all need to start your coin node locally or inside VM and try to access it via HTTP e.g. using curl
or http
.
You need to study your coin API to get list of calls for implementing Blockchain and
Wallet interfaces.
Note: single method may require multiple API calls.
We next list of JSON RPC methods for Bitcoin integration:
For Ethereum Blockchain (ETH, ERC20) we use next list of methods:
During this step you will create your own ruby gem for implementing your coin Blockchain and Wallet classes.
We will use peatio-litecoin as example. My advice is to clone it and use as plugin development guide.
For more currencies examples check Bitcoin and Ethereum implementation.
bundle gem peatio-litecoin
Note: there is no requirements for gem naming and module hierarchy.
I use the next list of gems (you could specify preferred by you inside you gem):
spec.add_dependency "activesupport", "~> 5.2.3"
spec.add_dependency "better-faraday", "~> 1.0.5"
spec.add_dependency "faraday", "~> 0.15.4"
spec.add_dependency "memoist", "~> 0.16.0"
spec.add_dependency "peatio", "~> 0.6.1" # Required.
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "mocha", "~> 1.8"
spec.add_development_dependency "pry-byebug"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 3.5"
Note: peatio gem is required.
bundle install
You could start from saving few responses and then extend your mock factory. Peatio-litecoin spec/resources directory has the following structure:
tree spec/resources
spec/resources
โโโ getbalance
โย ย โโโ response.json
โโโ getblock
โย ย โโโ 40500.json
โโโ getblockcount
โย ย โโโ 40500.json
โโโ getblockhash
โย ย โโโ 40500.json
โโโ getnewaddress
โย ย โโโ response.json
โโโ listaddressgroupings
โย ย โโโ response.json
โโโ methodnotfound
โย ย โโโ error.json
โโโ sendtoaddress
โโโ response.json
You could organize files and directories as you wish. Peatio-litecoin has the following lib and spec structure:
tree lib
lib
โโโ peatio
โโโ litecoin
โย ย โโโ blockchain.rb
โย ย โโโ client.rb
โย ย โโโ hooks.rb
โย ย โโโ railtie.rb
โย ย โโโ version.rb
โย ย โโโ wallet.rb
โโโ litecoin.rb
tree spec/peatio
spec/peatio
โโโ litecoin
โย ย โโโ blockchain_spec.rb
โย ย โโโ client_spec.rb
โย ย โโโ wallet_spec.rb
โโโ litecoin_spec.rb
First of all try to find reliable ruby client for your coin and implement own if there is no such. We don't provide client interface so you could construct client in the way it's convenient for you but note that it's your gem base because you will use it widely during Blockchain and Wallet implementation.
client = Peatio::Litecoin::Client.new('http://user:[email protected]:19332') # => #<Peatio::Litecoin::Client:0x00007fca61d82650 @json_rpc_endpoint=#<URI::HTTP http://user:[email protected]:19332>>
client.json_rpc(:getblockcount) # => 1087729
client.json_rpc(:getnewaddress) # => "QQPyC9uTQ1YKu3V1Dr4rNqHkHgJG3qr8JC"
E.g. specs for peatio-litecoin client:
bundle exec rspec spec/peatio/litecoin/client_spec.rb
Peatio::Litecoin::Client
initialize
should not raise Exception
json_rpc
getblockcount
should not raise Exception
should eq 40500
methodnotfound
should raise Peatio::Litecoin::Client::ResponseError with "Method not found (-32601)"
notfound
should raise Peatio::Litecoin::Client::Error
connectionerror
should raise Peatio::Litecoin::Client::ConnectionError
Finished in 0.01355 seconds (files took 1.11 seconds to load)
6 examples, 0 failures
module Peatio
module Litecoin
class Blockchain < Peatio::Blockchain::Abstract
# Your custom logic goes here.
end
end
end
I suggest using the next order of methods implementation:
* initialize
* configure
* latest_block_number
* fetch_block!
* load_balance_of_address! (optional)
E.g. specs for peatio-litecoin blockchain:
Peatio::Litecoin::Blockchain
features
defaults
override defaults
custom feautures
configure
default settings
currencies and server configuration
latest_block_number
returns latest block number
raises error if there is error in response body
build_transaction
three vout tx
builds formatted transactions for passed transaction
multiple currencies
builds formatted transactions for passed transaction per each currency
single vout transaction
builds formatted transactions for each vout
fetch_block!
builds expected number of transactions
all transactions are valid
load_balance_of_address!
address with balance is defined
requests rpc listaddressgroupings and finds address balance
requests rpc listaddressgroupings and finds address with zero balance
address is not defined
requests rpc listaddressgroupings and do not find address
client error is raised
raise wrapped client error
Finished in 0.02604 seconds (files took 1.14 seconds to load)
16 examples, 0 failures
module Peatio
module Litecoin
class Wallet < Peatio::Blockchain::Abstract
# Your custom logic goes here.
end
end
end
I suggest using the next order of methods implementation:
* initialize
* configure
* create_address!
* create_transaction!
* load_balance! (optional)
E.g. specs for peatio-litecoin wallet:
Peatio::Litecoin::Wallet
configure
requires wallet
requires currency
sets settings attribute
create_address!
request rpc and creates new address
create_transaction!
requests rpc and sends transaction without subtract fees
load_balance!
requests rpc with getbalance call
Finished in 0.01205 seconds (files took 1.08 seconds to load)
6 examples, 0 failures
Peatio::Blockchain.registry[:litecoin] = Litecoin::Blockchain.new
Peatio::Wallet.registry[:litecoind] = Litecoin::Wallet.new
For more info check hooks.rb and railtie.rb.
Note: You could just copy paste this files and change wallet and blockchain names.
Every story which touch blockchain or wallet should work successfully:
* deposit address generation
* deposit detection
* blockchain synchronization
* deposit collection
* withdraw creation
* withdraw confirmation
Documentation folder for Litecoin has the following structure:
docs
โโโ integration.md
โโโ json-rpc.md
โโโ testnet.md
integration.md
Describe full plugin integration flow in integration.md. Image Build and Peatio Configuration sections are required.
Don't forget to describe custom steps here e.g.
"Send some XRP for wallet activation" or "For ERC20 integration fee wallet with ETH is required".
json-rpc.md
List all API calls used for gem development here with examples and description.
testnet.md
Give instructions how to get coins in testent.
Note: it's minimalistic doc structure. More doc is more love for your plugin.
For doing it left comment with your plugin link and short description here.