Daniel McKenzie, 23, pleaded guilty at Selkirk Sheriff Court to being concerned in the supply of heroin in Lawyers Brae and Elm Row in Galashiels last November.
McKenzie, of Church Square in Galashiels, was given a 12-month community payback order.
Fiona Hamilton, prosecuting, said undercover police officers caught McKenzie selling drugs during an operation on November 26 at 11am.
She explained: “They contacted the accused and asked him if he would be able to supply them with drugs.
“The officer received a call from the number they had for the accused.
“He was told he would call them back later and asking them to meet.
“At 2.16pm, they met and the accused was holding a wrap in his left hand which he handed to the officer. It contained 0.4 grammes of heroin valued at £20.
“On November 27, the officers received a message from the accused saying he was not in the area at the time but would contact them when he was in Gala.
“There was a further meeting and he proceeded to provide the officers again with a wrap of heroin for £20.
“He discussed with the officers that he would continue to supply them with heroin.
“On November 28, there was a third occasion of messages being exchanged.
“He met the officers again and gave them heroin to the value of £40 and said to the officers that he was able to supply them with as much heroin as they wanted.”
A raid was then carried out at McKenzie’s property.
In interview afterwards, he claimed he had since stopped taking drugs.
Sheriff Raymond McMenamin told McKenzie: “You were supplying heroin to fund your own habit.
“You were involved in a serious trade here. It could have resulted in a prison sentence, but I am able to impose an alternative to custody here.”