A man who “bulk bought” cocaine, MDMA and cannabis and sold the surplus to his friends to pay for his own habit has avoided going to prison.