In electrical distribution grids, the constantly increasing number of power generation devices based on renewables demands a transition from a centralized to a distributed generation paradigm. In fact, power injection from distributed energy resources (DERs) can be selectively controlled to achieve other objectives beyond supporting loads, such as the minimization of the power losses along the distribution lines and the subsequent increase of the grid hosting capacity. However, these technical achievements are only possible if alongside electrical optimization schemes, a suitable market model is set up to promote cooperation from the end users. In contrast with the existing literature, where energy trading and electrical optimization of the grid are often treated separately, or the trading strategy is tailored to a specific electrical optimization objective, in this paper, we consider their joint optimization. We also allow for a modular approach, where the market model can support any smart grid optimization goal. Specifically, we present a multi-objective optimization problem accounting for energy trading, where: 1) DERs try to maximize their profit, resulting from selling their surplus energy; 2) the loads try to minimize their expense; and 3) the main power supplier aims at maximizing the electrical grid efficiency through a suitable discount policy. This optimization problem is proved to be non-convex, and an equivalent convex formulation is derived. Centralized solutions are discussed and a procedure to distribute the solution is proposed. Numerical results to demonstrate the effectiveness of the so obtained optimal policies are finally presented, showing the proposed model results in economic benefits for all the users (generators and loads) and in an increased electrical efficiency for the grid.