feature/share-libs
rb 73 lines 2.27 KB
Raw
1 class WalletSolanaJob < ApplicationJob
2 queue_as :default
3
4 def perform(user_id)
5 @user = User.find_by_id(user_id)
6 return unless @user
7
8 change_to_working_directory
9 generate_wallet
10 setup_bach_token_address
11 finish
12 end
13
14 private
15
16 def finish
17 wallet = Wallet.find_by(user_id: @user.id, wallet_type: Wallet.wallet_types[:solana])
18 if wallet.nil?
19 system("echo 'Wallet for #{@user.name} does not exist.'")
20 return
21 end
22 wallet.update(pubkey: @pubkey, seed_phrase: @seed_phrase, bach_token_account: @bach_token_account, status: :done)
23 wallet.save
24 end
25
26 def generate_wallet
27 system("echo 'Generating address for #{@user.name}'")
28
29 output = `solana-keygen new --outfile ./#{@user.id}-keypair.json --no-bip39-passphrase`
30 output_array = output.split("\n")
31 @pubkey = output_array[3].split(' ').last
32 @seed_phrase = output_array[6]
33
34 system("echo 'Wallet is generated for #{@user.name}'")
35 end
36
37 def change_to_working_directory
38 working_directory_path = if Rails.env.production?
39 '/mnt/HC_Volume_15126575/apps/musik88/solana_wallets/'
40 else
41 '../solana_wallets_devnet/'
42 end
43 Dir.chdir(working_directory_path)
44
45 system("echo 'Change working directory to #{working_directory_path}'")
46 end
47
48 def setup_bach_token_address
49 define_bach_token_id
50 setup_solana
51 output = `spl-token transfer --fund-recipient --allow-unfunded-recipient #{@bach_token_id} 0.001 #{@pubkey}`
52 output_array = output.split("\n")
53 @bach_token_account = output_array[3].split(' ').last
54 end
55
56 def setup_solana
57 if Rails.env.production?
58 system('solana config set --keypair ~/.config/solana/main.json')
59 system('solana config set --url https://api.mainnet-beta.solana.com')
60 else
61 system('solana config set --keypair ~/solana-wallets/main.json')
62 system('solana config set --url https://api.devnet.solana.com')
63 end
64 end
65
66 def define_bach_token_id
67 @bach_token_id = if Rails.env.production?
68 'CTQBjyrX8pYyqbNa8vAhQfnRXfu9cUxnvrxj5PvbzTmf'
69 else
70 'DENNuKzCcrLhEtxZ8tm7nSeef8qvKgGGrdxX6euNkNS7'
71 end
72 end
73 end