# Spread between wallets method [Please read first](daemon.md) ### files: - {$Dena_path}/app/workers/daemons/deposit.rb - {$Dena_path}/app/models/deposit.rb ### implementation If you have read the deposit doc, you know this function where is called. #### spread_between_wallets! function 1. Will Return to ex-process if the Spread column had value. 2. Finding the Deposit wallet form currency and pass it as input param to the WalletService class to create the object. 3. Calling `spread_deposit` function from **WalletService** class for the deposit record 4. Update the spread column for DB record with return value from step **3**. #### spread_deposit function 1. The Adapter will be configured with the wallet record data and the relevant blockchain and currency setting 2. Array list by data of the withdraw wallet form DB (HOT WARM COLD) 3. Map them to new data by name **destination_wallets**: - `address` : the wallet address - `balance` : the current coin balance of wallet - `max_balance` : how many coins the wallet can host - `min_collection_amount` : the minimum of accepted deposit (set in the Admin panel) - `skip_deposit_collection` : boolean type to skip deposit or not (set in the Admin panel) 4. Set the zero the balance for the last wallet. Since last wallet is considered to be the most secure we need always. All money which doesn't fit to other wallets will be collected to last wallet. 5. Call `spread_between_wallets` from *WalletService* with the deposit record and the **destination_wallets** #### spread_between_wallets function 1. Returning empty array if deposit amount smaller than the minimum of `min_collection amount` of wallets. 2. left_amount variable is initialized with amount of the deposit (*original_amount*) 3. starting a loop on `destination_wallets`. 4. `amount_for_wallet` = choose the minimum value between available wallet balance and left_amount 5. Setting zero for `amount_for_wallet` if this variable smaller than `min_collection_amount` 6. `left_amount` will equal to (`left_amount` minus `amount_for_wallet`) 7. If amount left is too small we will not able to collect it.So we collect everything to current wallet.
`amount_for_wallet` = `amount_for_wallet` + `left_amount` and `left_amount` = 0 8. Creating `Peatio::Transaction` object: - to_address : `to_address` from **destination_wallets** - amount : `amount_for_wallet` - currency_id: currency_id of the deposit record - status : :skipped if `skip_deposit_collection` is true 9. Ending the loop on `destination_wallets` and now we have `spread` variable that includes **transactions**. 10. If deposit amount doesn't fit to any wallet, collect it to the last one. (`left_amount` doesnt become zero). 11. Remove zero and skipped transactions from spread. 12. Return `spread` variable.