description |
---|
Make micro payments in ETH with signatures over a open session |
Author: Viraz Malhotra****
Source code: https://github.com/scaffold-eth/scaffold-eth-examples/tree/payment-channel
Intended audience: Intermediate
Topics: Scaffold-eth basics, State Channels
Table of Contents
We will show you how a Minimal Viable Payment Channel can be built and also will demonstrate how you can spin it up locally as a playground. We take the case of a student and teacher/mentor just to simpilify things, so basically if a student is facing a issue related to code so he can consult a teacher for a time period and create a payment channel and steam eth in the form of micro payments throught the session by just sending in signatures and the teacher can at any time within the session withdraw and close it.
Let's start our environment for tinkering and exploring how NFT auction would work.
- Clone the repo first
git clone -b payment-channel https://github.com/scaffold-eth/scaffold-eth-examples.git payment-channel
cd payment-channel
- Install dependencies
yarn install
- Start local chain
yarn chain
- Start your React frontend
yarn start
- Deploy your smart contracts to a local blockchain
yarn deploy
- Start the backend server which is responsible for storing signatures
yarn backend
Let's navigate to packages/hardhat/contracts
folder and check out what contracts we have there.
We are mostly interested in MVPC.sol
smart contract which contains all the logic for NFT auction.
So basically there are 3 main functions:
function open(address signer, address payable destination, uint256 timeout) public payable
responsible for opening the channel and setting a session/channel id by encoding the parameters passed in this functionfunction close(bytes32 id, uint256 value, bytes memory signature, bytes memory receiverSignature) public
responsible for closing the stream to be calld by the receiver of the streamfunction withdraw(bytes32 optionalId) public
responsible for getting the left over stake back to the channel creator incase the receiver does not close the stream
Firstly, let's get us some funds using local faucet.
open a payment channel and you can also see the previous payment channels created
after clicking on channel detail button set the eth stream / minute amount
the receiver in this case a teacher can view the payment channel details and can claim and close it any time till the session does not expire
the channel creator can close and withdraw the stream funds too espcially when the receiver does not close the channel
Join the telegram support chat 💬 to ask questions and find others building with 🏗 scaffold-eth!