This smart contract is designed to manage a loyalty program. Users can earn and spend points, and the contract owner can define new rewards. It is built using Solidity and can be tested using Ganache and Truffle.
owner
: The address of the contract owner. Only the owner can add new rewards and assign points.points
: A mapping from addresses to points. It stores the points for each user.rewards
: A mapping from reward IDs to descriptions. Rewards that users can redeem are stored here.nextRewardId
: A counter indicating the next available ID for a new reward.
PointsEarned
: Emitted when a user earns points.PointsRedeemed
: Emitted when a user spends points to redeem a reward.NewRewardAdded
: Emitted when a new reward is added to the program.
onlyOwner
: This modifier ensures that only the owner can execute certain functions.
-
Constructor: On contract deployment, the constructor sets the owner as the address that deployed the contract and initializes
nextRewardId
to 1. -
setOwner: Changes the contract owner's address. Only the current owner can execute this.
-
addReward: Allows the owner to add a new reward to the program. This also increments
nextRewardId
. -
earnPoints: Allows the owner to assign points to a specific user address.
-
redeemPoints: Any user can redeem points for a specific reward as long as they have enough points.
-
getPoints: Returns the number of points a user has.
-
getReward: Returns the description of a reward given its ID.
-
Deployment: The owner deploys the contract. They become the owner and
nextRewardId
is initialized to 1. -
Reward Setup: The owner adds several rewards using
addReward
. For example, "Discount Coupon", "Free Item", etc. -
Point Accumulation: The owner assigns points to users using
earnPoints
. This could be in response to some user actions like making a purchase. -
Point Checking: Users can check their point balance at any time using
getPoints
. -
Reward Redemption: Users redeem their points for rewards using
redeemPoints
. Their point balance is decremented and the redemption is logged. -
Reward Checking: Users or the owner can view the available rewards using
getReward
. -
Ownership Transfer: If needed, the original owner can transfer ownership of the contract to another address using
setOwner
.
For testing and static code analysis, you can use Truffle, Ganache, and Slither.
# Compile the contract
truffle compile
# Run tests
truffle test
If called through a Docker container:
docker-compose run --rm loyalty truffle test
# or
docker compose run --rm loyalty truffle test
For static code analysis, you can use Slither:
# Run Slither in the project directory
slither .
If called through a Docker container:
docker-compose run --rm slither slither .
# or
docker compose run --rm slither slither .
This should provide a detailed assessment of code quality and potential vulnerabilities.
This contract is a basic example and should be thoroughly tested and reviewed before being used in a production environment.
Feel free to save this README.md file and use it as needed. It includes the badges for Solidity, Ganache, and Truffle, along with a detailed explanation of the contract.