The Royal Bank of Canada will buy the Canadian assets of online savings bank Ally Financial for up to $3.8 billion.

RBC confirmed the transaction Tuesday, which will see Canada's largest bank buy the Canadian auto finance and customer deposit business of Ally, which has been selling assets to repay a loan from the U.S. government.

Ally is the new name for General Motors former financing arm GMAC, which was spun off during the automaker's restructuring in 2009.

In Canada, Ally provides financing for about 600 auto dealerships across the country, and the company has 450,000 customers across Canada in its personal deposit taking business.

"Ally Canada will add significant scale to our existing consumer and commercial auto financing business and will strengthen RBC's position as a leader in the Canadian auto finance industry," RBC spokesman Dave McKay said.

"This is a strong business with favourable industry dynamics and the combination with RBC provides opportunities to leverage our existing strengths and cross-sell capabilities."

The bank says its net cost for the deal will be about $1.4 billion, after excluding excess capital to be acquired in the deal. Including excess capital, and depending on the size of the dividend taken out by the seller prior to closing, RBC will pay between $3.1 billion and $3.8 billion.

The deal is expected to close in the first quarter of next year.

"We view the transaction positively as it increases scale and builds out Royal’s very profitable domestic franchise," Barclays analyst John Aiken said of the deal.