Cain killed Abel in Genesis 4 and punished:
10 The Lord said, “What have you done? Listen! Your brother’s blood cries out to me from the ground. 11Now you are under a curse and driven from the ground, which opened its mouth to receive your brother’s blood from your hand. 12When you work the ground, it will no longer yield its crops for you. You will be a restless wanderer on the earth.”
Many centuries late, Moses commanded in Leviticus 24:17:
Anyone who takes the life of a human being is to be put to death.
Cain did not receive a death sentence for murder. On the other hand, Onan did for coitus interruptus in Genesis 38:
8 Then Judah said to Onan, “Sleep with your brother’s wife and fulfill your duty to her as a brother-in-law to raise up offspring for your brother.” 9But Onan knew that the child would not be his; so whenever he slept with his brother’s wife, he spilled his semen on the ground to keep from providing offspring for his brother. 10What he did was wicked in the Lord’s sight; so the Lord put him to death.
Deuteronomy 25:
7However, if a man does not want to marry his brother’s wife, she shall go to the elders at the town gate and say, “My husband’s brother refuses to carry on his brother’s name in Israel. He will not fulfill the duty of a brother-in-law to me.” 8Then the elders of his town shall summon him and talk to him. If he persists in saying, “I do not want to marry her,” 9his brother’s widow shall go up to him in the presence of the elders, take off one of his sandals, spit in his face and say, “This is what is done to the man who will not build up his brother’s family line.” 10That man’s line shall be known in Israel as The Family of the Unsandaled.
One would think that murder is more serious than onanism. Yet Onan was killed and not Cain. Why did God deal with these two wrongdoings so differently which were inconsistent with what he told Moses later?