Class: Peatio::Eos::Wallet
- Inherits:
-
Wallet::Abstract
- Object
- Wallet::Abstract
- Peatio::Eos::Wallet
- Defined in:
- lib/ultex/eos/wallet.rb
Defined Under Namespace
Classes: MissingTokenNameError
Constant Summary collapse
- PRECISION =
4
- ADDRESS_LENGTH =
12
- TOKEN_STANDARD =
"eosio.token"
Instance Method Summary collapse
- #configure(settings = {}) ⇒ Object
- #create_address!(options = {}) ⇒ Object
- #create_transaction!(transaction, _options = {}) ⇒ Object
-
#initialize(settings = {}) ⇒ Wallet
constructor
A new instance of Wallet.
- #load_balance! ⇒ Object
Constructor Details
#initialize(settings = {}) ⇒ Wallet
Returns a new instance of Wallet.
14 15 16 |
# File 'lib/ultex/eos/wallet.rb', line 14 def initialize(settings={}) @settings = settings end |
Instance Method Details
#configure(settings = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 |
# 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 = {}) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/ultex/eos/wallet.rb', line 31 def create_address!(={}) # 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=#{.fetch(:uid)}" {address: name, secret: @wallet.fetch(:secret)} rescue Peatio::Eos::Client::Error => e raise Peatio::Wallet::ClientError, e end |
#create_transaction!(transaction, _options = {}) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ultex/eos/wallet.rb', line 40 def create_transaction!(transaction, ={}) 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! ⇒ Object
66 67 68 69 70 71 72 73 |
# 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 |