diff --git a/smart_contract/contracts/Transactions.sol b/smart_contract/contracts/Transactions.sol index 51733b5..977e911 100644 --- a/smart_contract/contracts/Transactions.sol +++ b/smart_contract/contracts/Transactions.sol @@ -1,12 +1,13 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.0; - -import "hardhat/console.sol"; + +// import "hardhat/console.sol"; contract Transactions { uint256 transactionCount; + event Transfer(address from, address receiver, uint amount, string message, uint256 timestamp, string keyword); struct TransferStruct { @@ -19,8 +20,16 @@ contract Transactions { } TransferStruct[] transactions; - + +/// @notice Adds a transaction to the blockchain. +/// @param receiver The address receiving funds. +/// @param amount The amount being sent. +/// @param message A message associated with the transaction. +/// @param keyword A keyword for categorizing the transaction. function addToBlockchain(address payable receiver, uint amount, string memory message, string memory keyword) public { + require(receiver != address(0), "Invalid receiver address"); + require(amount > 0, "Amount must be greater than 0"); + transactionCount += 1; transactions.push(TransferStruct(msg.sender, receiver, amount, message, block.timestamp, keyword)); diff --git a/smart_contract/test/sample-test.js b/smart_contract/test/sample-test.js index 44e0fcb..0f0f724 100644 --- a/smart_contract/test/sample-test.js +++ b/smart_contract/test/sample-test.js @@ -1,19 +1,38 @@ const { expect } = require("chai"); const { ethers } = require("hardhat"); -describe("Greeter", function () { - it("Should return the new greeting once it's changed", async function () { - const Greeter = await ethers.getContractFactory("Greeter"); - const greeter = await Greeter.deploy("Hello, world!"); - await greeter.deployed(); - - expect(await greeter.greet()).to.equal("Hello, world!"); - - const setGreetingTx = await greeter.setGreeting("Hola, mundo!"); - - // wait until the transaction is mined - await setGreetingTx.wait(); - - expect(await greeter.greet()).to.equal("Hola, mundo!"); +describe("Transactions", function () { + let transactions + beforeEach( async()=>{ + const Transactions = await ethers.getContractFactory("Transactions"); + transactions = await Transactions.deploy(); + }) + it("Should reject zero address", async function () { + + // await transactions.deployed(); + const zeroAddress = ethers.ZeroAddress + // await expect(transactions.addToBlockchain(zeroAddress, 300,"message", "keyword")).to.be.revertedWithCustomError(transactions,"Transactions__InvalidReceiver") + // Note: Changed the error checking syntax + await expect( + transactions.addToBlockchain( + ethers.constants.AddressZero, // Invalid address + 100, "Hello", "Test" + ) + ).to.be.revertedWith("Invalid receiver address"); + }); + it("Should reject zero amount", async function () { + const Transactions = await ethers.getContractFactory("Transactions"); + transactions = await Transactions.deploy(); + // await transactions.deployed(); + const zeroAddress = ethers.ZeroAddress + // await expect(transactions.addToBlockchain(zeroAddress, 300,"message", "keyword")).to.be.revertedWithCustomError(transactions,"Transactions__InvalidReceiver") + // Note: Changed the error checking syntax + const address = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + await expect( + transactions.addToBlockchain( + address, + 0, "Hello", "Test" + ) + ).to.be.revertedWith("Amount must be greater than 0"); }); });