lib/peatio/bitgo/wallet.rb in peatio-bitgo-2.6.0 vs lib/peatio/bitgo/wallet.rb in peatio-bitgo-2.6.1

- old
+ new

@@ -1,8 +1,9 @@ module Peatio module Bitgo class Wallet < Peatio::Wallet::Abstract + TIME_DIFFERENCE_IN_MINUTES = 10 def initialize(settings = {}) @settings = settings end @@ -22,14 +23,18 @@ end def create_address!(options = {}) currency = erc20_currency_id options.deep_symbolize_keys! - if options.dig(:pa_details,:address_id).present? + + if options.dig(:pa_details, :address_id).present? && + options.dig(:pa_details, :updated_at).present? && + time_difference_in_minutes(options.dig(:pa_details, :updated_at)) >= TIME_DIFFERENCE_IN_MINUTES + response = client.rest_api(:get, "#{currency}/wallet/#{wallet_id}/address/#{options.dig(:pa_details, :address_id)}") { address: response['address'], secret: bitgo_wallet_passphrase } - else + elsif options.dig(:pa_details, :address_id).blank? response = client.rest_api(:post, "#{currency}/wallet/#{wallet_id}/address") { address: response['address'], secret: bitgo_wallet_passphrase, details: { address_id: response['id'] }} end rescue Bitgo::Client::Error => e raise Peatio::Wallet::ClientError, e @@ -109,26 +114,35 @@ rescue Bitgo::Client::Error => e raise Peatio::Wallet::ClientError, e end def trigger_webhook_event(event) - currency_id = @wallet.fetch(:testnet).present? ? 't' + @currency.fetch(:id) : @currency.fetch(:id) - return unless currency_id == event['coin'] && @wallet.fetch(:wallet_id) == event['wallet'] + currency = @wallet.fetch(:testnet).present? ? 't' + @currency.fetch(:id) : @currency.fetch(:id) + return unless currency == event['coin'] && @wallet.fetch(:wallet_id) == event['wallet'] if event['type'] == 'transfer' transactions = fetch_transfer!(event['transfer']) return { transfers: transactions } - elsif event['address_confirmation'] - # TODO Add Address confirmation + elsif event['type'] == 'address_confirmation' + address_id = fetch_address_id(event['address']) + return { address_id: address_id, currency_id: currency_id } end end def register_webhooks!(url) transfer_webhook(url) address_confirmation_webhook(url) end + def fetch_address_id(address) + currency = erc20_currency_id + client.rest_api(:get, "#{currency}/wallet/#{wallet_id}/address/#{address}") + .fetch('id') + rescue Bitgo::Client::Error => e + raise Peatio::Wallet::ClientError, e + end + def fetch_transfer!(id) # TODO: Add Rspecs for this one response = client.rest_api(:get, "#{currency_id}/wallet/#{wallet_id}/transfer/#{id}") parse_entries(response['entries']).map do |entry| to_address = if response.dig('coinSpecific', 'memo').present? @@ -167,11 +181,11 @@ }) end def address_confirmation_webhook(url) client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/webhooks", { - type: 'address_confirmation_webhook', + type: 'address_confirmation', allToken: true, url: url, label: "webhook for #{url}", listenToFailureStates: false }) @@ -250,12 +264,16 @@ "#{value.to_d} - #{x.to_d} must be equal to zero." end x.to_i end + def time_difference_in_minutes(updated_at) + (Time.now - updated_at)/60 + end + def define_transaction_state(state) case state - when 'unconfrimed' + when 'unconfirmed' 'pending' when 'confirmed' 'success' when 'failed','rejected' 'failed'