Skip to content

Commit

Permalink
update for univesal channel and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsdkeys committed Feb 15, 2024
1 parent 8f28161 commit 71e5aba
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 24 deletions.
48 changes: 32 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# Template for IBC enabled Soldity contracts

This tutorial enables to send an IBC packet from an "Xcounter" contract on either OP or Base. The packet will ensure that a counter variable on either contract remains in sync.
This repo provides a starter project to build [IBC](https://github.com/cosmos/ibc) enabled Solidity contracts that connect rollups to one another Polymer Hub, through the [vIBC core contracts](https://github.com/open-ibc/vibc-core-smart-contracts).

The repo is compatible with both Hardhat and Foundry development environments.

Find more information on building with (v)IBC and Polymer in the [Polymer documentation](https://docs.polymerlabs.org).

## Install dependencies

To use the quickstart tutorial, make sure that you have all dependencies installed.
To compile your contracts and start testing, make sure that you have all dependencies installed.

From the root directory run:
```bash
Expand Down Expand Up @@ -32,30 +36,33 @@ There's three types of scripts in the project:

- `deploy.js` and `deploy-config.js` allow you to deploy your application contract
- `create-channel.js` and `create-channel-config.js` creates a channel
- `send-packet.js` sends packets over an existing channel
- `send-packet.js` and `send-universal-packet.js` sends packets over an existing channel (custom or universal).

For every script you'll find a field in the config.json!!

Make sure to update the config with the intended files before running one of the scripts like so:
```bash
npx hardhat run scripts/send-packet.js --network optimism
```

**NOTE** Make sure to align the `--network` flag value to be compatible with your config values either on optimism or base.

## Deploy
### Deploy

Run:
```bash
# format node scripts/deploy-config.js [source] [destination]
node scripts/deploy-config.js optimism base
# format node scripts/deploy-config.js [source] [destination] [universal-channel-bool]
node scripts/deploy-config.js optimism base true
```
for an application that will use a universal channel, or:
```bash
# or
node scripts/deploy-config.js optimism base false
```
for an application that uses custom channels.

To deploy instances of the contracts on optimism as the source and base as the destination chains. (You can also switch the order)

Also this script will take the output of the deployment and update the config file with all the relevant information.

Then run:
### Create a channel

In case you're using universal channels, you can skip this step and move on the sending packets.

To create a custom channel, run:
```bash
node scripts/create-channel-config.js
```
Expand All @@ -66,8 +73,17 @@ Also this script will take the output of the channel creation and update the con

Check out the [channel tab in the explorer](https://explorer.prod.testnet.polymer.zone/channels) to find out if the correct channel-id's related to your contracts were updated in the config.

Finally run:
### Send packets
Finally Run:
```bash
npx hardhat run scripts/send-universal-packet.js --network optimism
```
to send a packet over a **universal channel**. You can pick either optimism or base to send the packet from.

Or run:
```bash
npx hardhat run scripts/send-packet.js --network optimism
```
to send a packet. You can pick either optimism or base to send the packet from.
to send a packet over a **custom channel**. You can pick either optimism or base to send the packet from.

**NOTE** Make sure to align the `--network` flag value to be compatible with your config values either on optimism or base.
28 changes: 20 additions & 8 deletions scripts/deploy-config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const { exec } = require("child_process");
const fs = require("fs");
const path = require("path");
const ibcConfig = require("../ibc.json");

// Run script with source and destination networks as arguments
// Example:
// $ node deploy-config.js optimism base
const source = process.argv[2];
const destination = process.argv[3];
const universalChannel = process.argv[4];

if (!source || !destination) {
console.error('Usage: node deploy-config.js <source_network> <destination_network>');
console.error('Usage: node deploy-config.js <source_network> <destination_network> <universal_channel_bool>');
process.exit(1);
}

if (process.argv[4] === undefined) {
console.error('Usage: node deploy-config.js <source_network> <destination_network> <universal_channel_bool>');
process.exit(1);
}

Expand All @@ -19,16 +26,21 @@ function updateConfig(network, address, isSource) {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));

// Update the config object
if (isSource) {
config["createChannel"]["srcChain"] = network;
config["createChannel"]["srcAddr"] = address;
if (universalChannel === "false") {
if (isSource) {
config["createChannel"]["srcChain"] = network;
config["createChannel"]["srcAddr"] = address;
} else {
config["createChannel"]["dstChain"] = network;
config["createChannel"]["dstAddr"] = address;
}

config["sendPacket"][`${network}`]["portAddr"] = address;
} else {
config["createChannel"]["dstChain"] = network;
config["createChannel"]["dstAddr"] = address;
config["sendUniversalPacket"][`${network}`]["portAddr"] = address;
config["sendUniversalPacket"][`${network}`]["channelId"] = ibcConfig[`${network}`]["universalChannel"];
}

config["sendPacket"][`${network}`]["portAddr"] = address;

// Write the updated config back to the file
fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
}
Expand Down
57 changes: 57 additions & 0 deletions scripts/send-universal-packet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require('hardhat');
const config = require('../config.json');
const sendConfig = config.sendUniversalPacket;

async function main() {
const accounts = await hre.ethers.getSigners();

const networkName = hre.network.name;
// Get the contract type from the config and get the contract
const contractType = config["deploy"][`${networkName}`];

const ibcAppSrc = await hre.ethers.getContractAt(
`${contractType}`,
sendConfig[`${networkName}`]["portAddr"]
);

// Do logic to prepare the packet
const channelId = sendConfig[`${networkName}`]["channelId"];
const channelIdBytes = hre.ethers.encodeBytes32String(channelId);
const timeoutSeconds = sendConfig[`${networkName}`]["timeout"];

// Send the packet
await ibcAppSrc.connect(accounts[1]).sendUniversalPacket(
channelIdBytes,
timeoutSeconds,
optionalArgs // add optional args here depending on the contract
)
console.log("Sending packet");

// Active waiting for the packet to be received and acknowledged
// @dev You'll need to implement this based on the contract's logic
let acked = false;
let counter = 0;
do {
const acked = await ibcAppSrc.findAck(sequence);
if (!acked) {
console.log("ack not received. waiting...");
await new Promise((r) => setTimeout(r, 2000));
counter++;
}
} while (!acked && counter<100);

console.log("Packet lifecycle was concluded successfully: " + acked);
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

0 comments on commit 71e5aba

Please sign in to comment.