lib/peatio/bitgo/wallet.rb in peatio-bitgo-1.1.0 vs lib/peatio/bitgo/wallet.rb in peatio-bitgo-1.1.1
- old
+ new
@@ -12,23 +12,25 @@
@settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
@wallet = @settings.fetch(:wallet) do
raise Peatio::Wallet::MissingSettingError, :wallet
- end.slice(:uri, :address, :secret, :bitgo_access_token, :bitgo_wallet_id, :bitgo_test_net)
+ end.slice(:uri, :address, :secret, :access_token, :wallet_id, :testnet)
@currency = @settings.fetch(:currency) do
raise Peatio::Wallet::MissingSettingError, :currency
end.slice(:id, :base_factor, :code, :options)
end
def create_address!(options = {})
- if options[:address_id].present?
- response = client.rest_api(:get, "/wallet/#{bitgo_wallet_id}/address/#{options[:address_id]}")
+ currency = erc20_currency_id
+ options.deep_symbolize_keys!
+ 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 }
else
- response = client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/address")
+ 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
end
@@ -47,11 +49,11 @@
baseFeeInfo = fee.dig('feeInfo','fee')
fee = baseFeeInfo.present? ? baseFeeInfo : fee.dig('txInfo','Fee')
amount -= fee.to_i
end
- txid = client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/sendcoins", {
+ txid = client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/sendcoins", {
address: transaction.to_address.to_s,
amount: amount.to_s,
walletPassphrase: bitgo_wallet_passphrase
}.compact).fetch('txid')
@@ -62,23 +64,22 @@
raise Peatio::Wallet::ClientError, e
end
def build_raw_transaction(transaction)
- client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/tx/build", {
+ client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/tx/build", {
recipients: [{
address: transaction.to_address,
amount: convert_to_base_unit(transaction.amount).to_s
}]
}.compact)
end
def create_eth_transaction(transaction, options = {})
amount = convert_to_base_unit(transaction.amount)
- amount -= options.fetch(:gas_limit).to_i * options.fetch(:gas_price).to_i if options.dig(:subtract_fee)
- txid = client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/sendcoins", {
+ txid = client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/sendcoins", {
address: transaction.to_address.to_s,
amount: amount.to_s,
walletPassphrase: bitgo_wallet_passphrase,
gas: options.fetch(:gas_limit).to_i,
gasPrice: options.fetch(:gas_price).to_i
@@ -87,19 +88,30 @@
transaction.hash = normalize_txid(txid)
transaction
end
def load_balance!
- response = client.rest_api(:get, "/wallet/#{bitgo_wallet_id}")
- convert_from_base_unit(response.fetch('balanceString'))
+ if @currency.fetch(:options).slice(:erc20_contract_address).present?
+ load_erc20_balance!
+ else
+ response = client.rest_api(:get, "#{currency_id}/wallet/#{wallet_id}")
+ convert_from_base_unit(response.fetch('balanceString'))
+ end
rescue Bitgo::Client::Error => e
raise Peatio::Wallet::ClientError, e
end
+ def load_erc20_balance!
+ response = client.rest_api(:get, "#{erc20_currency_id}/wallet/#{wallet_id}?allTokens=true")
+ convert_from_base_unit(response.dig('tokens', currency_id, 'balanceString'))
+ rescue Bitgo::Client::Error => e
+ raise Peatio::Wallet::ClientError, e
+ end
+
def trigger_webhook_event(event)
- currency_id = @wallet.fetch(:bitgo_test_net).present? ? 't' + @currency.fetch(:id) : @currency.fetch(:id)
- return unless currency_id == event['coin'] && @wallet.fetch(:bitgo_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['address_confirmation']
@@ -112,11 +124,11 @@
address_confirmation_webhook(url)
end
def fetch_transfer!(id)
# TODO: Add Rspecs for this one
- response = client.rest_api(:get, "/wallet/#{bitgo_wallet_id}/transfer/#{id}")
+ 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?
build_address(response.dig('coinSpecific', 'memo').first)
else
entry['address']
@@ -143,21 +155,21 @@
rescue Bitgo::Client::Error => e
raise Peatio::Wallet::ClientError, e
end
def transfer_webhook(url)
- client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/webhooks", {
+ client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/webhooks", {
type: 'transfer',
allToken: true,
url: url,
label: "webhook for #{url}",
listenToFailureStates: false
})
end
def address_confirmation_webhook(url)
- client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/webhooks", {
+ client.rest_api(:post, "#{currency_id}/wallet/#{wallet_id}/webhooks", {
type: 'address_confirmation_webhook',
allToken: true,
url: url,
label: "webhook for #{url}",
listenToFailureStates: false
@@ -171,28 +183,39 @@
end
private
def client
- currency_id = @currency.fetch(:id) { raise Peatio::Wallet::MissingSettingError, :id }
uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
- access_token = @wallet.fetch(:bitgo_access_token) { raise Peatio::Wallet::MissingSettingError, :bitgo_access_token }
+ access_token = @wallet.fetch(:access_token) { raise Peatio::Wallet::MissingSettingError, :access_token }
- currency_code_prefix = @wallet.fetch(:bitgo_test_net) ? 't' : ''
- uri = uri.gsub(/\/+\z/, '') + '/' + currency_code_prefix + currency_id
+ currency_code_prefix = @wallet.fetch(:testnet) ? 't' : ''
+ uri = uri.gsub(/\/+\z/, '') + '/' + currency_code_prefix
@client ||= Client.new(uri, access_token)
end
def build_address(memo)
"#{memo['address']}?memoId=#{memo['value']}"
end
+ # All these functions will have to be done with the coin set to eth or teth
+ # since that is the actual coin type being used.
+ def erc20_currency_id
+ return 'eth' if @currency.fetch(:options).slice(:erc20_contract_address).present?
+
+ currency_id
+ end
+
+ def currency_id
+ @currency.fetch(:id) { raise Peatio::Wallet::MissingSettingError, :id }
+ end
+
def bitgo_wallet_passphrase
@wallet.fetch(:secret)
end
- def bitgo_wallet_id
- @wallet.fetch(:bitgo_wallet_id)
+ def wallet_id
+ @wallet.fetch(:wallet_id)
end
def normalize_txid(txid)
txid.downcase
end