# Blockchain Deposit Watcher
Peatio daemons are controlled by this gem: [God](http://godrb.com/).
##### note: just we use the God if want using The Peatio in local without docker, in collection service, daemons handle by docker container.
## Daemon
##### start daemon with the god:
`god -c lib/daemons/daemons.god`
**When the Peatio is being initialized, the God starts all daemons**
##### stop daemon with the god:
`god stop`. *God will still be up.*
##### stop God and all daemons:
`god terminate`
##### restart God:
`god restart`
##### status God:
`god status`
#### [please read it](../../../README.md)
## Deposit Daemon
###### this Daemon, become a docker service by daemons.yaml that call in service.rake
[first read deposits_flow](../../../docs/peatio/deposits_flow.md)
**so we use new version of deposit**
#### files:
- {$Dena_path}/app/workers/daemons/deposit.rb
- {$Dena_path}/app/workers/daemons/blockchain.rb
#### implementation
daemon dockerize by below code and file:
- bash -c "bundle exec ruby lib/daemons/daemons.rb deposit" (called in Alvand Service up rake)
- {$Dena_path}/lib/daemons/daemons.rb
We decided to remove to AMQP base deposit daemons and create a new deposit daemon that will work on deposit states changes
and will prevent immediate proceeding of erc20 deposits.
New deposit process diagram:

1. Blockchain daemon process blocks and filter platform deposits and save them in our DB. [document](blockchain.md)
3. In the deposit daemons we select each 60s deposits with state `processing` and `fee_processing`.
4. For `processing` deposits we are checking if plugin implement method `prepare_deposit_collection!` if it doesn't we immediately process the deposit and collect deposit to the `hot`, `warm`, `cold` wallets.
this collection is done with help of WalletService class, that one of its responsibility is **spreading** between wallets.
[read more about WalletService](../withdraw/withdraw-coin.md)
every deposit record has column named `spread` that getting array value like this:
`[{"to_address"=>"xyxzx", "amount"=>"0.00083", "currency_id"=>"btc", "status"=>"pending", "hash"=>"dcffd5b7fabaa"}]`,
valued by (`spread_between_wallets!` *function* in deposit model) that was called in this daemon
each row of the Spread array, will become transaction record and will be send to the Blockchain by WalletService and its the Adapter (`collect_deposit!` function).
If plugin implement method `prepare_deposit_collection!` daemon processing of collection fees and change deposit state to `fee_processing`.
For deposits with `fee_processing` state, we select each minute deposits that have `updated_at` older than 5 minutes and process them. With time condition we are sure that fee transaction has already been executed.
```mermaid
graph TD
A((Blockchain)) -->|Deposit| B[Blockchain Daemon]
B --> |waiting for numbers of confirmations| C[Deposit Daemon]
D(BlockchainService) --> B
E(WalletService) --> C
```