class Peatio::Eos::Wallet
Constants
- ADDRESS_LENGTH
- PRECISION
- TOKEN_STANDARD
Public Class Methods
new(settings={})
click to toggle source
# File lib/ultex/eos/wallet.rb, line 14 def initialize(settings={}) @settings = settings end
Public Instance Methods
configure(settings={})
click to toggle source
# File lib/ultex/eos/wallet.rb, line 18 def configure(settings={}) @settings.merge!(settings.slice(*SUPPORTED_SETTINGS)) @wallet = @settings.fetch(:wallet) do raise Peatio::Wallet::MissingSettingError, :wallet end.slice(:uri, :address, :secret) @currency = @settings.fetch(:currency) do raise Peatio::Wallet::MissingSettingError, :wallet end.slice(:id, :base_factor, :options) raise MissingTokenNameError if @currency.dig(:options, :eos_token_name).blank? end
create_address!(options={})
click to toggle source
# File lib/ultex/eos/wallet.rb, line 31 def create_address!(options={}) # For EOS and all others eosio.token deposits we use one EOS account which is defined like deposit wallet address in peatio. # In Peatio EOS plugin we will define owner of deposit by user unige identifier (UID) name = "#{@wallet.fetch(:address)}?memo=#{options.fetch(:uid)}" {address: name, secret: @wallet.fetch(:secret)} rescue Peatio::Eos::Client::Error => e raise Peatio::Wallet::ClientError, e end
create_transaction!(transaction, _options={})
click to toggle source
# File lib/ultex/eos/wallet.rb, line 40 def create_transaction!(transaction, _options={}) tx = transaction amount = normalize_amount(tx.amount) address = normalize_address(@wallet.fetch(:address)) # Pack main transaction info into hash packed_data = client.json_rpc("/v1/chain/abi_json_to_bin", Peatio::Eos::TransactionSerializer.to_pack_json(address: address, to_address: tx.to_address, amount: amount)).fetch("binargs") info = client.json_rpc("/v1/chain/get_info") # Get block info block = client.json_rpc("/v1/chain/get_block", "block_num_or_id" => info.fetch("last_irreversible_block_num")) ref_block_num = info.fetch("last_irreversible_block_num") & 0xFFFF # Get transaction expiration expiration = normalize_expiration(block.fetch("timestamp")) # Sign transaction before push signed = client.json_rpc("/v1/wallet/sign_transaction", Peatio::Eos::TransactionSerializer.to_sign_json(ref_block_num: ref_block_num, block_prefix: block.fetch("ref_block_prefix"), expiration: expiration, address: address, packed_data: packed_data, secret: @wallet.fetch(:secret), chain_id: info.fetch("chain_id")), 8900) txid = client.json_rpc("/v1/chain/push_transaction", Peatio::Eos::TransactionSerializer.to_push_json(address: address, packed_data: packed_data, expiration: signed.fetch("expiration"), block_num: signed.fetch("ref_block_num"), block_prefix: signed.fetch("ref_block_prefix"), signature: signed.fetch("signatures"))).fetch("transaction_id") tx.hash = txid tx rescue Peatio::Eos::Client::Error => e raise Peatio::Wallet::ClientError, e end
load_balance!()
click to toggle source
# File lib/ultex/eos/wallet.rb, line 66 def load_balance! balance = client.json_rpc("/v1/chain/get_currency_balance", "account" => @wallet.fetch(:address), "code" => TOKEN_STANDARD) .find {|b| b.split[1] == @currency.dig(:options, :eos_token_name) } balance.blank? ? 0 : normalize_balance(balance) rescue Peatio::Eos::Client::Error => e raise Peatio::Wallet::ClientError, e end
Private Instance Methods
client()
click to toggle source
# File lib/ultex/eos/wallet.rb, line 93 def client uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri } @client ||= Client.new(uri) end
normalize_address(address)
click to toggle source
# File lib/ultex/eos/wallet.rb, line 77 def normalize_address(address) address&.split('?memo=').first end
normalize_amount(amount)
click to toggle source
# File lib/ultex/eos/wallet.rb, line 81 def normalize_amount(amount) "%.#{PRECISION}f" % amount + " #{@currency.dig(:options, :eos_token_name)}" end
normalize_balance(balance)
click to toggle source
# File lib/ultex/eos/wallet.rb, line 85 def normalize_balance(balance) balance.chomp(@currency.dig(:options, :eos_token_name)).to_d end
normalize_expiration(time)
click to toggle source
# File lib/ultex/eos/wallet.rb, line 89 def normalize_expiration(time) (Time.parse(time) + 3600).iso8601(6).split("+").first end