Minimum Viable Payment Channel
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.
- 1.Clone the repo first
git clone -b payment-channel https://github.com/scaffold-eth/scaffold-eth-examples.git payment-channel
- 1.Install dependencies
- 1.Start local chain
- 1.Start your React frontend
- 1.Deploy your smart contracts to a local blockchain
- 1.Start the backend server which is responsible for storing signatures
Let's navigate to
packages/hardhat/contractsfolder and check out what contracts we have there.
We are mostly interested in
MVPC.solsmart 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 payableresponsible for opening the channel and setting a session/channel id by encoding the parameters passed in this function
function close(bytes32 id, uint256 value, bytes memory signature, bytes memory receiverSignature) publicresponsible for closing the stream to be calld by the receiver of the stream
function withdraw(bytes32 optionalId) publicresponsible 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