You're in a really tough spot because on the one hand, you have a fair idea that your daughter's lifestyle is going to be bad for your grand-daughter - either directly (i.e., physical violence) or indirectly (i.e., being around bad things/bad people will ultimately traumatize the girl, and will end badly for her). Unfortunately, if your daughter really is a true sociopath, she will be able to lie, cheat, and manipulate to get her needs met. I said that to say this: If she (daughter) does not want to give you custody of the child, then you are going to have to convince a court that your daughter is too unfit of a mother to have ANY contact with her daughter, and that will be a difficult maneuver, given your daughter's tendency to outright deny the truth and to make you look like the bad guy (all rules are off when you deal with a sociopath). Then, you will be labeled as the enemy by your daughter, and she may either act out aggressively toward you or leave and never see you (and you not see your grand-daughter) ever again.
Tough spot, but I I will answer your question more directly in this way: If you truly feel your grand-daughter is in danger because of how poorly your daughter is behaving, then you should do whatever you can to separate them. There are few things that will mess with a kid's head like having an unstable, possibly criminal, parent who has poor boundaries and hangs out with bad people. I like that you will structure your custody to be contingent upon your daughter receiving treatment - this may give you more of a leg to stand on in court.
My immediate advice is to consult an attorney who can give you a more concrete answer as to how to proceed through the legal system. Too, a psychologist to evaluate your daughter and grand-daughter may ultimately be necessary, but an attorney is the clear first step. If you are satisfied with the response, please hit "Accept." That is the only way i can receive credit for my answer. Thanks-