Search⌘ K
AI Features

Receiving Payments

Explore how to update Solidity smart contracts to accept Ether payments and implement a balance retrieval function. Understand transaction handling, testing practices, and converting Ether to Wei units. This lesson guides you through writing failing tests, adding necessary contract code, and verifying successful balance updates.

Receive payments

We can now store addresses and split ratios inside a smart contract. Let’s update our contract so that it can also receive payments. Let’s start by writing a failing test for this:

Node.js
it("can receive transactions in Ether", async () => {
const Contract =
await ethers.getContractFactory(
"Partnership"
);
const [owner, person1] =
await hre.ethers.getSigners();
const addresses = [
owner.address,
person1.address,
];
const splitRatios = [1, 1];
const contract = await Contract.deploy(
addresses,
splitRatios
);
await contract.deployed();
// send ether to contract
await owner.sendTransaction({
to: contract.address,
value: ethers.utils.parseEther("1.0"),
});
});

Let’s take a look at the code above:

  • Lines 23–26: We use the owner.sendTransaction() function to send Ethers the contract from the owner account. ...