Glencore Plc is negotiating a long-term contract to ship cobalt to Tesla Inc.’s new electric-vehicle factory in Shanghai, according to people familiar with the matter.

A deal would help Tesla avoid a supply squeeze on the key battery metal as it pushes into the world’s largest car market, and mark a win for Glencore after a tough spell for its cobalt business.

Executives from both companies hammered out terms of the deal before an official ceremony to mark the first sales from the Shanghai plant earlier this month, said one of the people, who asked not to be identified discussing commercial negotiations. They declined to give details about the size and value of the supply deal.

A Glencore spokesman declined to comment, while a representative for Tesla didn’t immediately respond to a request for comment.

The contract will help Tesla shore up its cobalt supply as it ramps up output at the so-called Gigafactory, which was built in just 11 months with significant support from the Chinese government. The opening of the plant has helped propel Tesla’s shares to record highs, as investors turn bullish on Elon Musk’s ambitions of transforming the company into a global mass-market automaker.

Glencore, the world’s largest cobalt miner, is in a prime position to benefit from a boom in electric-vehicle sales. But so far, it’s struggled to make that happen. The company booked losses last year related to cobalt after prices collapsed in 2018 from too much supply.

After customers reneged on contracts in response to the slump, Glencore spent last year locking in new long-term deals with customers throughout the electric-vehicle supply chain. BMW AG has signed up to buy cobalt from its mines in Australia, while battery materials suppliers GEM Co. and Umicore SA have also inked contracts.

Direct deals with miners are rare in the automotive industry, and the agreements between Glencore, Tesla and BMW are a sign car-makers are concerned about securing sufficient cobalt from ethical sources. Nearly three-quarters of the world’s cobalt comes from the Democratic Republic of Congo, and as much of 20% of the country’s output is produced at informal makeshift mines where fatalities and human-rights abuses are commonplace.