I think the foundation of morality can be found in our genes. Humans tends to complicate morality with rules and restrictions, both necessary and not, but I think the foundational principle is something quite simple. We have apparently noticed that, on balance, our well-being is increased when we consider the needs the desires of others in our moment-to-moment quest to get what we want out of life. When we ignore the needs and desires of others, when we act selfishly in other words, it often sparks retributive impulses from others. This can, and often does, negatively affect our lives. But by acting with respect what others want, we’ve found we can sidestep unnecessary conflict in the moment. And indeed, a selfless act for another person’s benefit can often be returned to us by that person at some later date.
Human civilization has intellectualized this idea to an extent and calls it “The Social Contract,” but I think is only a description of the idea, rather than a prescription for it. We see glimmers of moral behavior in animals, in our primate cousins. Chimps have a clear sense of inequity when it comes to food rewards. Monkey will indeed go so far as to starve themselves if they believe it will prevent their cage-mates from receiving electric shocks. Even mice will show greater distress at the suffering of familar mice than they do with unfamiliar mice.
And for those of us who don’t recognize the needs and desires of others when they act (the psychopaths and sociopaths), in both cases, we can account for their behavior through scientific observation - their brains work differently, and in ways that make this sort of behavior quite predictable.