A MAN has narrowly avoided being sent to prison for selling cocaine in order to fund his own addiction and to repay a drug debt, a court heard.