-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from swellander/feat/list-requests
Merged, any front end change is better than none, and I still don't know how to check how changes look without merging lol
- Loading branch information
Showing
9 changed files
with
428 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.22; | ||
|
||
/** | ||
@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 | ||
*/ | ||
struct PayRequest { | ||
uint256 amount; | ||
bool completed; | ||
} | ||
|
||
contract Easy2Pay { | ||
address public owner; | ||
|
||
/** | ||
@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__PaymentAlreadyCompleted(); | ||
error Easy2Pay__FailedToSendEther(); | ||
error Easy2Pay__UnauthorizedAccess(); | ||
|
||
modifier onlyOwner() { | ||
if (msg.sender != owner) revert Easy2Pay__UnauthorizedAccess(); | ||
_; | ||
} | ||
|
||
constructor() { | ||
owner = msg.sender; | ||
} | ||
|
||
function requestPayment(uint256 _amount) public { | ||
uint256 id = payRequests[msg.sender].length; | ||
payRequests[msg.sender].push(PayRequest(_amount, false)); | ||
} | ||
|
||
function getRequests(address receiver) public view returns(PayRequest[] memory) { | ||
return payRequests[receiver]; | ||
} | ||
|
||
function pay(address receiver, uint256 _requestId) public payable { | ||
PayRequest storage request = payRequests[receiver][_requestId]; | ||
|
||
if (receiver == address(0)) | ||
revert Easy2Pay__RequestDoesNotExist(); | ||
|
||
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}(""); | ||
|
||
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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useSearchParams } from "next/navigation"; | ||
import { useConnectModal } from "@rainbow-me/rainbowkit"; | ||
import type { NextPage } from "next"; | ||
import { useAccount } from "wagmi"; | ||
import { useScaffoldContractWrite } from "~~/hooks/scaffold-eth"; | ||
|
||
const Requests: NextPage = () => { | ||
const { isConnected } = useAccount(); | ||
const [hasBeenCalled, setHasBeenCalled] = useState(false); | ||
const { openConnectModal } = useConnectModal(); | ||
const searchParams = useSearchParams(); | ||
const recipient = searchParams.get("recipient") || undefined; | ||
const amount = searchParams.get("amount") || undefined; | ||
const requestId = searchParams.get("requestId") || undefined; | ||
const { writeAsync: sendPaymentTx, isLoading } = useScaffoldContractWrite({ | ||
contractName: "Easy2Pay", | ||
functionName: "pay", | ||
args: [recipient, BigInt(requestId || "0")], | ||
// for payable functions, expressed in eth | ||
value: BigInt(amount || "0"), | ||
// the number of block confirmations to wait for before considering transaction to be confirmed (default : 1). | ||
blockConfirmations: 1, | ||
// the callback function to execute when the transaction is confirmed. | ||
onBlockConfirmation: txnreceipt => { | ||
console.log("transaction blockhash", txnreceipt.blockHash); | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
if (!isConnected) openConnectModal?.(); | ||
}, [isConnected, openConnectModal]); | ||
|
||
useEffect(() => { | ||
const sendPaymentTxAndHandleErr = async () => { | ||
const response = await sendPaymentTx(); | ||
console.log({ response }); | ||
}; | ||
|
||
if (recipient && amount && requestId && !hasBeenCalled && !isLoading && isConnected) { | ||
sendPaymentTxAndHandleErr(); | ||
setHasBeenCalled(true); | ||
} | ||
}, [recipient, amount, requestId, hasBeenCalled, isConnected]); | ||
|
||
return <></>; | ||
}; | ||
|
||
export default Requests; |
Oops, something went wrong.