From f0046d821457595ba7ea40ea00b073e5fa3dfb1c Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Sun, 20 Oct 2024 12:07:33 +0700 Subject: [PATCH] Create state_channel_example.js --- .../examples/state_channel_example.js | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 blockchain_integration/pi_network/pi-network-layer2-scaling/examples/state_channel_example.js diff --git a/blockchain_integration/pi_network/pi-network-layer2-scaling/examples/state_channel_example.js b/blockchain_integration/pi_network/pi-network-layer2-scaling/examples/state_channel_example.js new file mode 100644 index 000000000..143490779 --- /dev/null +++ b/blockchain_integration/pi_network/pi-network-layer2-scaling/examples/state_channel_example.js @@ -0,0 +1,43 @@ +// state_channel_example.js +const { ethers } = require("hardhat"); +const networkConfig = require("./network_config.json"); + +async function main() { + const [owner, participant1, participant2] = await ethers.getSigners(); + + // Load the ChannelManager and StateChannel contracts + const channelManagerAddress = networkConfig.networks.rinkeby.rollupManagerAddress; // Assuming ChannelManager is deployed at this address + const ChannelManager = await ethers.getContractFactory("ChannelManager"); + const channelManager = await ChannelManager.attach(channelManagerAddress); + + const StateChannel = await ethers.getContractFactory("StateChannel"); + const stateChannel = await StateChannel.deploy(channelManager.address, participant1.address, participant2.address); + await stateChannel.deployed(); + + // Open the channel + console.log("Opening channel..."); + await stateChannel.connect(participant1).openChannel(); + console.log("Channel opened successfully!"); + + // Update the state + const newState = ethers.utils.keccak256(ethers.utils.toUtf8Bytes("New State")); + console.log("Updating state to:", newState); + await stateChannel.connect(participant1).updateState(newState); + console.log("State updated successfully!"); + + // Get the current state + const currentState = await stateChannel.getCurrentState(); + console.log("Current state:", currentState); + + // Close the channel + console.log("Closing channel..."); + await stateChannel.connect(participant1).closeChannel(); + console.log("Channel closed successfully!"); +} + +main() + .then(() => process.exit(0)) + .catch((error) => { + console.error(error); + process.exit(1); + });