lib/peatio/bitgo/wallet.rb in peatio-bitgo-2.5.2 vs lib/peatio/bitgo/wallet.rb in peatio-bitgo-2.6.0
- old
+ new
@@ -1,10 +1,8 @@
module Peatio
module Bitgo
class Wallet < Peatio::Wallet::Abstract
- TIME_DIFFERENCE_IN_MINUTES = 10
- XLM_MEMO_TYPES = { 'memoId': 'id', 'memoText': 'text', 'memoHash': 'hash', 'memoReturn': 'return' }
def initialize(settings = {})
@settings = settings
end
@@ -24,18 +22,14 @@
end
def create_address!(options = {})
currency = erc20_currency_id
options.deep_symbolize_keys!
-
- 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
-
+ if options.dig(:pa_details,:address_id).present?
response = client.rest_api(:get, "#{currency}/wallet/#{wallet_id}/address/#{options.dig(:pa_details, :address_id)}")
{ address: response['address'], secret: bitgo_wallet_passphrase }
- elsif options.dig(:pa_details, :address_id).blank?
+ else
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
@@ -56,11 +50,11 @@
fee = baseFeeInfo.present? ? baseFeeInfo : fee.dig('txInfo','Fee')
amount -= fee.to_i
end
txid = client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/sendcoins", {
- address: normalize_address(transaction.to_address.to_s),
+ address: transaction.to_address.to_s,
amount: amount.to_s,
walletPassphrase: bitgo_wallet_passphrase,
memo: xlm_memo(transaction.to_address.to_s)
}.compact).fetch('txid')
@@ -115,35 +109,26 @@
rescue Bitgo::Client::Error => e
raise Peatio::Wallet::ClientError, e
end
def trigger_webhook_event(event)
- currency = @wallet.fetch(:testnet).present? ? 't' + @currency.fetch(:id) : @currency.fetch(:id)
- return unless currency == event['coin'] && @wallet.fetch(:wallet_id) == event['wallet']
+ 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']
if event['type'] == 'transfer'
transactions = fetch_transfer!(event['transfer'])
return { transfers: transactions }
- elsif event['type'] == 'address_confirmation'
- address_id = fetch_address_id(event['address'])
- return { address_id: address_id, currency_id: currency_id }
+ elsif event['address_confirmation']
+ # TODO Add Address confirmation
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?
@@ -159,11 +144,12 @@
currency_id: @currency.fetch(:id),
amount: convert_from_base_unit(entry['valueString']),
hash: normalize_txid(response['txid']),
to_address: to_address,
block_number: response['height'],
- txout: response['index'].to_i,
+ # TODO: Add sendmany support
+ txout: 0,
status: state
)
transaction if transaction.valid?
end.compact
@@ -181,11 +167,11 @@
})
end
def address_confirmation_webhook(url)
client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/webhooks", {
- type: 'address_confirmation',
+ type: 'address_confirmation_webhook',
allToken: true,
url: url,
label: "webhook for #{url}",
listenToFailureStates: false
})
@@ -219,29 +205,23 @@
currency_id
end
def xlm_memo(address)
- build_xlm_memo(address) if @currency.fetch(:id) == 'xlm'
- end
-
- def build_xlm_memo(address)
- case address.split('?').last.split('=').first
- when 'memoId'
- memo_value_from(address, 'memoId')
- when 'memoText'
- memo_value_from(address, 'memoText')
- when 'memoHash'
- memo_value_from(address, 'memoHash')
- when 'memoReturn'
- memo_value_from(address, 'memoReturn')
+ if @currency.fetch(:id) == 'xlm'
+ {
+ type: "id",
+ value: "#{memo_id_from(address)}"
+ }
end
end
- def memo_value_from(address, type)
- memo_value = address.partition(type + '=').last
- return { type: XLM_MEMO_TYPES[type.to_sym], value: memo_value } if memo_value.present?
+ def memo_id_from(address)
+ memo_id = address.partition('memoId=').last
+ memo_id = 0 if memo_id.empty?
+
+ memo_id
end
def currency_id
@currency.fetch(:id) { raise Peatio::Wallet::MissingSettingError, :id }
end
@@ -252,18 +232,10 @@
def wallet_id
@wallet.fetch(:wallet_id)
end
- def normalize_address(address)
- if @currency.fetch(:id) == 'xlm'
- address.split('?').first
- else
- address
- end
- end
-
def normalize_txid(txid)
txid.downcase
end
def convert_from_base_unit(value)
@@ -278,16 +250,12 @@
"#{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 'unconfirmed'
+ when 'unconfrimed'
'pending'
when 'confirmed'
'success'
when 'failed','rejected'
'failed'