Skip to content

Commit

Permalink
Minor corrections to contract
Browse files Browse the repository at this point in the history
  • Loading branch information
luloxi committed Dec 31, 2023
1 parent 3df3a7e commit b8c9d66
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 34 deletions.
63 changes: 30 additions & 33 deletions packages/foundry/contracts/Easy2Pay.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
pragma solidity ^0.8.19;

/**
@dev Struct to store payment requests
@variables amount: Amount of USD requested
@variables receiver: Address of payment receiver
@variables completed: Boolean to determine if payment was succesfully made
*/
* @dev Struct to store payment requests
* @variables amount: Amount of USD requested
* @variables completed: Boolean to determine if payment was succesfully made
*/
struct PayRequest {
uint256 amount;
bool completed;
Expand All @@ -15,17 +14,14 @@ struct PayRequest {
contract Easy2Pay {
address public owner;

/**
@dev Mapping to store PayRequest structs mapped to a unique requestId
*/
/**
* @dev Mapping to store PayRequest structs mapped to a unique requestId
*/
mapping(address receiver => PayRequest[]) public payRequests;

// Custom errors
error Easy2Pay__RequestDoesNotExist();
error Easy2Pay__InsufficientEther(
uint256 requestedAmount,
uint256 actualAmount
);
error Easy2Pay__InsufficientEther(uint256 requestedAmount, uint256 actualAmount);
error Easy2Pay__PaymentAlreadyCompleted();
error Easy2Pay__FailedToSendEther();
error Easy2Pay__UnauthorizedAccess();
Expand All @@ -39,46 +35,47 @@ contract Easy2Pay {
owner = msg.sender;
}

function requestPayment(uint256 _amount) public {
uint256 id = payRequests[msg.sender].length;
payRequests[msg.sender].push(PayRequest(_amount, false));
// Function in case a payment is received where msg.data must be empty
receive() external payable {
if (msg.sender != address(0)) revert Easy2Pay__FailedToSendEther();
}

function getRequests(address receiver) public view returns(PayRequest[] memory) {
return payRequests[receiver];
// Fallback function is called when msg.data is not empty
fallback() external payable {
if (msg.sender != address(0)) revert Easy2Pay__FailedToSendEther();
}

function setOwner(address _newOwner) public onlyOwner {
owner = _newOwner;
}

function requestPayment(uint256 _amount) public {
payRequests[msg.sender].push(PayRequest(_amount, false));
}

function pay(address receiver, uint256 _requestId) public payable {
PayRequest storage request = payRequests[receiver][_requestId];

if (receiver == address(0))
if (receiver == address(0)) {
revert Easy2Pay__RequestDoesNotExist();
}

if (request.amount > msg.value)
if (request.amount > msg.value) {
revert Easy2Pay__InsufficientEther(request.amount, msg.value);
}

if (request.completed) revert Easy2Pay__PaymentAlreadyCompleted();

request.completed = true;

// Call returns a boolean value indicating success or failure.
// This is the current recommended method to use to transfer ETH.
(bool sent, ) = receiver.call{value: msg.value}("");
(bool sent,) = receiver.call{value: msg.value}("");

if (!sent) revert Easy2Pay__FailedToSendEther();
}

// Function in case a payment is received where msg.data must be empty
receive() external payable {
if (msg.sender != address(0)) revert Easy2Pay__FailedToSendEther();
}

// Fallback function is called when msg.data is not empty
fallback() external payable {
if (msg.sender != address(0)) revert Easy2Pay__FailedToSendEther();
}

function setOwner(address _newOwner) public onlyOwner {
owner = _newOwner;
function getRequests(address receiver) public view returns (PayRequest[] memory) {
return payRequests[receiver];
}
}
3 changes: 3 additions & 0 deletions packages/foundry/deployments/31337.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"networkName": "Anvil"
}
177 changes: 176 additions & 1 deletion packages/nextjs/contracts/deployedContracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,181 @@
*/
import { GenericContractsDeclaration } from "~~/utils/scaffold-eth/contract";

const deployedContracts = {} as const;
const deployedContracts = {
31337: {
Easy2Pay: {
address: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512",
abi: [
{
inputs: [],
stateMutability: "nonpayable",
type: "constructor",
},
{
inputs: [],
name: "Easy2Pay__FailedToSendEther",
type: "error",
},
{
inputs: [
{
internalType: "uint256",
name: "requestedAmount",
type: "uint256",
},
{
internalType: "uint256",
name: "actualAmount",
type: "uint256",
},
],
name: "Easy2Pay__InsufficientEther",
type: "error",
},
{
inputs: [],
name: "Easy2Pay__PaymentAlreadyCompleted",
type: "error",
},
{
inputs: [],
name: "Easy2Pay__RequestDoesNotExist",
type: "error",
},
{
inputs: [],
name: "Easy2Pay__UnauthorizedAccess",
type: "error",
},
{
stateMutability: "payable",
type: "fallback",
},
{
inputs: [
{
internalType: "address",
name: "receiver",
type: "address",
},
],
name: "getRequests",
outputs: [
{
components: [
{
internalType: "uint256",
name: "amount",
type: "uint256",
},
{
internalType: "bool",
name: "completed",
type: "bool",
},
],
internalType: "struct PayRequest[]",
name: "",
type: "tuple[]",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "owner",
outputs: [
{
internalType: "address",
name: "",
type: "address",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "receiver",
type: "address",
},
{
internalType: "uint256",
name: "_requestId",
type: "uint256",
},
],
name: "pay",
outputs: [],
stateMutability: "payable",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "receiver",
type: "address",
},
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
name: "payRequests",
outputs: [
{
internalType: "uint256",
name: "amount",
type: "uint256",
},
{
internalType: "bool",
name: "completed",
type: "bool",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "uint256",
name: "_amount",
type: "uint256",
},
],
name: "requestPayment",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "_newOwner",
type: "address",
},
],
name: "setOwner",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
{
stateMutability: "payable",
type: "receive",
},
],
inheritedFunctions: {},
},
},
} as const;

export default deployedContracts satisfies GenericContractsDeclaration;

0 comments on commit b8c9d66

Please sign in to comment.