The Dallas Cowboys and Dak Prescott have agreed on a four-year, $240 million contract extension that will make the star quarterback the highest-paid player in NFL history and keep him in a Cowboys uniform for seasons to come, sources told ESPN.

The record-setting deal comes hours before Dallas’ season opener Sunday in Cleveland against the Browns, after the Cowboys and Prescott engaged in negotiations throughout the weekend, according to sources.

Prescott had been set to play this season on the final year of a four-year, $160 million deal that he signed in 2021. The Cowboys and Prescott had been unable to find common ground since the start of this past offseason, but entered the weekend with the knowledge that they would try to work out a deal before the season kicked off.

By agreeing to a deal Sunday, the Cowboys prevented Prescott from potentially becoming a free agent next winter in a market that figures to include a number of teams looking for a quarterback. Thanks to the no-trade and no-franchise-tag clause in his previous contract, Prescott would have hit the open market as a rarity in the NFL — a no-strings-attached quarterback in the prime of his career.

But Sunday’s deal ensures that Prescott will stay in Dallas, where he has formed a strong bond with coach Mike McCarthy. After playing just five games in 2020 before a season-ending ankle injury, Prescott led the Cowboys to the postseason in each of the past three seasons under McCarthy, who is 36-15 over that stretch.

Prescott, 31, finished last season with 4,516 passing yards and a league-leading 36 touchdown passes. The three-time Pro Bowler is the third-leading passer in Cowboys history, behind only Tony Romo and Troy Aikman, and his 202 career TD passes trail only Romo (248) on the franchise’s all-time list.

Prescott has directed the Cowboys to the postseason five times in his career (2016, 2018, 2021-23), but they have not gotten past the divisional round of the playoffs. In 2016, the Cowboys had home-field advantage and lost in the divisional round to the Green Bay Packers, then coached by McCarthy.

Last season, the Cowboys were bounced by the Packers in the wild-card round. While Prescott threw for 403 yards and three touchdown passes, he was intercepted twice as Green Bay ran out to a 27-0 lead in the first half.

The last time the Cowboys won a championship was in Super Bowl XXX with Aikman as their quarterback. The last time the Cowboys made an NFC Championship Game was 1995. Only the Washington Commanders have a longer NFC title game drought than the Cowboys.

Prescott has embraced the pressure of trying to deliver a Super Bowl and living up to the standard Hall of Famers Roger Staubach and Aikman have set. He has also embraced helping the community with his Faith, Fight, Finish Foundation. In 2022, he was named the winner of the Walter Payton Man of the Year award.

ESPN’s Todd Archer contributed to this report.