Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ RUN groupadd -r --gid ${GID} app \
RUN apt-get update && apt-get upgrade -y
RUN apt-get install default-libmysqlclient-dev -y

RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
apt-get install -y nodejs && \
npm install -g ethers

# Install Kaigara
RUN curl -Lo /usr/bin/kaigara https://github.com/openware/kaigara/releases/download/${KAIGARA_VERSION}/kaigara \
&& chmod +x /usr/bin/kaigara
Expand Down
37 changes: 37 additions & 0 deletions app/services/ethereum_account_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'net/http'
require 'json'
require 'uri'

class EthereumAccountService
CLEF_ENDPOINT = ENV.fetch('CLEF_RPC_URL', ENV['CLEF_RPC_ADDR'])

def self.create_address
uri = URI.parse(CLEF_ENDPOINT)
Rails.logger.warn "uri : #{uri}"
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request.body = {
id: 0,
jsonrpc: '2.0',
method: 'account_new',
params: []
}.to_json

response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end

parsed = JSON.parse(response.body)
if parsed['result']
Rails.logger.info { "[Clef] Created new ETH address: #{parsed['result']}" }
parsed['result']
else
Rails.logger.error { "[Clef] Failed to create account: #{parsed['error'] || response.body}" }
nil
end
rescue => e
Rails.logger.error { "[Clef] Exception while creating account: #{e.message}" }
Rails.logger.error { "[Clef] uri.hostname: #{uri.hostname}, uri.port: #{uri.port}" }
nil
end
end
17 changes: 12 additions & 5 deletions db/migrate/20200907133518_create_currencies_wallets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@ class CreateCurrenciesWallets < ActiveRecord::Migration[5.2]
def change
reversible do |dir|
dir.up do
create_join_table :currencies, :wallets do |t|
t.string :currency_id, index: true
t.integer :wallet_id, index: true
unless table_exists?(:currencies_wallets)
create_join_table :currencies, :wallets do |t|
# create_join_table automatically creates currency_id and wallet_id columns
# so we don't need to define them explicitly
end
end

# Add indexes if they don't exist
unless index_exists?(:currencies_wallets, [:currency_id, :wallet_id])
add_index :currencies_wallets, %i[currency_id wallet_id], unique: true
end
add_index :currencies_wallets, %i[currency_id wallet_id], unique: true

# Add links for the existing wallets and currencies
# Make sure to join wallets with currencies after migration via admin API
Wallet.find_each do |w|
CurrencyWallet.create(wallet_id: w.id, currency_id: w.currency_id)
CurrencyWallet.create(wallet_id: w.id, currency_id: w.currency_id) if w.currency_id.present?
end

add_reference :payment_addresses, :member, index: true, after: :id
Expand Down
18 changes: 18 additions & 0 deletions lib/js/create_wallet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require("fs");
const ethers = require("ethers");

async function createWallet(password) {
const wallet = ethers.Wallet.createRandom();
const keystore = await wallet.encrypt(password);

const output = {
address: wallet.address,
privateKey: wallet.privateKey,
keystore: keystore
};

console.log(JSON.stringify(output));
}

const password = process.argv[2] || "default-password";
createWallet(password);
1 change: 1 addition & 0 deletions lib/peatio/ethereum/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def initialize(endpoint, idle_timeout: 5)
@json_rpc_endpoint = URI.parse(endpoint)
@json_rpc_call_id = 0
@path = @json_rpc_endpoint.path.empty? ? "/" : @json_rpc_endpoint.path
Rails.logger.warn "path: #{@path}"
@idle_timeout = idle_timeout
end

Expand Down
25 changes: 15 additions & 10 deletions lib/peatio/ethereum/wallet_abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ def configure(settings = {})
end

def create_address!(options = {})
secret = options.fetch(:secret) { PasswordGenerator.generate(64) }
secret.yield_self do |password|
{ address: normalize_address(client.json_rpc(:personal_newAccount, [password])),
secret: password }
end
address = EthereumAccountService.create_address
raise "Failed to create ETH address from Clef" unless address

{
address: address,
secret: nil
}
rescue Ethereum::Client::Error => e
raise Peatio::Wallet::ClientError, e
end

def create_transaction!(transaction, options = {})
Rails.logger.warn "trans"
Rails.logger.warn "trans 1: #{@currency.dig(:options, contract_address_option).present?}"
Rails.logger.warn "trans 2: -- #{ @currency[:id] == native_currency_id}"
if @currency.dig(:options, contract_address_option).present?
create_erc20_transaction!(transaction)
elsif @currency[:id] == native_currency_id
Expand Down Expand Up @@ -78,6 +83,7 @@ def prepare_deposit_collection!(transaction, deposit_spread, deposit_currency)
Rails.logger.warn { "base_factor: #{@currency.fetch(:base_factor)}" }
Rails.logger.warn { "deposit amount: #{amount}" }
Rails.logger.warn { "deposit min_collection_amount: #{@currency.fetch(:min_collection_amount).to_d}" }

# If fee amount is greater than min collection amount
# system will detect fee collection as deposit
# To prevent this system will raise an error
Expand Down Expand Up @@ -218,7 +224,6 @@ def create_fee_transaction!(transaction, options, deposit_spread_size)
gas: '0x' + options.fetch(:gas_limit).to_i.to_s(16),
gasPrice: '0x' + options.fetch(:gas_price).to_i.to_s(16)
})

Rails.logger.warn "create_fee_transaction txid: #{txid}"
unless valid_txid?(normalize_txid(txid))
raise Ethereum::Client::Error, \
Expand Down Expand Up @@ -289,8 +294,8 @@ def send_transaction(params)
Rails.logger.warn "Start send : #{params}"
begin
txid = client.json_rpc(
:personal_sendTransaction,
[params.compact, @wallet.fetch(:secret)]
:eth_sendTransaction,
[params.compact]
)

Rails.logger.warn "Transaction sent successfully with txid: #{txid}"
Expand Down Expand Up @@ -321,8 +326,8 @@ def retry_with_higher_gas_price(params)
params[:nonce] = '0x' + get_nonce.to_s(16)

txid = client.json_rpc(
:personal_sendTransaction,
[params.compact, @wallet.fetch(:secret)]
:eth_sendTransaction,
[params.compact]
)

Rails.logger.warn "Transaction retried successfully with txid: #{txid}"
Expand Down