After a contract stalemate that had consumed nearly the first three weeks of training camp, J.R. Smith and the Cleveland Cavaliers have reached an agreement on a four-year deal worth $57 million, according to league sources.

Sources told ESPN that Smith and the Cavs came to terms Friday night on a pact that will guarantee the mercurial shooting guard $45 million over the next three seasons.

The fourth season of the contract is not fully guaranteed, sources told ESPN's Brian Windhorst, at which point Smith will be 34. But sources say Smith's final season will become fully guaranteed unless the Cavs waive him shortly after the 2018-19 season.

The Cavaliers announced the Smith re-signing Saturday but did not disclose terms.

"Bringing J.R. back as part of our core group was a priority for us," general manager David Griffin said in a statement. "He's a special part of this family and we are very pleased to have reached an agreement that we all can feel good about. We know he can't wait to get back on the court with his brothers and we are certain that everyone in this organization and city feels the same way."

The Cavs also announced they had waived guard Toney Douglas.

J.R. Smith had been in talks with the Cavs after he did not pick up his $5.3 million player option for the 2016-17 season in July. Brandon Dill/AP Images

On Friday, Smith said in a video posted to Uninterrupted that he was encouraged by the deal and that he would now get down to the daily business of basketball.

"We are extremely happy and proud to say that we have agreed to terms to come back to the Cleveland Cavaliers, aka the champs, aka my brothers. It's been a long road," Smith said. "I appreciate everybody sticking out through this rough patch of time. I know this holdout and everything else has been long and tedious for everybody, but it's extremely huge for us. We're happy to be a part of the Cleveland family and just want to take it one day at a time."

He also said his family was expecting another child.

Smith earned approximately $45.5 million through his first 12 seasons in the league, according to BasketballReference.com. This new contract, if fully vested, will more than double that amount in a third of the time.

Sources told ESPN that, after weeks of scant progress, interest this week from the Philadelphia 76ers in Smith might have helped sparked negotiations with the Cavaliers. The Sixers have roughly $27 million in salary-cap space and, as seen during the summer under new general manager Bryan Colangelo, made a legitimate push to try to sign Manu Ginobili away from the San Antonio Spurs.

The Sixers have a growing need in the backcourt in the wake of a recent wrist injury suffered by veteran offseason signee Jerryd Bayless. But bringing back Smith was described as no less than an absolute must by Cavs star LeBron James, who went public last month with his disappointment about the ongoing impasse just four days into training camp.

James showed his excitement after Smith made his announcement.

So damn happy for my Lil bro!! Well deserved. Good things happens to good people. I can't express how happy I am for you brother! Love https://t.co/cEGGwz3bdE — LeBron James (@KingJames) October 15, 2016

‎The deal struck Friday will push Cleveland's payroll to a league-record $125 million for the coming season with a luxury-tax bill in the $30 million range. The Cavs paid a league-record $54 million in taxes last season, but the NBA's well-chronicled offseason salary-cap spike will ultimately lead to a lower tax bill for 2016-17.

‎The Cavs and Smith had been in talks since the veteran guard declined his $5.3 million player option for the 2016-17 season in July, becoming an unrestricted free agent after averaging 12.4 points, 2.8 rebounds, 1.7 assists and 1.1 steals while hitting 204 3-pointers in 77 starts last season.

Cavs coach Tyronn Lue also had been vocal about the need to get Smith signed and in camp. He addressed the agreement after the Cavs' preseason loss to the Chicago Bulls on Friday night.

"It's great news," Lue said. "Any time you get your starting 2-guard back -- [he was] a big part of what we did the last two years. It means a lot, so hopefully we get the contract signed and we'll be going in the right direction."

Lue was unclear whether Smith would start in the Cavaliers' next preseason game Tuesday, but the coach did say he hopes Smith will be ready to start on opening night Oct. 25.

"That's my vision, yes," Lue said.

It was clear that Smith, whom James recently referred to as "a leader of the team and for me personally," never wanted to go elsewhere. Smith has been spending as much time as possible with his Cavs teammates this month away from the court.

It was a sentiment his wife, Jewel, affirmed in a message via Instagram.

"He Never...for one second... wanted to have to suit up in another teams uniform," she wrote in the post. "#CavNation was always #1 in His and Our Hearts."

‎When the sides were unable to strike a deal by the start of camp, Smith became the fourth Cavs player in the past 10 years -- joining Anderson Varejao, Sasha Pavlovic and Tristan Thompson -- to have contract negotiations drag well into October.

Thompson also lauded Smith on social media.

"He deserves every penny!!!" Thompson wrote. "#TheBandIsOfficiallyBack next stop... Cartier... on you."

Information from ESPN's Dave McMenamin and Nick Friedell was used in this report.